Note that there are some explanatory texts on larger screens.

plurals
  1. PORisk-Parity Portfolio Optimization using Extreme Optimization in C#
    text
    copied!<p>I'm trying to create a risk-parity portfolio in C# using the Extreme Optimization routines.</p> <p>I'm mostly trying them out to see if I like them or not before I buy them (I'm a student so money is tight).</p> <p>My idea was to implement this new kind of portfolio optimization called risk-parity. It basically says that in order to diversify your portfolio you should give equal risk to each of its components.</p> <p>I'm getting a null error when running np1.Solve() and I don't understand why. I thought that everything else was calculated by Extreme Optimization.<br> 1. What am I doing wrong?<br> 2. Is there a faster way to do this optimization that I'm not aware of?<br> 3. If you don't know the EO Libraries, but could implement this with something else in C#, could you please drop a comment on how you would go about solving this?</p> <p>By the way, the details on the portfolio construction are in the comments of the distance function, in case you're interested.</p> <p>Best regards,<br> Eduardo</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using Extreme.Statistics; using Extreme.Mathematics; using Extreme.Mathematics.Optimization; namespace TestingRiskParityOptimization { class Program { static void Main(string[] args) { NonlinearProgram np1 = new NonlinearProgram(2); Func&lt;Vector, double&gt; distance = DistanceFunction; np1.ObjectiveFunction = distance; np1.InitialGuess = Vector.CreateConstant(2, 1.0 / ((double)2)); np1.AddNonlinearConstraint(x =&gt; x[0] + x[1], ConstraintType.GreaterThanOrEqual, 0); Vector solution = np1.Solve(); Console.WriteLine("Solution: {0:F6}", solution); Console.WriteLine("Optimal value: {0:F6}", np1.OptimalValue); Console.WriteLine("# iterations: {0}", np1.SolutionReport.IterationsNeeded); Console.Write("Press Enter key to exit..."); Console.ReadLine(); } private static double DistanceFunction(Vector Weights) { Matrix Sigma = Matrix.Create(new double[,] { {0.1, 0.2}, {0.2, 0.4} }); // if VarP = Weights' * CovarMatrix * Weights and VolP = sqrt(VarP) // Then the marginal contribution to risk of an asset is the i-th number of // Sigma*Weights*VolP // And thus the contribution to risk of an asset is simply Weights . (Sigma*Weights/VarP) // we need to find weights such that Weights (i) * Row(i) of (Sigma*Weights/VarP) = 1/N // that is we want to minimize the distance of row vector (Weights (i) * Row(i) of (Sigma*Weights/VarP)) and vector 1/N double Variance = Vector.DotProduct(Weights, Sigma * Weights); Vector Beta = Sigma * Weights / Variance; for (int i = 0; i &lt; Beta.Length; i++) { // multiplies row of beta by weight to find the percent contribution to risk Beta[i] = Weights[i] * Beta[i]; } Vector ObjectiveVector = Vector.CreateConstant(Weights.Length, 1.0 / ((double)Weights.Length)); Vector Distance = Vector.Subtract(Beta, ObjectiveVector); return Math.Sqrt(Vector.DotProduct(Distance, Distance)); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload