Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8265569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:51:14+00:00 2026-06-08T04:51:14+00:00

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

  • 0

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

        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:51:15+00:00Added an answer on June 8, 2026 at 4:51 am

    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
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.