double BasicMarkowitz
( int n,
std::shared_ptr<ndarray<double, 1>> mu,
std::shared_ptr<ndarray<double, 2>> GT,
std::shared_ptr<ndarray<double, 1>> x0,
double w,
double gamma)
{
Model::t M = new Model("Basic Markowitz"); auto _M = finally([&]() { M->dispose(); });
Variable::t x = M->variable("x", n, Domain::greaterThan(0.0));
M->objective("obj", ObjectiveSense::Maximize, Expr::dot(mu, x));
M->constraint("budget", Expr::sum(x), Domain::equalsTo(w + sum(x0)));
M->constraint("risk", Expr::vstack(gamma, Expr::mul(GT, x)), Domain::inQCone());
M->solve();
SolutionStatus solsta = M->getPrimalSolutionStatus();
if (solsta != SolutionStatus::Optimal)
{
std::ostringstream oss;
oss << "Unexpected solution status: " << solsta << std::endl;
throw SolutionError(oss.str());
}
return dot(mu, x->level());
}