Sie sind auf Seite 1von 19

Finanzas en C++: Black Scholes Parte II

Mauricio Bedoya javierma36@hotmail.com Noviembre 2013


En esta secci on las clases se van a implementar con la ayuda de los punteros inteligetes (boost :: on. shared ptr <>). Del mismo modo, todos los ejemplos se desarrollan con el modelo de separaci

1.

Template Class

Al igual que una funci on template, una clase de un template es una familia de clases. Se puede especializar al igual que la funci on.
1

#i n c l u d e < i o s t r e a m > #i n c l u d e < s t r i n g > #i n c l u d e <cmath> u s i n g namespace s t d ; // Boost c o n f i g u r a t i o n Normal Data ( c d f and pdf ) u s i n g b o o s t : : math : : normal ; d o u b l e mean = 0 . 0 ; double s t a n d a r d e v i a t i o n = 1 . 0 ; normal N( mean , s t a n d a r d e v i a t i o n ) ; t e m p l a t e { typename L } c l a s s EuropeanOption { private : v o i d copy ( c o n s t EuropeanOption& o t h e r ) ; L r f ; // I n t e r e s t r a t e L s i g m a ; // V o l a t i l i t y L K ; // S t r i k e p r i c e L T ; // Expiry d a t e L S ; // Current u n d e r l y i n g p r i c e L q ; // Dividend s t r i n g optType ; // Option Type

11

13

15

17

// New copy f u n c t i o n

19

21

23

25

27

public :

29

31

EuropeanOption ( c o n s t L& r f , c o n s t L& sigma , c o n s t L& K, c o n s t L& T, c o n s t L& S , c o n s t L& q , c o n s t s t r i n g& optType ) ; // D e f a u l t C o n s t r u c t o r v i r t u a l EuropeanOption ( ) ; // D e f a u l t D e s t r u c t o r EuropeanOption ( c o n s t EuropeanOption& o t h e r ) ; // Copy C o n s t r u c t o r EuropeanOption& o p e r a t o r = ( c o n s t EuropeanOption& o t h e r ) ; // Copy Assignment // F u n c i o n e s L Call () const ; L Put ( ) c o n s t ; L Price () const ; . . // S e t t e r s ( Dont Return Value ) v o i d s e t r f ( c o n s t d o u b l e& r f ) ; v o i d s e t s i g m a ( c o n s t d o u b l e& sigma ) ; . . // G e t t e r s ( r e t u r n v a l u e ) L get rf () ; L get time () ; . . };

33

35

37

39

41

43

45

47

49

Listing 1: TemplateEuropeanOptionDenition.hpp La implementaci on es un poco diferente a la expuesta en una clase est andar.
1

#i n c l u d e T e m p l a t e E u r o p e a n O p t i o n D e f i n i t i o n . hpp template <typename L> EuropeanOption : : copy ( c o n s t EuropeanOption& o t h e r ) { rf = other . r f ; sigma = other . sigma ; K = other . K ; T = other . T ; q = other . q ; optType = o t h e r . optType ; } template <typename L> EuropeanOption <L > : : EuropeanOption ( ( c o n s t L& r f , c o n s t L& sigma , c o n s t L& K, c o n s t L & T, c o n s t L& S , c o n s t L& q , c o n s t s t r i n g& optType ) : r f ( r f ) , s i g m a ( sigma ) , K (K ) , T (T) , S ( S ) , q ( q ) { } template <typename L> EuropeanOption <L > : : EuropeanOption ( ) { }

11

13

15

17

19

21

23

25

27

template <typename L> EuropeanOption <L > : : EuropeanOption ( c o n s t EuropeanOption& o t h e r ) { copy ( o t h e r ) ; return this ; } template <typename L> EuropeanOption <L>& EuropeanOption <L > : : o p e r a t o r = ( c o n s t EuropeanOption& o t h e r ) { i f ( t h i s == o t h e r ) { return this ;} else { copy ( o t h e r ) ; return this ; } // F u n c i o n e s template <typename L> L EuropeanOption <L > : : C a l l ( ) c o n s t { L b = rf q; L tmp = sigma s q r t (T) ; L d1 = ( l o g ( S/K) + ( b + ( sigma sigma ) 0 . 5 ) T) /tmp ; L d2 = d1 tmp ; r e t u r n ( S exp ( ( b r f ) T) c d f (N, d1 ) ) (K exp( r f T) c d f (N, d2 ) ) ; }

29

31

33

35

37

39

41

43

45

47

49

51

53

55

57

template <typename L> L EuropeanOption <L > : : Put ( ) c o n s t { L b = rf q; L tmp = sigma s q r t (T) ; L d1 = ( l o g ( S/K) + ( b + ( sigma sigma ) 0 . 5 ) T) /tmp ; L d2 = d1 tmp ; r e t u r n (K exp( r T) c d f (N, d2 ) ) ( S exp ( ( br ) T) c d f (N, d1 ) ) ;

59

61

} template <typename L> L EuropeanOption <L > : : P r i c e ( ) c o n s t { i f ( optType==C ) { return Call () ;} e l s e i f ( optType == P ) { r e t u r n Put ( ) ; } else { c e r r << C o r P upper c a s e . Check optType << e n d l ; } } .

63

65

67

69

71

73

75

77

. // S e t t e r s ( Dont Return Value ) template <typename L> v o i d EuropeanOption : : s e t r f ( c o n s t L& r f ) { r f = rf ;} template <typename L> v o i d EuropeanOption : : s e t s i g m a ( c o n s t L& sigma ) { s i g m a = sigma ; } . . // G e t t e r s ( r e t u r n v a l u e ) template <typename L> L EuropeanOption : : g e t r f ( ) { return r f ;} . . };

79

81

83

85

87

89

91

Listing 2: TemplateEuropeanOptionImplementation.hpp Identica las diferencias al implementar una clase est andar y un template de una clase. La implementaci on en main, con punteros inteligentes seria:
1

#i n c l u d e TemplateEuropeanOptionImplementation . hpp #i n c l u d e < b o o s t . s h a r e p t r . hpp> u s i n g namespace b o o s t ;

11

13

i n t main ( ) { d o u b l e r f = 0 . 1 , sigma = 0 . 3 6 , time = 0 . 5 , S = 1 0 5 . 0 , K = 1 0 0 , q = 0 ; s t r i n g o p t t y p e = C ; b o o s t : : s h a r e d p t r <EuropeanOption <double > > o p t i o n 1 ( new EuropeanOption <double >( r f , sigma , time , S , K, q , o p t t y p e ) ) ; c o u t << P r i c e i s : << Opcion1 > p r i c e ( ) << e n d l ; c o u t << D e l t a i s : << Opcion1 > d e l t a ( ) << e n d l ; Opcion1 > s e t S ( 1 0 0 ) ; c o u t << New P r i c e i s : << Opcion1 > p r i c e ( ) << e n d l ; }

caption = main.cpp Identicar la forma alternativa para acceder a los operadores y funciones del objeto, cuando se trabaja con punteros.

2.

Inheritance, Macro & Variables Globales

Hasta ahora, siempre se ha denido e inicializado el tipo boost::math::normal N fuera de la clase. Imaginar que se tienen dos contratos de opciones que dieren signicativamente y ambos emplean 4

el tipo boost::math::normal N. Si procedemos como hasta ahora, e inicializamos los constructores de ambos contratos en main.cpp, el compilador generar a un error debido a que la variable N se ha 1 re-denido . Es un error muy corriente cuando se esta empezando a programar en C++. Considere el siguiente ejemplo:
#i n c l u d e < i o s t r e a m > #i n c l u d e < s t r i n g > #i n c l u d e <cmath> #i n c l u d e < b o o s t : : math : : d i s t r i b u t i o n s : : normal > u s i n g namespace s t d ;
7 8 1

#i n c l u d e < i o s t r e a m > #i n c l u d e < s t r i n g > #i n c l u d e <cmath> #i n c l u d e < b o o s t : : math : : d i s t r i b u t i o n s : : normal > u s i n g namespace s t d ; // Boost c o n f i g u r a t i o n Normal Data ( c d f and pdf ) u s i n g b o o s t : : math : : normal ; d o u b l e mean = 0 . 0 ; double s t a n d a r d e v i a t i o n = 1 . 0 ; normal N( mean , s t a n d a r d e v i a t i o n ) ; c l a s s FutureOption {......};

5 6

10

12

// Boost c o n f i g u r a t i o n Normal Data ( c d f and pdf ) u s i n g b o o s t : : math : : normal ; d o u b l e mean = 0 . 0 ; double s t a n d a r d e v i a t i o n = 1 . 0 ; normal N( mean , s t a n d a r d e v i a t i o n ) ; c l a s s BarrierOption {......};

11

13 14

15

Figura 1: BarrierOptionDenition.hpp

Figura 2: FutureOptionDenition.hpp

A continuaci on en main.cpp, se incluye las respectivas implementaciones. Luego se inicializan los respectivos constructores de ambos contratos y se ejecuta.

One denition rule.

#i n c l u d e FutureOptionImplementation . hpp #i n c l u d e B a r r i e r O p t i o n I m p l e m e n t a t i o n . hpp #i n c l u d e < i o s t r e a m > u s i n g namespace s t d ; i n t main ( ) { c o u t << B a r r i e r O p t i o n ( . . . . . . ) << e n d l ; c o u t << FutureOption ( . . . . . . . ) << e n d l ; }

11

El compilador generar a un error diciendo que la variable N ha sido re-denida. Tanto en FutureOptionImplmentation.hpp, como en BarrierOptionImplementation.hpp se inicializa boost::math::norma N. Por lo tanto, la variable N se esta deniendo dos veces. Las soluciones que contemplaremos ser an: 1. Cambiar el nombre (una chapuza pero funciona). En BarrierOptionDenition.hpp denir boost::math::normal N1. En este caso, N diere de N1. 2. Inheritance La clase BarrierOption y FutureOption podr an acceder a los constructores y funciones de boost::math::normal. En este caso, boost::math::normal ser a la clase base. Como ejemplo, considere
1

#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 o s t r e a m > <s t r i n g > <cmath> < b o o s t : : math : : d i s t r i b u t i o n s : : normal >

11

c l a s s B a r r i e r O p t i o n : p u b l i c b o o s t : : math : : normal ; { private : ...... b o o s t : : math : : normal N; public : B a r r i e r O p t i o n ( c o n s t d o u b l e& S , c o n s t d o u b l e& K , . . . . . . . . ) ; v i r t u a l BarrierOption () ; . . . };

13

15

17

19

Para que el compilador no gener e un error, ser a necesario inicializar el constructor que se encuentra en boost::math::normal, as
1

#i n c l u d e B a r r i e r O p t i o n D e f i n i t i o n . hpp B a r r i e r O p t i o n ( c o n s t d o u b l e& S , c o n s t d o u b l e& K , . . . . . . . ) :N( 0 , 1 ) , S ( S ) , K( K ) , ......

3. Macros Observar que en cada denici on se incluyen los mismos archivos .hpp: string, cmath y boost::math::distribution::normal. Si tenemos 100 tipos de contratos de opciones y todos ellos utilizan los mismos archivos .hpp, se estar an incluyendo 100 veces lo mismo, reduciendo signicativamente el tiempo de compilaci on del c odigo. Para evitar esta practice incorrecta, se implementan macros. En un archivo Header1.hpp se tendr a:
#i f n d e f Header1 #d e f i n e Header1 //BOOST LIBRARY #i n c l u d e < b o o s t /math/ d i s t r i b u t i o n s / normal . hpp> //STANDARD LIBRARY #i n c l u d e <cmath> #i n c l u d e < v e c t o r > #i n c l u d e < s t r i n g > #i n c l u d e < a s s e r t . h> //GLOBAL VARIABLES e x t e r n b o o s t : : math : : normal N; #e n d i f

10

12

14

16

Si no esta denido (#if ndef ), se dene(#def ine). De esta forma se limita el crecimiento de include signicativamente. La linea 14 establece una variable global (no la construye). extern le dice al compilador que N se construir a en alguna otra parte del proyecto. De esta forma, la clase BarrierOptionDenition.hpp, quedar a:
#i n c l u d e <Header1 . hpp>
2

c l a s s BarrierOption ; { private : ......

10

12

14

public : B a r r i e r O p t i o n ( c o n s t d o u b l e& S , c o n s t d o u b l e& K , . . . . . . . . ) ; v i r t u a l BarrierOption () ; . . . };

Un ejemplo completo de implementaci on y denici on del contrato LogOption es:


/ L o g O p t i o n D e f i n i t i o n . hpp V e r i f i c a t i o n d a t e : 3/02/14 S o u r c e : At t h e Complete Guide t o Option P r i c i n g V a l u a t i o n ( page 1 2 1 ) Created by M a u r i c i o Bedoya on 3 / 0 2 / 1 4 . Copyright ( c ) 2014 M a u r i c i o Bedoya . A l l r i g h t s r e s e r v e d . / #i f n d e f L o g O p t i o n D e f i n i t i o n #d e f i n e L o g O p t i o n D e f i n i t i o n #i n c l u d e <Option / OptionsHeader / Header1 . hpp>

10

12

14

16

18

20

22

24

template <typename L> c l a s s LogOption { private : v o i d copy ( c o n s t LogOption& o t h e r ) ; void check data ( ) ; L S , K, r f , T, sigma , q ; b o o s t : : math : : normal N; public : LogOption ( ) ; LogOption ( c o n s t L& S , c o n s t L& K , c o n s t L& r f , c o n s t L& T , c o n s t L& sigma , c o n s t L& q ) ; v i r t u a l LogOption ( ) ; LogOption ( c o n s t LogOption& o t h e r ) ; LogOption& o p e r a t o r = ( c o n s t LogOption& LogOption& o p e r a t o r ( ) ( c o n s t LogOption&

26

28

30

32

// // // // // //

Price Strike Risk Free r a t e Maturity Volatility Dividend y i e l d

34

36

other ) ; other ) ;

38

40

// FUNCTION L b ( ) , tmp ( ) , f ( ) , d1 ( ) ; L P r i c e ( ) , D e l t a ( ) , D e l t a ( c o n s t L& ds ) , Gamma( ) , Vega ( ) , Theta ( ) , Rho ( ) ; // SETTER void s e t void set void s e t void set void set void s e t

42

44

46

48

S ( c o n s t L& new S ) ; K ( c o n s t L& new K ) ; r f ( c o n s t L& n e w r f ) ; T ( c o n s t L& new T ) ; s i g m a ( c o n s t L& new sigma ) ; q ( c o n s t L& new q ) ;

50

52

// GETTER L g e t S ( ) , get K ( ) , g e t r f ( ) , get T ( ) , g e t s i g m a ( ) , g e t q ( ) ; // SENSIBILITY v o i d e v a l ( c o n s t d o u b l e& x , c o n s t s t d : : s t r i n g& parameter ) ; s t d : : v e c t o r <L> S e n s i b i l i t i e s ( c o n s t s t d : : v e c t o r <L>& g r i d , c o n s t c h a r& c a s e , c o n s t s t d : : s t r i n g& parameter ) ; };

54

56

58

60

#e n d i f

// / LogOption Implementation . hpp V e r i f i c a t i o n d a t e : 3/02/14 S o u r c e : At t h e Complete Guide t o Option P r i c i n g V a l u a t i o n ( page 1 2 1 ) Created by M a u r i c i o Bedoya on 3 / 0 2 / 1 4 . Copyright ( c ) 2014 M a u r i c i o Bedoya . A l l r i g h t s r e s e r v e d . / #i f n d e f LogOption Implementation #d e f i n e LogOption Implementation #i n c l u d e <Option / S i n g l e A s s e t /Log/ D e f i n i t i o n . hpp>

11

13

15

17

19

21

23

template <typename L> v o i d LogOption <L > : : copy ( c o n s t LogOption& o t h e r ) { S = other . S ; K = o t h e r .K; r f = other . r f ; T = other .T;

25

sigma = o t h e r . sigma ; q = o t h e r . sigma ; } template <typename L> v o i d LogOption <L > : : c h e c k d a t a ( ) { a s s e r t ( S>0 && K>0 && T> 0) ; a s s e r t ( r f >0 && r f < 1) ; a s s e r t ( sigma >0 && sigma < 1) ; a s s e r t ( q>=0 && q < 1) ; } template <typename L> LogOption <L > : : LogOption ( ) { } template <typename L> LogOption <L > : : LogOption ( c o n s t L& S , c o n s t L& K , c o n s t L& r f , c o n s t L& T , c o n s t L& sigma , c o n s t L& q ) :N( 0 , 1 ) , S ( S ) ,K( K ) , r f ( r f ) ,T( T ) , sigma ( s i g m a ) ,q( q ) { check data () ;} template <typename L> LogOption <L > : : LogOption ( ) { } template <typename L> LogOption <L > : : LogOption ( c o n s t LogOption& o t h e r ) { copy ( o t h e r ) ; return this ; } template <typename L> LogOption <L>& LogOption <L > : : o p e r a t o r = ( c o n s t LogOption& o t h e r ) { i f ( t h i s != &o t h e r ) { copy ( o t h e r ) ; return this ; } else { return this ;} } template <typename L> LogOption <L>& LogOption <L > : : o p e r a t o r ( ) ( c o n s t LogOption& o t h e r )

27

29

31

33

35

37

39

41

43

45

47

49

51

53

55

57

59

61

63

65

67

69

71

73

75

10

{ return this ;}
77

// FUNCTION
79

81

template <typename L> L LogOption <L > : : b ( ) { return r f q ;} template <typename L> L LogOption <L > : : tmp ( ) { r e t u r n sigma s q r t (T) ; } template <typename L> L LogOption <L > : : f ( ) { r e t u r n ( b ( ) 0.5 ( sigma sigma ) ) T; }

83

85

87

89

91

93

95

template <typename L> L LogOption <L > : : d1 ( ) { r e t u r n ( l o g ( S/K)+f ( ) ) /tmp ( ) ; } template <typename L> L LogOption <L > : : P r i c e ( ) { r e t u r n exp( r f T) pdf (N, d1 ( ) ) tmp ( ) + exp( r f T) ( l o g ( S/K)+f ( ) ) c d f (N, d1 ( ) ) ; } template <typename L> L LogOption <L > : : D e l t a ( ) { L ds = 0 . 0 1 ; LogOption <L> Opcion1 ( S+ds , K, r f , T, sigma , q ) ; LogOption <L> Opcion2 ( Sds , K, r f , T, sigma , q ) ; L v a l 1 = Opcion1 . P r i c e ( ) ; L v a l 2 = Opcion2 . P r i c e ( ) ; r e t u r n ( v a l 1 v a l 2 ) / ( 2 ds ) ;

97

99

101

103

105

107

109

111

} template <typename L> L LogOption <L > : : D e l t a ( c o n s t L& ds ) { LogOption <L> Opcion1 ( S+ds , K, r f , T, sigma , q ) ; LogOption <L> Opcion2 ( Sds , K, r f , T, sigma , q ) ; L v a l 1 = Opcion1 . P r i c e ( ) ; L v a l 2 = Opcion2 . P r i c e ( ) ; r e t u r n ( v a l 1 v a l 2 ) / ( 2 ds ) ; }

113

115

117

119

121

123

125

127

template <typename L> L LogOption <L > : :Gamma( ) { L ds = 0 . 0 1 ;

11

129

131

133

LogOption <L> Opcion1 ( S + ds , K, r f , T, sigma , q ) ; LogOption <L> Opcion2 ( S ds , K, r f , T, sigma , q ) ; LogOption <L> Opcion3 ( S , K, r f , T, sigma , q ) ; L v a l 1 = Opcion1 . P r i c e ( ) ; L v a l 2 = Opcion2 . P r i c e ( ) ; L v a l 3 = Opcion3 . P r i c e ( ) ; r e t u r n ( v a l 1 2 v a l 3 + v a l 2 ) / ( ds ds ) ; }

135

137

139

141

143

145

template <typename L> L LogOption <L > : : Vega ( ) { L ds = 0 . 0 1 ; LogOption <L> Opcion1 ( S , K, r f , T, sigma + ds , q ) ; LogOption <L> Opcion2 ( S , K, r f , T, sigma ds , q ) ; L v a l 1 = Opcion1 . P r i c e ( ) ; L v a l 2 = Opcion2 . P r i c e ( ) ; return ( val1 val2 ) / 2; }

147

149

151

153

155

157

159

161

163

165

167

template <typename L> L LogOption <L > : : Theta ( ) { L ds , val1 , v a l 2 ; LogOption <L> Opcion2 ( S , K, r f , T, sigma , q ) ; v a l 2 = Opcion2 . P r i c e ( ) ; i f (T <= 1 . 0 / 3 6 5 ) { ds = 0 . 0 0 0 0 1 ; LogOption <L> Opcion1 ( S , K, r f , ds , sigma , q ) ; v a l 1 = Opcion1 . P r i c e ( ) ; } else { ds = 1 . 0 / 3 6 5 ; LogOption <L> Opcion1 ( S , K, r f , T ds , sigma , q ) ; v a l 1 = Opcion1 . P r i c e ( ) ; } return ( val1 val2 ) ; }

169

171

173

175

177

179

template <typename L> L LogOption <L > : : Rho ( ) { L ds = 0 . 0 1 ; LogOption <L> Opcion1 ( S , K, r f + ds , T, sigma , q ) ; LogOption <L> Opcion2 ( S , K, r f ds , T, sigma , q ) ; L v a l 1 = Opcion1 . P r i c e ( ) ; L v a l 2 = Opcion2 . P r i c e ( ) ;

12

181

return ( val1 val2 ) / 2; }

183

// SETTER
185

187

template <typename L> v o i d LogOption <L > : : s e t S ( c o n s t L& new S ) { S = new S ; } template <typename L> v o i d LogOption <L > : : s e t K ( c o n s t L& new K ) {K = new K ; } template <typename L> v o i d LogOption <L > : : s e t r f ( c o n s t L& n e w r f ) { r f = new rf ;} template <typename L> v o i d LogOption <L > : : s e t T ( c o n s t L& new T ) {T = new T ; } template <typename L> v o i d LogOption <L > : : s e t s i g m a ( c o n s t L& new sigma ) { sigma = new sigma ; } template <typename L> v o i d LogOption <L > : : s e t q ( c o n s t L& new q ) { q = new q ; } // GETTER

189

191

193

195

197

199

201

203

205

207

209

211

213

template <typename L> L LogOption <L > : : g e t S ( ) { return S ;} template <typename L> L LogOption <L > : : get K ( ) { r e t u r n K; } template <typename L> L LogOption <L > : : g e t r f ( ) { return r f ;} template <typename L> L LogOption <L > : : get T ( ) { r e t u r n T; } template <typename L> L LogOption <L > : : g e t s i g m a ( ) { r e t u r n sigma ; }

215

217

219

221

223

225

227

229

231

13

233

template <typename L> L LogOption <L > : : g e t q ( ) { return q ;}

235

237

239

241

243

245

247

249

251

253

255

// SENSIBILITY template <typename L> v o i d LogOption <L > : : e v a l ( c o n s t d o u b l e& x , c o n s t s t d : : s t r i n g& parameter ) { i f ( parameter == S ) { b o o s t : : bind (&LogOption : : s e t S , 1 , 2 ) ( t h i s , x ) ; } e l s e i f ( parameter == K ) { b o o s t : : bind (&LogOption : : set K , 1 , 2 ) ( t h i s , x ) ; } e l s e i f ( parameter == r f ) { b o o s t : : bind (&LogOption : : s e t r f , 1 , 2 ) ( t h i s , x ) ; } e l s e i f ( parameter == T ) { b o o s t : : bind (&LogOption : : set T , 1 , 2 ) ( t h i s , x ) ; } e l s e i f ( parameter == sigma ) { b o o s t : : bind (&LogOption : : s e t s i g m a , 1 , 2 ) ( t h i s , x ) ; } e l s e i f ( parameter == q ) { b o o s t : : bind (&LogOption : : s e t q , 1 , 2 ) ( t h i s , x ) ; } else { s t d : : c e r r << I n c o r r e c t parameter \ n ; } check data () ; } template <typename L> s t d : : v e c t o r <L> LogOption <L > : : S e n s i b i l i t i e s ( c o n s t s t d : : v e c t o r <L>& g r i d , c o n s t c h a r& c a s e , c o n s t s t d : : s t r i n g& parameter ) { s t d : : v e c t o r <L> R e s u l t ; i f ( c a s e == P ) { BOOST FOREACH( d o u b l e x , g r i d ) { e v a l ( x , parameter ) ; R e s u l t . push back ( b o o s t : : bind (&LogOption : : P r i c e , t h i s ) ( ) ) ; } return Result ; } e l s e i f ( c a s e == D ) { BOOST FOREACH( d o u b l e x , g r i d ) { e v a l ( x , parameter ) ; R e s u l t . push back ( b o o s t : : bind (&LogOption : : Delta , t h i s ) ( ) ) ; } return Result ; } e l s e i f ( c a s e == G ) { BOOST FOREACH( d o u b l e x , g r i d )

257

259

261

263

265

267

269

271

273

275

277

279

281

14

283

{ e v a l ( x , parameter ) ; R e s u l t . push back ( b o o s t : : bind (&LogOption : : Gamma, t h i s ) ( ) ) ; } return Result ; } e l s e i f ( c a s e == V ) { BOOST FOREACH( d o u b l e x , g r i d ) { e v a l ( x , parameter ) ; R e s u l t . push back ( b o o s t : : bind (&LogOption : : Vega , t h i s ) ( ) ) ; } return Result ; } e l s e i f ( c a s e == R ) { BOOST FOREACH( d o u b l e x , g r i d ) { e v a l ( x , parameter ) ; R e s u l t . push back ( b o o s t : : bind (&LogOption : : Rho , t h i s ) ( ) ) ; } return Result ; } e l s e i f ( c a s e == T ) { BOOST FOREACH( d o u b l e x , g r i d ) { e v a l ( x , parameter ) ; R e s u l t . push back ( b o o s t : : bind (&LogOption : : Theta , t h i s ) ( ) ) ; } return Result ; } else { s t d : : c e r r << I n c o r r e c t c a s e , check L o g C o n t r a c t d e f i n i t i o n . hpp \ n ; } } #e n d i f

285

287

289

291

293

295

297

299

301

303

305

307

309

311

313

315

317

319

321

/ L o g O p t i o n t e s t . hpp V e r i f i c a t i o n d a t e : 3/02/14 S o u r c e : At t h e Complete Guide t o Option P r i c i n g V a l u a t i o n ( page 1 2 1 ) Created by M a u r i c i o Bedoya on 3 / 0 2 / 1 4 . Copyright ( c ) 2014 M a u r i c i o Bedoya . A l l r i g h t s r e s e r v e d . /

15

11

13

#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 o s t r e a m > < b o o s t / format . hpp> < b o o s t / s h a r e d p t r . hpp> <Option / S i n g l e A s s e t /Log/ Implementation . hpp>

15

u s i n g namespace s t d ;
17

19

21

23

25

i n t main ( ) { double S = 100; double K = 80; double r f = 0 . 0 8 ; double T = 0 . 7 5 ; d o u b l e sigma = 0 . 3 5 ; d o u b l e q = 0 . 0 4 ; // Dividend y i e l d b o o s t : : s h a r e d p t r <LogOption <double > > Opt1 ( new LogOption <double >(S , K, r f , T, sigma , q ) ) ; b o o s t : : format Formater ( % 7s %4.5d \ n ) ;

27

29

31

33

35

37

// FUNCTIONS c o u t << FUNCTIONS \ n ; c o u t << Formater % P r i c e : c o u t << Formater % D e l t a : c o u t << Formater % Gamma: c o u t << Formater % Vega : c o u t << Formater % Rho : c o u t << Formater % Theta : // ASIGNMENT OPERATOR

% Opt1> P r i c e ( ) % Opt1>D e l t a ( ) % Opt1>Gamma( ) % Opt1>Vega ( ) ; % Opt1>Rho ( ) ; % Opt1>Theta ( )

; ; ;

39

41

b o o s t : : s h a r e d p t r <LogOption <double > > Opt2 ; Opt2 = Opt1 ; cout cout cout cout cout cout cout cout << << << << << << << << \ n ; FUNCTIONS \ n ; Formater % P r i c e : % Opt2> P r i c e ( ) Formater % D e l t a : % Opt2>D e l t a ( ) Formater % Gamma: % Opt2>Gamma( ) Formater % Vega : % Opt2>Vega ( ) ; Formater % Rho : % Opt2>Rho ( ) ; Formater % Theta : % Opt2>Theta ( )

43

45

47

; ; ;

49

51

53

55

57

59

// GET DATA c o u t << \ n ; c o u t << GET DATA \ n ; c o u t << Formater % S : % Opt1> g e t S ( ) ; c o u t << Formater % K : % Opt1>get K ( ) ; c o u t << Formater % r f : % Opt1> g e t r f ( ) ; c o u t << Formater % T : % Opt1>get T ( ) ;

16

61

c o u t << Formater % sigma : % Opt1> g e t s i g m a ( ) ; c o u t << Formater % q : % Opt1> g e t q ( ) ; // PROVE SETTER YOURSELF

63

65

c o u t << \ n \ n ;
67

69

// GRID EVALUATION v e c t o r <double > s i g m a g r i d = g r i d ( 0 . 2 , 0 . 6 , 5 ) ; v e c t o r <double >New S1 = Opt1> S e n s i b i l i t i e s ( s i g m a g r i d , P , sigma ) ; c o u t << Formater % New sigma % New Price ; f o r ( i n t i= 0 ; i ! = 5 ; ++i ) { c o u t << Formater % s i g m a g r i d . a t ( i ) % New S1 . a t ( i ) ; } }

71

73

75

77

3.

Organizando el C odigo

En el escritorio crea una carpeta con el nombre C++ y dentro de la misma una carpeta con el nombre Opciones. En el ejemplo anterior, se incluye varios archivos .hpp : Option/SingleAsset/Log/Denition.hpp, Option/SingleAsset/Log/Implementation.hpp. En mi caso, para que Xcode compile, segu los siguientes pasos: 1. Identicar el path a anexar en el compilador (xcode 4.5.2)

17

2. Anexar el path de los archivos .hpp (Header) en el compilador Ignora el path de QuantLib.

18

3. Anexar el path de las librer as (din amicas y est aticas). En este caso, solo aplica para Boost (boost/math/normal).

19

Das könnte Ihnen auch gefallen