#inf c++ visual studio -
i came across question in calculating sum of double. when set iteration 100000, function asian_call_mc still return number. however, when set iteration around 500000 , above, begin return 1.#inf. can tell me why happens , how solve it? using visual studio 2013 write c++ code.
double avg_price(double init_p, double impl_vol, double drift, int step, double deltasqrt) { //calculate average price of 1 sample path //delta = t/ step //drift = (risk_free - div_y - impl_vol*impl_vol / 2)*(t / step) double sa = 0.0; double st = init_p; (int = 0; < step; i++) { st = st*exp(drift + impl_vol*deltasqrt*normal_gen()); //sa = sa * / (i + 1) + st / (i + 1); sa += st; } sa = sa / double(step); return sa; } double asian_call_mc(double strike_p, double t, double init_p, double impl_vol, double risk_free, double div_y, int iter, int step) { //calculate constants in advance reduce computation time double drift = (risk_free - div_y - impl_vol*impl_vol / 2)*double(t / step); double deltasqrt = sqrt(double(t / step)); //generate x1, average x , y double cur_p = avg_price(init_p,impl_vol,drift,step,deltasqrt); double pay_o=0.0; double x = max(cur_p - strike_p,0.0); //double y = pow(x, 2.0); //generate x2 xn (int = 0; < iter; i++) { cur_p = avg_price(init_p, impl_vol, drift, step, deltasqrt); x = max(cur_p - strike_p,0.0); //double q = double(i) / double(i + 1); //pay_o = pay_o *i/(i+1) + x / (i + 1); pay_o += x; //y = (1 - (1 / (i + 1)))*y + x*x / (i + 1); } //pay_o = pay_o / double(iter); //stdev = sqrt((y - pow(pay_o , 2)) / (iter - 1)); //return pay_o*exp(-risk_free*t) ; return pay_o; }
it looks want compute mean values. the way people learn calculate mean sum values, divide sum number of values contributed sum.
this method has few problems associated -- example, adding many values might give sum large variable holding it.
another technique used, accumulates "running" mean instead of sum. running mean's value mean value samples accumulated, never blows overflow (floating-point infinity) value (except when 1 of accumulated samples infinity).
the example below demonstrates how calculate running mean. calculates sum , shows how sum/count compares running mean (to show same -- haven't let run long enough overflow sum).
the example uses c-library rand(), demonstration purposes -- needed calculate mean values from.
#include <cstdlib> #include <ctime> #include <iostream> #include <iomanip> int main() { srand(static_cast<unsigned>(time(0))); double count = 0; double running_mean = 0; double sum = 0; auto start = time(0); auto end = start + 5; while(time(0) < end) { double sample = rand(); count += 1; running_mean += (sample - running_mean)/count; sum += sample; } std::cout << std::setprecision(12); std::cout << "running mean:" << running_mean << " count:" << count << '\n'; double sum_mean = sum / count; std::cout << "sum:" << sum << " sum/count:" << sum_mean << '\n'; }
edit: tried -- technique appeared in commented-out lines missed in op's code
unlike computing average value accumulating grand sum, running mean technique cannot overflow @ point. knowing tried , didn't problem, probable cause becomes 1 of iteration's terms is, inf. single inf term added, accumulated sum or mean become inf , stay inf.
the section of code normal_gen()
used inside argument call exp
function. name normal_gen()
sounds source of normally-distributed random values. usual implementation employs box–muller transform, cannot produce values on 7 standard-deviations away mean. if box-muller generator causing inf, occur within fewer iterations reported. however, more advanced generators can produce more extreme values -- ideally normal distribution has nonzero probability of producing any finite real value.
if randomly large normal sample causing problem, correlation increased iteration count not more iterations inflate sum, point of overflowing adding more values -- more iterations gave program better chance hit unlikely random value result in inf term.
Comments
Post a Comment