April 20th, 2016, 8:29 am
Sorry about being a bit curt. Thank you cuch for your suggestion. I am quickly pasting the code for one example integral zdz. Please copy it in your matlab editor or simply clone in any other language. As I have set up the simulation, you can trivially modify the simple monte carlo using equations I gave in previous post vs how we traditionally do an accurate short stepping monte carlo. Excuse any possible minor errors as I am not being formal. @Cuch and others, your feedback is requested.[$]\int _0^t z(s)\text{$\sigma $dz}(s)=\frac{2.5}{\sqrt{2\text{Pi}}}z(0)\sigma \sqrt{t}N(0,1) +.5\frac{2.5}{\sqrt{2\text{Pi}}}\sigma ^2t\text{ }N(0,1)^2-.5\sigma ^2t[$]Here is the main function.function [] = SDE_Compare_Zintegrals_Infiniti_public( )s1 = RandStream('mt19937ar'); RandStream.setDefaultStream(s1); savedState = s1.State; z0=1;%z0=10;%z0=.5;sigma0=0.710%sigma0=0.710/4%sigma0=0.710*4T_index=200; %T_index=2000; %T_index=20; dt=.005/1.0;T=(T_index)*dt;paths=1000000; zz(1:paths)=z0; I2(1:paths)=0.0;%integral zdz over time by traditional short stepped monte carlo.zzT(1:paths)=0.0;%One step integral zdz calculated by my method for nn=1:T_index %Loop over time. t=(nn)*dt; t2=(nn-1)*dt; Random1=randn(size(zz)); RandTemp1=Random1.*sqrt(dt); I2=I2+(RandTemp1*sigma0).*(zz); zz = zz +RandTemp1*sigma0; end %My One Step method calculation starts Random1=randn(size(zzT)); zzT=z0*sigma0.*Random1*T.^.5*2.5/sqrt(2*pi)+... %Excuse the coefficients 2.5/sqrt(2pi). It is unity. I followed as I wrote on internet. Please modify. .5*sigma0.^2* Random1.^2.*T*2.5/sqrt(2*pi)-... .5*sigma0.^2.*T; I2_avg=sum(I2)./paths zzT_avg=sum(zzT)./paths I2_avg2=sum(abs(I2))./paths zzT_avg2=sum(abs(zzT))./pathsBinSize=.05;%Here you can change the resolution of monte carlo density. You might have to look at the graph and if it is jagged and noisy, increase the bin sizeMaxCutOff=1000;%Max cut off is a value given to density generation program. [XDensity,IndexOutX,IndexMaxX] = MakeDensityFromSimulation_Infiniti(I2,paths,BinSize,MaxCutOff );[XDensity2,IndexOutX2,IndexMaxX2] = MakeDensityFromSimulation_Infiniti(zzT,paths,BinSize,MaxCutOff ); plot(IndexOutX(1:IndexMaxX),XDensity(1:IndexMaxX),'g',IndexOutX2(1:IndexMaxX2),XDensity2(1:IndexMaxX2),'r') str=input('look at overlaid Graph comparison of traditional Monte carlo density of zdz(green line) with density of SDE generated from one step monte carlo method(red line)>'); endHere is the helper function to graph from monte carlofunction [XDensity,IndexOut,IndexMax] = MakeDensityFromSimulation_Infiniti(X,Paths,BinSize,MaxCutOff )%Processes monte carlo paths to return a series Xdensity as a function of IndexOut. IndexMax is the maximum value of index.% Xmin=0; Xmax=0; for p=1:Paths if(X(p)>MaxCutOff) X(p)=MaxCutOff; end if(Xmin>real(X(p))) Xmin=real(X(p)); end if(Xmax<real(X(p))) Xmax=real(X(p)); end end IndexMax=floor((Xmax-Xmin)/BinSize+.5)+1 XDensity(1:IndexMax)=0.0; for p=1:Paths index=floor(real(X(p)-Xmin)/BinSize+.5)+1; if((index)<1) index=1; end if(real(index)>IndexMax) index=IndexMax; end XDensity(index)=XDensity(index)+1.0/Paths/BinSize; end IndexOut(1:IndexMax)=Xmin+(0:(IndexMax-1))*BinSize; end
You think life is a secret, Life is only love of flying, It has seen many ups and downs, But it likes travel more than the destination. Allama Iqbal