public class MarkowitzByQP extends Object
setRiskAversionCoefficient(double)
with a positive q before reading getOptimalWeights() or fw().
The weights are null before a successful solve.PortfolioUtils.The unity constraint is added automatically. q penalizes variance, not standard deviation. Holding expected returns fixed while scaling covariance changes the effective risk penalty. A benchmark changes Sharpe-ratio calculations, not the fixed-q objective itself. Treat inputs as immutable while optimizing.
Each risk-aversion setter solves again and replaces the stored weights. Retain the returned weight vector for each frontier point before the next solve. If a solve throws, do not assume a previously stored portfolio solves the newly requested q. Inconsistent bounds or other constraints can make the QP infeasible. This class neither forecasts returns nor backtests rebalancing, turnover, fees, slippage or cash flows.
Use estimates for a common horizon, request the solve explicitly, then check feasibility and reconcile the objective sign.
import dev.nm.algebra.linear.matrix.doubles.matrixtype.dense.DenseMatrix;
import dev.nm.algebra.linear.vector.doubles.Vector;
import dev.nm.algebra.linear.vector.doubles.dense.DenseVector;
import tech.nmfin.portfoliooptimization.markowitz.MarkowitzByQP;
import tech.nmfin.portfoliooptimization.PortfolioUtils;
DenseVector expectedReturns = new DenseVector(0.08, 0.12);
DenseMatrix covariance = new DenseMatrix(new double[][]{{0.04, 0}, {0, 0.09}});
MarkowitzByQP optimizer = new MarkowitzByQP(expectedReturns, covariance,
new DenseVector(0.2, 0.2), new DenseVector(0.8, 0.8));
assert optimizer.getOptimalWeights() == null;
optimizer.setRiskAversionCoefficient(1.0);
Vector weights = optimizer.getOptimalWeights();
assert Math.abs(weights.get(1) + weights.get(2) - 1) < 1e-7;
for (int i = 1; i <= weights.size(); ++i) {
assert weights.get(i) >= 0.2 - 1e-7 && weights.get(i) <= 0.8 + 1e-7;
}
double expectedReturn = PortfolioUtils.getPortfolioReturns(weights, expectedReturns);
double variance = PortfolioUtils.getPortfolioVariance(weights, covariance);
assert Math.abs(optimizer.fw() - (variance - expectedReturn)) < 1e-10;
| Constructor and Description |
|---|
MarkowitzByQP(Vector mu,
Matrix sigma)
Constructs a Markowitz portfolio from expected future returns
and future covariance, assuming no short selling constraint and
zero benchmark rate.
|
MarkowitzByQP(Vector mu,
Matrix sigma,
QPConstraint constraints)
Constructs a Markowitz portfolio from expected future returns
and future covariance, assuming zero benchmark rate for Sharpe
ratio calculation.
|
MarkowitzByQP(Vector mu,
Matrix sigma,
QPConstraint constraints,
double benchmarkRate)
Constructs a Markowitz portfolio from expected future returns
and future covariance.
|
MarkowitzByQP(Vector mu,
Matrix sigma,
Vector lower,
Vector upper)
Constructs a Markowitz portfolio from expected future returns
and future covariance, with lower and upper limits on asset
weights, assuming zero benchmark rate.
|
MarkowitzByQP(Vector mu,
Matrix sigma,
Vector lower,
Vector upper,
double benchmarkRate)
Constructs a Markowitz portfolio from expected future returns
and future covariance for a given benchmark rate, with lower and
upper limits on asset weights.
|
| Modifier and Type | Method and Description |
|---|---|
double |
fw()
Evaluates the minimized objective \(q\operatorname{Var}(w^{\mathsf T}r)-\mathbb{E}[w^{\mathsf T}r]\) at the stored weights.
|
double |
getOptimalRiskAversionCoefficient()
Gets the optimal risk aversion coefficient w.r.t.
|
double |
getOptimalRiskAversionCoefficient(double lower,
double initial,
double upper)
Gets the optimal risk aversion coefficient w.r.t.
|
protected Vector |
getOptimalW(double q)
Solves w_eff = argmin {q * (w' Σ w) - w'r}.
|
Vector |
getOptimalWeights()
Gets the Markowitz optimal portfolio weights, for a given risk aversion
coefficient.
|
double |
getRiskAversionCoefficientForTargetReturn(double r,
double lower,
double upper,
int maxIterations) |
double |
getRiskAversionCoefficientForTargetVariance(double var,
double lower,
double upper,
int maxIterations) |
void |
setRiskAversionCoefficient(double q)
Sets the risk aversion coefficient, effectively moving along the
efficient frontier.
|
public MarkowitzByQP(Vector mu, Matrix sigma, QPConstraint constraints)
mu - expected future returns of assetssigma - future covariance of assetsconstraints - the additional constraints on w_effpublic MarkowitzByQP(Vector mu, Matrix sigma, QPConstraint constraints, double benchmarkRate)
mu - expected future returns of assetssigma - future covariance of assetsconstraints - the additional constraints on w_effbenchmarkRate - the benchmark rate used to compute Sharpe ratiopublic MarkowitzByQP(Vector mu, Matrix sigma)
mu - expected future returns of assetssigma - future covariance of assetspublic MarkowitzByQP(Vector mu, Matrix sigma, Vector lower, Vector upper)
mu - expected future returns of assetssigma - future covariance of assetslower - the lower limits of asset weightsupper - the upper limits of asset weightspublic MarkowitzByQP(Vector mu, Matrix sigma, Vector lower, Vector upper, double benchmarkRate)
mu - expected future returns of assetssigma - future covariance of assetslower - the lower limits of asset weightsupper - the upper limits of asset weightsbenchmarkRate - the benchmark rate used to compute Sharpe ratiopublic void setRiskAversionCoefficient(double q)
throws QPInfeasible,
Exception
q - the risk aversion coefficientQPInfeasibleExceptionprotected Vector getOptimalW(double q) throws Exception
q - the risk-aversion coefficientQPInfeasible - when the underlying quadratic programming problem is
infeasibleExceptionpublic Vector getOptimalWeights()
public double fw()
public double getOptimalRiskAversionCoefficient(double lower,
double initial,
double upper)
throws Exception
lower - lower bound for the search, e.g., 0initial - an initial guess of where the optimal risk aversion
coefficient may beupper - upper bound for the searchExceptionpublic double getOptimalRiskAversionCoefficient()
public double getRiskAversionCoefficientForTargetReturn(double r,
double lower,
double upper,
int maxIterations)
throws NoRootFoundException
NoRootFoundExceptionpublic double getRiskAversionCoefficientForTargetVariance(double var,
double lower,
double upper,
int maxIterations)
throws NoRootFoundException
NoRootFoundExceptionCopyright © 2010-2026 NM Group. All Rights Reserved.