[ojAlgo-user] Markowitz Problem
Mathematics, linear algebra and optimisation
Brought to you by:
apete
From: Cristian B. <cri...@ho...> - 2014-01-16 20:23:14
|
/* * Copyright 1997-2014 Optimatika (www.optimatika.se) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ //package org.ojalgo.finance.portfolio; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; //import org.ojalgo.TestUtils; import org.ojalgo.access.Access2D.Builder; import org.ojalgo.constant.BigMath; import org.ojalgo.function.PrimitiveFunction; import org.ojalgo.function.UnaryFunction; import org.ojalgo.matrix.BasicMatrix; import org.ojalgo.matrix.PrimitiveMatrix; import org.ojalgo.matrix.store.PhysicalStore; import org.ojalgo.matrix.store.PrimitiveDenseStore; import org.ojalgo.finance.portfolio.MarketEquilibrium; import org.ojalgo.finance.portfolio.MarkowitzModel; import org.ojalgo.random.Uniform; import org.ojalgo.type.StandardType; public class TestEquilibrium { public static void main(final String[] args) { final int assetNum = 2; final double[][] om = { { 0.0036, -0.0054 }, { -0.0054, 0.0324 } }; final TestEquilibrium tm = new TestEquilibrium(); final BasicMatrix covariances = tm.getACovariances(om); System.out.println(covariances); final BigDecimal riskAversion = new BigDecimal(1000.0); MarketEquilibrium marketEquilibrium = new MarketEquilibrium(covariances, riskAversion); marketEquilibrium = marketEquilibrium.clean(); Builder<PrimitiveMatrix> expectedExcessReturns1 = PrimitiveMatrix.getBuilder(assetNum, 1); expectedExcessReturns1 = expectedExcessReturns1.set(0, 0, 0.0400); expectedExcessReturns1 = expectedExcessReturns1.set(1, 0, 0.1300); System.out.println("Return Matrix:" + expectedExcessReturns1.build()); final MarkowitzModel markowitzModel = new MarkowitzModel(marketEquilibrium, expectedExcessReturns1.build()); //markowitzModel.setTargetReturn(new BigDecimal("0.08")); markowitzModel.setTargetReturn(new BigDecimal("0.20")); for (int i = 0; i < assetNum; i++) { markowitzModel.setLowerLimit(i, new BigDecimal(0.0)); markowitzModel.setUpperLimit(i, new BigDecimal(1.0)); } final List<BigDecimal> re = markowitzModel.getWeights(); System.out.println("=======result===================="); for (int i = 0; i < re.size(); i++) { System.out.println(re.get(i)); } System.out.println("=======result===================="); System.out.println(markowitzModel.getMeanReturn()); System.out.println(markowitzModel.getReturnVariance()); return; } public TestEquilibrium() { super(); } public BasicMatrix<?> getACovariances(final double[][] returns) { final int row = returns.length; final int col = returns[0].length; Builder<PrimitiveMatrix> covariances = PrimitiveMatrix.getBuilder(row, col); for (int i = 1; i <= row; i++) { for (int j = i; j <= col; j++) { covariances = covariances.set(i - 1, j - 1, returns[i - 1][j - 1]); covariances = covariances.set(j - 1, i - 1, returns[j - 1][i - 1]); } } return covariances.build(); } } |