{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lec12_MC_regression

lec12_MC_regression - from pylab import#brings in...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
from pylab import * #brings in vectorized version of 'random' module # #def MCint(f,xmin,xmax,n,m): # below=0 # BoxArea=m*(xmax-xmin) # for i in range(n): # x=uniform(xmin,xmax) #generates a single float number # y=uniform(0,m) # if y < f(x): # below+=1 # area = below/float(n)*BoxArea # return area # ## Vectorized version of above - it is MUCH faster for high n values #def MCint_vec(f,xmin,xmax,n,m): # BoxArea=m*(xmax-xmin) # x=uniform(xmin,xmax,n) #here generates a vector of random floats and length n # y=uniform(0,m,n) # below=y[y < f(x)].size #picks out indices of y values less than f(x) # area=below/float(n) * BoxArea # return area # #def MCint_plot(f,xmin,xmax,n,m): # x=uniform(xmin,xmax,n) # y=uniform(0,m,n) # print x.shape, y.shape # xf=linspace(xmin,xmax,100) # plot(x,y,'ro',mfc='white',lw=2) # plot(xf,f(xf),'b-',lw=2) # ylim(0,m) # xlabel('$x$',fontsize=18) # ylabel('$f(x)$',fontsize=18) # return 0 # #def f(x): # return x*sin(x) # #Itrue=np.pi # #print 'Itrue = ',Itrue #print 'N \t \t Iest' #print '-'*30 # #nvals = [100,1000,int(1e4),int(1e5),int(1e6),int(1e7)] #errs=[] ##MCint_plot(f,0,np.pi,1000,2) #for n in nvals: # Iest=MCint(f,0,np.pi,n,2) # print '%7i \t %2.6f' % (n,Iest) # errs.append(abs(Itrue - Iest))
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}