Anda di halaman 1dari 15

Optimization in Quantlib

Mauricio Bedoya javierma36@gmail.com February 2014


The following procedure works in Mac OS X Lion 10.7.5. I assume that Xcode and Boost are already installed. Just copy the content in a .cpp le 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 like linprog and fzero Matlab functions.

1. Problem 1
2 f (x) = ex(1) [4x2 (1) + 2x(2) + 4x(1) x(2) + 2x(2) + 1]

Minimize

(1)

c l a s s o b j e t i v e : p u b l i c CostFunction { public : Real v a l u e ( c o n s t Array& x ) c o n s t { Real r e s = 4 x [ 0 ] x [ 0 ] + 2 x [ 1 ] x [ 1 ] + 4 x [ 0 ] x [ 1 ] + 2 x [1] + 1 ; r e s = exp ( x [ 0 ] ) ; return res ; } D i s p o s a b l e <Array > v a l u e s ( c o n s t Array& x ) c o n s t { Array r e s ( 1 ) ; res [ 0 ] = value (x) ; return res ; } };

11

13

15

i n t main ( ) { S i z e m a x I t e r a t i o n s =1000; S i z e m i n S t a t I t e r a t i o n s =100; Real r o o t E p s i l o n =1e 8; Real f u n c t i o n E p s i l o n =1e 9; Real g r a d i e n t N o r m E p s i l o n =1e 5; E n d C r i t e r i a myEndCrit ( m a x I t e r a t i o n s , minStatIterations , rootEpsilon , functionEpsilon , gradientNormEpsilon ) ; o b j e t i v e func ; NoConstraint c o n s t r a i n t ; Problem myProblem ( func , c o n s t r a i n t , Array ( 2 , 0 . 1 ) ) ; Simplex s o l v e r 1 ( 0 . 1 ) ; E n d C r i t e r i a : : Type s o l v e d C r i t 3 = s o l v e r 1 . minimize ( myProblem , myEndCrit ) ; s t d : : c o u t << C r i t e r i a Simplex : << s o l v e d C r i t 3 << s t d : : e n d l ; // Stop c r i t e r i a s t d : : c o u t << Root Simplex : << myProb3 . c u r r e n t V a l u e ( ) << s t d : : e n d l ; // S o l u t i o n s t d : : c o u t << Min F Value Simplex : << myProb3 . f u n c t i o n V a l u e ( ) << s t d : : e n d l ; // O b j e t i v e Function v a l u e }

10

12

14

16

18

20

22

2. Problem 2 f (x) = 5x(1) 4x(2) 6x(3) Constraints (lineal) x(1) x(2) + x(3) 20 3x(1) + 2x(2) + 4x(3) 42 3x(1) + 2x(2) 30 x(1) , x(2) , x(3) positive.
#i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e <c s t d l i b > <cmath> <i o s t r e a m > < q l / q u a n t l i b . hpp> <v e c t o r > < b o o s t / format . hpp> < b o o s t / f u n c t i o n . hpp>

Minimize

(2)

u s i n g namespace QuantLib ;
10

12

// o b j e c t i v e f u n c t i o n t o be maximized c l a s s Function : p u b l i c CostFunction { public : // must o v e r r i d e t h i s member f u n c t i o n v i r t u a l Real v a l u e ( c o n s t Array& x ) c o n s t { QL REQUIRE( x . s i z e ( ) ==3, Three i n p u t data ! ) ; r e t u r n 5 x [ 0 ] 4 x [ 1 ] 6 x [ 2 ] ; } // must o v e r r i d e t h i s member f u n c t i o n v i r t u a l D i s p o s a b l e <Array > v a l u e s ( c o n s t Array& x ) c o n s t { QL REQUIRE( x . s i z e ( ) ==3, Three i n p u t data ! ) ; Array v a l u e s ( 1 ) ; values [ 0 ] = value (x) ; return values ; } }; // a g g r e g a t e s a l l c o n s t r a i n t e x p r e s s i o n s i n t o a s i n g l e c o n s t r a i n t c l a s s Constraints : public Constraint { public : C o n s t r a i n t s ( c o n s t s t d : : v e c t o r <b o o s t : : f u n c t i o n <Real ( Array )> >& e x p r e s s i o n s ) : C o n s t r a i n t ( b o o s t : : s h a r e d p t r < C o n s t r a i n t : : Impl >(new C o n s t r a i n t s : : Impl ( e x p r e s s i o n s ) ) ) {}

14

16

18

20

22

24

26

28

30

32

34

private : // c o n s t r a i n t i m p l e m e n t a t i o n c l a s s Impl : p u b l i c C o n s t r a i n t : : Impl { public : Impl ( c o n s t s t d : : v e c t o r <b o o s t : : f u n c t i o n <Real ( Array )> >& e x p r e s s i o n s ) : e x p r e s s i o n s ( e x p r e s s i o n s ) {}

10

12

14

16

b o o l t e s t ( c o n s t Array& x ) c o n s t { f o r ( auto i t e r = e x p r e s s i o n s . b e g i n ( ) ; i t e r < e x p r e s s i o n s . end ( ) ; ++i t e r ) { i f (!( iter ) (x) ) { return f a l s e ;} } // w i l l o n l y g e t h e r e i f a l l c o n s t r a i n t s s a t i s f i e d return true ; } private : c o n s t s t d : : v e c t o r <b o o s t : : f u n c t i o n <Real ( Array )> > e x p r e s s i o n s ; }; }; i n t main ( ) { Function o b j F u n c t i o n ; // o p t i m i z a t i o n c o n s t r a i n t s s t d : : v e c t o r <b o o s t : : f u n c t i o n <Real ( Array )> > // c o n s t r a i n t s implemented a s C++11 lambda c o n s t r a i n t s [ 0 ] = [ ] ( c o n s t Array& x ) { r e t u r n c o n s t r a i n t s [ 1 ] = [ ] ( c o n s t Array& x ) { r e t u r n 42) ; } ; c o n s t r a i n t s [ 2 ] = [ ] ( c o n s t Array& x ) { r e t u r n

18

20

22

24

26

28

constraints (3) ; expressions ( x [ 0 ] x [ 1 ] + x [ 2 ] <= 2 0 ) ; } ; ( 3 x [ 0 ] + 2 x [ 1 ] + 4 x [ 2 ] <= ( 3 x [ 0 ] + 2 x [ 1 ] <= 3 0 ) ; } ;

30

32

34

36

38

// i n s t a n t i a t e c o n s t r a i n t s P o s i t i v e C o n s t r a i n t pos ; Constraints fConstraints ( constraints ) ; C o m p o s i t e C o n s t r a i n t f C o n s t ( f C o n s t r a i n t s , pos ) ; // end c r i t e r i a t h a t w i l l t e r m i n a t e s e a r c h S i z e m a x I t e r a t i o n s = 1 0 0 0 0 0 ; // end s e a r c h a f t e r 1000 i t e r a t i o n s i f no solution S i z e m i n S t a t I t e r a t i o n s = 1 0 ; // don t spend more than 10 i t e r a t i o n s a t a s i n g l e point

40

42

Real r o o t E p s i l o n = 1 e 12; // end s e a r c h i f a b s o l u t e d i f f e r e n c e o f c u r r e n t and l a s t r o o t v a l u e i s below e p s i l o n Real f u n c t i o n E p s i l o n = 1 e 12; // end s e a r c h i f a b s o l u t e d i f f e r e n c e o f c u r r e n t and l a s t f u n c t i o n v a l u e i s below e p s i l o n Real g r a d i e n t E p s i l o n = 1 e 12; // end s e a r c h i f a b s o l u t e d i f f e r e n c e o f norm o f c u r r e n t and l a s t g r a d i e n t i s below e p s i l o n EndCriteria endCriteria ( maxIterations , minStatIterations , rootEpsilon , functionEpsilon , gradientEpsilon ) ; Array I n i t V a l ( 3 , 1 . 0 ) ; Problem g e t V a l u e s ( o b j F u n c ti o n , fConst , I n i t V a l ) ; // u s e t h e Simplex method Simplex s o l v e r ( . 1 ) ; // i f t h e a l g o r i t h m i s a b l e t o l o c a t e an o p t i m a l s o l u t i o n , i t w i l l s t o p s ea rc h in g at a s t a t i o n a r y point in the search space E n d C r i t e r i a : : Type s o l u t i o n = s o l v e r . minimize ( g e t V a l u e s , e n d C r i t e r i a ) ; s t d : : c o u t << b o o s t : : format ( Simplex s o l u t i o n type : % s ) % s o l u t i o n << s t d : : e n d l ; c o n s t Array& s t d : : c o u t << s t d : : c o u t << s t d : : c o u t << } r e s u l t s = getValues . currentValue () ; x [ 0 ] : << r e s u l t s [ 0 ] << s t d : : e n d l ; x [ 1 ] : << r e s u l t s [ 1 ] << s t d : : e n d l ; x [ 2 ] : << r e s u l t s [ 2 ] << s t d : : e n d l ;

11

13

15

17

19

21

3. Problem 3 f (x) = 2x(1) + 3x(2) Constraints (lineal) x(1) + 2x(2) 8 2x(1) + x(2) 10 x(2) 3 x(1) , x(2) positive.
1

Maximize

(3)

#i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e

<c s t d l i b > <cmath> <i o s t r e a m > < q l / q u a n t l i b . hpp> <v e c t o r > < b o o s t / format . hpp> < b o o s t / f u n c t i o n . hpp>

u s i n g namespace QuantLib ;

// o b j e c t i v e f u n c t i o n t o be maximized c l a s s Function : p u b l i c CostFunction { public : // must o v e r r i d e t h i s member f u n c t i o n v i r t u a l Real v a l u e ( c o n s t Array& x ) c o n s t { QL REQUIRE( x . s i z e ( ) ==2, Two i n p u t data ! ) ; r e t u r n 1 ( 2 x [ 0 ] + 3 x [ 1 ] ) ; } // must o v e r r i d e t h i s member f u n c t i o n v i r t u a l D i s p o s a b l e <Array > v a l u e s ( c o n s t Array& x ) c o n s t { QL REQUIRE( x . s i z e ( ) ==2, Two i n p u t data ! ) ; Array v a l u e s ( 1 ) ; values [ 0 ] = value (x) ; return values ; } }; // a g g r e g a t e s a l l c o n s t r a i n t e x p r e s s i o n s i n t o a s i n g l e c o n s t r a i n t c l a s s Constraints : public Constraint { public : C o n s t r a i n t s ( c o n s t s t d : : v e c t o r <b o o s t : : f u n c t i o n <Real ( Array )> >& e x p r e s s i o n s ) : C o n s t r a i n t ( b o o s t : : s h a r e d p t r < C o n s t r a i n t : : Impl >(new C o n s t r a i n t s : : Impl ( e x p r e s s i o n s ) ) ) {} private : // c o n s t r a i n t i m p l e m e n t a t i o n c l a s s Impl : p u b l i c C o n s t r a i n t : : Impl { public : Impl ( c o n s t s t d : : v e c t o r <b o o s t : : f u n c t i o n <Real ( Array )> >& e x p r e s s i o n s ) : e x p r e s s i o n s ( e x p r e s s i o n s ) {}

11

13

15

17

19

21

23

25

27

29

31

33

35

37

39

41

b o o l t e s t ( c o n s t Array& x ) c o n s t { f o r ( auto i t e r = e x p r e s s i o n s . b e g i n ( ) ; i t e r < e x p r e s s i o n s . end ( ) ; ++i t e r ) { i f (!( iter ) (x) ) { return f a l s e ;} } // w i l l o n l y g e t h e r e i f a l l c o n s t r a i n t s s a t i s f i e d return true ; }

private : c o n s t s t d : : v e c t o r <b o o s t : : f u n c t i o n <Real ( Array )> > e x p r e s s i o n s ; }; }; i n t main ( ) { Function o b j F u n c t i o n ; // o p t i m i z a t i o n c o n s t r a i n t s s t d : : v e c t o r <b o o s t : : f u n c t i o n <Real ( Array )> > // c o n s t r a i n t s implemented a s C++11 lambda c o n s t r a i n t s [ 0 ] = [ ] ( c o n s t Array& x ) { r e t u r n c o n s t r a i n t s [ 1 ] = [ ] ( c o n s t Array& x ) { r e t u r n c o n s t r a i n t s [ 2 ] = [ ] ( c o n s t Array& x ) { r e t u r n

10

constraints (3) ; expressions ( x [ 0 ] + 2 x [ 1 ] <= 8 ) ; } ; ( 2 x [ 0 ] + x [ 1 ] <= 1 0 ) ; } ; ( x [ 1 ] <= 3 ) ; } ;

12

14

16

18

20

// i n s t a n t i a t e c o n s t r a i n t s P o s i t i v e C o n s t r a i n t pos ; Constraints fConstraints ( constraints ) ; C o m p o s i t e C o n s t r a i n t f C o n s t ( f C o n s t r a i n t s , pos ) ; // end c r i t e r i a t h a t w i l l t e r m i n a t e s e a r c h S i z e m a x I t e r a t i o n s = 1 0 0 0 0 0 ; // end s e a r c h a f t e r 1000 i t e r a t i o n s i f no solution S i z e m i n S t a t I t e r a t i o n s = 1 0 ; // don t spend more than 10 i t e r a t i o n s a t a s i n g l e point Real r o o t E p s i l o n = 1 e 12; // end s e a r c h i f a b s o l u t e d i f f e r e n c e o f c u r r e n t and l a s t r o o t v a l u e i s below e p s i l o n Real f u n c t i o n E p s i l o n = 1 e 12; // end s e a r c h i f a b s o l u t e d i f f e r e n c e o f c u r r e n t and l a s t f u n c t i o n v a l u e i s below e p s i l o n Real g r a d i e n t E p s i l o n = 1 e 12; // end s e a r c h i f a b s o l u t e d i f f e r e n c e o f norm o f c u r r e n t and l a s t g r a d i e n t i s below e p s i l o n EndCriteria endCriteria ( maxIterations , minStatIterations , rootEpsilon , functionEpsilon , gradientEpsilon ) ; Array I n i t V a l ( 2 , 0 . 1 ) ; Problem g e t V a l u e s ( o b j F u n c t i o n , fConst , I n i t V a l ) ; // u s e t h e Simplex method Simplex s o l v e r ( . 1 ) ; // i f t h e a l g o r i t h m i s a b l e t o l o c a t e an o p t i m a l s o l u t i o n , i t w i l l s t o p s ea rc h in g at a s t a t i o n a r y point in the search space E n d C r i t e r i a : : Type s o l u t i o n = s o l v e r . minimize ( g e t V a l u e s , e n d C r i t e r i a ) ; s t d : : c o u t << b o o s t : : format ( Simplex s o l u t i o n type : % s ) % s o l u t i o n << s t d : : endl ;

22

24

26

28

30

32

34

36

38

40

c o n s t Array& r e s u l t s = g e t V a l u e s . c u r r e n t V a l u e ( ) ; s t d : : c o u t << x [ 0 ] : << r e s u l t s [ 0 ] << s t d : : e n d l ; s t d : : c o u t << x [ 1 ] : << r e s u l t s [ 1 ] << s t d : : e n d l ; }

2.

LevenbergMarquardt

Implied volatility estimation. This is like Least squre.


#i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e <c s t d l i b > <cmath> <i o s t r e a m > < q l / q u a n t l i b . hpp> <v e c t o r > < b o o s t / format . hpp> < b o o s t / f u n c t i o n . hpp> <Option / S i n g l e A s s e t / B l a c k S c h o l e s / Implementation . hpp>

10

u s i n g namespace QuantLib ; // o b j e c t i v e f u n c t i o n t o be maximized c l a s s I m p l i e d V o l a t i l i t y F u n c t i o n : p u b l i c CostFunction { private : Real C a l l p r i c e ; Real S , K, q ; Rate r f ; Time T ; c h a r Opt Type ; public : I m p l i e d V o l a t i l i t y F u n c t i o n ( c o n s t Real& S , c o n s t Real& K , c o n s t Rate& r f , c o n s t Time& T , c o n s t Real& q , c o n s t c h a r& Opt Type , c o n s t Real& C a l l p r i c e ) : S ( S ) ,K( K ) , r f ( r f ) ,T( T ) , q ( q ) , Opt Type ( Opt Type ) , C a l l p r i c e ( C a l l p r i c e ) {} // must o v e r r i d e t h i s member f u n c t i o n v i r t u a l Real v a l u e ( c o n s t Array& x ) c o n s t { QL REQUIRE( x . s i z e ( ) ==1, One i n p u t data ! ) ; search Array tmpRes = v a l u e s ( x ) ; Real r e s = tmpRes [ 0 ] tmpRes [ 0 ] ; return res ; } // Square r o o t

12

14

16

18

20

22

24

26

// one v o l a t i l i t y t o

28

30

// must o v e r r i d e t h i s member f u n c t i o n v i r t u a l D i s p o s a b l e <Array > v a l u e s ( c o n s t Array& x ) c o n s t { QL REQUIRE( x . s i z e ( ) ==1, One i n p u t data ! ) ; // one v o l a t i l i t y t o search Array r e s ( 1 ) ; B l a c k S c h o l e s <Real > opt1 ( S , K, r f , T, x [ 0 ] , q , Opt Type ) ; r e s [ 0 ] = opt1 . P r i c e ( ) C a l l p r i c e ; return res ; } }; i n t main ( ) { Real s p o t = 9 8 . 5 1 ; V o l a t i l i t y vol =0.134; Real K1 = 8 7 . 0 ; Rate q =0.0 , r f = 0 . 0 1 ; c h a r Opt Type = C ; Time tau = 0 . 6 ; B l a c k S c h o l e s <Real > P r i c e ( spot , K1 , r f , tau , vol , q , Opt Type ) ; use yours . Real O p t i o n P r i c e = P r i c e . P r i c e ( ) ; // My f u n c t i o n ,

11

13

15

17

19

21

23

25

s t d : : c o u t << O p t i o n P r i c e : << O p t i o n P r i c e << s t d : : e n d l ; I m p l i e d V o l a t i l i t y F u n c t i o n o b j F u n c t i o n ( spot , K1 , r f , tau , q , Opt Type , Option Price ) ; // end c r i t e r i a t h a t w i l l t e r m i n a t e s e a r c h S i z e m a x I t e r a t i o n s = 1 0 0 0 0 0 ; // end s e a r c h a f t e r 1000 i t e r a t i o n s i f no solution S i z e m i n S t a t I t e r a t i o n s = 1 0 ; // don t spend more than 10 i t e r a t i o n s a t a s i n g l e point Real r o o t E p s i l o n = 1 e 12; // end s e a r c h i f a b s o l u t e d i f f e r e n c e o f c u r r e n t and l a s t r o o t v a l u e i s below e p s i l o n Real f u n c t i o n E p s i l o n = 1 e 12; // end s e a r c h i f a b s o l u t e d i f f e r e n c e o f c u r r e n t and l a s t f u n c t i o n v a l u e i s below e p s i l o n Real g r a d i e n t E p s i l o n = 1 e 12; // end s e a r c h i f a b s o l u t e d i f f e r e n c e o f norm o f c u r r e n t and l a s t g r a d i e n t i s below e p s i l o n EndCriteria endCriteria ( maxIterations , minStatIterations , rootEpsilon , functionEpsilon , gradientEpsilon ) ;

27

29

31

33

35

Array I n i t V a l ( 1 ) ; InitVal [0]= 0 . 9 ; // I f i n i t i a l v a l u e i s c l o s e t o z e r o , i t wont c o m p i l e NoConstraint c o n s t r a i n t s ; Problem g e t V a l u e s ( o b j F u n c t i o n , c o n s t r a i n t s , I n i t V a l ) ; // u s e t h e Leven method ( l i k e f z e r o i n Matlab ) LevenbergMarquardt Lm; // i f t h e a l g o r i t h m i s a b l e t o l o c a t e an o p t i m a l s o l u t i o n , i t w i l l s t o p searching at a s t a t i o n a r y point in // t h e s e a r c h s p a c e E n d C r i t e r i a : : Type s o l u t i o n = Lm. minimize ( g e t V a l u e s , e n d C r i t e r i a ) ; s t d : : c o u t << b o o s t : : format ( S o l u t i o n type : % s ) % s o l u t i o n << s t d : : e n d l ; c o n s t Array& r e s u l t s = g e t V a l u e s . c u r r e n t V a l u e ( ) ; s t d : : c o u t << Min f u n c t i o n v a l u e : << g e t V a l u e s . f u n c t i o n V a l u e ( ) << s t d : : endl ; s t d : : c o u t << V o l a t i l i t y : << r e s u l t s [ 0 ] << s t d : : e n d l ; }

11

13

15

17

4. Find two implied volatility (Array Input)


2

#i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e

<c s t d l i b > <cmath> <i o s t r e a m > < q l / q u a n t l i b . hpp> <v e c t o r > < b o o s t / format . hpp> < b o o s t / f u n c t i o n . hpp> <Option / S i n g l e A s s e t / B l a c k S c h o l e s / Implementation . hpp> <a l g o r i t h m >

10

u s i n g namespace QuantLib ;
12

14

// o b j e c t i v e f u n c t i o n t o be maximized c l a s s I m p l i e d V o l a t i l i t y F u n c t i o n : p u b l i c CostFunction { private : Array O p t i o n P r i c e s ; Real S , K, q ; Rate r f ; Time T ; c h a r Opt Type ;

16

18

20

10

public : I m p l i e d V o l a t i l i t y F u n c t i o n ( c o n s t Real& S , c o n s t Real& K , c o n s t Rate& r f , c o n s t Time& T , c o n s t Real& q , c o n s t c h a r& Opt Type , c o n s t Array& O p t i o n P r i c e s ) : S ( S ) ,K( K ) , r f ( r f ) ,T( T ) , q ( q ) , Opt Type ( Opt Type ) , O p t i o n P r i c e s ( O p t i o n P r i c e s ) {} // must o v e r r i d e t h i s member f u n c t i o n v i r t u a l Real v a l u e ( c o n s t Array& x ) c o n s t { QL REQUIRE( x . s i z e ( ) ==2, Two i n p u t data ! ) ; Array tmpRes = v a l u e s ( x ) ; Real r e s = tmpRes [ 0 ] tmpRes [ 0 ] ; Real r e s 1 = tmpRes [ 1 ] tmpRes [ 1 ] ; return res + res1 ; // Square r o o t } // must o v e r r i d e t h i s member f u n c t i o n v i r t u a l D i s p o s a b l e <Array > v a l u e s ( c o n s t Array& x ) c o n s t { QL REQUIRE( x . s i z e ( ) ==2, Two i n p u t data ! ) ; auto A r r s i z e = O p t i o n P r i c e s . s i z e ( ) ; Array r e s ( A r r s i z e ) ; f o r ( auto i = 0 ; i != A r r s i z e ; ++i ) { B l a c k S c h o l e s <Real > opt1 ( S , K, r f , T, x [ i ] , q , Opt Type ) ; r e s [ i ] = opt1 . P r i c e ( ) O p t i o n P r i c e s [ i ] ; } return res ; } }; i n t main ( ) { Real s p o t = 9 8 . 5 1 ; V o l a t i l i t y vol1 = 0.134; V o l a t i l i t y vol2 = 0.2354; Real K1 = 8 7 . 0 ; Rate q =0.0 , r f = 0 . 0 1 ; c h a r Opt Type = C ; Time tau = 0 . 6 ;

11

13

15

17

19

21

23

25

27

29

31

// To f i n d // To f i n d

33

35

37

39

41

B l a c k S c h o l e s <Real > P r i c e 1 ( spot , K1 , r f , tau , vol1 , q , Opt Type ) ; B l a c k S c h o l e s <Real > P r i c e 2 ( spot , K1 , r f , tau , vol2 , q , Opt Type ) ; Array O p t i o n p r i c e s ( 2 ) ; Option prices [ 0 ] = Price1 . Price () ; Option prices [ 1 ] = Price2 . Price () ; s t d : : c o u t << O p t i o n P r i c e 1 : << O p t i o n p r i c e s [ 0 ] << s t d : : e n d l ; s t d : : c o u t << O p t i o n P r i c e 2 : << O p t i o n p r i c e s [ 1 ] << s t d : : e n d l ;

43

45

11

I m p l i e d V o l a t i l i t y F u n c t i o n o b j F u n c t i o n ( spot , K1 , r f , tau , q , Opt Type , Option prices ) ; // end c r i t e r i a t h a t w i l l t e r m i n a t e s e a r c h S i z e m a x I t e r a t i o n s = 1 0 0 0 0 0 ; // end s e a r c h a f t e r 1000 i t e r a t i o n s i f no solution S i z e m i n S t a t I t e r a t i o n s = 1 0 ; // don t spend more than 10 i t e r a t i o n s a t a s i n g l e point Real r o o t E p s i l o n = 1 e 12; // end s e a r c h i f a b s o l u t e d i f f e r e n c e o f c u r r e n t and l a s t r o o t v a l u e i s below e p s i l o n Real f u n c t i o n E p s i l o n = 1 e 12; // end s e a r c h i f a b s o l u t e d i f f e r e n c e o f c u r r e n t and l a s t f u n c t i o n v a l u e i s below e p s i l o n Real g r a d i e n t E p s i l o n = 1 e 12; // end s e a r c h i f a b s o l u t e d i f f e r e n c e o f norm o f c u r r e n t and l a s t g r a d i e n t i s below e p s i l o n EndCriteria endCriteria ( maxIterations , minStatIterations , rootEpsilon , functionEpsilon , gradientEpsilon ) ;

11

13

15

Array I n i t V a l ( 2 ) ; InitVal [0]= 0 . 2 ; I n i t V a l [ 1 ] = 0 . 1 5 ; // I f i n i t i a l v a l u e i s c l o s e t o z e r o , i t wont c o m p i l e NoConstraint c o n s t r a i n t s ; Problem g e t V a l u e s ( o b j F u n c t i o n , c o n s t r a i n t s , I n i t V a l ) ; // u s e t h e Leven method LevenbergMarquardt Lm; // i f t h e a l g o r i t h m i s a b l e t o l o c a t e an o p t i m a l s o l u t i o n , i t w i l l s t o p searching at a s t a t i o n a r y point in // t h e s e a r c h s p a c e E n d C r i t e r i a : : Type s o l u t i o n = Lm. minimize ( g e t V a l u e s , e n d C r i t e r i a ) ; s t d : : c o u t << b o o s t : : format ( Simplex s o l u t i o n type : % s ) % s o l u t i o n << s t d : : endl ; c o n s t Array& s t d : : c o u t << endl ; s t d : : c o u t << s t d : : c o u t << } r e s u l t s = getValues . currentValue () ; Min f u n c t i o n v a l u e : << g e t V a l u e s . f u n c t i o n V a l u e ( ) << s t d : : V o l a t i l i t y : << r e s u l t s [ 0 ] << s t d : : e n d l ; V o l a t i l i t y : << r e s u l t s [ 1 ] << s t d : : e n d l ;

17

19

21

23

25

27

29

3.

Nonlinear constraint optimization (like fmincon in Matlab)

For this methods use: SteepestDescent, ConjugateGradient, BFGS. Gradient required. Result similar to Matlab.

12

2 2 f ( x) = 4 x 2 (1) + 2x(2) + 2x(3) 33x(1) + 16x(2) 24x(3)

Maximize

(4)

Constraints (non-lineal) 3x(1) 2x2 (2) 7 4x(1) x(3) 11


#i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e <c s t d l i b > <cmath> <i o s t r e a m > < q l / q u a n t l i b . hpp> <v e c t o r > < b o o s t / format . hpp> < b o o s t / f u n c t i o n . hpp>

u s i n g namespace QuantLib ;
10

12

// o b j e c t i v e f u n c t i o n t o be maximized c l a s s Function : p u b l i c CostFunction { public : // must o v e r r i d e t h i s member f u n c t i o n v i r t u a l Real v a l u e ( c o n s t Array& x ) c o n s t { QL REQUIRE( x . s i z e ( ) ==3, Two i n p u t data ! ) ; r e t u r n 4 x [ 0 ] x [ 0 ] + 2 x [ 1 ] x [ 1 ] + 2 x [ 2 ] x [ 2 ] 33 x [ 0 ] + 16 x [ 1 ] 24 x

14

16

18

20

[2]; } v i r t u a l v o i d g r a d i e n t ( Array& grad , c o n s t Array& x ) c o n s t { grad [ 0 ] = 8 x [ 0 ] 3 3 ; grad [ 1 ] = 4 x [ 1 ] + 1 6 ; grad [ 2 ] = 4 x [ 2 ] 2 4 ; } v i r t u a l Real valueAndGradient ( Array& grad , c o n s t Array& x ) c o n s t { it g r a d i e n t ( grad , x ) ; return value (x) ; } // must o v e r r i d e t h i s member f u n c t i o n v i r t u a l D i s p o s a b l e <Array > v a l u e s ( c o n s t Array& x ) c o n s t { QL REQUIRE( x . s i z e ( ) ==3, Two i n p u t data ! ) ; Array v a l u e s ( 1 ) ; values [ 0 ] = value (x) ; return values ; } }; // Need i t

22

24

26

28

// Need

30

32

34

36

38

40

13

// a g g r e g a t e s a l l c o n s t r a i n t e x p r e s s i o n s i n t o a s i n g l e c o n s t r a i n t c l a s s Constraints : public Constraint { public : C o n s t r a i n t s ( c o n s t s t d : : v e c t o r <b o o s t : : f u n c t i o n <Real ( Array )> >& e x p r e s s i o n s ) : C o n s t r a i n t ( b o o s t : : s h a r e d p t r < C o n s t r a i n t : : Impl >(new C o n s t r a i n t s : : Impl ( e x p r e s s i o n s ) ) ) {} private : // c o n s t r a i n t i m p l e m e n t a t i o n c l a s s Impl : p u b l i c C o n s t r a i n t : : Impl { public : Impl ( c o n s t s t d : : v e c t o r <b o o s t : : f u n c t i o n <Real ( Array )> >& e x p r e s s i o n s ) : e x p r e s s i o n s ( e x p r e s s i o n s ) {}

10

12

14

16

18

20

22

b o o l t e s t ( c o n s t Array& x ) c o n s t { f o r ( auto i t e r = e x p r e s s i o n s . b e g i n ( ) ; i t e r < e x p r e s s i o n s . end ( ) ; ++ iter ) { i f (!( iter ) (x) ) { return f a l s e ;} } // w i l l o n l y g e t h e r e i f a l l c o n s t r a i n t s s a t i s f i e d return true ; } private : c o n s t s t d : : v e c t o r <b o o s t : : f u n c t i o n <Real ( Array )> > e x p r e s s i o n s ; }; }; i n t main ( ) { Function o b j F u n c t i o n ; // o p t i m i z a t i o n c o n s t r a i n t s s t d : : v e c t o r <b o o s t : : f u n c t i o n <Real ( Array )> >

24

26

28

30

32

34

constraints (2) ;

36

38

// c o n s t r a i n t s implemented a s C++11 lambda e x p r e s s i o n s c o n s t r a i n t s [ 0 ] = [ ] ( c o n s t Array& x ) { r e t u r n ( 3 x [ 0 ] 2 x [ 1 ] x [ 1 ] 7 . 0 <= 0.0) ;}; c o n s t r a i n t s [ 1 ] = [ ] ( c o n s t Array& x ) { r e t u r n ( 4 x [ 0 ] x [ 2 ] x [ 2 ] 1 1 . 0 <= 0 . 0 ) ;};

14

// i n s t a n t i a t e c o n s t r a i n t s P o s i t i v e C o n s t r a i n t pos ; Constraints fConstraints ( constraints ) ; constraints in x C o m p o s i t e C o n s t r a i n t f C o n s t ( f C o n s t r a i n t s , pos ) ; constraints in x

// Use t h i s i f no p o s i t i v e // u s e t h i s i f p o s i t i v e

11

// end c r i t e r i a t h a t w i l l t e r m i n a t e s e a r c h S i z e m a x I t e r a t i o n s = 1 0 0 0 0 0 ; // end s e a r c h a f t e r 1000 i t e r a t i o n s i f no s o l u t i o n S i z e m i n S t a t I t e r a t i o n s = 1 0 ; // don t spend more than 10 i t e r a t i o n s a t a s i n g l e point Real r o o t E p s i l o n = 1 e 12; // end s e a r c h i f a b s o l u t e d i f f e r e n c e o f c u r r e n t and l a s t r o o t v a l u e i s below e p s i l o n Real f u n c t i o n E p s i l o n = 1 e 12; // end s e a r c h i f a b s o l u t e d i f f e r e n c e o f c u r r e n t and l a s t f u n c t i o n v a l u e i s below e p s i l o n Real g r a d i e n t E p s i l o n = 1 e 12; // end s e a r c h i f a b s o l u t e d i f f e r e n c e o f norm o f c u r r e n t and l a s t g r a d i e n t i s below e p s i l o n EndCriteria endCriteria ( maxIterations , minStatIterations , rootEpsilon , functionEpsilon , gradientEpsilon ) ; Array I n i t V a l ( 3 ) ; InitVal [ 0 ] = 4; InitVal [ 1 ] = 2; InitVal [ 2 ] = 2; Problem g e t V a l u e s ( o b j F u n c t i o n , f C o n s t r a i n t s , I n i t V a l ) ;

13

15

17

19

//

21

23

// Methods ( A l l t h r e e methods works and r e a c h r e s u l t s e q u a l t o Matlab ) . S t e e p e s t D e s c e n t sd ; C o n j u g a t e G r a d i e n t cg ; BFGS s o l ; // i f t h e a l g o r i t h m i s a b l e t o l o c a t e an o p t i m a l s o l u t i o n , i t w i l l s t o p searching at a s t a t i o n a r y point in // t h e s e a r c h s p a c e E n d C r i t e r i a : : Type s o l u t i o n = cg . minimize ( g e t V a l u e s , e n d C r i t e r i a ) ; // change t o any o t h e r Method ( sd , cg , s o l ) s t d : : c o u t << b o o s t : : format ( S o l u t i o n type : % s ) % s o l u t i o n << s t d : : e n d l ; s t d : : c o u t << b o o s t : : format ( Root : % s ) % g e t V a l u e s . c u r r e n t V a l u e ( )<< s t d : : e n d l ; s t d : : c o u t << Min F Value : << g e t V a l u e s . f u n c t i o n V a l u e ( ) << s t d : : e n d l ; }

25

27

29

31

Welcome to Quantlib.

15