public final class MarkowitzModel extends FinancePortfolio
The Markowitz model, in this class, is defined as:
min (RAF/2) [w]T[C][w] - [w]T[r]
subject to |[w]| = 1
RAF 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. ( 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 the MarketEquilibrium
or
FinancePortfolio.Context
used to instantiate the MarkowitzModelsetTargetReturn(BigDecimal)
setTargetVariance(BigDecimal)
Optionally you may setLowerLimit(int, BigDecimal), setUpperLimit(int, BigDecimal) or setShortingAllowed(boolean).
To get the optimal asset weighs you simply call getWeights()
or 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);
FinancePortfolio.Context
MATRIX_FACTORY
Constructor and Description |
---|
MarkowitzModel(BasicMatrix covarianceMatrix,
BasicMatrix expectedExcessReturns) |
MarkowitzModel(FinancePortfolio.Context portfolioContext) |
MarkowitzModel(MarketEquilibrium marketEquilibrium,
BasicMatrix expectedExcessReturns) |
Modifier and Type | Method and Description |
---|---|
org.ojalgo.finance.portfolio.LowerUpper |
addConstraint(BigDecimal lowerLimit,
BigDecimal upperLimit,
int... assetIndeces)
Will add a constraint on the sum of the asset weights specified by the asset indices.
|
protected BasicMatrix |
calculateAssetReturns() |
protected BasicMatrix |
calculateAssetReturns(BasicMatrix aWeightsVctr) |
protected BasicMatrix |
calculateAssetWeights()
Constrained optimisation.
|
protected BasicMatrix |
calculateAssetWeights(BasicMatrix aReturnsVctr) |
protected Scalar<?> |
calculatePortfolioReturn(BasicMatrix aWeightsVctr,
BasicMatrix aReturnsVctr) |
double |
calculatePortfolioReturn(FinancePortfolio weightsPortfolio) |
protected Scalar<?> |
calculatePortfolioVariance(BasicMatrix aWeightsVctr) |
double |
calculatePortfolioVariance(FinancePortfolio weightsPortfolio) |
protected void |
calibrate(BasicMatrix aWeightsVctr,
BasicMatrix aReturnsVctr) |
void |
clearAllConstraints() |
BasicMatrix |
getAssetReturns() |
BasicMatrix |
getAssetVolatilities() |
BasicMatrix |
getAssetWeights() |
BasicMatrix |
getCorrelations() |
BasicMatrix |
getCovariances() |
MarketEquilibrium |
getMarketEquilibrium() |
double |
getMeanReturn()
The mean/expected return of this instrument.
|
double |
getReturnVariance()
The instrument's return variance.
|
Scalar<?> |
getRiskAversion() |
String[] |
getSymbols() |
List<BigDecimal> |
getWeights()
This method returns a list of the weights of the Portfolio's contained assets.
|
protected BasicMatrix |
handle(Optimisation.Result optimisationResult) |
boolean |
isShortingAllowed() |
org.ojalgo.finance.portfolio.OptimisedPortfolio.Optimiser |
optimiser() |
protected void |
reset() |
void |
setLowerLimit(int assetIndex,
BigDecimal lowerLimit) |
void |
setRiskAversion(Number factor) |
void |
setShortingAllowed(boolean allowed) |
void |
setTargetReturn(BigDecimal targetReturn)
Will set the target return to whatever you input and the target variance to
null . |
void |
setTargetVariance(BigDecimal targetVariance)
Will set the target variance to whatever you input and the target return to
null . |
void |
setUpperLimit(int assetIndex,
BigDecimal upperLimit) |
int |
size() |
List<SimpleAsset> |
toSimpleAssets() |
SimplePortfolio |
toSimplePortfolio() |
String |
toString() |
compareTo, forecast, getConformance, getLossProbability, getLossProbability, getSharpeRatio, getSharpeRatio, getValueAtRisk, getValueAtRisk95, getVolatility, normalise, normalise
public MarkowitzModel(BasicMatrix covarianceMatrix, BasicMatrix expectedExcessReturns)
public MarkowitzModel(FinancePortfolio.Context portfolioContext)
public MarkowitzModel(MarketEquilibrium marketEquilibrium, BasicMatrix expectedExcessReturns)
public org.ojalgo.finance.portfolio.LowerUpper addConstraint(BigDecimal lowerLimit, BigDecimal upperLimit, int... assetIndeces)
public final void clearAllConstraints()
public final void setLowerLimit(int assetIndex, BigDecimal lowerLimit)
public final void setTargetReturn(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.
setTargetVariance(BigDecimal)
public final void setTargetVariance(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.
setTargetReturn(BigDecimal)
public final void setUpperLimit(int assetIndex, BigDecimal upperLimit)
public String toString()
protected BasicMatrix calculateAssetWeights()
protected void reset()
public final boolean isShortingAllowed()
public org.ojalgo.finance.portfolio.OptimisedPortfolio.Optimiser optimiser()
public final void setShortingAllowed(boolean allowed)
protected final BasicMatrix calculateAssetReturns()
protected final BasicMatrix handle(Optimisation.Result optimisationResult)
public final double calculatePortfolioReturn(FinancePortfolio weightsPortfolio)
calculatePortfolioReturn
in interface FinancePortfolio.Context
public final double calculatePortfolioVariance(FinancePortfolio weightsPortfolio)
calculatePortfolioVariance
in interface FinancePortfolio.Context
public final BasicMatrix getAssetReturns()
getAssetReturns
in interface FinancePortfolio.Context
public final BasicMatrix getAssetVolatilities()
getAssetVolatilities
in interface FinancePortfolio.Context
public final BasicMatrix getAssetWeights()
public final BasicMatrix getCorrelations()
getCorrelations
in interface FinancePortfolio.Context
public final BasicMatrix getCovariances()
getCovariances
in interface FinancePortfolio.Context
public final MarketEquilibrium getMarketEquilibrium()
public final double getMeanReturn()
FinancePortfolio
getMeanReturn
in class FinancePortfolio
public final double getReturnVariance()
FinancePortfolio
getReturnVariance
in class FinancePortfolio
public final Scalar<?> getRiskAversion()
public final String[] getSymbols()
public final List<BigDecimal> getWeights()
FinancePortfolio
getWeights
in class FinancePortfolio
public final void setRiskAversion(Number factor)
public int size()
size
in interface FinancePortfolio.Context
public final List<SimpleAsset> toSimpleAssets()
public final SimplePortfolio toSimplePortfolio()
protected final BasicMatrix calculateAssetReturns(BasicMatrix aWeightsVctr)
protected final BasicMatrix calculateAssetWeights(BasicMatrix aReturnsVctr)
protected final Scalar<?> calculatePortfolioReturn(BasicMatrix aWeightsVctr, BasicMatrix aReturnsVctr)
protected final Scalar<?> calculatePortfolioVariance(BasicMatrix aWeightsVctr)
protected final void calibrate(BasicMatrix aWeightsVctr, BasicMatrix aReturnsVctr)
Copyright © 2017 Optimatika. All rights reserved.