# Risk-Parity Portfolio Optimization using Extreme Optimization in C#

I'm trying to create a risk-parity portfolio in C# using the Extreme Optimization routines.

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).

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.

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.

1. What am I doing wrong?

2. Is there a faster way to do this optimization that I'm not aware of?

3. If you don't know the EO Libraries, but could implement this with something else, could you please drop a comment on how you would go about solving this?

By the way, the details on the portfolio construction are in the comments of the distance function, in case you're interested.

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<Vector, double> distance = DistanceFunction;
np1.ObjectiveFunction = distance;
np1.InitialGuess = Vector.CreateConstant(2, 1.0 / ((double)2));

np1.AddNonlinearConstraint(x => x + x, 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...");

}

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 < 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));

}
}
}

• Are you sure of your constraint? aren't you willing to do $x_1+x_2=1$? – SRKX Jul 15 '12 at 20:04
• It isn't necessary since I can give them the right weights after the optimization (the distance from the optimal risk-parity weights won't change after a multiplication by a constant). – Eduardo Sahione Jul 16 '12 at 10:46
• The focus of portfolio optimization is the correlation among assets. If assets are perfectly related, the risk-parity approach does not do you any good. By the way, what exactly is the benefits of this risk-parity approach ? (purely for diversification ? ) – user2935 Sep 12 '12 at 3:35
• The model you are trying to solve is a convex QP, which can be solved with solvers like cplex and gurobi which are free for students and have c# interfaces. – David Nehme Sep 12 '12 at 16:51

In case anyone is interested, I solved it using Nelder-Mead's algorithm instead. The performance could be better, but I didn't want to waste any more time in it.

Here's the final solution:

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)
{
Func<Vector, double> distance = DistanceFunction;

nm1.ObjectiveFunction = DistanceFunction;
nm1.ContractionFactor = 0.5;
nm1.ExpansionFactor = 2;
nm1.ReflectionFactor = -2;
nm1.SolutionTest.AbsoluteTolerance = 1e-15;
nm1.InitialGuess = Vector.CreateConstant(2, 1.0 / ((double)2));
nm1.ExtremumType = ExtremumType.Minimum;
Vector solution = nm1.FindExtremum();

Console.WriteLine("Solution: {0:F6}", solution);
Console.WriteLine("  Estimated error: {0}", nm1.EstimatedError);
Console.WriteLine("  # iterations: {0}", nm1.IterationsNeeded);
Console.WriteLine("  # function evaluations: {0}", nm1.EvaluationsNeeded);
Console.Write("Press Enter key to exit...");

}

private static double DistanceFunction(Vector Weights)
{
Matrix Sigma = Matrix.Create(new double[,] {
{0.1, 0.23},
{0.23, 0.7}
});
// 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 < 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));

}
}
}