Stokastisk simulering och Monte Carlo-metoder Beräkningsvetenskap 2, 2009.
Vad det här blocket handlar om: En klass av dåliga numeriska metoder som fortfarande fungerar (dåligt) när alla bra metoder inte fungerar alls.
Mina mål med modulen Att ni ska förstå den principiella skillnaden mellan en stokastisk och en deterministisk modell. Att ni ska förstå skillnaden på en deterministisk och stokastisk metod. Att ni ska kunna resonera kring de generella principerna för Monte Carlo. Att ni ska kunna resonera och redogöra för några situationer där Monte Carlo lämpar sig att använda. Att ni ska förstå hur Monte Carlo kan användas för att beräkna en integral (i höga dimensioner).
Ett första exempel: storskalig stokastisk simulering med URDME (Cullhed, Engblom, Hellander, 2009) COMSOL MP, MATLAB, ANSI C... (demo av URDME)
Frågeställning: Hur lång ifrån origo befinner sig partikel i genomsnitt vid tiden t=1s? Och hur ser fördelningen ut? Q
Not all paths are equally probable though, and they are governed b probability distribution. Here, the random variables are the position of the particle at tim for each time t t 0, the position is a stochastic variable, and we can w process {B t : t 0}. While a mathematical discussion of Brownian is way out of the scope of this course, we state below the propert characterize the process B t as it will tell us how to simulate it in a numerical algorithm. Exempel: Fri diffusion 1. B 0 = 0 (almost surely). 2. B t is continuous (almost surely). 3. B t has independent, normally distributed increments, B t B s N (0, t s), 0 s t. Property 1 above simply means that the process starts in origo. If w to simulate it starting in another point, say x 0, we can simply simu 11 M
Suppose that a random number generator is available that generates random numbers according to the standard normal distribution N (0, 1). Using this generator, explain how can you generate random numbers from the distribution N (x t, t). Exempel: Fri diffusion We can now state an algorithm to generate a realization of the process. It is given in pseudocode in Algorithm 1. Algorithm 1 Free diffusion of a particle Initialize: choose a time step t, an initial position x 0 and a final time T f. Set t = t 0 and x t0 = x 0. while t < T f do Generate a random number R N (0, 1). x t+ t = x t + tr. t = t + t. end while Figure 1 shows one trajectory of a freely diffusing particle starting in origo. Computer exercise 5 Stokastisk modell för hur partikeln rör sig. Ovan, en algoritm, baserad på normalfördelade slumptal, för att simulera partikels bana. Write a Matlab function that uses a while or for-loop in order to simulate the motion of a particle undergoing 2D Brownian motion according to Algorithm 1. The input to the function should be the final f time T f, the starting point x 0 in the plane and the time step t. A n dim Brownian motion is composed of n independent 1D motions. Normally distributed random numbers can be generated in Matlab using the function randn.
En trajektorie... 0.6 0.4 0.2 0!0.2!0.4!0.6!0.8!1.8!1.6!1.4!1.2!1!0.8!0.6!0.4!0.2 0 0.2 Stokastisk simulering...
Monte Carlo: 4500 4000 3500 3000 2500 2000 1500 1000 500 0 0 1 2 3 4 5 6 7 8 d = 1.13
Varför stokastiska modeller? Vi kan inte hitta en (tillräckligt enkel) deterministisk modell som duger för att beskriva vad som sker. Mätfel: vi har inte utrustning nog bra att mäta exakt, modellera mätfelet med slumpen. Vi vill till exempel veta hur våra mätfel sprids i modellen.
Varför fungerar Monte Carlo? Vi generar en (oberoende) datamängd (sample) och gör en statistisk analys på denna data. Så länge vi gör statistiken korrekt så kommer vi att få ett resultat som approximerar det vi är ute efter! Vanligast: Vi är ute efter ett medelvärde. En unbiased estimator av medelvärdet är: µ = 1 N N i=1 x i
Every time we estimate some unknown quantity by generating a sample (the data set composed of the realizations we generate) and computing the sample average as we will do when we evaluate integrals with Monte Carlo, we rely on the law of large numbers. Even if you have not seen the theorem before, Stora you have talens probably lag used it (LLN) many times, especially if you have taken some experimental courses. It states that if we repeatedly sample a stochastic variable, the sample mean converges to the true expected value. Loosely speaking, this result says that the Monte Carlo method is consistent. Theorem 1 (weak) Law of large numbers (LLN) Let X 1, X 2,..., X n be independent random variables with mean µ and set X n = 1 n n i=1 X i. Then P ( X n δ < µ < X n + δ) 1 as n for any δ > 0. Computer exercise 1 Implement Det vill säga: a small Matlab Det aritmetiska routine that medelvärdet simulates throwskonvergerar with a dice. For each successive throw, compute the arithmetic mean based on all throws so far mot anddet plotsanna it as a function medelvärdet. of N, the number of throws. To which value does the mean appear to converge? (Vi såg detta i praktiken på labben, kommer ni ihåg?) The LLN provides us with consistency, but does not give any information regarding the error in the approximation or the rate of convergence to the true expected value. This is taken care of by the central limit theorem.
Oberoende variabler? Genererar vi oberoende realisationer? Ja, i den mån vår pseudoslumptalsgenerator ger oberoende slumtal. Bra generatorer går igenom många tuffa test på oberoende, men självklart är de inte helt oberoende eftersom de inte är riktiga slumptal. Slutsats: Så länge vi gör statistiken rätt är Monte Carlo proceduren konsistent. (Jämför med ODE lösare.)
Varför fungerar MC dåligt? Monte Carlo-metoder konvergerar väldigt långsamt! Det har också att göra med statistiken... För ett givet antal punkter n är det aritmetiska medelvärdet själv en stokastisk variabel... För varje ny datamängd blir värdet lite olika. (Det såg vi också på labben... )
Tärningskast >> for i=1:10000 mu(i)=mean(floor(1+6*rand(1,10000))); end >> hist(mu,100) 350 300 250 200 150 100 50 0 3.42 3.44 3.46 3.48 3.5 3.52 3.54 3.56 3.58 3.6
each successive throw, compute the arithmetic mean based on all throws so far and plot it as a function of N, the number of throws. To which value does the mean appear to converge? The LLN provides us with consistency, but does not give any information Central limit theorem (CLT) regarding the error in the approximation the rate of convergence to the true expected value. This is taken care of by the central limit theorem. Theorem 2 Central limit theorem (CLS) Let X 1, X 2,..., X n be a sequence of independent identically distributed random variables with finite mean µ and variance σ 2 > 0 and let X n = 1 n n i=1 X i. Then n( X n µ) D N(0, σ 2 ) as n The CLS states that the distribution of the error in the sample mean µ tends Fördelningen to a normal distribution för det and aritmetiska that the variance medelvärdet decay as Ngår 1 if mot N is en the sample size and this means that the error decays as N 1/2. Thus, Monte normalfördelning med varians Carlo methods are converging very slowly, and are often computationally intensive. However, the convergence rate is insensitive to the dimensionality 4 σ 2 n
Feluppskattning CLT ger oss ett sätt att uppskatta felet i en Monte Carlo beräkning med ett konfidensintervall (statistik) Unbiased estimator för variansen: s 2 = 1 n 1 n (x i µ) 2 i=1 µ [ µ 1.96 ɛ, µ + 1.96 ɛ ] ɛ s/ n
Felet... Är (assymptotiskt) av O( 1 n ) För att halvera felet behövs ungefär fyra gånger så många realisationer! För många tillämpningar är algoritmen för att generera realisationer (jämför partikeln) mycket beräkningsintensiv!
Varför används då Monte Carlo? Jo, ibland har man inget val! Det är antingen Monte Carlo eller ingenting alls och då tar man Monte Carlo! Exempel från labben: Högdimensionell integral. Expontentiell tillväxt i beräkningspunkter med en kvadraturregel. 100-D integral, 7 punkter i varje dimension -> 3.2345e+84 beräkningspunkter. Fler än antalet atomer i universum??
Högdimensionella problem Samma problematik, the curse of dimensionality inspirerar forskning vid TDB: Systembiologi - varje molekylslag tillför en dimension: 20 olika sorters proteiner -> 20D. (Per Lötstedt) Finansmatematik: Varje aktie i en aktieportfölj blir en till dimension. (Lina Von Sydow, Elisabeth Larsson) Kvantkemi: Varje atom i molekylen ger (minst) en till dimension. (Sverker Holmgren)