📄 controlvariatetest_1.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_1.java * * Created on January 20, 2002, 5PM */ package Examples.ControlVariates;import Statistics.*;import java.lang.Math.*; /** * <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>The Monte Carlo mean and controlled Monte Carlo mean of X using a sample * of size 100 are defined as random variables in their own right and the * variance of these random variables computed.</p> * * <p>The two variances are computed from a sample of size 100</p> * * <p>The idea is to check if use of the control variate does reduce * the variance</p> * * @author Michael J. Meyer */public class ControlVariateTest_1{ public static void main(String[] args) { final int nSamples=100; // sample size /* The random variable X without control variate. * Recall that Random.U1() delivers a uniform draw from [0,1] * and Random.STN() delivers a standard normal deviate. * No conditioning so the time parameter t is ignored. */ final 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 // the Monte Carlo mean as a random variable final RandomVariable mean=new RandomVariable(){ public double getValue(int t) { return X.expectation(nSamples); } // end getValue }; //end X // variance of the uncontrolled mean double variance=mean.variance(100); String message="Variance of uncontrolled mean = "+variance; System.out.println(message); // The random variable X with control variate. final 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 // the controlled Monte Carlo mean as a random variable final RandomVariable controlled_mean=new RandomVariable(){ public double getValue(int t) { return Xc.expectation(nSamples); } // end getValue }; //end Xc // variance of the controlled mean double controlled_variance=controlled_mean.variance(100); message="Variance of controlled mean = "+controlled_variance; System.out.println(message); } // end main } // end CVTest1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -