Search in sources :

Example 1 with RationalMatrix

use of org.ojalgo.matrix.RationalMatrix in project ojAlgo-finance by optimatika.

the class PortfolioProblems method testP20130329.

/**
 * we have a problem with MarkowitzModel. I have produced a little source that explain the problem. We
 * have set 3 different TargetReturn on the same data.. With targets near to the best end worst target
 * return, the MarkowitzModel works fine. With targets within the interval of best and worst return, seem
 * that the MarkowitzModel is not able to find a correct list of weights. If you try this program and use
 * target of 0.08 or 0.13 or 0.12 you can see a correct solution. With a target of 0.10 MarkowitzModel is
 * not able to find a valid solution.
 */
@Test
public void testP20130329() {
    final BasicMatrix tmpCovariances = RationalMatrix.FACTORY.rows(new double[][] { { 0.00360000, 0.001800000000 }, { 0.001800000000, 0.00090000 } });
    // final Eigenvalue<Double> tmpEvD = Eigenvalue.makePrimitive(true);
    // tmpEvD.compute(tmpCovariances, true);
    // BasicLogger.debug("Eigenvalues: {}", tmpEvD.getEigenvalues());
    final MarketEquilibrium tmpMarketEquilibrium = new MarketEquilibrium(tmpCovariances, BigMath.THOUSAND);
    final Builder<RationalMatrix> tmpExcessReturnsBuilder = RationalMatrix.FACTORY.getBuilder(2, 1);
    tmpExcessReturnsBuilder.set(0, 0, 0.1400);
    tmpExcessReturnsBuilder.set(1, 0, 0.0800);
    final BasicMatrix tmpExcessReturns = tmpExcessReturnsBuilder.build();
    final MarkowitzModel tmpMarkowitzModel = new MarkowitzModel(tmpMarketEquilibrium, tmpExcessReturns);
    tmpMarkowitzModel.setLowerLimit(0, BigMath.ZERO);
    tmpMarkowitzModel.setUpperLimit(0, BigMath.ONE);
    tmpMarkowitzModel.setLowerLimit(1, BigMath.ZERO);
    tmpMarkowitzModel.setUpperLimit(1, BigMath.ONE);
    tmpMarkowitzModel.setShortingAllowed(false);
    for (int t = 8; t <= 14; t++) {
        final BigDecimal tmpTargetReturn = BigFunction.DIVIDE.invoke(new BigDecimal(t), BigMath.HUNDRED);
        tmpMarkowitzModel.setTargetReturn(tmpTargetReturn);
        final List<BigDecimal> tmpWeights = tmpMarkowitzModel.getWeights();
        final State tmpOptimisationState = tmpMarkowitzModel.optimiser().getState();
        if (DEBUG) {
            BasicLogger.debug("State {} {}", tmpOptimisationState, tmpWeights);
        }
        TestUtils.assertTrue("Optimisation State", tmpOptimisationState.isOptimal());
        TestUtils.assertTrue("Asset0 >= 0.0", tmpWeights.get(0).signum() >= 0);
        TestUtils.assertTrue("Asset1 >= 0.0", tmpWeights.get(1).signum() >= 0);
        TestUtils.assertTrue("Asset0 <= 1.0", tmpWeights.get(0).compareTo(BigMath.ONE) <= 0);
        TestUtils.assertTrue("Asset1 <= 1.0", tmpWeights.get(1).compareTo(BigMath.ONE) <= 0);
        TestUtils.assertEquals("Asset0 + Asset1 == 1.0", 1.0, tmpWeights.get(0).add(tmpWeights.get(1)).doubleValue(), 0.0001);
    }
}
Also used : BasicMatrix(org.ojalgo.matrix.BasicMatrix) State(org.ojalgo.optimisation.Optimisation.State) RationalMatrix(org.ojalgo.matrix.RationalMatrix) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Example 2 with RationalMatrix

use of org.ojalgo.matrix.RationalMatrix in project ojAlgo by optimatika.

the class ConvexProblems method testP20080208.

/**
 * Another case of looping in the ActiveSetSolver's constraint (de)activation.
 */
@Test
public void testP20080208() {
    // create expected returns matrix
    final PrimitiveMatrix tmpExpectedReturns = PrimitiveMatrix.FACTORY.rows(new double[][] { { 9.997829 }, { 10.008909 }, { 10.010849 }, { 9.998919 }, { 10.055549 }, { 9.999127 }, { 9.999720 }, { 10.049002 }, { 9.988769 }, { 9.990095 } });
    // create covariance matrix
    final PrimitiveMatrix tmpCovariances = PrimitiveMatrix.FACTORY.rows(new double[][] { { 0.014661954677318977, 3.459112088561122E-4, 0.007798752920910871, 0.0020921425081866503, 0.001846944297640248, 1.0531906931335766E-4, -2.7515614291198E-4, 0.0034083900074454894, 1.1859491261103433E-4, -0.0027421673864628264 }, { 3.459112088561122E-4, 0.008695862475003915, 0.004154360841751649, -2.661685231819661E-4, -0.0015999007544258263, 3.590680217774603E-4, -0.00186976624370318, 0.0010975416828213752, -5.512038393911129E-4, -0.0010605923775744853 }, { 0.007798752920910871, 0.004154360841751649, 0.032945930970836965, 0.0037716078815399324, -2.2919474365382624E-4, 3.3938035033219876E-4, -0.0015613122026082874, 0.0010975697179894332, 2.296422665244149E-4, -0.001709517941787044 }, { 0.0020921425081866503, -2.661685231819661E-4, 0.0037716078815399324, 0.0057162979859706736, 5.573137056500744E-4, 4.91132887765294E-4, -9.94830474250937E-4, 8.331708084069932E-4, -6.595917138470072E-4, -0.0018693519327569541 }, { 0.001846944297640248, -0.0015999007544258263, -2.2919474365382624E-4, 5.573137056500744E-4, 0.03230071314144326, -2.2320789666419312E-4, -2.2639506820057415E-4, 0.010695663287043154, 0.0014569847730040847, 0.002160537177809949 }, { 1.0531906931335766E-4, 3.590680217774603E-4, 3.3938035033219876E-4, 4.91132887765294E-4, -2.2320789666419312E-4, 0.0017540170708301957, 5.153195618913916E-5, 7.339825618468765E-4, -9.309096233432093E-6, -1.814362059740286E-4 }, { -2.7515614291198E-4, -0.00186976624370318, -0.0015613122026082874, -9.94830474250937E-4, -2.2639506820057415E-4, 5.153195618913916E-5, 0.00809348822665732, -0.0017672866424053742, 3.058672988166145E-4, 0.001201578905822851 }, { 0.0034083900074454894, 0.0010975416828213752, 0.0010975697179894332, 8.331708084069932E-4, 0.010695663287043154, 7.339825618468765E-4, -0.0017672866424053742, 0.013913761913235494, 0.0012785124957521252, 5.298368056593439E-4 }, { 1.1859491261103433E-4, -5.512038393911129E-4, 2.296422665244149E-4, -6.595917138470072E-4, 0.0014569847730040847, -9.309096233432093E-6, 3.058672988166145E-4, 0.0012785124957521252, 0.004650801896027841, 5.437156659657787E-4 }, { -0.0027421673864628264, -0.0010605923775744853, -0.001709517941787044, -0.0018693519327569541, 0.002160537177809949, -1.814362059740286E-4, 0.001201578905822851, 5.298368056593439E-4, 5.437156659657787E-4, 0.007359495478781133 } });
    // create asset variables - cost and weighting constraints
    final Variable[] tmpVariables = new Variable[(int) tmpExpectedReturns.countRows()];
    for (int i = 0; i < tmpVariables.length; i++) {
        tmpVariables[i] = new Variable("VAR" + i);
        final int row = i;
        tmpVariables[i].weight(TypeUtils.toBigDecimal(tmpExpectedReturns.get(row, 0)).negate());
        // set the constraints on the asset weights
        // require at least a 8% allocation to each asset
        tmpVariables[i].lower(new BigDecimal("0.08"));
        // require no more than 12% allocation to each asset
        tmpVariables[i].upper(new BigDecimal("0.12"));
    }
    // exception here...
    final RationalMatrix tmpExpected = RationalMatrix.FACTORY.rows(new double[][] { { 0.07999999999998897 }, { 0.1199999999999636 }, { 0.07999999999999526 }, { 0.08000000000004488 }, { 0.11999999999999084 }, { 0.12000000000018606 }, { 0.11999999999996151 }, { 0.12000000000000167 }, { 0.08000000000001738 }, { 0.08000000000005617 } });
    ConvexProblems.doEarly2008(tmpVariables, tmpCovariances, tmpExpected);
}
Also used : Variable(org.ojalgo.optimisation.Variable) PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) BigDecimal(java.math.BigDecimal) RationalMatrix(org.ojalgo.matrix.RationalMatrix) Test(org.junit.jupiter.api.Test)

Example 3 with RationalMatrix

use of org.ojalgo.matrix.RationalMatrix in project ojAlgo by optimatika.

the class ConvexProblems method testP20080124.

/**
 * Another case of looping in the ActiveSetSolver's constraint (de)activation.
 */
@Test
public void testP20080124() {
    // create expected returns matrix
    final PrimitiveMatrix expectedReturnsMatrix = PrimitiveMatrix.FACTORY.rows(new double[][] { { 10.012158 }, { 9.996046 }, { 10.000744 }, { 9.990585 }, { 9.998392 }, { 9.996614 }, { 10.010531 }, { 10.001401 }, { 9.997447 }, { 9.993817 }, { 9.998537 }, { 9.995741 }, { 9.987224 }, { 9.992392 } });
    // create covariance matrix
    final PrimitiveMatrix covarianceMatrix = PrimitiveMatrix.FACTORY.rows(new double[][] { { 0.0013191354374342357, 7.786471466322114E-5, -3.810886655309235E-5, -2.28102405899103E-4, -1.2589115740653127E-4, -1.3247692268411991E-5, 1.422624656557158E-4, -2.7176361887359125E-5, 8.675127894495302E-5, -8.116577287090551E-5, -8.468380774247271E-6, 4.930080166695193E-5, -2.774138231533918E-4, -3.148322898570031E-5 }, { 7.786471466322114E-5, 0.001028250547816086, 8.986425197170406E-4, -1.0341435238579975E-5, 6.472902968147139E-4, 2.9014435841747375E-4, 1.0640414444602855E-4, 5.638694128451113E-4, 6.024515366195699E-4, -1.094867665517237E-4, 6.177221606260711E-6, -5.682215091954099E-5, 2.7178074500896235E-4, 0.0010146062950574643 }, { -3.810886655309235E-5, 8.986425197170406E-4, 0.0012477403456464075, -1.8104847201530489E-4, 9.299199981666304E-4, 3.486383951982303E-4, 1.0246402606579107E-4, 7.009722990366382E-4, 6.545695073447614E-4, -1.1680969171500155E-4, 7.123493385355658E-5, 1.559414390174896E-5, 1.972605480880284E-4, 9.368808845809186E-4 }, { -2.28102405899103E-4, -1.0341435238579975E-5, -1.8104847201530489E-4, 6.250793590180099E-4, -5.4721911720097E-6, 1.3081826023829458E-4, -5.644046856412501E-5, -1.1282043806099452E-5, -6.729835202722053E-5, 1.3929681542737307E-4, 3.698155248637573E-6, 5.0269944317023966E-5, 5.344931460074395E-4, -1.1654882792112444E-4 }, { -1.2589115740653127E-4, 6.472902968147139E-4, 9.299199981666304E-4, -5.4721911720097E-6, 0.001181357476541527, 3.0334522038028824E-4, 2.6983840497611894E-4, 6.983493701701867E-4, 5.68816790613126E-4, -7.899505299987754E-5, 1.05074262063586E-5, 1.137295188785598E-4, 1.9732025136606058E-4, 6.631330613471645E-4 }, { -1.3247692268411991E-5, 2.9014435841747375E-4, 3.486383951982303E-4, 1.3081826023829458E-4, 3.0334522038028824E-4, 3.372068413122505E-4, 1.1067468759384309E-4, 2.6589126866881173E-4, 2.1364931019670806E-4, -4.201239472520589E-5, 2.32769639721745E-5, 5.847559594073046E-6, 1.9925897592339058E-4, 1.9671375386540353E-4 }, { 1.422624656557158E-4, 1.0640414444602855E-4, 1.0246402606579107E-4, -5.644046856412501E-5, 2.6983840497611894E-4, 1.1067468759384309E-4, 0.001484755064835215, 1.2295961703024863E-4, 1.0843198781689372E-4, -2.1292328294313923E-5, -4.152686600769749E-6, 1.163599038579726E-4, -3.14739599261259E-4, 2.4519847977412686E-4 }, { -2.7176361887359125E-5, 5.638694128451113E-4, 7.009722990366382E-4, -1.1282043806099452E-5, 6.983493701701867E-4, 2.6589126866881173E-4, 1.2295961703024863E-4, 5.563328439145604E-4, 4.4816730200338125E-4, -3.4729832814007256E-5, -6.028818604193519E-7, 3.192976987126335E-5, 1.7402262469809026E-4, 5.182632389125651E-4 }, { 8.675127894495302E-5, 6.024515366195699E-4, 6.545695073447614E-4, -6.729835202722053E-5, 5.68816790613126E-4, 2.1364931019670806E-4, 1.0843198781689372E-4, 4.4816730200338125E-4, 6.277134808325468E-4, -4.988229718603287E-5, -5.5018781802344255E-6, -1.3231260300518203E-5, 8.214207901880769E-5, 5.841470978796527E-4 }, { -8.116577287090551E-5, -1.094867665517237E-4, -1.1680969171500155E-4, 1.3929681542737307E-4, -7.899505299987754E-5, -4.201239472520589E-5, -2.1292328294313923E-5, -3.4729832814007256E-5, -4.988229718603287E-5, 3.5152692612068785E-4, -9.358092257358399E-6, 4.962216896551324E-6, 1.291957229930161E-4, -1.5046975508620905E-4 }, { -8.468380774247271E-6, 6.177221606260711E-6, 7.123493385355658E-5, 3.698155248637573E-6, 1.05074262063586E-5, 2.32769639721745E-5, -4.152686600769749E-6, -6.028818604193519E-7, -5.5018781802344255E-6, -9.358092257358399E-6, 4.8495980378967104E-5, 1.1704645004909169E-5, 1.814918597253607E-5, 1.2448218299234062E-5 }, { 4.930080166695193E-5, -5.682215091954099E-5, 1.559414390174896E-5, 5.0269944317023966E-5, 1.137295188785598E-4, 5.847559594073046E-6, 1.163599038579726E-4, 3.192976987126335E-5, -1.3231260300518203E-5, 4.962216896551324E-6, 1.1704645004909169E-5, 1.802684481609152E-4, 1.0475986793792914E-5, -4.113641419540392E-5 }, { -2.774138231533918E-4, 2.7178074500896235E-4, 1.972605480880284E-4, 5.344931460074395E-4, 1.9732025136606058E-4, 1.9925897592339058E-4, -3.14739599261259E-4, 1.7402262469809026E-4, 8.214207901880769E-5, 1.291957229930161E-4, 1.814918597253607E-5, 1.0475986793792914E-5, 7.843917688960864E-4, 1.231995848356005E-4 }, { -3.148322898570031E-5, 0.0010146062950574643, 9.368808845809186E-4, -1.1654882792112444E-4, 6.631330613471645E-4, 1.9671375386540353E-4, 2.4519847977412686E-4, 5.182632389125651E-4, 5.841470978796527E-4, -1.5046975508620905E-4, 1.2448218299234062E-5, -4.113641419540392E-5, 1.231995848356005E-4, 0.0011885193322126312 } });
    // create asset variables - cost and weighting constraints
    final Variable[] tmpVariables = new Variable[(int) expectedReturnsMatrix.countRows()];
    for (int i = 0; i < tmpVariables.length; i++) {
        tmpVariables[i] = new Variable("VAR" + i);
        final int row = i;
        tmpVariables[i].weight(TypeUtils.toBigDecimal(expectedReturnsMatrix.get(row, 0)).negate());
        // set the constraints on the asset weights
        // require at least a 2% allocation to each asset
        tmpVariables[i].lower(new BigDecimal("0.05"));
        // require no more than 80% allocation to each asset
        tmpVariables[i].upper(new BigDecimal("0.35"));
    // tmpVariables[i].setUpperLimit(new BigDecimal("1.00"));
    }
    final RationalMatrix tmpExpected = RationalMatrix.FACTORY.rows(new double[][] { { 0.3166116715239731 }, { 0.050000000001624065 }, { 0.04999999999827016 }, { 0.05000000000034928 }, { 0.049999999999891145 }, { 0.049999999997416125 }, { 0.08338832846287945 }, { 0.05000000000178943 }, { 0.05000000000085164 }, { 0.04999999999937388 }, { 0.050000000012470555 }, { 0.04999999999966884 }, { 0.050000000000484546 }, { 0.049999999995857476 } });
    ConvexProblems.doEarly2008(tmpVariables, covarianceMatrix, tmpExpected);
}
Also used : Variable(org.ojalgo.optimisation.Variable) PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) BigDecimal(java.math.BigDecimal) RationalMatrix(org.ojalgo.matrix.RationalMatrix) Test(org.junit.jupiter.api.Test)

Example 4 with RationalMatrix

use of org.ojalgo.matrix.RationalMatrix in project ojAlgo by optimatika.

the class ConvexProblems method testP20080117.

/**
 * The ActiveSetSolver ended up in a loop activating/deactivating constraints. Eventually it returned
 * null, and that eventually resulted in a NullPointerException. Since Q is not positive semidefinite
 * validation has to be turned off
 */
@Test
public void testP20080117() {
    // create expected returns matrix
    final PrimitiveMatrix tmpReturns = PrimitiveMatrix.FACTORY.rows(new double[][] { { -0.007155942261937039 }, { -0.003665887902733331 }, { -0.004130184341000032 }, { -0.005639860515211043 }, { 0.0007211966666666817 }, { 0.0003258225000000077 }, { -0.005754291666666666 }, { -0.004264291666666667 }, { -0.0017500000000000003 } });
    // create covariance matrix
    final PrimitiveMatrix tmpCovariances = PrimitiveMatrix.FACTORY.rows(new double[][] { { 0.001561410465201063, 0.00006366128201274021, -0.0001323096896759724, 0.0000909074052724909, 0.00003172000033558704, 0.00001955483223848944, -0.00013771504482647386, -0.00004858457275314645, -0.000012954723060403266 }, { 0.00006366128201274021, 0.00016419786524761803, -0.00001566288911558343, -0.00008688646089751923, 0.0000027349925543017186, 0.0000012356159598500247, -0.000024367796639005863, -0.000017576048221096555, -0.0000070052245518771815 }, { -0.0001323096896759724, -0.00001566288911558343, 0.0001430155985985913, 0.00007675339168559199, -0.00007600590426518823, 0.000032976538909267937, 0.00009520305608240259, 0.00007373075639042642, -0.000007477057858706954 }, { 0.0000909074052724909, -0.00008688646089751923, 0.00007675339168559199, 0.000967519991100896, -0.0000533460293834595, 0.00008665760416026126, 0.00014591175388747613, 0.0001232364989586903, 0.00011097998789484925 }, { 0.00003172000033558704, 0.0000027349925543017186, -0.00007600590426518823, -0.0000533460293834595, 0.000025267064307337795, -0.00003089584520279407, -0.00005593123237578969, -0.000017013960349712132, 0.0000013056146551724419 }, { 0.00001955483223848944, 0.0000012356159598500247, 0.000032976538909267937, 0.00008665760416026126, -0.00003089584520279407, 0.0001625499447274783, 0.00008242949058588471, 0.00010276895784859992, 0.0000005898510775862205 }, { -0.00013771504482647386, -0.000024367796639005863, 0.00009520305608240259, 0.00014591175388747613, -0.00005593123237578969, 0.00008242949058588471, 0.000560956958802083, 0.0002838794236862429, 0.00009143821659482758 }, { -0.00004858457275314645, -0.000017576048221096555, 0.00007373075639042642, 0.0001232364989586903, -0.000017013960349712132, 0.00010276895784859992, 0.0002838794236862429, 0.00021068964250359204, 0.00004461044181034483 }, { -0.000012954723060403266, -0.0000070052245518771815, -0.000007477057858706954, 0.00011097998789484925, 0.0000013056146551724419, 0.0000005898510775862205, 0.00009143821659482758, 0.00004461044181034483, 0.00006761920797413792 } });
    // final MarketEquilibrium tmpME = new MarketEquilibrium(tmpCovariances, BigMath.PI.multiply(BigMath.E));
    // create asset variables - cost and weighting constraints
    final Variable[] tmpVariables = new Variable[(int) tmpReturns.countRows()];
    for (int i = 0; i < tmpVariables.length; i++) {
        tmpVariables[i] = new Variable("VAR" + i);
        final int row = i;
        tmpVariables[i].weight(TypeUtils.toBigDecimal(tmpReturns.get(row, 0)).negate());
        // set the constraints on the asset weights
        // require at least a 2% allocation to each asset
        tmpVariables[i].lower(new BigDecimal("0.02"));
        // require no more than 80% allocation to each asset
        tmpVariables[i].upper(new BigDecimal("0.80"));
    }
    final RationalMatrix tmpExpected = RationalMatrix.FACTORY.rows(new double[][] { { 0.02 }, { 0.02 }, { 0.02 }, { 0.02 }, { 0.80 }, { 0.06 }, { 0.02 }, { 0.02 }, { 0.02 } });
    ConvexProblems.doEarly2008(tmpVariables, tmpCovariances, tmpExpected);
}
Also used : Variable(org.ojalgo.optimisation.Variable) PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) BigDecimal(java.math.BigDecimal) RationalMatrix(org.ojalgo.matrix.RationalMatrix) Test(org.junit.jupiter.api.Test)

Example 5 with RationalMatrix

use of org.ojalgo.matrix.RationalMatrix in project ojAlgo by optimatika.

the class ConvexProblems method testP20080204.

/**
 * Another case of looping in the ActiveSetSolver's constraint (de)activation.
 */
@Test
public void testP20080204() {
    // create expected returns matrix
    final PrimitiveMatrix tmpExpectedReturns = PrimitiveMatrix.FACTORY.rows(new double[][] { { 9.994620 }, { 10.011389 }, { 10.004353 }, { 9.998293 }, { 10.056851 }, { 9.997920 }, { 9.999011 }, { 10.050971 }, { 9.989124 }, { 9.989912 } });
    // create covariance matrix
    final PrimitiveMatrix tmpCovariances = PrimitiveMatrix.FACTORY.rows(new double[][] { { 0.014531344652473037, 4.444675045533674E-4, 0.007234717654072837, -9.455312097865225E-4, 0.0016345464996349748, 1.5256808879495097E-4, 0.00226325818749439, 0.003534367267672946, -4.2669306842991344E-5, 6.902267133060073E-5 }, { 4.444675045533674E-4, 0.008511422662647488, 0.0039821105759899845, 5.543408872612397E-4, -0.0015797828516888929, 1.3505400134130176E-4, -1.5215492836142527E-4, 9.381119889780555E-4, -4.5861204247023084E-4, -2.4226694503921645E-5 }, { 0.007234717654072837, 0.0039821105759899845, 0.031037646466036784, -0.0022701157440735394, -3.187028053841407E-4, 5.182461519304137E-4, -3.681340242039795E-4, 0.001526984686166616, 1.603885118040309E-4, -1.359858314115312E-4 }, { -9.455312097865225E-4, 5.543408872612397E-4, -0.0022701157440735394, 0.005637141895898889, 7.89377521930992E-4, 5.004781934410127E-4, -9.79221967172284E-4, -2.912861228906251E-4, 7.842012412867984E-4, 0.0010866808807429532 }, { 0.0016345464996349748, -0.0015797828516888929, -3.187028053841407E-4, 7.89377521930992E-4, 0.03263062480163135, 6.041130577612135E-5, 6.883489096710362E-4, 0.010830183513887228, 0.0016425608963272292, 0.002481787652249504 }, { 1.5256808879495097E-4, 1.3505400134130176E-4, 5.182461519304137E-4, 5.004781934410127E-4, 6.041130577612135E-5, 0.001733612375709255, 2.8742157640452992E-5, -3.654534740999083E-4, 9.896178753749563E-5, -1.703972415991329E-5 }, { 0.00226325818749439, -1.5215492836142527E-4, -3.681340242039795E-4, -9.79221967172284E-4, 6.883489096710362E-4, 2.8742157640452992E-5, 0.008167191690212253, -0.0010075092076978207, -4.293010139199468E-4, -6.615640978331292E-4 }, { 0.003534367267672946, 9.381119889780555E-4, 0.001526984686166616, -2.912861228906251E-4, 0.010830183513887228, -3.654534740999083E-4, -0.0010075092076978207, 0.013796198054188104, 0.0013541164478127973, -2.2401086720669167E-5 }, { -4.2669306842991344E-5, -4.5861204247023084E-4, 1.603885118040309E-4, 7.842012412867984E-4, 0.0016425608963272292, 9.896178753749563E-5, -4.293010139199468E-4, 0.0013541164478127973, 0.004743485149287524, 0.0011464293217708277 }, { 6.902267133060073E-5, -2.4226694503921645E-5, -1.359858314115312E-4, 0.0010866808807429532, 0.002481787652249504, -1.703972415991329E-5, -6.615640978331292E-4, -2.2401086720669167E-5, 0.0011464293217708277, 0.007398229661528494 } });
    // create asset variables - cost and weighting constraints
    final Variable[] tmpVariables = new Variable[(int) tmpExpectedReturns.countRows()];
    for (int i = 0; i < tmpVariables.length; i++) {
        tmpVariables[i] = new Variable("VAR" + i);
        final int row = i;
        tmpVariables[i].weight(TypeUtils.toBigDecimal(tmpExpectedReturns.get(row, 0)).negate());
        // set the constraints on the asset weights
        // require at least a 8% allocation to each asset
        tmpVariables[i].lower(new BigDecimal("0.08"));
        // require no more than 12% allocation to each asset
        tmpVariables[i].upper(new BigDecimal("0.12"));
    }
    // exception here...
    final RationalMatrix tmpExpected = RationalMatrix.FACTORY.rows(new double[][] { { 0.08000000000000602 }, { 0.12000000000002384 }, { 0.08000000000000054 }, { 0.10643232489190736 }, { 0.12000000000002252 }, { 0.11999999999979595 }, { 0.09356767510776097 }, { 0.11999999999998154 }, { 0.07999999999999653 }, { 0.08000000000000498 } });
    ConvexProblems.doEarly2008(tmpVariables, tmpCovariances, tmpExpected);
}
Also used : Variable(org.ojalgo.optimisation.Variable) PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) BigDecimal(java.math.BigDecimal) RationalMatrix(org.ojalgo.matrix.RationalMatrix) Test(org.junit.jupiter.api.Test)

Aggregations

BigDecimal (java.math.BigDecimal)8 Test (org.junit.jupiter.api.Test)8 RationalMatrix (org.ojalgo.matrix.RationalMatrix)8 PrimitiveMatrix (org.ojalgo.matrix.PrimitiveMatrix)5 Variable (org.ojalgo.optimisation.Variable)5 ComplexNumber (org.ojalgo.scalar.ComplexNumber)2 NumberContext (org.ojalgo.type.context.NumberContext)2 BasicMatrix (org.ojalgo.matrix.BasicMatrix)1 State (org.ojalgo.optimisation.Optimisation.State)1