forum.alglib.net

ALGLIB forum
It is currently Sun Dec 22, 2024 7:36 pm

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Quadratic program returns TerminationType 5
PostPosted: Fri Sep 23, 2011 5:17 am 
Offline

Joined: Wed Sep 21, 2011 4:39 pm
Posts: 5
Hi Sergey

Please help me.

I am trying to use your quadratic program optimiser.

I have attached a very simple Visual Basic.NET module that shows what I am trying to do. (I changed the extension to .txt, to get through firewalls. Change it to .VB before you open it.)

The procedure "TestNoBudget" works.

The procedure "TestWithBudgetConstraint" does not. It returns a TerminationType of -5, which means that I shouldn't use the Cholesky algorithm or something.

I've also attached an Excel workbook that shows the correct solution for the "TestWithBudgetConstraint" example.


Here's the code:

Public Class AlglibTest

''' <summary>
''' Standard mean-variance portfolio optimisation: A quadratic problem.
''' Minimise: -aWeight' aExpReturn + .5 aWeight' matCov aWeight
''' Input the linear term as the negative of the expected returns
''' </summary>
Public Sub TestNoBudget()

Dim matCov(,) As Double 'The quadratic term
Dim aNegativeExpReturn() As Double 'The linear term (the negative of expected returns)
Dim aBoundLower(), aBoundUpper() As Double
Dim aStartingWeight(), aOptimalWeight() As Double
Dim TerminationType As Integer

matCov = New Double(,) _
{{0.142, 0.056, 0.061}, _
{0.056, 0.181, 0.031}, _
{0.061, 0.031, 0.08}}
aNegativeExpReturn = New Double() {-0.1, -0.1, -0.1}
aBoundLower = New Double() {0, 0, 0}
aBoundUpper = New Double() {Double.PositiveInfinity, Double.PositiveInfinity, Double.PositiveInfinity}
aStartingWeight = New Double() {1 / 3, 1 / 3, 1 / 3}

aOptimalWeight = GetOptWeight(matCov, aNegativeExpReturn, aBoundLower, aBoundUpper, aStartingWeight, TerminationType)

End Sub


''' <summary>
''' As above, but including a constraint that the weights add to 1.
''' This is done using a Lagrangian multiplier
''' augmented into the input vector and matrix.
''' </summary>
Public Sub TestWithBudgetConstraint()

Dim matCov(,) As Double
Dim aNegativeExpReturn() As Double
Dim aBoundLower(), aBoundUpper() As Double
Dim aStartingWeight(), aOptimalWeight() As Double
Dim TerminationType As Integer

matCov = New Double(,) _
{{0.142, 0.056, 0.061, 1}, _
{0.056, 0.181, 0.031, 1}, _
{0.061, 0.031, 0.08, 1}, _
{1, 1, 1, 0}}
aNegativeExpReturn = New Double() {-0.1, -0.1, -0.1, -1}
aBoundLower = New Double() {0, 0, 0, Double.NegativeInfinity}
aBoundUpper = New Double() {Double.PositiveInfinity, Double.PositiveInfinity, Double.PositiveInfinity, Double.PositiveInfinity}
aStartingWeight = New Double() {0.25, 0.25, 0.25, 0.25}

aOptimalWeight = GetOptWeight(matCov, aNegativeExpReturn, aBoundLower, aBoundUpper, aStartingWeight, TerminationType)

TerminationType = TerminationType

End Sub


''' <summary>
''' Using Alglib's quadratic program minimiser
''' </summary>
Public Function GetOptWeight _
(ByVal matCov(,) As Double, ByVal aNegativeExpReturn() As Double, _
ByVal aBoundLower() As Double, ByVal aBoundUpper() As Double, _
ByVal aStartingWeight() As Double, _
ByRef TerminationType As Integer) _
As Double()

Try

Dim AlglibState As minqpstate = New XAlglib.minqpstate()
Dim AlglibReport As minqpreport = New XAlglib.minqpreport()
Dim aOptWeight() As Double
Dim NumDimensions As Integer

NumDimensions = aNegativeExpReturn.GetLength(0)

'Set-up the problem and solve for this RA and Starting weights
XAlglib.minqpcreate(NumDimensions, AlglibState)
XAlglib.minqpsetquadraticterm(AlglibState, matCov)
XAlglib.minqpsetlinearterm(AlglibState, aNegativeExpReturn)
XAlglib.minqpsetstartingpoint(AlglibState, aStartingWeight)
XAlglib.minqpsetbc(AlglibState, aBoundLower, aBoundUpper)
XAlglib.minqpoptimize(AlglibState)
XAlglib.minqpresults(AlglibState, aOptWeight, AlglibReport)

TerminationType = AlglibReport.terminationtype

'Return optimal weights
Return aOptWeight

Catch ex As System.Exception
Dim mEx As New ExceptionIMapsControl(GetCurrentMethod, ex)
Throw mEx

End Try

End Function


End Class


Attachments:
AlglibTest.txt [3.58 KiB]
Downloaded 659 times
AugumentedSolution.xls [15.5 KiB]
Downloaded 624 times

_________________
Thank you,
Regards
Reg Bust
Top
 Profile  
 
 Post subject: Re: Quadratic program returns TerminationType 5
PostPosted: Fri Sep 23, 2011 9:57 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
That's because your program is non-strictly convex - matCov[3][3] is zero - and Cholesky-based algorithm can't be used. Current version of minQP supports only convex programs.

If you want to solve such QP problem, you can switch to BLEIC algorithm - it supports boundary/linear constraints and numerical differentiation (use MinBLEICCreateF), so you won't have to calculate derivatives of quadratic function.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 24 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group