Search in sources :

Example 1 with Optimisation

use of org.ojalgo.optimisation.Optimisation in project ojAlgo-finance by optimatika.

the class MarkowitzModel method calculateAssetWeights.

/**
 * Constrained optimisation.
 */
@Override
protected BasicMatrix calculateAssetWeights() {
    if (this.getOptimisationOptions().logger_appender != null) {
        BasicLogger.debug();
        BasicLogger.debug("###################################################");
        BasicLogger.debug("BEGIN RAF: {} MarkowitzModel optimisation", this.getRiskAversion());
        BasicLogger.debug("###################################################");
        BasicLogger.debug();
    }
    Optimisation.Result tmpResult;
    if ((myTargetReturn != null) || (myTargetVariance != null)) {
        final double tmpTargetValue;
        if (myTargetVariance != null) {
            tmpTargetValue = myTargetVariance.doubleValue();
        } else if (myTargetReturn != null) {
            tmpTargetValue = myTargetReturn.doubleValue();
        } else {
            tmpTargetValue = _0_0;
        }
        tmpResult = this.generateOptimisationModel(_0_0).minimise();
        double tmpTargetNow = _0_0;
        double tmpTargetDiff = _0_0;
        double tmpTargetLast = _0_0;
        if (tmpResult.getState().isFeasible()) {
            double tmpCurrent;
            double tmpLow;
            double tmpHigh;
            if (this.isDefaultRiskAversion()) {
                tmpCurrent = INIT;
                tmpLow = MAX;
                tmpHigh = MIN;
            } else {
                tmpCurrent = this.getRiskAversion().doubleValue();
                tmpLow = tmpCurrent * INIT;
                tmpHigh = tmpCurrent / INIT;
            }
            do {
                final ExpressionsBasedModel tmpModel = this.generateOptimisationModel(tmpCurrent);
                tmpResult = tmpModel.minimise();
                tmpTargetLast = tmpTargetNow;
                if (myTargetVariance != null) {
                    tmpTargetNow = this.calculatePortfolioVariance(tmpResult).doubleValue();
                } else if (myTargetReturn != null) {
                    tmpTargetNow = this.calculatePortfolioReturn(tmpResult, this.calculateAssetReturns()).doubleValue();
                } else {
                    tmpTargetNow = tmpTargetValue;
                }
                tmpTargetDiff = tmpTargetNow - tmpTargetValue;
                if (this.getOptimisationOptions().logger_appender != null) {
                    BasicLogger.debug();
                    BasicLogger.debug("RAF:   {}", tmpCurrent);
                    BasicLogger.debug("Last: {}", tmpTargetLast);
                    BasicLogger.debug("Now: {}", tmpTargetNow);
                    BasicLogger.debug("Target: {}", tmpTargetValue);
                    BasicLogger.debug("Diff:   {}", tmpTargetDiff);
                    BasicLogger.debug("Iteration:   {}", tmpResult);
                    BasicLogger.debug();
                }
                if (tmpTargetDiff < _0_0) {
                    tmpLow = tmpCurrent;
                } else if (tmpTargetDiff > _0_0) {
                    tmpHigh = tmpCurrent;
                }
                tmpCurrent = PrimitiveFunction.SQRT.invoke(tmpLow * tmpHigh);
            } while (!TARGET_CONTEXT.isSmall(tmpTargetValue, tmpTargetDiff) && TARGET_CONTEXT.isDifferent(tmpHigh, tmpLow));
        }
    } else {
        tmpResult = this.generateOptimisationModel(this.getRiskAversion().doubleValue()).minimise();
    }
    return this.handle(tmpResult);
}
Also used : Optimisation(org.ojalgo.optimisation.Optimisation) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel)

Aggregations

ExpressionsBasedModel (org.ojalgo.optimisation.ExpressionsBasedModel)1 Optimisation (org.ojalgo.optimisation.Optimisation)1