P1-W-V2 Irrflug BargardiRoszkowskiWasmer Corr1

download P1-W-V2 Irrflug BargardiRoszkowskiWasmer Corr1

of 15

Transcript of P1-W-V2 Irrflug BargardiRoszkowskiWasmer Corr1

  • 8/8/2019 P1-W-V2 Irrflug BargardiRoszkowskiWasmer Corr1

    1/15

    Practical Laboratory Course I Materials Science AS09

    Random Walk & Random NumberGenerators

    Students:Fabio Bargardi [email protected]

    Martin [email protected] [email protected]

    26.11.2009First correction: 07.12.2009

    Assistant: Ding Yi [email protected]

    Bargardi, Roszkowski, Wasmer | 07.12.2009 1/15

    mailto:[email protected]:[email protected]:[email protected]:[email protected]:[email protected]:[email protected]:[email protected]:[email protected]:[email protected]:[email protected]:[email protected]
  • 8/8/2019 P1-W-V2 Irrflug BargardiRoszkowskiWasmer Corr1

    2/15

    Abstract

    Statistical processes like random walks have many applications in materials simulation and modeling.In this experiment we use two given Matlab programs of random walks. We derive analytically themean squared distance and the probability of the distance for a general random walk. This obtainedterminology is verified by experiments with the programs by visual and numerical interpretation. Wefurther investigate the reliability of different random number generators for and with random walksimulations. The Matlab randint() function and varying number sets for a linear congruentialgenerator are used. We propose a quantitative measure of scale-invariance of random walks.

    Bargardi, Roszkowski, Wasmer | 07.12.2009 2/15

  • 8/8/2019 P1-W-V2 Irrflug BargardiRoszkowskiWasmer Corr1

    3/15

    1. Introduction

    1. RandomWalk

    A random walk is defined as the movement of a point on a lattice where each successive step has the

    same discrete length, but a random direction. As the movement of a polymer in solution, the alignmentof dipoles in a ferromagnet or movement of defects in a material are influenced by chance (e.g. thermalexcitation), these and other physical systems can be simulated by stochastic processes. This is due tothe fact that the mean values of a thusly simulated variable are statistically reproducible. Random walktheory can also be analytically applied to physical theories, like Fick's laws for diffusion.In the first part of this experiment we examined outputs of a simple 2D random walk simulation inMatlab. For the sake of clearness, it is necessary to define the terminology.

    Let us consider a random walk on a lattice with ddimensions and lattice step length a.We may ask two questions:

    1. What is the mean distance from starting point to ending point after a certain amount of time or

    number of steps? What is the mean squared distance?2. What is the probability of finding the end point at a given mean distance (mean squared

    distance)?

    The unit vector on the lattice is defined as

    (1)

    with ri=a12a22 ford = 2. The mean of r , defined as r , always equates to zero (ford = 1,a = 1, NS= 1: 1 1 ). The end-to-end vector of one random walk (NW = 1) withNSsteps is

    (2)

    What is the mean end-to-end distance? It is the average ofNW equally likely random walks withNSsteps, defined as r . ForNS = 1, this would lead to the mean of2 times dvectors, which areequally probable (2 vectors ford = 1, 4 vectors ford = 2 and so on). ForNS vectors, there are

    NW=d2NS equally likely walks (see page 5). To get to r , we use the following trick. We

    define

    (3)

    as the square root dot product of r with itself, so that ri=a12a2

    2=riri ford = 2. It should be

    noted that r2=rr . Now we can derive the mean squared distance.

    Bargardi, Roszkowski, Wasmer | 07.12.2009 3/15

    r= r=r2

    ri=a e ,e=1

    r=i=1

    NS

    ri

  • 8/8/2019 P1-W-V2 Irrflug BargardiRoszkowskiWasmer Corr1

    4/15

    (4)

    According to equation (1), r=0 . In equation (4), that leads to rirj =0 , vectors with i jcancel each other out (see equation (6.2)). We derive the mean squared distance to be:

    (5)

    To answer the initial question 1., the mean distance of a random walk withNS steps is:

    (6)

    An important fact about random walks is their scale invariance. Let us imagine that we combine four ofour initial steps into one new step continuously. We get a new random walk:

    (7)

    Equation (9) allows us to derive the new lattice step length.

    (7.1)

    This can be written as a general scaling transformation:

    (7.2)

    The exponent is defined as the scaling exponent. For scale invariance to hold, the new walk musthave the same mean end-to-end distance and thus,NS and a have to be transformed at the same time.The resulting expression is a scale law.

    (7.3)

    A physical interpretation of scale invariance is, that above a definedNS , the phenomenon has no innerlength scale. The statistical behavior is indistinguishable on different scales without regard tomicroscopic properties. The scaling exponent has to be adjusted for different models. A self-avoidingwalk, where polymer elements cannot occupy the same space, a different dimensionality or lattice

    geometry, require a slightly different scaling exponent than a regular random walk with =12

    .

    Bargardi, Roszkowski, Wasmer | 07.12.2009 4/15

    r2=rr=

    i , j=1

    NS

    rirj

    r2=

    i=1

    NS

    riri=i=1

    NS

    ri2=

    i=1

    NS

    a2=NSa

    2

    r2=r=NSa

    r= r '=NS'a '2

    r2=NSa

    2= r '

    2=NS'a '

    2=

    N

    4a '

    2

    a '2=4 a

    2

    a '2=4 a

    2

    NSNS'=NS

    n; aa '=n

    a

    r=a N

  • 8/8/2019 P1-W-V2 Irrflug BargardiRoszkowskiWasmer Corr1

    5/15

    For question 2., let us consider a random walk with d = 1, a = 1,NS = 3. At each step, the walker maychoose between one step forward or one step backward. Figure 1 shows the according schematic.

    It is easy to see, that forNS = 3 in this model, there are NW=2NS=8 possible random walks. This

    property is shared with Pascal's triangle. The possible number of ways to reach a distance k is given bythe binomial coefficient.

    (8)

    , where NS , k, NSk . For the actual distance, khas to be substituted by r.NS= 3, k= 0 isassigned to r= -3. kcorresponds to a random walk with one step forward or remaining on position. Theprobability to reach a certain distance kcan be calculated with

    (9)

    Bargardi, Roszkowski, Wasmer | 07.12.2009 5/15

    +3+2

    +1 +10 0

    -1 -1-2

    -3

    NS

    0 1 2 3

    3

    2

    1

    0

    k

    Figure 1: Tree diagram Question 2.

    NSk =NS!

    k ! NSk!

    pk=NS!

    2NSk ! NSk!

    -3 -1 1 3

    0

    0.5

    1

    1.5

    2

    2.5

    3

    3.5

    Table 1: Histogram for Question 2

    r

    r_

    x

    -4 -3 -2 -1 0 1 2 3 4

    0

    0.1

    0.2

    0.3

    0.4

    0.5

    0.6

    Table 2: Probability for Question 2

    r

    p(r)

  • 8/8/2019 P1-W-V2 Irrflug BargardiRoszkowskiWasmer Corr1

    6/15

    Table 1 shows the histogram ofr. Table 2 shows the binomial distribution according to equation (9).This means that the the probability of the output as a straight line increases toward walking around theorigin, in agreement with equation (6). It is a discrete distribution, but for largeNS, the central limittheorem [1] dictates thatp(r) converges with a normal distribution. Equation (11) can be transformed

    into a normal distribution equation ifkis substituted with1

    2rNS and by employing a variation

    of Stirling's approximation:

    (10)

    For the actual transformation, we refer to [2]. The resulting equation for the normal distribution:

    (11)

    2. Random Number Generators

    The feasibility of computer simulations with random walks is heavily dependent on random numbergenerators. A typical simple example is the linear congruential generator (LCG). It is defined by therecurrence relation:

    (12)

    , where a0,..., m1 is the factor, b0,..., m1 the increment, x0 the seed( x1,. .., xn0,..., m1 ), m2,3,4,... the module and n the number of state values. Thechoosing of the numbers is vital to get a low periodicity. The result is a string of pseudo-randomnumbers Xn1 .

    In the first part of the experiment statistical properties of a random walk were analysed. The secondpart dealt with the reliability of different random number generators.

    Bargardi, Roszkowski, Wasmer | 07.12.2009 6/15

    lnNS!NS12 lnNSNS

    12

    ln 2 12

    pr=2 NSa

    2

    dd

    2 expr

    2d

    NSa2

    Xn1=a Xnc mod m

  • 8/8/2019 P1-W-V2 Irrflug BargardiRoszkowskiWasmer Corr1

    7/15

    2. Materials & Methods

    1. Materials

    For all simulations a common desktop PC with a Matlab installation not older than R2007 was

    used.Two programs, a random walk in 2D and 1D were applied (see Appendix).The random number generator in use for the 2D walk was the Matlab function randint() [3].randint() uses the r = rand(n) generator algorithm which returns an n-by-n matrix containingpseudo-random values drawn from the standard uniform distribution on the open interval (0,1). For the1D version, a LCG (see Introduction) was applied with 2 different number sets. Set 1: c = 5, m =74364290, a = 37182142, (x0 =) ZZ = 5. (see appendix). Set 2: c =1013904223, m = 2^32, a = 1664525, (x0 =) ZZ = 5 (from Numerical Recipes,[4]). Functions of the program were commented out or activated according to the methods.

    2. Methods

    The 2D program was used first. Plots of one walk with steps ranging from 4 to 20,000 were plotted. Forseveral fixed numbers of steps, multiple walks were plotted. The lowest size of steps at which self-similarity occurs was searched visually in a qualitative manner.The 2D walk was then carried out with 10,000 walks and steps ranging from 4 to 100. Similar to theintroduction, histograms and probability distributions were plotted. The correlation with a normalizednormal distribution was examined visually, in a qualitative manner.

    The 1D program was used second. For 100 walks and steps ranging from 10 to 10,000 the standarddeviation output of Matlab was written down and plotted in Excel against the corresponding step

    number, together with a plot of equation (6). This was done with Set 1, Set 2 and some known worsesets for comparison in order to determine their reliability. The standard deviation was derived with theMatlab function norm(std()), where norm() returns the euclidian length of a vector.The 1D walk was then carried out with 1 walk, 20,000 and 100,000 steps with the correlation functionof the program (see page 15) activated in order to assess the sets' periods.

    Bargardi, Roszkowski, Wasmer | 07.12.2009 7/15

  • 8/8/2019 P1-W-V2 Irrflug BargardiRoszkowskiWasmer Corr1

    8/15

    3. Results

    1. 2D Random Walk with randint()

    Figure 2: Self-similarity of 2D random walks with increasing NS

    Please see the discussion.

    Bargardi, Roszkowski, Wasmer | 07.12.2009 8/15

    NS= 4 8 16

    32 64 128

    512

    256 256

    8,192 20,000 20,000

  • 8/8/2019 P1-W-V2 Irrflug BargardiRoszkowskiWasmer Corr1

    9/15

    Figure 3: Histograms and probability distributions of r for 10,000 2D random walks with increasing NS

    Figure 3 nicely affirms the reasoning thatp(r) converges with a normal distribution. Visually however,the deviation of the random walk histograms from the normal distribution is nearly identical for allNS.Toward largerNSand rthere appear more run away values but they don't change the deviation of themean value ofr. The displacement of the origin atNS= 331 and 40 in the histogram in comparison tothe normal distribution graph is just a glitch in the plotting program as one can compare the height ofthe corresponding rx with the positive values; they are equal.

    Bargardi, Roszkowski, Wasmer | 07.12.2009 9/15

    NS = 4 7 7

    7 10 20

    33 33 40

    70 95 95

  • 8/8/2019 P1-W-V2 Irrflug BargardiRoszkowskiWasmer Corr1

    10/15

    2. 1D Random Walk with linear congruential generator

    Table 3 shows the deviation of simulated random walks from equation (6). The plot of Set 1 is probablywrong, it should strictly increase (report note from assistant). For comparison, selected values of setswith high periodicity were plotted in Table 4. Set 4 repeats every other step and returned mostly zero.

    Bargardi, Roszkowski, Wasmer | 07.12.2009 10/15

  • 8/8/2019 P1-W-V2 Irrflug BargardiRoszkowskiWasmer Corr1

    11/15

    Figure 4: Periodicities of Set 1 ( NS = 20,000) and Set 2 ( NS = 100,000)

    The period of Set 1 could be determined to be around 8,000 steps, where the correlation function inFigure 4 returns 1. No periodicity for Set 2 was found forNS used in this experiment. (Note: Theprinted values of the x axis in the plot of Figure 2 are off by a factor of 10 a program error. The plotitself is correct.)

    Bargardi, Roszkowski, Wasmer | 07.12.2009 11/15

  • 8/8/2019 P1-W-V2 Irrflug BargardiRoszkowskiWasmer Corr1

    12/15

    4. Discussion

    1. 2D random walk with randint()

    For stochastic processes like a random walk with increasing NS, self-similarity is another term forscale-invariance. Without a physical measure, its occurrence above a certainNS is subject to opinion. InFigure 2,NS = 4 is contained in NS = 8, which is contained inNS = 16 and so on. Likewise, NS =20,000 can be approximated by NS = 8192, which can be approximated by NS = 512. Our groupdiscussed to different qualitative interpretations: forNS > 2^8, self-similarity occurs on the groundsthat above that step number, all walks have the same structural features (lines, squares and packs ofsquares). Alternatively: at NS > 10^4, all plots for a givenNS look very much alike.It was speculated that a quantitative measure of self-similarity in random walks could be made bycombining equations (6),(9),(11) with equations (7)-(7.3). A ratio of two probability distributions ormean end-to-end distances would be taken for two differingNS for a largeNW. Then, a scalingtransformation would be performed from oneNSto the other. The resulting scaling factors could be

    compared to the ratio above. A subjectively defined value of the ratio (subjective due to the absence ofphysical or chemical constraints) between the two might then lead to a quantitative measure of self-similarity.

    The conundrum that smallNS still make up for a nice approximation of the normal distribution is due tothe large value ofNW. If one would plotFigure 3 for an increasingly smaller number of walks, thediscrepancy with the normal distribution towards lowerNS would visually increase more than forlargerNS. The greater discrepancy with the green curve for largerrsimply occurs because there arefewer values of large rand so, they variance is greater. The function of the green curve is

    (13)

    Resolving for p rx returns:

    (14)

    Fora = 1, this looks equivalent to equation (11), the normal distribution of p r .

    Bargardi, Roszkowski, Wasmer | 07.12.2009 12/15

    lnp rx

    p 0=rx

    2

    N

    p rx =p 0exp [rx

    2

    N]

  • 8/8/2019 P1-W-V2 Irrflug BargardiRoszkowskiWasmer Corr1

    13/15

    2. 1D random walk with linear congruential generator

    The standard deviation ofris given by

    (15)

    The histogram of rx inFigure 3 visually confirms that statistically, most values of rx have aneuclidian length of N a , a = 1 (equation (6)). rx is just the distance from rx=0 to theinflexion point of the distribution curve.By consideringFigure 4, it is now evident that the plot of Set 1 in Table 3 starts to repeat at about 8,000steps ((with an offset of r of about 15) see assistant report note). That doesn't fit to the graph ofequation (6). Even atNS = 400, the output values are off by 50 %, and for bigger step numbers, thewalk with Set 1 looses all relation to equation (6), so Set 1 is a very bad set compared to Set 2.We tried to write a program which should plot the standard deviation of a 1D random walk with

    randint() as its random number generator. It would have been interesting to know if a periodicitycould be found. The standard deviation output was a constant, which we put down to our lack ofprogramming ability, so we had to abandon the idea.

    For further experiments with the random walk, we propose to address the open questions of ourdiscussion. The assumption of the quantitative measure of self-similarity should be investigatedanalytically and, if verified, in code. This could help in more complex simulations of models withphysical constraints [5]. The growth of discrepancy for lowerNS with the normal distribution of thehistogram of the 2D random walk with increasingly lowerNW should be examined.Though their importance in experiments is clear, our understanding of the random number generators

    remains poor. It is not necessary to derive their terminology as precisely as it has been done with therandom walk in this report. Nevertheless, more comparisons of the reliability not just between differentsets of LCGs, but also with randint()should be done in code.This is computationally costly. Sometimes we had to use lower step numbers as was recommended sothe computation could finish in time. So we would like to write more efficient algorithms for randomwalks [6].

    5. References

    [1] ttinger, Hans-Christian. Polymere I Skript, page 18,19.http://www.polyphys.mat.ethz.ch/education/polymer_physics/polyphys.pdfRetrieved November 26, 2009.[2] Fll, Helmut. Einfhrung in die Materialwissenschaft Hyperskript, Chapter 6.3.http://www.tf.uni-kiel.de/matwis/amat/mw1_ge/kap_6/backbone/r6_3_1.htmlRetrieved November 26, 2009.[3] Matlab Documentation. Functions, Randint.http://www.mathworks.com/access/helpdesk/help/toolbox/comm/ref/randint.htmlRetrieved November 26, 2009.

    [4] Wikipedia. Linear congruential generator.http://en.wikipedia.org/wiki/Linear_congruential_generator#LCGs_in_common_use.Retrieved November 26, 2009.[5] TU Clausthal, Institut fr Physikalische Chemie. GrundpraktikumComputersimulationen, page 3.http://www2.pc.tu-clausthal.de/edu/apr.shtmlRetrieved November 26, 2009.[6] Kai, Ingemar and Gaigalas Raimundas. Stochastic Simulation using Matlab.http://www.math.uu.se/research/telecom/software/stbasicproc.html#HighDimRWRetrieved November 26, 2009.

    Bargardi, Roszkowski, Wasmer | 07.12.2009 13/15

    r=

    Varr=r

    2

    ri

    2

    =r

    2

    0=

    NS=r

    http://rsta.royalsocietypublishing.org/content/364/1849/3285.abstracthttp://rsta.royalsocietypublishing.org/content/364/1849/3285.abstracthttp://www.tf.uni-kiel.de/matwis/amat/mw1_ge/kap_6/backbone/r6_3_1.htmlhttp://de.wikipedia.org/wiki/Formged%C3%A4chtnislegierunghttp://en.wikipedia.org/wiki/Linear_congruential_generator#LCGs_in_common_usehttp://en.wikipedia.org/wiki/Linear_congruential_generator#LCGs_in_common_usehttp://www2.pc.tu-clausthal.de/edu/apr.shtmlhttp://www.math.uu.se/research/telecom/software/stbasicproc.html#HighDimRWhttp://rsta.royalsocietypublishing.org/content/364/1849/3285.abstracthttp://www.tf.uni-kiel.de/matwis/amat/mw1_ge/kap_6/backbone/r6_3_1.htmlhttp://de.wikipedia.org/wiki/Formged%C3%A4chtnislegierunghttp://en.wikipedia.org/wiki/Linear_congruential_generator#LCGs_in_common_usehttp://www2.pc.tu-clausthal.de/edu/apr.shtmlhttp://www.math.uu.se/research/telecom/software/stbasicproc.html#HighDimRW
  • 8/8/2019 P1-W-V2 Irrflug BargardiRoszkowskiWasmer Corr1

    14/15

    6. Appendix

    1. Matlab program for 2D random walk

    % Praktikum I/II: Irrflug in 2D% by MH 2009-08-20

    N_Irrfluege = 1000; % Anzahl generierter IrrfluegeN_Schritte = 40; % Anzahl Schritte pro Irrflugx_Liste = []; % Liste mit Endpositionen aller Irrfluege

    for i = 1:N_Irrfluege % Schleife ueber verschiedene Irrfluegex = [0 0]; % Anfangsposition des i-ten IrrflugesIrrflug=[x]; % Liste "Irrflug" mit allen Positionenfor j = 1:N_Schritte % Schleife ueber Schritte

    dx = (2*randint()-1); % Zufallsschritt: dx = +1 oder dx = -1Koord = randint()+1; % Zufallsrichtung fuer Schritt: 1 oder 2x(Koord) = x(Koord) + dx; % entsprechende Koordinate aufdatierenIrrflug = [Irrflug; x]; % Liste "Irrflug" aufdatieren

    end % Ende der Schleife uber Schrittex_Liste = [x_Liste;x]; % Enposition in Liste uebertragen%figure(1); plot(Irrflug(:,1),Irrflug(:,2)); axis equal; %Irrflug plotten

    end

    x_Mittelwert = mean(x_Liste)x_Standardabweichung = norm(std(x_Liste))x_Liste

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Details zur graphischen Darstellung der x(1)-Werte%xmax = 2*floor(sqrt(N_Schritte));% max. x(1)-Werte fuer Plotx = -xmax:1:xmax; % Bereich der x(1)-Werte fuer Plotfigure(2); subplot(2,1,1); hist(x_Liste(:,1),x);

    % Histogramm der x(1)-Wertexlabel('r_x'); ylabel('Histogramm von r_x'); % Beschriftung der Achsenx_hist = histc(x_Liste(:,1),x); % Histogramm als Liste erstellenfigure(2); subplot(2,1,2);plot(x,log(x_hist/x_hist(xmax+1)),'or'); hold on;

    % Exponent der Verteilung plottenplot(x,-(x.*x)/N_Schritte,'-g'); hold on;

    % theoret. Verhalten fuer Exponentenxlabel('r_x'); ylabel('log( p(r_x)/p(0) )'); % Beschriftung der Achsen

    2. Matlab program for 1D random walk

    % Praktikum I/II: Irrflug in 1D% by MH 2009-08-20

    function main

    % Parameterspezifikation fuer Zufallszahlgenerator LCG% nicht aendernc = 5; % c = 1013904223;5;m = 2*5*7*11*13*17*19*23; % m = 2^32;2*5*7*11*13*17*19*23;a = floor(m/2)-3; % a = 1664525;floor(m/2)-3;

    Bargardi, Roszkowski, Wasmer | 07.12.2009 14/15

  • 8/8/2019 P1-W-V2 Irrflug BargardiRoszkowskiWasmer Corr1

    15/15

    ZZ = 5; % ZZ = 5;5;

    % Parameter fuer IrrflugN_Irrfluege = 1; % Anzahl generierter IrrfluegeN_Schritte = 1000000; % Anzahl Schritte pro Irrflugx_Liste = []; % Liste zur Datensammlung bereitstellenalle_dx = []; % Liste aller je bestimmten Inkremente dx

    for i = 1:N_Irrfluege % Schleife ueber verschiedene Irrfluege

    x = 0; % Anfangsposition des i-ten Irrflugesfor j = 1:N_Schritte % Schleife ueber Schritte

    ZZ = LCG(ZZ,c,m,a); % Zufallszahl zwischen 0 und mdx = 2*round(ZZ/m)-1; % Zufallsschritt: dx = +1 oder dx = -1x = x + dx; % Koordinate aufdatieren

    alle_dx = [alle_dx,dx];

    endx_Liste = [x_Liste;x]; % Enposition in Liste uebertragen

    end

    x_Mittelwert = mean(x_Liste)x_Standardabweichung = std(x_Liste)

    %figure(1); plot(x_Liste)

    figure(2); plot(Korrelation(alle_dx))xlabel('\Delta n'); ylabel('Korrelationsfunktion C(\Delta n)');

    end

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Zufallszahlgenerator%function x = LCG(x,c,m,a) % linear congruential generator (LCG)% one of the oldest and best-known pseudorandom number generator algorithms% 0 < m the "modulus"% 0 < a < m the "multiplier"% 0