def compute_opt_weight(env, t):

    """
    Computes the optimal weight of the risky asset for a given environment
    at a time t.

    Arguments
    ---------
    :param env : Environment instance
           Environment instance specifying the RL environment.
    :param t : int
           Period in episode for which the optimal weight of the risky asset
           should be computed.

    Returns
    -------
    :returns opt : float
             The optimal weight of the risky asset in the given environment
             in period t.
    """

    env.time = t

    # regime in time t:
    idxt = [t in v["periods"] for v in env.regimes.values()].index(True)
    rt = list(env.regimes.keys())[idxt]

    mu = env.regimes[rt]["mu"]
    sigma = env.regimes[rt]["sigma"]

    opt = (mu[1] - mu[0] + (sigma[1]**2)/2) / (env.theta * sigma[1]**2)

    return opt