📄 controlvariatetest_2.java
字号:
/* WARANTY NOTICE AND COPYRIGHTThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.Copyright (C) Michael J. Meyermatmjm@mindspring.comspyqqqdia@yahoo.com*//* * ControlVariateTest_2.java * * Created on January 20, 2002, 5PM */ package Examples.ControlVariates;import Statistics.*;import java.lang.Math.*; /** <p>Compares a Monte Carlo mean to a Monte Carlo mean computed using a * control variate.</p> * * <p>We set up the random variable X=U+eN where U is uniform on [0,1], N * standard normal and independent of U and e=0.1. As a control variate we use * Y=U-eN. Then the correlation rho(X,Y)=(1-12e^2)/(1+12e^2)=0.78571.</p> * * <p>We compute this correlation and also the mean of X both with and without * the use of the control variate from a sample of 100,000 observations of X. * The objective is to see how much closer the controlled mean is to the true * mean E(X)=1/2 than the uncontrolled mean.</p> * * @author Michael J. Meyer */public class ControlVariateTest_2{ public static void main(String[] args) { int nSamples=100; // sample size /* X without control variate * Recall that Random.U1() delivers a uniform draw from [0,1] * and Random.STN() delivers a standard normal deviate. */ RandomVariable X=new RandomVariable(){ public double getValue(int t) { double U=Random.U1(), // uniform in [0,1] N=Random.STN(), // standard normal e=0.1; return U+e*N; } // end getValue }; //end X //Monte Carlo mean without control variate double mean=X.expectation(nSamples); //cut this down to 5 decimals mean=1.0*Math.round(10000*mean)/10000; String message="True mean = 1/2\n"+ "Monte carlo mean without control variate = "+mean+ "\n\n"; System.out.println(message); // X with control variate ControlledRandomVariable Xc=new ControlledRandomVariable(){ public double[] getControlledValue(int t) { double U=Random.U1(), // uniform in [0,1] N=Random.STN(), // standard normal e=0.1, x=U+e*N, // X y=U-e*N; // control variate Y double[] result={x,y}; return result; } // end getValue public double getControlVariateMean(int t) { return 0.5; } }; //end Xc //correlation of X with control variate double rho=Xc.correlationWithControlVariate(20000); //cut this down to 5 decimals rho=1.0*Math.round(10000*rho)/10000; //Monte Carlo mean with control variate mean=Xc.expectation(nSamples); //cut this down to 5 decimals mean=1.0*Math.round(10000*mean)/10000; message="Correlation with control variate = "+rho+ "\nMonte carlo mean with control variate = "+mean; System.out.println(message); } // end main } // end CVTest1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -