Exercises Lecture III: Random numbers with non uniform distributions; simulations of simple random processes

Similar documents
Exercise n. II (Pseudo)random numbers with uniform distribution

Lecture 2: Generating random numbers #2

Exercises Lecture IX Ising Model

Lecture 2: Introduction to Numerical Simulation

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

PHYSICS 115/242 Homework 5, Solutions X = and the mean and variance of X are N times the mean and variance of 12/N y, so

AMath 483/583 Lecture 8

Goals for This Lecture:

An Introduction to Fortran

GENERATION OF RANDOM NUMBERS BY COMPUTER

Numerical Modelling in Fortran: day 2. Paul Tackley, 2017

Box-Cox Transformation

A Brief Introduction to Fortran of 15

Monte Carlo Integration and Random Numbers

Computational Methods. Randomness and Monte Carlo Methods

Goals for This Lecture:

Chapter 6. A Brief Introduction to Fortran David A. Padua

Physics 736. Experimental Methods in Nuclear-, Particle-, and Astrophysics. - Statistical Methods -

Problem Set 3 CMSC 426 Due: Thursday, April 3

Assignment no. 5 Implementing the Parallel Quick-sort algorithm in parallel distributed memory environment

Computer Data Analysis and Plotting

Lab 4: Automatical thresholding and simple OCR

2 3. Syllabus Time Event 9:00{10:00 morning lecture 10:00{10:30 morning break 10:30{12:30 morning practical session 12:30{1:30 lunch break 1:30{2:00 a

Box-Cox Transformation for Simple Linear Regression

6.1 Expression Evaluation. 1 of 21

Review Functions Subroutines Flow Control Summary

Recursive Estimation

CS302 Topics: * More on Simulation and Priority Queues * Random Number Generation

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

CS 112: Computer System Modeling Fundamentals. Prof. Jenn Wortman Vaughan April 21, 2011 Lecture 8

Compiling, running the code, generating output. patrick]$ ifort -o prog1 prog1.f patrick]$ prog1

Fortran. (FORmula TRANslator) History

write (unit=*,fmt=*) i =, i! will print: i = 3

Math 213 Exam 2. Each question is followed by a space to write your answer. Please write your answer neatly in the space provided.

l1 ls: A Matlab Solver for Large-Scale l 1 -Regularized Least Squares Problems

Example 1: Given below is the graph of the quadratic function f. Use the function and its graph to find the following: Outputs

LECTURE 0: Introduction and Background

Computational Methods of Scientific Programming. Lecturers Thomas A Herring Chris Hill

Reflection Seismology (SCPY 482) An Introduction to Fortran 90 Programming

WEEK 4 REVIEW. Graphing Systems of Linear Inequalities (3.1)

Numerical Simulation of Space Plasmas (I) [AP-4036] Lecture 4 by Ling-Hsiao Lyu April 2017

14.5 Directional Derivatives and the Gradient Vector

Will Monroe July 21, with materials by Mehran Sahami and Chris Piech. Joint Distributions

Administrivia. Next Monday is Thanksgiving holiday. Tuesday and Wednesday the lab will be open for make-up labs. Lecture as usual on Thursday.

Old Questions Name: a. if b. open c. output d. write e. do f. exit

AN INTRODUCTION TO FORTRAN 90 LECTURE 2. Consider the following system of linear equations: a x + a x + a x = b

Getting Started with MATLAB

BIOL Gradation of a histogram (a) into the normal curve (b)

College Algebra. Fifth Edition. James Stewart Lothar Redlin Saleem Watson

Control Constructs: Loops - while loops - iterative loops (counting loops)

Lecture 8: Jointly distributed random variables

Physics 736. Experimental Methods in Nuclear-, Particle-, and Astrophysics. - Statistics and Error Analysis -

Section 4.2 selected answers Math 131 Multivariate Calculus D Joyce, Spring 2014

Generative and discriminative classification techniques

The first few questions on this worksheet will deal with measures of central tendency. These data types tell us where the center of the data set lies.

Partial Derivatives (Online)

Introduction to Matlab

Using Excel for Graphical Analysis of Data

Scientific Computing: An Introductory Survey

Practical Exercise 1 Question 1: The Hello World Program Write a Fortran 95 program to write out Hello World on the screen.

ME1107 Computing Y Yan.

Lesson 19: The Graph of a Linear Equation in Two Variables Is a Line

Generative and discriminative classification techniques

Computers in Engineering COMP 208 Repetition and Storage Michael A. Hawker. Repetition. A Table of Values 9/20/2007

IQR = number. summary: largest. = 2. Upper half: Q3 =

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

SD 372 Pattern Recognition

PROBLEM SOLVING WITH FORTRAN 90

Using Random numbers in modelling and simulation

Subroutines and Functions

(Refer Slide Time 01:41 min)

Computers in Engineering. Subroutines Michael A. Hawker

Lesson 19: The Graph of a Linear Equation in Two Variables is a Line

Intrinsic Functions Outline

Testing Random- Number Generators

Exponential and Logarithmic Functions. College Algebra

A. Incorrect! This would be the negative of the range. B. Correct! The range is the maximum data value minus the minimum data value.

(c) 0 (d) (a) 27 (b) (e) x 2 3x2

MAPLE CARD. All commands and constants must be typed in the case (UPPER or lower) that is indicated. Syntax Rules

Physics 736. Experimental Methods in Nuclear-, Particle-, and Astrophysics. Lecture 14

Introduction to Internet of Things Prof. Sudip Misra Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

1 Introduction to MATLAB

Approximate First and Second Derivatives

3-1 Writing Linear Equations

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Math 233. Lagrange Multipliers Basics

ACE Tutorial. October, 2013 Byoung K. Choi and Donghun Kang

Fathom Dynamic Data TM Version 2 Specifications

Lecture 14. December 19, Department of Biostatistics Johns Hopkins Bloomberg School of Public Health Johns Hopkins University.

A quick introduction to STATA:

Numerical Modelling in Fortran: day 3. Paul Tackley, 2017

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

Random Numbers and Monte Carlo Methods

An introduction to Fortran. Daniel Price School of Physics and Astronomy Monash University Melbourne, Australia

Part I, Chapters 4 & 5. Data Tables and Data Analysis Statistics and Figures

R Programming Basics - Useful Builtin Functions for Statistics

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Scientific Programming in C VI. Common errors

SSJ User s Guide. Package randvar Generating Non-Uniform Random Numbers

Introduction to Fortran95 Programming Part II. By Deniz Savas, CiCS, Shef. Univ., 2018

Transcription:

Exercises Lecture III: Random numbers with non uniform distributions; simulations of simple random processes 1. Random numbers with non uniform distributions: Inverse Transformation Method (a) With the Inverse Transformation Method we can generate random numbers according to the exponential distribution f(z) = λe λz, starting from random numbers with uniform distribution: if x is the random variable with uniform distribution in [0,1], then z = ln(x) is distributed according to e z. Write a code implementing the algorithm. An example is given in expdev.f90. (b) Check doing a histogram that the random variate z generated with that algorithm is actually exponentially distributed. (What is convenient to plot in order to check this behavior? Hint: with gnuplot you can print the log of your data (e.g., suppose you saved the values of z in column 1 and its frequency in column 2, plot with u 1:(log($2)) or u 1:(log10($2)) )). (c) With gnuplot you can also do the fit of the histogram with an exponential function using the least-square method, with λ as fitting parameter. Check whether you get the expected value of λ. (It is convenient to make a semilog plot as suggested above and then make a least-square linear fit; the slope is λ) Remember that with the method of the least-square fit we get for a linear regression: y = ax + b: a = xy yy ( x) 2 ; b = y ax where ( x) 2 = x 2 x 2 (other definitions are trivial... ). 1

2. Random numbers with non uniform distributions: comparison between different algorithms Suppose you want to generate a random variate x in (-1,1) with distribution p(x) = 1 π (1 x2 ) 1/2. Consider both methods suggested below, do the histograms and check that both methods give correct results. (a) From the Inverse Transformation Method: generate a random number U with uniform distribution in [0,1] and consider x = sin π(2u 1). (b) Generate two random numbers U and V with uniform distribution in [0,1]. Disregard them if U 2 + V 2 > 1. Otherwise consider x = U 2 V 2 U 2 + V 2 Note 1: the last method has the advantage of using only elementary operations. Note 2: since x is also negative, pay attention to the algorithm used to make the histogram; you should notice the difference between the intrinsic functions int and nint; see also floor. From Chapman s book: AINT(A,KIND): Real elemental function - Returns A truncated to a whole number. AINT(A) is the largest integer which is smaller than A, with the sign of A. For example, AINT(3.7) is 3.0, and AINT(-3.7) is -3.0. - Argument A is Real; optional argument KIND is Integer ANINT(A,KIND): Real elemental function - Returns the nearest whole number to A. For example, ANINT(3.7) is 4.0, and AINT(-3.7) is -4.0. - Argument A is Real; optional argument KIND is Integer FLOOR(A,KIND): Integer elemental function - Returns the largest integer < or = A. For example, FLOOR(3.7) is 3, and FLOOR(-3.7) is -4. - Argument A is Real of any kind; optional argument KIND is Integer - Argument KIND is only available in Fortran 95 NINT(A[,KIND]) - Integer elemental function - Returns the nearest integer to the real value A. - A is Real 2

3. Random numbers with gaussian distribution: Box-Muller algorithm Consider the Box-Muller algorithm to generate a random number gaussian distribution (see for instance boxmuller.f90; the gasdev subroutine used inside is similar to what you can find in Numerical Recipes : it gives a gaussian distribution with σ = 1 and average µ = 0). Do a histogram of the data generated, calculate numerically from the sequence the average value and the variance, check with the expected results. 4. Simulation of radioactive decay (a) Write a program for a numerical simulation of the radioactive decay, with a decay parameter λ in input. (See for instance decay.f90). (b) Use the code with reasonable values of the parameters (e.g., N(0) about 1000) and save N(t) in a data file. Check whether N(t) = N(0)e λt as expected. (Hint: As for the exercise 1, you could make use of a least-square fit by considering ln N(t) vs. t, i.e. the relationship in a semilog form in order to manage a linear fit.) (c) Change N(0) (100 or less; 10000 or more). What do you see? Notice that in decay.f90 the upper bound of the inner loop (nleft) is changed within the execution of the loop; but in the execution the loop goes on up to the nleft set at the beginning of the loop; this ensures that the implementation of the algorithm is correct. See the programs checkloop.f90 and decay checkloop.f90 in the same directory. 5. Random deviates with other distributions (Optional) You can try t random.f90 which uses the module random.f90 to generate random deviates with other distributions. Remember to compile first the module: g95 (or gfortran) random.f90 t random.f90 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC expdev.f90 program test_expdev implicit none real :: lambda,delta,x integer :: i,n,nbin,ibin, sizer integer, dimension(:), allocatable :: histo, seed print*, " Generates random numbers x distributed as exp(-lambda*x)" call random_seed(sizer) allocate(seed(sizer)) print *, Here the seed has,sizer, components; insert them (or print "/") >\ read(*,*)seed call random_seed(put=seed) 3

print *," length of the sequence >" read *, n print *," exponential decay factor (lambda)>" read *, lambda print *," Collecting numbers generated up to 2/lambda (disregard the others)" print *," and normalizing the distribution in [0,+infinity[ " print *," Insert number of bins in the histogram>" read *, nbin delta = 2./lambda/nbin allocate (histo(nbin)) histo = 0 do i = 1,n call expdev(x) ibin = int (x/lambda/delta) + 1 if (ibin <= nbin)histo(ibin) = histo(ibin) + 1 open (unit=7,file="expdev.dat",status="replace",action="write") do ibin= 1,nbin write(unit=7,fmt=*)(ibin-0.5)*delta,histo(ibin)/float(n)/delta contains subroutine expdev(x) REAL, intent (out) :: x REAL :: r do call random_number(r) if(r > 0) exit x = -log(r) END subroutine expdev end program test_expdev CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC boxmuller.90 uses the Box-Muller algorithm to generate a random variate with a gaussian distribution (sigma = 1) program boxmuller implicit none real :: rnd,delta real, dimension(:), allocatable :: histog integer :: npts,i,ibin,maxbin,m 4

print*, input npts, maxbin > read*, npts,maxbin allocate(histog(-maxbin/2:maxbin/2)) histog = 0 delta = 10./maxbin do i = 1, npts call gasdev(rnd) ibin = nint(rnd/delta) if (abs(ibin) < maxbin/2) histog(ibin) = histog(ibin) + 1 open(1,file= gasdev.dat,status= replace ) do ibin = -maxbin/2, maxbin/2 write(1,*)ibin*delta, histog(ibin)/real(npts)/delta close(1) deallocate(histog) stop contains SUBROUTINE gasdev(rnd) IMPLICIT NONE REAL, INTENT(OUT) :: rnd REAL :: r2,x,y REAL, SAVE :: g LOGICAL, SAVE :: gaus_stored=.false. if (gaus_stored) then rnd=g gaus_stored=.false. else do call random_number(x) call random_number(y) x=2.*x-1. y=2.*y-1. r2=x**2+y**2 if (r2 > 0..and. r2 < 1.) exit r2=sqrt(-2.*log(r2)/r2) rnd=x*r2 g=y*r2 gaus_stored=.true. end if END SUBROUTINE gasdev end program boxmuller 5

CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC decay.f90 Simulation of radioactive decay PROGRAM decay IMPLICIT none REAL, PARAMETER :: lambda=0.2 REAL :: r INTEGER :: i, t, nleft, start, sizer integer, dimension(:), allocatable :: seed call random_seed(sizer) allocate(seed(sizer)) print *, Here the seed has,sizer, components; insert them (or print "/") > read(*,*)seed call random_seed(put=seed) initial values print *,"initial number of nuclei >" read *, start t = 1 initialize time nleft = start at the beginning N(t=0)=start N(t) nuclei left at time t, that have a given probability lambda of decay in the time interval t:t+dt OPEN(unit=7, FILE="decay.dat", status="replace",action="write") WRITE (unit=7,fmt=*) "# t, N(t)" WRITE (unit=7,fmt=*) "0 DO ", nleft REAL(nleft)/start time loop DO i = 1, nleft loop on the nuclei left call random_number(r) IF (r <= lambda) THEN nleft = nleft - 1 ENDIF END DO WRITE (unit=7,fmt=*) t, nleft or REAL(nleft)/start if (nleft == 0) exit t = t + 1 END DO close(7) stop END program decay update the number of nuclei left 6