Pregunta

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 in C#, 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.

Best regards,
Eduardo

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

        }
    }
}
¿Fue útil?

Solución

If the objective function computation throws, I strongly recommend that you run the code through the debugger to identify the exact location of the throwing code. My first bet is that the error happens due to vector size mismatch, for example in the matrix-vector multiplication. If you get to the bottom of this error, chances are that the optimization will then run smoothly.

If you want to try alternative algorithms instead, you might want to take a look at one of the following solutions. They all support specification of (non-) linear constraints, and it is not necessary to provide objective function and constraint gradients.

  • Microsoft Solver Foundation (msdn.microsoft.com/en-us/devlabs/hh145003.aspx), Microsoft's platform for mathematical optimization
  • Funclib, which uses Ipopt as the NLP solver
  • Cscobyla, C# port of the COBYLA2 algorithm, a direct search algorithm (c.f. Nelder-Mead) that supports nonlinear constraints
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top