From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Quadratic optimization Problem

Hi All,

I am having difficulty with a portfolio optimization problem. I can achieve what I want with amazing ease using MS Excel's solver add-in but I need to perform the same calculations within a LabVIEW app.

In the MS Excel solver you can find the portfolio with the lowest standard deviation by running the solver, selecting "min" and pointing it to the standard deviation cell and ask it to vary the portfolio stock weightings with a couple of constraints (they must add up to 100% and must be positive). This is simple and after MUCH reading I finally was able to achieve the same result in LabVIEW using the quadratic programming vi.

The problem I have is that I now want to calculate the optimal risky portfolio. In Excel this is easy as you run the solver, select "max" and point to the cell where you calculate the Sharpe ratio. I cannot find a way to get the quadratic programming vi to calculate a MAXIMUM of anything. I have looked through several other vi's in the optimization palette but a) I cannot see how to calculate a maximum and b) I cannot work out how to supply the correct objective function.

Given the sheer volume of material out there on portfolio optimization I am embarrassed I cannot solve this in LabVIEW. I am hoping there are a few of you out there that have looked at this and can point me in the right direction. Below is a link to the excel way of solving this.

I can provide my VI (after some massaging) if you want, but I suspect this is a case of "Hey, you should just use <solution.vi> and feed it <required inputs> in this format".

Thanks in advance.

0 Kudos
Message 1 of 12
(3,690 Views)

Hi Phil,

 

can you provide the math formulas as "math formulas" instead of a video link?

(This would be the first step to create a program: get the formulas you want to implement…)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 12
(3,670 Views)

The formulas are all over the internet in various forms. It's far simpler for me to grab an image of the maths than try and type it out as you will see from the image below.

 

image.png

 

As I mentioned previously, this is a maximisation problem, not minimisation. So I am struggling to work out how to use the quadratic programming VI.

 

0 Kudos
Message 3 of 12
(3,645 Views)

@phil_prismtc wrote:

Hi All,

I am having difficulty with a portfolio optimization problem. I can achieve what I want with amazing ease using MS Excel's solver add-in but I need to perform the same calculations within a LabVIEW app.

 

The EXCEL solver is a handy tool to solve a linear equation system, this is very likely to be doable in LabView

[...]

This is simple and after MUCH reading I finally was able to achieve the same result in LabVIEW using the quadratic programming vi.

Well done.

Ok, so the app you want to write in LabView has to accept an input portfolio - and after some processing, it outputs an optimized portfolio based on the input portfolio

[...]

In the MS Excel solver you can find the portfolio with the lowest standard deviation by running the solver, selecting "min" and pointing it to the standard deviation cell and ask it to vary the portfolio stock weightings with a couple of constraints (they must add up to 100% and must be positive).

[...]

So, you achieved to calculate the portfolio with the lowest standard deviation with "quadratic programming.vi" ?                         Can you provide an successful example of an input and output dataset using the LabView "quadratic programming.vi"?

 

The problem I have is that I now want to calculate the optimal risky portfolio. In Excel this is easy as you run the solver, select "max" and point to the cell where you calculate the Sharpe ratio. I cannot find a way to get the quadratic programming vi to calculate a MAXIMUM of anything.

Is the optimal risky portfolio defined as the portfolio with the highest standard deviation ?



 

0 Kudos
Message 4 of 12
(3,634 Views)

Here is the VI (LV2016) that calculates the portfolio with the minimum standard deviation. This is (relatively) easy as the QP vi is designed to find a minimum.

The optimization process takes the input portfolio described in terms of the co-variance matrix and expected returns for each constituent stock.

The Minimum Variance Portfolio (MVP) is the one where the weightings of each stock are tuned to produce the lowest portfolio standard deviation. This is what the attached VI does and has been verified by the Excel solver.

The optimal risky portfolio is the one that has the highest Sharpe Ratio as described above.

0 Kudos
Message 5 of 12
(3,616 Views)

I like math, but Economics is just mysterious to me.  I did see stuff on the Web for Tangency Portfolios, which I gather is what you want to compute, but I don't understand what they are talking about ...

 

Bob Schor

0 Kudos
Message 6 of 12
(3,610 Views)

I completely understand Bob. This IS a math problem, but phrased in economics terms which makes it all that much harder to discern exactly which way to approach the problem. Selecting the correct mathematical approach (solver) to use is half my problem. Being able to understand how LabVIEW implements the correct approach is the second challenge I have.

0 Kudos
Message 7 of 12
(3,607 Views)

Here's a really crazy idea -- you have some function/measure you are attempting to maximize.  Is it something that you know (because, for example, it is a square) can never go to zero?  If that is the case, could you run a minimization problem in 1/Function?  The values that minimize the reciprocal should (logically) maximize the function itself ...  But that's just too weird to be correct.

 

Bob Schor

0 Kudos
Message 8 of 12
(3,602 Views)

I have thought about this. Unfortunately, the Sharpe ratio (Sr) can go negative which is definitely sub-optimal. I have thought about minimizing 100-Sr but I have not gone through the heartache of trying to massage those constraints into the QP vi. There is a nice reduction of the problem described above into what I think is a set of linear equations:

image.png

 

 

0 Kudos
Message 9 of 12
(3,597 Views)

 

 

 wrote:

The problem I have is that I now want to calculate the optimal risky portfolio. In Excel this is easy as you run the solver, select "max" and point to the cell where you calculate the Sharpe ratio.

 

 


So, we assume there exists a valid  solution which can be calculated via EXCEL solver, which is described in the youtube video.

Does this "EXCEL solver" algorithm also work for the data in  Portfolio Optimization example.vi ‏23 KB ?

 

 


@phil_prismtc wrote:

I cannot find a way to get the quadratic programming vi to calculate a MAXIMUM of anything.

We know, "Quadratic Programming VI" is not designed  to do your task.

We don't know if we can tweak it to do your task,

but we assume this is possible

 

At least we know that we need additional data,

e.g. the Sharpe ratio

maybe something else?

 

 


@phil_prismtc wrote:

 There is a nice reduction of the problem described above into what I think is a set of linear equations:

image.png


Is there  a geometrical representation of your problem?
https://www.quora.com/What-is-the-tangency-portfolio-and-how-do-I-derive-it

http://www.ece.northwestern.edu/local-apps/matlabhelp/toolbox/optim/tutor15b.html

 

 

0 Kudos
Message 10 of 12
(3,579 Views)