⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 urns.java

📁 金融资产定价,随机过程,MONTE CARLO 模拟 JAVA 程序和文档资料
💻 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*//* * urns.java * * Created on January 24, 2002, 9:00 PM */  package Examples.Probability;import Statistics.*;import Processes.*;import java.lang.Math.*; /** Two urns are filled with 100 white respectively 200 black balls. *  The following operation is performed 1000 times: a ball is selected at  * random from both urns and the balls exchanged. We expect white and black  * balls to be mixed in proportion 1:2 in both urns after 1000 exchanges  * (equilibrium distribution).</p> * * The <b>main</b> method of this class is a console program checking this * fact by allocating the process both as a <code>MarkovChain</code> * and as an <code>SFSMarkovChain</code>. Both computations generate the same  * number of sample paths and are timed to get an idea how much faster an  * <code>SFSMarkovChain</code> generates samples.</p> * * <p>All parameters fixed in source code. No user interaction. See book,  * chapter 2, example 2.</p> * * @author  Michael J. Meyer */public class Urns{    public static void main(String[] args)    {         final int n=100,       // balls in urn1                   m=200,       // balls in urn2                   T=1000,       // horizon                   j_0=100;      // initial state                  // Solution as a MarkovChain         System.out.println("Computation as a MarkovChain:");                  // timing         long Before=System.currentTimeMillis(), After, time;                  // allocate the Markov chain (number of balls in urn1)          final MarkovChain X=new MarkovChain(T,j_0){                      // define the transition kernel             public double q(int t, int i, int j)             {                  //cast to double to avoid integer division                  double fi=(double)i;                    double p_1=fi/n, q_1=1-p_1,                         p_2=(n-fi)/m, q_2=1-p_2;                                           if((i>=0)&&(i<=n)&&(j==i))  return p_1*p_2+q_1*q_2;                  if((i>0)&&(i<=n)&&(j==i-1)) return p_1*q_2;                  if((i>=0)&&(i<n)&&(j==i+1)) return q_1*p_2;                                    return 0; // all other cases                                } // end q                        }; // end X                    //allocate the random variable X_T          RandomVariable X_T=new RandomVariable(){                        public double getValue(int t)              {                   double[] x=X.get_path();                   X.newPathBranch(t);                   return x[T];                              } //end getValue                          }; // end X_T                                 System.out.println("Patience...\n\n");                      //compute the expectation E(X_T) over a sample of 20000 paths           double E=X_T.expectation(2000);           //cut this down to 5 decimals           E=Math.round(10000*E)/10000;                      After=System.currentTimeMillis();           time=After-Before;                      String message="Expected number of white balls in urn1 after"+                          " 1000 exchanges = "+E+"\n"+                          "Time: "+time+" milliseconds\n\n";           System.out.println(message);                                 /************************************************************/                      // solution as a SFSMarkovChain           System.out.println("Computation as a SFSMarkovChain:");                      //timing           Before=System.currentTimeMillis();                             // allocate the SFSMarkovChain           final SFSMarkovChain Y=new SFSMarkovChain(T,j_0,n+1){                          // define the transition probabilities q(i,j)               public double q(int i,int j)               {                   //cast to double to avoid integer division                   double fi=(double)i;                     double p_1=fi/n, q_1=1-p_1,                          p_2=(n-fi)/m, q_2=1-p_2;                                             if((i>=0)&&(i<=n)&&(j==i))  return p_1*p_2+q_1*q_2;                   if((i>0)&&(i<=n)&&(j==i-1)) return p_1*q_2;                   if((i>=0)&&(i<n)&&(j==i+1)) return q_1*p_2;                                      return 0;   // all other cases                                 } // end q                                                // define a(i), b(i)                public int a(int i){ return i-1; }                public int b(int i){ return i+1; }                                            }; // end Y                                       //allocate the random variable Y_T            RandomVariable Y_T=new RandomVariable(){                         public double getValue(int t)               {                    double[] y=Y.get_path();                    Y.newPathBranch(t);                    return y[T];                               } //end getValue                           }; // end Y_T                                  System.out.println("Patience...\n\n");                       //compute the expectation E(X_T) over a sample of 20000 paths            E=Y_T.expectation(2000);            //cut this down to 5 decimals            E=Math.round(10000*E)/10000;                       After=System.currentTimeMillis();            time=After-Before;                       message="Expected number of white balls in urn1 after"+                    " 1000 exchanges = "+E+"\n"+                    "Time: "+time+" milliseconds";            System.out.println(message);                                      } // end main      } // end Urns                                                                                                                                                                                                 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -