33 const std::string bsm_model_program =
34 " var d1 := (log(s / k) + (r + v^2 / 2) * t) / (v * sqrt(t)); "
35 " var d2 := d1 - v * sqrt(t); "
37 " if (callput_flag == 'call') "
38 " s * ncdf(d1) - k * e^(-r * t) * ncdf(d2); "
39 " else if (callput_flag == 'put') "
40 " k * e^(-r * t) * ncdf(-d2) - s * ncdf(-d1); "
49 std::string callput_flag;
53 symbol_table_t symbol_table;
54 symbol_table.add_variable(
"s",s);
55 symbol_table.add_variable(
"k",k);
56 symbol_table.add_variable(
"t",t);
57 symbol_table.add_variable(
"r",r);
58 symbol_table.add_variable(
"v",v);
59 symbol_table.add_constant(
"e",e);
60 symbol_table.add_stringvar(
"callput_flag",callput_flag);
62 expression_t expression;
63 expression.register_symbol_table(symbol_table);
66 parser.compile(bsm_model_program,expression);
68 callput_flag =
"call";
70 const T bsm_call_option_price = expression.value();
74 const T bsm_put_option_price = expression.value();
76 printf(
"BSM(call, %5.3f, %5.3f, %5.3f, %5.3f, %5.3f) = %10.6f\n",
78 bsm_call_option_price);
80 printf(
"BSM(put , %5.3f, %5.3f, %5.3f, %5.3f, %5.3f) = %10.6f\n",
82 bsm_put_option_price);
84 const T put_call_parity_diff =
85 (bsm_call_option_price - bsm_put_option_price) -
86 (s - k * std::exp(-r * t));
88 printf(
"Put-Call parity difference: %20.17f\n", put_call_parity_diff);