Skip to content

Instantly share code, notes, and snippets.

@ahmadyan
Last active August 29, 2015 14:21
Show Gist options
  • Save ahmadyan/32d3d4b0d2ee275f9bd3 to your computer and use it in GitHub Desktop.
Save ahmadyan/32d3d4b0d2ee275f9bd3 to your computer and use it in GitHub Desktop.
void mc(double r, double** samples){
cout << "Monte Carlo simulation" << endl ;
using namespace boost::math;
double sample_inside=0;
cout << "sizeof(sample_count)=" << sizeof(samples) << endl ;
for(int i=0;i<sample_count;i++){
double x = samples[i][0];
double y = samples[i][1];
if(check(x,y,r)){
sample_inside++;
mc_stream << "set obj "<< 10+i<< " circle at " << x << " ," << y << " size .001 fc rgb \"blue\" \n" << endl ;
}else{
mc_stream << "set obj "<< 10+i<< " circle at " << x << " ," << y << " size .001 fc rgb \"red\" \n" << endl ;
}
}
cout << " samples=" << sample_inside << " " << sample_count << endl ;
//computing the confidence interval
double area = (double)sample_inside / (double)sample_count ;
double deviation = sqrt( area * (1-area));
double alpha = 0.05;
//students_t dist(deviation-1);
//double T = quantile( complement( dist, alpha/ 2));
double T = 1.96; //corresponds with 95% confidence interval
cout << "T=" << T << endl ;
double w = T * sqrt(deviation / (double)sample_count) ;
//double w = T*deviation / sqrt(double(deviation));
cout << "w=" << w << endl ;
cout << "confidence interval= [" << area - w << " , " << area + w << " ] " << endl ;
cout << "estimated area = " << area << endl ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment