📄 gamblersfortune.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*//* * GamblersFortune.java * * Created on January 18, 2002, 5PM */ package Examples.Probability;import Statistics.*;import Processes.*;import java.lang.Math.*; /** <p>Console program setting up a biased random walk X and checking the * gambler's expected winnings (losses really) and the probability of losing it * all under the following stopping rule:</p> * *<p>Stop after 20000 throws or if you are 5 ahead or 100 down, whichever *comes first.</p> * * @author Michael J. Meyer */public class GamblersFortune{ public static void main(String[] args) { String message; message="Starting with zero dollars we bet repeatedly one dollar\n"+ "on the throw of a coin where the odds are 0.499/0.501\n"+ "against us. We examine expected winnings under a\n"+ "stopping strategy.\n\n"; System.out.println(message); int T=20000; //time steps to horizon double X_0=0.0; //initial value final BiasedRandomWalk X=new BiasedRandomWalk(T,X_0,0.499); message="Strategy: we stop after 20000 throws or if we are\n"+ "5 ahead or 100 down, whichever comes first.\n"+ "Expectations computed from 50000 paths. "+ "Patience, it takes a while.\n"; System.out.println(message); //here's the quitting rule, an abstract class instantiated //in front of your very eyes: StoppingTime tau=new StoppingTime(){ public boolean stop(int t) { final double[] path=X.get_path(); return ((t==20000)||(path[t]==+5)||(path[t]==-100)); } }; //end tau //the random walk sampled at tau final RandomVariable X_tau=X.sampledAt(tau); //Monte Carlo mean, sample size 50000 double E=X_tau.expectation(50000); //cut this down to 5 decimals E=1.0*Math.round(10000*E)/10000; message="Expected winnings = "+E; System.out.println(message); //indicator function of the event [X_tau=-1000] RandomVariable Y=new RandomVariable(){ public double getValue(int t) { return (X_tau.getValue(t)==-100) ? 1:0; } }; //end Y // probability of total loss double q=Y.expectation(50000); //cut this down to 5 decimals q=1.0*Math.round(10000*q)/10000; message="Probability of losing 100 = "+q; System.out.println(message); } //end main }//end Gambler's fortune
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -