Module ojalgo

Class MarkowitzModel

java.lang.Object
org.ojalgo.data.domain.finance.portfolio.FinancePortfolio
org.ojalgo.data.domain.finance.portfolio.MarkowitzModel
All Implemented Interfaces:
Comparable<FinancePortfolio>, FinancePortfolio.Context

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:
  1. invalid reference
    #setRiskAversion(Number)
    unless this was already set in the MarketEquilibrium or FinancePortfolio.Context used to instantiate the MarkowitzModel
  2. setTargetReturn(BigDecimal)
  3. setTargetVariance(BigDecimal)

Optionally you may setLowerLimit(int, BigDecimal), setUpperLimit(int, BigDecimal) or setShortingAllowed(boolean).

To get the optimal asset weighs you simply call FinancePortfolio.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);

Author:
apete
  • Constructor Details

  • Method Details

    • addConstraint

      public org.ojalgo.data.domain.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. Either (but not both) of the limits may be null.
    • clearAllConstraints

      public void clearAllConstraints()
    • setLowerLimit

      public void setLowerLimit(int assetIndex, BigDecimal lowerLimit)
    • setTargetReturn

      public 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.

      See Also:
    • setTargetVariance

      public 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.

      See Also:
    • setUpperLimit

      public void setUpperLimit(int assetIndex, BigDecimal upperLimit)
    • toString

      public String toString()
    • calculateAssetWeights

      protected MatrixR064 calculateAssetWeights()
      Constrained optimisation.
    • reset

      protected void reset()
    • isShortingAllowed

      public final boolean isShortingAllowed()
    • optimiser

      public org.ojalgo.data.domain.finance.portfolio.OptimisedPortfolio.Optimiser optimiser()
    • setShortingAllowed

      public final void setShortingAllowed(boolean allowed)
    • calculateAssetReturns

      protected final MatrixR064 calculateAssetReturns()
    • handle

      protected final MatrixR064 handle(Optimisation.Result optimisationResult)
    • calculatePortfolioReturn

      public final double calculatePortfolioReturn(FinancePortfolio weightsPortfolio)
      Specified by:
      calculatePortfolioReturn in interface FinancePortfolio.Context
    • calculatePortfolioVariance

      public final double calculatePortfolioVariance(FinancePortfolio weightsPortfolio)
      Specified by:
      calculatePortfolioVariance in interface FinancePortfolio.Context
    • getAssetReturns

      public final MatrixR064 getAssetReturns()
      Specified by:
      getAssetReturns in interface FinancePortfolio.Context
    • getAssetVolatilities

      public final MatrixR064 getAssetVolatilities()
      Specified by:
      getAssetVolatilities in interface FinancePortfolio.Context
    • getAssetWeights

      public final MatrixR064 getAssetWeights()
    • getCorrelations

      public final MatrixR064 getCorrelations()
      Specified by:
      getCorrelations in interface FinancePortfolio.Context
    • getCovariances

      public final MatrixR064 getCovariances()
      Specified by:
      getCovariances in interface FinancePortfolio.Context
    • getMarketEquilibrium

      public final MarketEquilibrium getMarketEquilibrium()
    • getMeanReturn

      public final double getMeanReturn()
      Description copied from class: FinancePortfolio
      The mean/expected return of this instrument. May return either the absolute or excess return of the instrument. The context in which an instance is used should make it clear which. return.
      Specified by:
      getMeanReturn in class FinancePortfolio
    • getReturnVariance

      public final double getReturnVariance()
      Description copied from class: FinancePortfolio
      The instrument's return variance. Subclasses must override either FinancePortfolio.getReturnVariance() or FinancePortfolio.getVolatility().
      Overrides:
      getReturnVariance in class FinancePortfolio
    • getRiskAversion

      public final Scalar<?> getRiskAversion()
    • getSymbols

      public final String[] getSymbols()
    • getWeights

      public final List<BigDecimal> getWeights()
      Description copied from class: FinancePortfolio
      This method returns a list of the weights of the Portfolio's contained assets. An asset weight is NOT restricted to being a share/percentage - it can be anything. Most subclasses do however assume that the list of asset weights are shares/percentages that sum up to 100%. Calling FinancePortfolio.normalise() will transform any set of weights to that form.
      Specified by:
      getWeights in class FinancePortfolio
    • setRiskAversion

      public final void setRiskAversion(Comparable<?> factor)
    • size

      public int size()
      Specified by:
      size in interface FinancePortfolio.Context
    • toSimpleAssets

      public final List<SimpleAsset> toSimpleAssets()
    • toSimplePortfolio

      public final SimplePortfolio toSimplePortfolio()
    • calculateAssetReturns

      protected final MatrixR064 calculateAssetReturns(MatrixR064 aWeightsVctr)
    • calculateAssetWeights

      protected final MatrixR064 calculateAssetWeights(MatrixR064 aReturnsVctr)
    • calculatePortfolioReturn

      protected final Scalar<?> calculatePortfolioReturn(MatrixR064 aWeightsVctr, MatrixR064 aReturnsVctr)
    • calculatePortfolioVariance

      protected final Scalar<?> calculatePortfolioVariance(MatrixR064 aWeightsVctr)
    • calibrate

      protected final void calibrate(MatrixR064 aWeightsVctr, MatrixR064 aReturnsVctr)