Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
0Activity
0 of .
Results for:
No results containing your search query
P. 1
Optimization with QuantLib

Optimization with QuantLib

Ratings: (0)|Views: 42|Likes:
Published by Mauricio Bedoya
Learning through examples.
BFGS
SteepestDescent
ConjugateGradient
Simplex
LevenbergMarquardt
Learning through examples.
BFGS
SteepestDescent
ConjugateGradient
Simplex
LevenbergMarquardt

More info:

Published by: Mauricio Bedoya on Mar 20, 2014
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

03/20/2014

pdf

text

original

 
Optimization in Quantlib
Mauricio Bedoya javierma36@gmail.comFebruary 2014
The following procedure works in Mac OS X Lion 10.7.5. I assume that Xcode and Boost arealready installed. Just copy the content in a .cpp file to compile. Im not going to explain the code, just implement a few algorithms. For additional information, check:
 http://quantlib.org
1. Simplex
Use this procedure to solve any problem with linear constraints. Simplex algorithm works likelinprog and fzero Matlab functions.1. Problem 1
(
x
) =
 e
x
(1)
 [4
x
2(1)
 + 2
x
2(2)
 + 4
x
(1)
x
(2)
 + 2
x
(2)
 + 1] Minimize (1)
1
 class objetive : public CostFunction
{
public :
3
 Real value ( const Array& x) const
{
Real res = 4
 
 x[0]
x [0] + 2
 
 x [1]
 
 x [1] + 4
 
 x [0]
 
 x [1] + 2
 
 x[1] + 1 ;
5
 res
 
= exp(x [0]) ;
7
 return res ;
}
9
 Disposable
<
Array
>
 values ( const Array& x) const
{
Array res (1) ;
11
 res [0] = value (x) ;
13
 return res ;
}
15
 }
;
1
 
int main()
2
 {
4
 Size maxIterations =1000;Size minStatIterations =100;
6
 Real rootEpsilon =1e
8;Real functionEpsilon =1e
9;
8
 Real gradientNormEpsilon =1e
5;EndCriteria myEndCrit ( maxIterations ,
10
 minStatIterations ,rootEpsilon ,
12
 functionEpsilon ,gradientNormEpsilon ) ;
14
 objetive func ;NoConstraint constraint ;
16
 Problem myProblem (func , constraint , Array (2 ,0.1) ) ;Simplex solver1 (0.1) ;
18
 EndCriteria : : Type solvedCrit3 = solver1 . minimize(myProblem ,myEndCrit) ;
20
 std : : cout
 <<
 ” Criteria Simplex :
 <<
 solvedCrit3
 <<
 std : : endl ; //Stop criteriastd : : cout
 <<
 ” Root Simplex :
 <<
 myProb3 . currentValue ()
 <<
 std : : endl; // Solution
22
 std : : cout
 <<
 ” Min F Value Simplex :
 <<
 myProb3 . functionValue ()
 <<
 std: : endl ; // Objetive Function value
}
2
 
2. Problem 2
(
x
) =
 −
5
x
(1)
 −
 4
x
(2)
 −
 6
x
(3)
 Minimize (2)Constraints (lineal)
x
(1)
 −
 x
(2)
 +
 x
(3)
 ≤
 203
x
(1)
 + 2
x
(2)
 + 4
x
(3)
 ≤
 423
x
(1)
 + 2
x
(2)
 ≤
 30
x
(1)
,x
(2)
,x
(3)
 positive.
#include
 <
cstdlib
>
2
 #include
 <
cmath
>
#include
 <
iostream
>
4
 #include
 <
ql/quantlib .hpp
>
#include
 <
vector
>
6
 #include
 <
boost/format .hpp
>
#include
 <
boost/function .hpp
>
8
using namespace QuantLib ;
10
// objective function to be maximized
12
 class Function : public CostFunction
 {
14
 public ://must override this member function
16
 virtual Real value ( const Array& x) const
 {
QL REQUIRE(x. size ()==3, ”Three input data ! ) ;
18
return
 
5
 
 x [0]
 
 4
 
 x [1]
 
 6
 
 x [ 2 ] ;
20
 }
//must override this member function
22
 virtual Disposable
<
Array
>
 values ( const Array& x) const
 {
QL REQUIRE(x. size ()==3, ”Three input data ! ) ;
24
 Array values (1) ;values [0] = value (x) ;
26
 return values ;
}
28
 }
;
30
 //aggregates all constraint expressions into a single constraintclass Constraints : public Constraint
 {
32
public :
34
 Constraints ( const std : : vector
<
boost : : function
<
Real(Array)
> >
&expressions ) : Constraint ( boost : : shared ptr
<
Constraint : : Impl
>
(new Constraints: : Impl( expressions ) ) )
 {}
3

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->