Monte Carlo simulation is an analytic method that imitates a physical system by using randomly generated values for uncertain variables. It is named after the famous casino in Monaco. Monte Carlo simulations are widely applicable when other techniques fail due to their computational intensity. They involve repeating a simulation many times to generate a range of possible scenarios and then averaging the results. Accuracy increases with the square root of the number of repetitions. Common applications include designing nuclear reactors, predicting star evolution, and forecasting stock markets.
Monte Carlo simulation is an analytic method that imitates a physical system by using randomly generated values for uncertain variables. It is named after the famous casino in Monaco. Monte Carlo simulations are widely applicable when other techniques fail due to their computational intensity. They involve repeating a simulation many times to generate a range of possible scenarios and then averaging the results. Accuracy increases with the square root of the number of repetitions. Common applications include designing nuclear reactors, predicting star evolution, and forecasting stock markets.
Monte Carlo simulation is an analytic method that imitates a physical system by using randomly generated values for uncertain variables. It is named after the famous casino in Monaco. Monte Carlo simulations are widely applicable when other techniques fail due to their computational intensity. They involve repeating a simulation many times to generate a range of possible scenarios and then averaging the results. Accuracy increases with the square root of the number of repetitions. Common applications include designing nuclear reactors, predicting star evolution, and forecasting stock markets.
Monte Carlo simulation is an analytic method that imitates a physical system by using randomly generated values for uncertain variables. It is named after the famous casino in Monaco. Monte Carlo simulations are widely applicable when other techniques fail due to their computational intensity. They involve repeating a simulation many times to generate a range of possible scenarios and then averaging the results. Accuracy increases with the square root of the number of repetitions. Common applications include designing nuclear reactors, predicting star evolution, and forecasting stock markets.
Download as DOC, PDF, TXT or read online from Scribd
Download as doc, pdf, or txt
You are on page 1of 15
Monte carlo Simulation in java
This section under major construction.
In 1953 Enrico Fermi, John Pasta, and Stanslaw Ulam created the frst "computer experiment" to study a vibrarting atomic lattice. Nonlinear system couldn't be analyzed by classical mathematics. Simulation = analytic method that imitates a physical system. Monte Carlo simulation = use randomly generated values for uncertain variables. Named after famous casino in Monaco. At essentially each step in the evolution of the calculation, Repeat several times to generate range of possible scenarios, and average results. Widely applicable brute force solution. Computationally intensive, so use when other techniques fail. Typically, accuracy is proportional to square root of number of repetitions. Such techniques are widely applied in various domains including: designing nuclear reactors, predicting the evolution of stars, forecasting the stock market, etc. Generating random numbers. The math library function Math.random generate a pseudo-random number greater than or equal to 0.0 and less than 1.0. If you want to generate random integers or booleans, the best way is to use the library Random. Program RandomDemo.java illustrates how to use it. Random random = new Random(); boolean a = random.nextBoolean(); // true or false int b = random.nextInt(); // between -2^! and 2^! - ! int " = random.nextInt(!##); // between # and $$ double d = random.next%ouble(); // between #.# and !.# double e = random.next&aussian(); // &aussian with mean # and stdde' = ! Note that you should only create a new Random object once per program. You will not get more "random" results by creating more than one. For debugging, you may wish to produce the same sequence of pseudo-random number each time your program executes. To do this, invoke the constructor with a lon( argument. Random random = new Random(!2)*+,-); The pseudo-random number generator will use 1234567 as the seed. Use .e"ureRandom for cryptographically secure pseudo-random numbers, e.g., for cryptography or slot machines. Linear congruential random number generator. With integer types we must be cognizant of overfow. Consider a * b (mod m) as an example (either in context of a^b (mod m) or linear congruential random number generator: Given constants a, c, m, and a seed x[0], iterate: x = (a * x + c) mod m. Park and Miller suggest a = 16807, m = 2147483647, c = 0 for 32-bit signed integers. To avoid overfow, use Schrage's method. /re"om0ute1 2 = m / a3 r = m 4 a Iterate1 x = a 5 (x - x/ 2) 5 2) - r 5 (x / 2) Exercise: compute cycle length. Library of probability functions. OR-Objects contains many classic probability distributions and random number generators, including Normal, F, Chi Square, Gamma, Binomial, Poisson. You can download the jar fle here. Program ProbDemo.java illustrates how to use it. It generate one random value from the gamma distribution and 5 from the binomial distribution. Note that the method is called (etRandom."aler and not(etRandom."alar. &amma%istribution x = new &amma%istribution(23 ); .6stem.out.0rintln(x.(etRandom."aler()); Binomial%istribution 6 = new Binomial%istribution(#.!3 !##); .6stem.out.0rintln(6.(etRandom7e"tor(*)); Queuing models. M/M/1, etc. A manufacturing facility has M identical machines. Each machine fails after a time that is exponentially distributed with mean 1 / . A single repair person is responsible for maintaining all the machines, and the time to fx a machine is exponentially distributed with mean 1 / . Simulate the fraction of time in which no machines are operational. Difusion-limited aggregation. Difuse = undergo random walk. The physical process difusion-limited aggregation (DLA) models the formation of an aggregate on a surface, including lichen growth, the generation of polymers out of solutions, carbon deposits on the walls of a cylinder of a Diesel engine, path of electric discharge, and urban settlement. The modeled aggregate forms when particles are released one at a time into a volume of space and, infuenced by random thermal motion, they difuse throughout the volume. There is a fnite probability that the short-range attraction between particles will infuence the motion. Two particles which come into contact with each other will stick together and form a larger unit. The probability of sticking increases as clusters of occupied sites form in the aggregate, stimulating further growth. Simulate this process in 2D using Monte Carlo methods: Create a 2D grid and introduce particles to the lattice through a launching zone one at a time. After a particle is launched, it wanders throughout with a random walk until it either sticks to the aggregate or wanders of the lattice into the kill zone. If a wandering particle enters an empty site next to an occupied site, then the particle's current location automatically becomes part of the aggregate. Otherwise, the random walk continues. Repeat this process until the aggregate contains some pre-determined number of particles. Reference: Wong, Samuel, Computational Methods in Physics and Engineering, 1992. Program DLA.java simulates the growth of a DLA with the following properties. It uses the helper data type Picture.java. Set the initial aggregate to be the bottom row of the N-by-N lattice. Launch the particles from a random cell in top row. Assume that the particle goes up with probability 0.15, down with probability 0.35, and left or right with probability 1/4 each. Continue until the particles stick to a neighboring cell (above, below, left, right, or one of the four diagonals) or leaves the N-by-N lattice. The preferred downward direction is analogous to the efect of a temperature gradient on Brownian motion, or like how when a crystal is formed, the bottom of the aggregate is cooled more than the top; or like the infuence of a gravitational force. For efect, we color the particles in the order they are released according to the rainbow from red to violet. Below are three simulations with N = 176; here is an image with N = 600. Brownian motion. Brownian motion is a random process used to model a wide variety of physical phenomenon including the dispersion of ink fowing in water, and the behavior of atomic particles predicted by quantum physics. (more applications). Fundamental random process in the universe. It is the limit of a discrete random walk and the stochastic analog of the Gaussian distribution. It is now widely used in computational fnance, economics, queuing theory, engineering, robotics, medical imaging, biology, and fexible manufacturing systems. First studied by a Scottish botanist Robert Brown in 1828 and analyzed mathematically by Albert Einstein in 1905. Jean-Baptiste Perrin performed experiments to confrm Einstein's predictions and won a Nobel Prize for his work. An applet to illustrate physical process that may govern cause of Brownian motion. Simulating a Brownian motion. Since Brownian motion is a continuous and stochastic process, we can only hope to plot one path on a fnite interval, sampled at a fnite number of points. We can interpolate linearly between these points (i.e., connect the dots). For simplicitly, we'll assume the interval is from 0 to 1 and the sample points t 0 , t 1 , ..., t N are equally spaced in this interval. To simulate a standard Brownian motion, repeatedly generate independent Gaussian random variables with mean 0 and standard deviation sqrt(1/N). The value of the Brownian motion at time i is the sum of the frst i increments. Geometric Brownian motion. A variant of Brownian motion is widely used to model stock prices, and the Nobel-prize winning Black-Scholes model is centered on this stochastic process. A geometric Brownian motion with drift and volatility is a stochastic process that can model the price of a stock. The parameter models the percentage drift. If = 0.10, then we expect the stock to increase by 10% each year. The parameter models the percentage volatility. If = 0.20, then the standard deviation of the stock price over one year is roughly 20% of the current stock price. To simulate a geometric Brownian motion from time t = 0 to t = T, we follow the same procedure for standard Brownian motion, but multiply the increments, instead of adding them, and incorporate the drift and volatility parameters. Specifcally, we multiply the current price by by (1 + t + sqrt(t)Z), where Z is a standard Gaussian and t = T/N Start with X(0) = 100, = 0.04. construction of BM. Black-Scholes formula. Move to here? Ising model. The motions of electrons around a nucleus produce a magnetic feld associated with the atom. These atomic magnets act much like conventional magnets. Typically, the magnets point in random directions, and all of the forces cancel out leaving no overall magnetic feld in a macroscopic clump of matter. However, in some materials (e.g., iron), the magnets can line up producing a measurable magnetic feld. A major achievement of 19th century physics was to describe and understand the equations governing atomic magnets. The probability that state S occurs is given by the Boltzmann probability density function P(S) = e - E(S)/kT / Z, where Z is the normalizing constant (partition function) sum e -E(A)/kT over all states A, k is Boltzmann's constant, T is the absolute temperature (in degrees Kelvin), and E(S) is the energy of the system in state S. Ising model proposed to describe magnetism in crystalline materials. Also models other naturally occurring phenomena including: freezing and evaporation of liquids, protein folding, and behavior of glassy substances. Ising model. The Boltzmann probability function is an elegant model of magnetism. However, it is not practical to apply it for calculating the magnetic properties of a real iron magnet because any macroscopic chunk of iron contains an enormous number atoms and they interact in complicated ways. The Ising model is a simplifed model for magnets that captures many of their important properties, including phase transitions at a critical temperature. (Above this temperature, no macroscopic magnetism, below it, systems exhibits magnetism. For example, iron loses its magnetization around 770 degrees Celsius. Remarkable thing is that transition is sudden.) reference First introduced by Lenz and Ising in the 1920s. In the Ising model, the iron magnet is divided into an N-by-N grid of cells. (Vertex = atom in crystal, edge = bond between adjacent atoms.) Each cell contains an abstract entity known as spin. The spin s i of cell i is in one of two states: pointing up (+1) or pointing down (-1). The interactions between cells is limited to nearest neighbors. The total magnetism of the system M = sum of s i . The total energy of the system E = sum of - J s i s j , where the sum is taken over all nearest neighbors i and j. The constant J measures the strength of the spin-spin interactions (in units of energy, say ergs). [The model can be extended to allow interaction with an external magnetic feld, in which case we add the term -B sum of s k over all sites k.] If J > 0, the energy is minimized when the spins are aligned (both +1 or both -1) - this modelsferromagnetism. if J < 0, the energy is minimized when the spins are oppositely aligned - this models antiferromagnetism. Given this model, a classic problem in statistical mechanics is to compute the expected magenetism. A state is the specifcation of the spin for each of the N^2 lattice cells. The expected magnetism of the system E[M] = sum of M(S) P(S) over all states S, where M(S) is the magnetism of state S, and P(S) is the probability of state S occurring according to the Boltzmann probability function. Unfortunately, this equation is not amenable to a direct computational because the number of states S is 2 N*N for an N-by-N lattice. Straightforward Monte Carlo integration won't work because random points will not contribute much to sum. Need selective sampling, ideally sample points proportional to e -E/kT . (In 1925, Ising solved the problem in one dimension - no phase transition. In a 1944 tour de force, Onsager solved the 2D Ising problem exactly. His solution showed that it has a phase transition. Not likely to be solved in 3D - see intractability section.) Metropolis algorithm. Widespread usage of Monte Carlo methods began with Metropolis algorithm for calculation of rigid-sphere system. Published in 1953 after dinner conversation between Metropolis, Rosenbluth, Rosenbluth, Teller, and Teller. Widely used to study equilibrium properties of a system of atoms. Sample using Markov chain using Metropolis' rule: transition from A to B with probability 1 if E <= 0, and with probability e -E/kT if E > 0. When applied to the Ising model, this Markov chain is ergodic (similar to Google PageRank requirement) so the theory underlying the Metropolis algorithm applies. Converges to stationary distribution. Program Cell.java, State.java, and Metropolis.java implements the Metropolis algorithm for a 2D lattice. Ising.java is a procedural programming version. "Doing physics by tossing dice." Simulate complicated physical system by a sequence of simple random steps. Measuring physical quantities. Measure magnetism, energy, specifc heat when system has thermalized (the system has reached a thermal equilibrium with its surrounding environment at a common temperature T). Compute the average energy and the average magenetization over time. Also interesting to compute the variance of the energy or specifc heat <c> = <E 2 > - <E> 2 , and the variance of the magnetization or susceptibility<> = <M 2 > - <M> 2 . Determining when system has thermalized is a challenging problem - in practice, many scientists use ad hoc methods. Phase transition. Phase transition occurs when temperature T c is 2 / ln(1 + sqrt(2)) = 2.26918). T c is known as the Curie temperature. Plot magnetization M (average of all spins) vs. temperature (kT = 1 to 4). Discontinuity of slope is signature of second order phase transition. Slope approaches infnity. Plot energy (average of all spin- spin interactions) vs. temperature (kT = 1 to 4). Smooth curve through phase transition. Compare against exact solution. Critical temperature for which algorithm dramatically slows down. Below are the 5000th sample trajectory for J/kT = 0.4 (hot / disorder) and 0.47 (cold / order). The system becomes magnetic as temperature decreases; moreover, as temperature decreases the probability that neighboring sites have the same spin increasing (more clumping). Experiments. Start will above critical temperature. State converges to nearly uniform regardless of initial state (all up, all down, random) and fuctuates rapidly. Zero magnetization. Start well below critical temperature. Start all spins with equal value (all up or all down). A few small clusters of opposite spin form. Start well below critical temperature. Start with random spins. Large clusters of each spin form; eventually simulation makes up its mind. Equally likely to have large clusters in up or down spin. Start close to critical temperature. Large clusters form, but fuctuate very slowly.
Exact solution for Ising model known for 1D and 2D; NP-hard for 3d and nonplanar graphs. Models phase changes in binary alloys and spin glasses. Also models neural networks, focking birds, and beating heart cells. Over 10,000+ papers published using the Ising model. Java Simulation --- Calculate PI using Random Numbers import java.util.*; public class CalculatePI2 { public static boolean isInside (double xPos, double Pos! { boolean result; double distance " Mat#.s$rt((xPos * xPos! % (Pos * Pos!!; i& (distance ' (! result " &alse; return(distance ' (!; ) public static double computePI (int num*#ro+s! { ,andom random-en " ne+ ,andom (Sstem.current*imeMillis(!!; int #its " .; double PI " .; &or (int i " .; i '" num*#ro+s; i%%! { double xPos " (random-en.next/ouble(!! * 2 0 (..; double Pos " (random-en.next/ouble(!! * 2 0 (..; i& (isInside(xPos, Pos!! { #its%%; ) PI " (1 * (#its2num*#ro+s!!; ) return PI; )
public static void main (Strin345 ar3s! { Scanner reader " ne+ Scanner (Sstem.in!; Sstem.out.println(6*#is pro3ram approximates PI usin3 t#e Monte Carlo met#od.6!; Sstem.out.println(6It simulates t#ro+in3 darts at a dartboard.6!; Sstem.out.print(6Please enter number o& t#ro+s7 6!; int num*#ro+s " reader.nextInt(!; double PI " computePI(num*#ro+s!; double /i&&erence " PI 0 Mat#.PI; Sstem.out.println (68umber o& t#ro+s " 6 % num*#ro+s % 6, Computed PI " 6 % PI % 6, /i&&erence " 6 % /i&&erence !; ) ) Introduction A class of computational algorithms that rely on repeated random sampling to compute their results are called the Monte Carlo methods. Monte Carlo methods are often used when simulating physical and mathematical systems. Because of their reliance on repeated computation and random or pseudo-random numbers, Monte Carlo methods are most suited to calculation by a computer. Monte Carlo methods tend to be used when it is infeasible or impossible to compute an exact result with a deterministic algorithm. Monte Carlo algorithms work based on the aw of arge !umbers. It says that if you generate a large number of samples, e"entually, you will get the approximate desired distribution. Monte Carlo methods ha"e three characteristics# $. %andom sample generation &. 'he input distribution is known (. !umerical experiments 'he direct output of the Monte Carlo simulation method is the generation of random sampling. )ther performance or statistical outputs are indirect methods which depend on the applications. 'here are many di*erent numerical experiments that can be done, probability distribution is one of them. +robability distribution identi,es either the probability of each "alue of an unidenti,ed random "ariable -when the "ariable is discrete., or the probability of the "alue falling within a particular inter"al -when the "ariable is continuous.. 'hat is e/ui"alent to saying that for random "ariables 0 with the distribution in /uestion, +r10 2 a3 2 4 for all real numbers a. 'hat is, the probability that 0 attains the "alue a is 5ero, for any number a. If the distribution of 0 is continuous, then 0 is called a continuous random "ariable. !ormal distribution, continuous uniform distribution, beta distribution, and 6amma distribution are well known absolutely continuous distributions. In the sample program, I will be using a 'riangular distribution, which is is a continuous probability distribution with a lower limit a, mode c, and upper limit b. 'riangular distribution is therefore often used in business decision making, particularly in simulations. 6enerally, when not much is known about the distribution of an outcome -say, only its smallest and largest "alues., it is possible to use uniform distribution. But, if the most likely outcome is also known, then the outcome can be simulated by a 'riangular distribution. 7hilst Monte Carlo methods can be a "aluable aid to decision making, they should not be used without an understanding of the process being modeled. 'ypically, in any acti"ity, there are a few critical inputs, and it is these which should be the focus of the analysis, not trying to attribute complex distributions to minor "ariables. 8o, a Monte Carlo analysis should be preceded by a sensiti"ity analysis to determine what the important parameters are. Background 'here are many ways of approaching a Monte Carlo analysis, but a good starting point is a 9ow chart of the processing being modeled. :or the purposes of this simulation, I will be focus on a tennis tournament simulation based on game winnings. 'he 9ow chart of the process goes like this# ;sing the code <ere is an example where we ha"e two players, each with "arying skills and talent. Assume they each ha"e played $4 tournaments and we ha"e gotten the minimum and maximum number of wins -assuming winning the tournament has the same number of wins as all the other tournaments.. 7e can also compute the mean win for all the $4 tournaments they ha"e played. Collapse = Copy Code double13 >ar7ins 2 new double13?player$,player&@3A double13 min7ins 2 new double13?$$,$&@A double13 mean7ins 2 new double13?$&.B,$(@A double13 max7ins 2 new double13?$C,$B@A double13 results 2 MonteCarlo.simulate-&, new double13 ? $$, $& @, new double13 ? $&.B, $( @, new double13 ? $C, $B @.A 'he main simulation is done using the following triangular distribution# Collapse = Copy Code public static double triangular-double Min,double Mode,double Max. ? // Declarations double %24.4A // Initialise %andom r 2 new %andom-.A % 2 r.!extDouble-.A //between 0.0 and 1.0 gaussian // Triangular if - % 22 -- Mode - Min. E - Max - Min... ? return ModeA @ else if - % F -- Mode - Min. E - Max - Min... ? return Min G Math.8/rt- % H - Max - Min. H - Mode - Min..A else ? return Max - Math.8/rt--$ - %. H - Max - Min. H - Max - Mode..A @ @ As seen in the output, player $ is the most likely to win. <owe"er, player & is not far behind. 8o, with a little more practice and training, player & manages to reduce the mean number of wins on the next $4 tournaments. 8imulating this again will show that he has greatly impro"ed his performance and is most likely to win o"er player $. )utput# Collapse = Copy Code 8imulation %esults 7insB&B$ 7insICIJ +layer$ win probability B&.B$ - +layer& win probabilityIC.IJ )utput after training# Collapse = Copy Code 8imulation %esults 7insI$$J 7insBKK$ +layer$ win probability I$.$J - +layer& win probabilityBK.K$# 'here are some instances where it is the other way around. As I ha"e mentioned ealier, by the aw of arge !umbers, if you increase the number of simulated tournaments, you will notice that either player will always be the winner because the expected result will always come closer because of the large number of simulations. More examples on the Monte Carlo algorithm can be found here. Application Monte Carlo methods are often used in +hysics to determine neutron traLectories or to simulate atomic clusters. In the $JB4s, Monte Carlo methods were used to study nuclear cascades. 7hen a particle collides with a nucleus, a nuclear cascade is produced. 'he particleMs path after the collision determines whether or not a new particle, a pion, is created. Monte Carlo methods keep track of the colliding particleMs path after the collision and determine the probability of the production of pions. Monte Carlo algorithms are useful here because it is diNcult and time-consuming to simulate the collisions. By running the algorithm enough times, the results are usually statistically signi,cant, and we are able to successfully calculate the probability of the production of nuclear cascades. Chemistry is another ,eld where Monte Carlo methods come in handy. Monte Carlo methods are often used to e"aluate integrals. 7hile integrals can be e"aluated by many other mathematical methods, it is, sometimes too complex for other methods to e"aluate an integral as its dimensionality grows. !ot being able to e"aluate an integral with growing dimensions is known as the Ocurse of dimensionalityM. <owe"er, Monte Carlo methods outdo this curse because they work increasingly better with more complex integrals that contain high dimensions. P"aluation of multidimensional integrals is useful in calculating the "olume of molecules. 8ince molecules do not ha"e de,ned boundaries, "olume calculations in"ol"e multidimensional integration. It is diNcult to calculate the "olume without the use of Monte Carlo methods because of the complexity in"ol"ed and also because de,nite boundaries of the molecules are unde,ned. >olumes of molecules are often de,ned by a range of electron densities, so Monte Carlo methods use random number generation and a probability distribution function to e"aluate the "olumes of molecules. )ne of the most popular applications of the Monte Carlo algorithms is in the ,eld of ,nance. Monte Carlo methods aid the analysis of ,nancial instruments, portfolios, and assets. 7hen a ,nancial instrument or asset is being analy5ed to label its "alue, many complex e/uations, the "alues of which may be uncertain, are used to reach a ,nal answer. 8ince Monte Carlo methods work well with highly complex e/uations, their use becomes "ital in the calculation of uncertain "alues, which then in turn help analy5e the ,nal "alue of the instrument or asset in /uestion. A speci,c OMonte Carlo )ption ModelM is used to e"aluate future prices of options. Many uncertain "alues a*ect the ,nal "alue of these ,nancial optionsA Monte Carlo methods use random number generation to lay the "arious price paths and then calculate a ,nal option "alue. 2* Code is in Monte Carlo.9ip *2