(self, n_assets, max_weight=1)
Construct.
Construct.
Construct .
def __init__(self, n_assets, max_weight=1):
        """Construct."""
        super().__init__()
        covmat_sqrt = cp.Parameter((n_assets, n_assets))
        rets = cp.Parameter(n_assets)
        alpha = cp.Parameter(nonneg=True)

        w = cp.Variable(n_assets)
        ret = rets @ w
        risk = cp.sum_squares(covmat_sqrt @ w)
        reg = alpha * (cp.norm(w) ** 2)

        prob = cp.Problem(cp.Maximize(ret - risk - reg),
                          [cp.sum(w) == 1,
                           w >= 0,
                           w <= max_weight
                           ])

        assert prob.is_dpp()

        self.cvxpylayer = CvxpyLayer(prob, parameters=[rets, covmat_sqrt, alpha], variables=[w])
def __init__ ( self , n_assets , max_weight = 1 ) : super ( ) . __init__ ( ) covmat_sqrt = cp . Parameter ( ( n_assets , n_assets ) ) rets = cp . Parameter ( n_assets ) alpha = cp . Parameter ( nonneg = True ) w = cp . Variable ( n_assets ) ret = rets @ w risk = cp . sum_squares ( covmat_sqrt @ w ) reg = alpha * ( cp . norm ( w ) ** 2 ) prob = cp . Problem ( cp . Maximize ( ret - risk - reg ) , [ cp . sum ( w ) == 1 , w >= 0 , w <= max_weight ] ) assert prob . is_dpp ( ) self . cvxpylayer = CvxpyLayer ( prob , parameters = [ rets , covmat_sqrt , alpha ] , variables = [ w ] )