/// <summary> Build and run the risk model in multiple iterations. /// Put the results into the plan. /// </summary> public bool BuildRiskModel(DataTable plan, int iterations) { int m = _stockNames.Length; for (int reqIx = 0; reqIx < iterations; reqIx++) { InteriorPointSolver solver = new InteriorPointSolver(); int[] allocations = new int[m]; for (int invest = 0; invest < m; invest++) { string name = _stockNames[invest]; solver.AddVariable(name, out allocations[invest]); solver.SetBounds(allocations[invest], 0, 1); } int expectedReturn; solver.AddRow("expectedReturn", out expectedReturn); // expected return must beat the minimum asked solver.SetBounds(expectedReturn, (double)plan.Rows[reqIx]["minimum"], double.PositiveInfinity); int unity; solver.AddRow("Investments sum to one", out unity); solver.SetBounds(unity, 1, 1); // expected return is a weighted linear combination of investments. // unity is a simple sum of the investments for (int invest = m; 0 <= --invest; ) { solver.SetCoefficient(expectedReturn, allocations[invest], _means[invest]); solver.SetCoefficient(unity, allocations[invest], 1); } // The variance of the result is a quadratic combination of the covariants and allocations. int variance; solver.AddRow("variance", out variance); for (int invest = m; 0 <= --invest; ) { for (int jnvest = m; 0 <= --jnvest; ) { solver.SetCoefficient(variance, _covariance[invest, jnvest], allocations[invest], allocations[jnvest]); } } // the goal is to minimize the variance, given the linear lower bound on asked return. solver.AddGoal(variance, 0, true); InteriorPointSolverParams lpParams = new InteriorPointSolverParams(); solver.Solve(lpParams); if (solver.Result != LinearResult.Optimal) return false; for (int invest = m; 0 <= --invest; ) { plan.Rows[reqIx][_stockNames[invest]] = (double)solver.GetValue(allocations[invest]); } plan.Rows[reqIx]["actual"] = (double)solver.GetValue(expectedReturn); plan.Rows[reqIx]["Std.Dev."] = Math.Sqrt((double)solver.Statistics.Primal); plan.Rows[reqIx]["variance"] = (double)solver.GetValue(variance); } return true; }
public bool ModeloRiesgo(DataTable plan, int iterations) { int m = Companias.Length; for (int reqIx = 0; reqIx < iterations; reqIx++) { InteriorPointSolver solver = new InteriorPointSolver(); int[] asignaciones = new int[m]; for (int i = 0; i < m; i++) { solver.AddVariable(Companias[i], out asignaciones[i]); solver.SetBounds(asignaciones[i], 0, 1); } int rentabilidad; solver.AddRow("rentabilidad", out rentabilidad); // La rentabilidad debe superar el minimo pedido solver.SetBounds(rentabilidad, (double)plan.Rows[reqIx]["minimum"], double.PositiveInfinity); int unity; solver.AddRow("Invertir la suma a", out unity); solver.SetBounds(unity, 1, 1); // El rendimiento esperado es una combinacion lineal ponderada de las inversiones // unity = suma de inversiones for (int invest = m; 0 <= --invest;) { solver.SetCoefficient(rentabilidad, asignaciones[invest], media[invest]); solver.SetCoefficient(unity, asignaciones[invest], 1); } // The variance of the result is a quadratic combination of the covariants and allocations. int varianza; solver.AddRow("varianza", out varianza); for (int invest = m; 0 <= --invest;) { for (int jnvest = m; 0 <= --jnvest;) { solver.SetCoefficient(varianza, covarianza[invest, jnvest], asignaciones[invest], asignaciones[jnvest]); } } // the goal is to minimize the variance, given the linear lower bound on asked return. solver.AddGoal(varianza, 0, true); InteriorPointSolverParams lpParams = new InteriorPointSolverParams(); solver.Solve(lpParams); if (solver.Result != LinearResult.Optimal) { return(false); } for (int invest = m; 0 <= --invest;) { plan.Rows[reqIx][Companias[invest]] = (double)solver.GetValue(asignaciones[invest]); } plan.Rows[reqIx]["actual"] = (double)solver.GetValue(rentabilidad); plan.Rows[reqIx]["Std.Dev."] = Math.Sqrt((double)solver.Statistics.Primal); } return(true); }
public void SolveQuadratic(RvolSolveModel model_) { int m = model_.CurrencyLines.Length; //int iterations = 1; //for (int reqIx = 0; reqIx < iterations; ++reqIx) { InteriorPointSolver solver = new InteriorPointSolver(); int[] allocations = new int[m]; for (int invest = 0; invest < m; ++invest) { string name = model_.CurrencyLines[invest].Ccy.Code; solver.AddVariable(name, out allocations[invest]); solver.SetBounds(allocations[invest], model_.CurrencyLines[invest].MinWeight, model_.CurrencyLines[invest].MaxWeight); } int expectedReturn; solver.AddRow("expectedReturn", out expectedReturn); //int sumZero; //solver.AddRow("sumZero", out sumZero); for (int invest = 0; invest < m; ++invest) { solver.SetCoefficient(expectedReturn, allocations[invest], model_.CurrencyLines[invest].ExpectedReturn); //solver.SetCoefficient(sumZero, allocations[invest], 0); } int variance; solver.AddRow("variance", out variance); for (int invest = 0; invest < m; ++invest) { for (int jnvest = 0; jnvest < m; ++jnvest) { solver.SetCoefficient(variance, model_.Covar.Data[invest, jnvest], allocations[invest], allocations[jnvest]); } } var varianceTarget = Math.Pow(model_.TargetVol, 2d); solver.SetBounds(variance, double.NegativeInfinity, varianceTarget); // max expected return solver.AddGoal(expectedReturn, 1, false); InteriorPointSolverParams lpParams = new InteriorPointSolverParams(); solver.Solve(lpParams); //if (solver.Result != LinearResult.Optimal) for (int invest = m; 0 <= --invest;) { model_.CurrencyLines[invest].Weight = (double) solver.GetValue(allocations[invest]); } } }
public bool BuildRiskModel() { int m = portfolio.NumPositions; InteriorPointSolver solver = new InteriorPointSolver(); int[] allocations = new int[m]; int counter = 0; foreach (Position p in portfolio.Positions.Values) { solver.AddVariable(p.Symbol, out allocations[counter]); solver.SetBounds(allocations[counter], -1, 1); counter++; } int expectedReturn; solver.AddRow("expectedReturn", out expectedReturn); // expected return must beat the minimum asked solver.SetBounds(expectedReturn, minReturn, double.PositiveInfinity); int unity; solver.AddRow("Investments sum to one", out unity); solver.SetBounds(unity, -1, 1); // expected return is a weighted linear combination of investments. // unity is a simple sum of the investments for (int invest = m; 0 <= --invest;) { solver.SetCoefficient(expectedReturn, allocations[invest], returns[invest]); solver.SetCoefficient(unity, allocations[invest], 1); } // The variance of the result is a quadratic combination of the covariants and allocations. int variance; solver.AddRow("variance", out variance); for (int invest = m; 0 <= --invest;) { for (int jnvest = m; 0 <= --jnvest;) { solver.SetCoefficient(variance, covariance[invest, jnvest], allocations[invest], allocations[jnvest]); } } // the goal is to minimize the variance, given the linear lower bound on asked return. solver.AddGoal(variance, 0, true); InteriorPointSolverParams lpParams = new InteriorPointSolverParams(); solver.Solve(lpParams); if (solver.Result != LinearResult.Optimal) return false; for (int i = 0; i < m; i++) { } counter = 0; foreach (Position p in portfolio.Positions.Values) { Console.Write(p.Symbol + " " + (double)solver.GetValue(allocations[counter++]) + " "); Console.WriteLine(); } Console.WriteLine((double) solver.GetValue(expectedReturn) + " " + Math.Sqrt((double) solver.Statistics.Primal) + "\n"); return true; }
public bool BuildRiskModel() { int m = portfolio.NumPositions; InteriorPointSolver solver = new InteriorPointSolver(); int[] allocations = new int[m]; int counter = 0; foreach (Position p in portfolio.Positions.Values) { solver.AddVariable(p.Symbol, out allocations[counter]); solver.SetBounds(allocations[counter], -1, 1); counter++; } int expectedReturn; solver.AddRow("expectedReturn", out expectedReturn); // expected return must beat the minimum asked solver.SetBounds(expectedReturn, minReturn, double.PositiveInfinity); int unity; solver.AddRow("Investments sum to one", out unity); solver.SetBounds(unity, -1, 1); // expected return is a weighted linear combination of investments. // unity is a simple sum of the investments for (int invest = m; 0 <= --invest;) { solver.SetCoefficient(expectedReturn, allocations[invest], returns[invest]); solver.SetCoefficient(unity, allocations[invest], 1); } // The variance of the result is a quadratic combination of the covariants and allocations. int variance; solver.AddRow("variance", out variance); for (int invest = m; 0 <= --invest;) { for (int jnvest = m; 0 <= --jnvest;) { solver.SetCoefficient(variance, covariance[invest, jnvest], allocations[invest], allocations[jnvest]); } } // the goal is to minimize the variance, given the linear lower bound on asked return. solver.AddGoal(variance, 0, true); InteriorPointSolverParams lpParams = new InteriorPointSolverParams(); solver.Solve(lpParams); if (solver.Result != LinearResult.Optimal) { return(false); } for (int i = 0; i < m; i++) { } counter = 0; foreach (Position p in portfolio.Positions.Values) { Console.Write(p.Symbol + " " + (double)solver.GetValue(allocations[counter++]) + " "); Console.WriteLine(); } Console.WriteLine((double)solver.GetValue(expectedReturn) + " " + Math.Sqrt((double)solver.Statistics.Primal) + "\n"); return(true); }
public OptimizationResult OptimizePortfolioAllocation(OptimizationData data) { int assetCount = data.Stocks.Count; InteriorPointSolver solver = new InteriorPointSolver(); int[] allocations = new int[assetCount]; for (int i = 0; i < assetCount; i++) { solver.AddVariable(data.Stocks[i].Symbol, out allocations[i]); if (data.Stocks[i].Symbol == "SPY") solver.SetBounds(allocations[i], 0, 0); else solver.SetBounds(allocations[i], 0, 1); } int expectedRateOfReturn; solver.AddRow("expectedRateOfReturn", out expectedRateOfReturn); solver.SetBounds(expectedRateOfReturn, data.MinimumReturn, double.PositiveInfinity); int unity; solver.AddRow("Investments sum to one", out unity); solver.SetBounds(unity, 1, 1); for (int i = 0; i < assetCount; i++) { solver.SetCoefficient(expectedRateOfReturn, allocations[i], data.Stocks[i].MeanReturnRate); solver.SetCoefficient(unity, allocations[i], 1); } int variance; solver.AddRow("variance", out variance); for (int i = 0; i < assetCount; i++) { for (int j = 0; j < assetCount; j++) { solver.SetCoefficient(variance, data.Stocks[i].Covariances[data.Stocks[j].Symbol], allocations[i], allocations[j]); } } solver.AddGoal(variance, 0, true); InteriorPointSolverParams lpParams = new InteriorPointSolverParams(); solver.Solve(lpParams); bool optimal = false; bool feasible = false; if (solver.Result == LinearResult.Optimal) { optimal = feasible = true; } else if (solver.Result == LinearResult.Feasible) { optimal = false; feasible = true; } List<AssetResult> assetResults = new List<AssetResult>(); for (int i = 0; i < assetCount; i++) { assetResults.Add(new AssetResult { Symbol = data.Stocks[i].Symbol, Allocation = (double)solver.GetValue(allocations[i]) }); } OptimizationResult result = new OptimizationResult { Optimal = optimal, Feasible = feasible, ExpectedReturn = (double)solver.GetValue(expectedRateOfReturn), Results = assetResults }; return result; }