Class MarkowitzModel
- java.lang.Object
-
- org.ojalgo.data.domain.finance.portfolio.FinancePortfolio
-
- org.ojalgo.data.domain.finance.portfolio.EquilibriumModel
-
- org.ojalgo.data.domain.finance.portfolio.OptimisedPortfolio
-
- org.ojalgo.data.domain.finance.portfolio.MarkowitzModel
-
- All Implemented Interfaces:
java.lang.Comparable<FinancePortfolio>
,FinancePortfolio.Context
public final class MarkowitzModel extends OptimisedPortfolio
The Markowitz model, in this class, is defined as:
min (RAF/2) [w]T[C][w] - [w]T[r]
subject to |[w]| = 1RAF stands for Risk Aversion Factor. Instead of specifying a desired risk or return level you specify a level of risk aversion that is used to balance the risk and return.
The expected returns for each of the assets must be excess returns. Otherwise this formulation is wrong.
The total weights of all assets will always be 100%, but shorting can be allowed or not according to your preference. ( OptimisedPortfolio.setShortingAllowed(boolean) ) In addition you may set lower and upper limits on any individual asset. ( setLowerLimit(int, BigDecimal) and setUpperLimit(int, BigDecimal) )
Risk-free asset: That means there is no excess return and zero variance. Don't (try to) include a risk-free asset here.
Do not worry about the minus sign in front of the return part of the objective function - it is handled/negated for you. When you're asked to supply the expected excess returns you should supply precisely that.
Basic usage instructions
After you've instantiated the MarkowitzModel you need to do one of three different things:#setRiskAversion(Number)
unless this was already set in theMarketEquilibrium
orFinancePortfolio.Context
used to instantiate the MarkowitzModelsetTargetReturn(BigDecimal)
setTargetVariance(BigDecimal)
Optionally you may setLowerLimit(int, BigDecimal), setUpperLimit(int, BigDecimal) or OptimisedPortfolio.setShortingAllowed(boolean).
To get the optimal asset weighs you simply call
EquilibriumModel.getWeights()
orEquilibriumModel.getAssetWeights()
.If the results are not what you expect the first thing you should try is to turn on optimisation model validation:
model.optimisation().validate(true);
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.ojalgo.data.domain.finance.portfolio.OptimisedPortfolio
OptimisedPortfolio.Optimiser, OptimisedPortfolio.Template
-
Nested classes/interfaces inherited from class org.ojalgo.data.domain.finance.portfolio.FinancePortfolio
FinancePortfolio.Context
-
-
Field Summary
Fields Modifier and Type Field Description private static double
_0_0
private static double
INIT
private static double
MAX
private static double
MIN
private java.util.HashMap<int[],LowerUpper>
myConstraints
private ExpressionsBasedModel
myOptimisationModel
private java.math.BigDecimal
myTargetReturn
private java.math.BigDecimal
myTargetVariance
private static NumberContext
TARGET_CONTEXT
-
Fields inherited from class org.ojalgo.data.domain.finance.portfolio.OptimisedPortfolio
BALANCE, VARIANCE
-
Fields inherited from class org.ojalgo.data.domain.finance.portfolio.FinancePortfolio
MATRIX_FACTORY
-
-
Constructor Summary
Constructors Constructor Description MarkowitzModel(FinancePortfolio.Context portfolioContext)
MarkowitzModel(MarketEquilibrium marketEquilibrium, MatrixR064 expectedExcessReturns)
MarkowitzModel(MatrixR064 covarianceMatrix, MatrixR064 expectedExcessReturns)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description LowerUpper
addConstraint(java.math.BigDecimal lowerLimit, java.math.BigDecimal upperLimit, int... assetIndeces)
Will add a constraint on the sum of the asset weights specified by the asset indices.protected MatrixR064
calculateAssetWeights()
Constrained optimisation.(package private) Scalar<?>
calculatePortfolioReturn(Access1D<?> weightsVctr, MatrixR064 returnsVctr)
(package private) Scalar<?>
calculatePortfolioVariance(Access1D<?> weightsVctr)
void
clearAllConstraints()
private ExpressionsBasedModel
generateOptimisationModel(double riskAversion)
protected void
reset()
void
setLowerLimit(int assetIndex, java.math.BigDecimal lowerLimit)
void
setTargetReturn(java.math.BigDecimal targetReturn)
Will set the target return to whatever you input and the target variance tonull
.void
setTargetVariance(java.math.BigDecimal targetVariance)
Will set the target variance to whatever you input and the target return tonull
.void
setUpperLimit(int assetIndex, java.math.BigDecimal upperLimit)
java.lang.String
toString()
-
Methods inherited from class org.ojalgo.data.domain.finance.portfolio.OptimisedPortfolio
calculateAssetReturns, getOptimisationOptions, getVariable, handle, isShortingAllowed, makeModel, optimiser, setShortingAllowed
-
Methods inherited from class org.ojalgo.data.domain.finance.portfolio.EquilibriumModel
calculateAssetReturns, calculateAssetWeights, calculatePortfolioReturn, calculatePortfolioReturn, calculatePortfolioVariance, calculatePortfolioVariance, calibrate, getAssetReturns, getAssetVolatilities, getAssetWeights, getCorrelations, getCovariances, getMarketEquilibrium, getMeanReturn, getReturnVariance, getRiskAversion, getSymbols, getWeights, isDefaultRiskAversion, setRiskAversion, size, toSimpleAssets, toSimplePortfolio
-
Methods inherited from class org.ojalgo.data.domain.finance.portfolio.FinancePortfolio
compareTo, forecast, getConformance, getLossProbability, getLossProbability, getSharpeRatio, getSharpeRatio, getValueAtRisk, getValueAtRisk95, getVolatility, normalise, normalise
-
-
-
-
Field Detail
-
_0_0
private static final double _0_0
-
INIT
private static final double INIT
-
MAX
private static final double MAX
-
MIN
private static final double MIN
-
TARGET_CONTEXT
private static final NumberContext TARGET_CONTEXT
-
myConstraints
private final java.util.HashMap<int[],LowerUpper> myConstraints
-
myOptimisationModel
private transient ExpressionsBasedModel myOptimisationModel
-
myTargetReturn
private java.math.BigDecimal myTargetReturn
-
myTargetVariance
private java.math.BigDecimal myTargetVariance
-
-
Constructor Detail
-
MarkowitzModel
public MarkowitzModel(FinancePortfolio.Context portfolioContext)
-
MarkowitzModel
public MarkowitzModel(MarketEquilibrium marketEquilibrium, MatrixR064 expectedExcessReturns)
-
MarkowitzModel
public MarkowitzModel(MatrixR064 covarianceMatrix, MatrixR064 expectedExcessReturns)
-
-
Method Detail
-
addConstraint
public LowerUpper addConstraint(java.math.BigDecimal lowerLimit, java.math.BigDecimal upperLimit, int... assetIndeces)
Will add a constraint on the sum of the asset weights specified by the asset indices. Either (but not both) of the limits may be null.
-
clearAllConstraints
public void clearAllConstraints()
-
setLowerLimit
public void setLowerLimit(int assetIndex, java.math.BigDecimal lowerLimit)
-
setTargetReturn
public void setTargetReturn(java.math.BigDecimal targetReturn)
Will set the target return to whatever you input and the target variance to
null
.Setting the target return implies that you disregard the risk aversion factor and want the minimum risk portfolio with return that is equal to or as close to the target as possible.
There is a performance penalty for setting a target return as the underlying optimisation model has to be solved several (many) times with different pararmeters (different risk aversion factors).
Setting a target return (or variance) is not recommnded. It's much better to simply modify the risk aversion factor.
- See Also:
setTargetVariance(BigDecimal)
-
setTargetVariance
public void setTargetVariance(java.math.BigDecimal targetVariance)
Will set the target variance to whatever you input and the target return to
null
.Setting the target variance implies that you disregard the risk aversion factor and want the maximum return portfolio with risk that is equal to or as close to the target as possible.
There is a performance penalty for setting a target variance as the underlying optimisation model has to be solved several (many) times with different pararmeters (different risk aversion factors).
Setting a target variance is not recommnded. It's much better to modify the risk aversion factor.
- See Also:
setTargetReturn(BigDecimal)
-
setUpperLimit
public void setUpperLimit(int assetIndex, java.math.BigDecimal upperLimit)
-
toString
public java.lang.String toString()
- Overrides:
toString
in classEquilibriumModel
-
generateOptimisationModel
private ExpressionsBasedModel generateOptimisationModel(double riskAversion)
-
calculateAssetWeights
protected MatrixR064 calculateAssetWeights()
Constrained optimisation.- Specified by:
calculateAssetWeights
in classEquilibriumModel
-
reset
protected void reset()
- Overrides:
reset
in classOptimisedPortfolio
-
calculatePortfolioReturn
Scalar<?> calculatePortfolioReturn(Access1D<?> weightsVctr, MatrixR064 returnsVctr)
-
-