📄 gamblersfortune_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*//* * GamblersFortune_1.java * * Created on January 23, 2002, 6:20 PM */ package Examples.Probability;import Statistics.*;import Processes.*;import java.lang.Math.*; /** <p> Gambler starts with 6 dollars and bets 1 dollar on unfavorably loaded * coin (0.49/0.51) until he has 10 dollars or is wiped out. Random walk * allocated as a Markov chain. Possible states of the fortune 0,1,2,...,10. * Computes terminal (equilibrium) probability distribution.</p> * * @author Michael J. Meyer */public class GamblersFortune_1{ public static void main(String[] args) { final int T=2000; //time steps to horizon double j_0=6; //initial fortune final double p=0.49; //probability of success String message= "Gambler starts with 6 dollars and bets 1 dollar on unfavorably\n"+ "loaded coin (0.49/0.51) until he has 10 dollars or is wiped out.\n"+ "Possible states of the fortune 0,1,2,...,10.\n\n"+ "Probabilities of being in state j=0,1,...,10 at time T= "+T+ ":\nPatience it takes a while:\n"; System.out.println(message); /* allocate gamblers fortune as a Markov chain on the fly by defining * the transition probabilities q(t,i,j) in the body of the * constructor call */ final MarkovChain gf=new MarkovChain(T,j_0){ public double q(int t, int i, int j) { if((i==0)&&(j==0)) return 1; if ((i==10)&&(j==10)) return 1; if((i>0)&&(i<10)) { if(j==i-1) return 1-p; if(j==i+1) return p; } return 0; // all other cases } //end q };// end gf //allocate the ramdom vector X=(1_{[gf_T=j]})_{j=0,...,10} RandomVector X=new RandomVector(11){ public double[] getValue(int t) { double[] x=new double[11]; double[] path=gf.get_path(); gf.newPathBranch(t); x[(int)path[T]]=1; //all other components zero return x; } //end getValue }; // end X //expectation of X over 10000 paths double[] Q=X.expectation(10000); for(int i=0;i<11;i++) { Q[i]=1.0*Math.round(1000*Q[i])/1000; System.out.print(Q[i]+" "); } } // end main } // end GamblersFortune_1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -