Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| teaching:exos:simulations_random_walks_codes [2013/11/13 16:35] – créée villersd | teaching:exos:simulations_random_walks_codes [2018/11/05 12:09] (Version actuelle) – [Avec analyse de la distribution :] villersd | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| ====== Simulations numériques de marches aléatoires : programmes en Python ====== | ====== Simulations numériques de marches aléatoires : programmes en Python ====== | ||
| - | <sxh python; title : 01_Random.py> | + | <note tip>Pour une bonne compréhension, |
| - | #!/usr/ | + | </note> |
| + | ===== Génération de nombres aléatoires ===== | ||
| + | |||
| + | < | ||
| # | # | ||
| + | # -*- coding: utf-8 -*- | ||
| + | """ | ||
| + | cf. documentation cf http:// | ||
| + | random number generation - génération de nombres aléatoires | ||
| + | functions of interest : choice, randint, seed | ||
| + | """ | ||
| from random import * | from random import * | ||
| - | # cf. documentation cf http:// | ||
| - | # random number generation - génération de nombres aléatoires | ||
| - | # functions of interest : choice, randint, seed | ||
| - | facepiece=[' | + | facepiece = [' |
| - | valeurpiece=[0.01, | + | valeurpiece = [0.01, |
| - | #for i in range(1): | + | for i in range(1): |
| # choice : random choice of an element from a list | # choice : random choice of an element from a list | ||
| - | | + | print(choice(facepiece), |
| # randint : return a random integer number between 2 values (including limits) | # randint : return a random integer number between 2 values (including limits) | ||
| - | | + | print(randint(0, |
| - | | + | print(choice(range(0, |
| | | ||
| Ligne 29: | Ligne 35: | ||
| seed() | seed() | ||
| for i in range(10): | for i in range(10): | ||
| - | print randint(1000, | + | print(randint(1000, |
| - | print " " | + | print(" ") |
| - | </sxh> | + | </ |
| + | |||
| + | ===== Histogrammes de nombres aléatoires | ||
| + | |||
| + | <code python 02_random_histogram.py> | ||
| + | # | ||
| + | # -*- coding: utf-8 -*- | ||
| + | |||
| + | from random import * # cf. documentation cf http:// | ||
| + | import numpy as np | ||
| + | import matplotlib.pyplot as plt # http:// | ||
| + | import matplotlib.mlab as mlab # http:// | ||
| + | |||
| + | # | ||
| + | seed() | ||
| + | |||
| + | rval = [] | ||
| + | for j in range(100000): | ||
| + | rval.append(randint(0, | ||
| + | |||
| + | # print rval # uncomment just to see the list of random numbers | ||
| + | |||
| + | # analysis - histogram | ||
| + | # http:// | ||
| + | xh = np.array(rval) | ||
| + | # print(xh) | ||
| + | |||
| + | fig = plt.figure() | ||
| + | ax = fig.add_subplot(111) | ||
| + | |||
| + | n, bins, patches = ax.hist(xh, 50, facecolor=' | ||
| + | print(n) | ||
| + | print(bins) | ||
| + | |||
| + | # modifier le nombre de nombres générés, les nombres de classes-bins, | ||
| + | |||
| + | plt.show() | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Représenter le déplacement d'un objet ===== | ||
| + | |||
| + | <code python 03_tkinter_simple_move.py> | ||
| + | # | ||
| + | # -*- coding: utf-8 -*- | ||
| + | |||
| + | from tkinter import * | ||
| + | import time | ||
| + | |||
| + | window = Tk() | ||
| + | sizex = 400 | ||
| + | sizey = 200 | ||
| + | canvas = Canvas(window, | ||
| + | canvas.pack() | ||
| + | x = 100 # initial left-most edge of first ball | ||
| + | y = 30 # initial top-most edge of first ball | ||
| + | r = 20 # ball diameter | ||
| + | depx = 2 # displacement at each move in x direction | ||
| + | depy = 1 # displacement at each move in y direction | ||
| + | |||
| + | ball=canvas.create_oval(x, | ||
| + | |||
| + | #moves | ||
| + | no_moves = 140 | ||
| + | for j in range(no_moves): | ||
| + | canvas.move(ball, | ||
| + | canvas.after(20) | ||
| + | canvas.update() | ||
| + | |||
| + | time.sleep(5) # on attend quelques secondes | ||
| + | window.destroy() | ||
| + | </ | ||
| + | |||
| + | ===== Représenter le déplacement de nombreux points ===== | ||
| + | <code python 04_tkinter_many_moves.py> | ||
| + | # | ||
| + | # -*- coding: utf-8 -*- | ||
| + | |||
| + | from tkinter import * | ||
| + | import time | ||
| + | from random import * | ||
| + | |||
| + | window = Tk() | ||
| + | sizex = 400 | ||
| + | sizey = 600 | ||
| + | canvas = Canvas(window, | ||
| + | canvas.pack() | ||
| + | x = 100 # initial left-most edge of first ball | ||
| + | y = 30 # initial top-most edge of first ball | ||
| + | r = 16 # ball diameter | ||
| + | depx = 2 # displacement at each move in x direction | ||
| + | depy = 0 # displacement at each move in y direction | ||
| + | |||
| + | # create balls: | ||
| + | no_particles = 20 | ||
| + | dy = (sizey-2.*y)/ | ||
| + | print(dy) | ||
| + | ball_list = [] | ||
| + | for i in range(no_particles): | ||
| + | ball = canvas.create_oval(x, | ||
| + | y = y+dy | ||
| + | ball_list.append(ball) | ||
| + | |||
| + | #moves | ||
| + | no_moves = 100 | ||
| + | for j in range(no_moves): | ||
| + | for ball in ball_list: | ||
| + | canvas.move(ball, | ||
| + | # canvas.move(ball, | ||
| + | canvas.after(10) | ||
| + | canvas.update() | ||
| + | |||
| + | time.sleep(5) # on attend quelques secondes | ||
| + | window.destroy() | ||
| + | </ | ||
| + | |||
| + | ===== Marche aléatoire d'un petit nombre de pas ===== | ||
| + | |||
| + | <code python 05_tkinter_random_walk_few_steps_1D.py> | ||
| + | # | ||
| + | # -*- coding: utf-8 -*- | ||
| + | |||
| + | from tkinter import * | ||
| + | from random import choice | ||
| + | import numpy as np | ||
| + | import matplotlib.pyplot as plt # http:// | ||
| + | import matplotlib.mlab as mlab # http:// | ||
| + | |||
| + | window = Tk() | ||
| + | sizex = 200 | ||
| + | sizey = 600 | ||
| + | canvas = Canvas(window, | ||
| + | canvas.pack() | ||
| + | x = 100 # initial left-most edge of first ball | ||
| + | y = 1 # initial top-most edge of first ball | ||
| + | r = 4 # ball diameter | ||
| + | depx = 10 # displacement at each move in x direction | ||
| + | depy = 0 | ||
| + | |||
| + | # create balls: | ||
| + | no_particles = 6400 | ||
| + | dy = (sizey-2.*y)/ | ||
| + | print(dy) | ||
| + | ball_list = [] | ||
| + | for i in range(no_particles): | ||
| + | ball = canvas.create_oval(x, | ||
| + | y = y+dy | ||
| + | ball_list.append(ball) | ||
| + | |||
| + | # | ||
| + | no_moves = 6 # number of moves | ||
| + | for j in range(no_moves): | ||
| + | for ball in ball_list: | ||
| + | canvas.move(ball, | ||
| + | canvas.after(1) | ||
| + | canvas.update() | ||
| + | |||
| + | #analysis - histogram | ||
| + | # see http:// | ||
| + | xpos=[] | ||
| + | for ball in ball_list: | ||
| + | posi = canvas.coords(ball) | ||
| + | xpos.append(((no_moves+1.)/ | ||
| + | # le facteur (no_moves+1.)/ | ||
| + | xh = np.array(xpos) | ||
| + | #print(xh) | ||
| + | |||
| + | fig = plt.figure() | ||
| + | ax = fig.add_subplot(111) | ||
| + | n, bins, patches = ax.hist(xh, (no_moves)+1, | ||
| + | print(n, | ||
| + | |||
| + | plt.show() | ||
| + | |||
| + | # | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Marche aléatoire d'un grand nombre de pas ===== | ||
| + | |||
| + | <code python 06_tkinter_random_walk_many_steps_1D.py> | ||
| + | # | ||
| + | # -*- coding: utf-8 -*- | ||
| + | |||
| + | from tkinter import * | ||
| + | from random import choice | ||
| + | import numpy as np | ||
| + | import matplotlib.pyplot as plt # http:// | ||
| + | import matplotlib.mlab as mlab # http:// | ||
| + | |||
| + | window = Tk() | ||
| + | sizex = 400 | ||
| + | sizey = 400 | ||
| + | canvas = Canvas(window, | ||
| + | canvas.pack() | ||
| + | x = 200 # initial left-most edge of first ball | ||
| + | y = 1 # initial top-most edge of first ball | ||
| + | r = 4 # ball diameter | ||
| + | depx = 1 # displacement at each move in x direction | ||
| + | depy = 0 | ||
| + | |||
| + | # create balls: | ||
| + | no_particles = 1600 | ||
| + | dy = (sizey-2.)/ | ||
| + | print(dy) | ||
| + | ball_list = [] | ||
| + | for i in range(no_particles): | ||
| + | ball = canvas.create_oval(x, | ||
| + | y = y+dy | ||
| + | ball_list.append(ball) | ||
| + | |||
| + | #moves | ||
| + | no_moves = 200 | ||
| + | for j in range(no_moves): | ||
| + | for ball in ball_list: | ||
| + | canvas.move(ball, | ||
| + | canvas.after(1) | ||
| + | canvas.update() | ||
| + | |||
| + | #analysis - histogram | ||
| + | # see http:// | ||
| + | xpos = [] | ||
| + | for ball in ball_list: | ||
| + | posi = canvas.coords(ball) | ||
| + | xpos.append((posi[0]-x)/ | ||
| + | xh = np.array(xpos) | ||
| + | # compute the mean mu and sigma from xh (and/or theoretical value from random walk result) | ||
| + | mu = np.mean(xh) | ||
| + | sigma = np.std(xh) | ||
| + | fig = plt.figure() | ||
| + | ax = fig.add_subplot(111) | ||
| + | # print xh | ||
| + | n, bins, patches = ax.hist(xh, 10, facecolor=' | ||
| + | print(n, | ||
| + | # hist uses np.histogram to create ' | ||
| + | |||
| + | ax.set_xlabel(' | ||
| + | ax.set_ylabel(' | ||
| + | |||
| + | ax.grid(True) | ||
| + | |||
| + | plt.show() | ||
| + | |||
| + | # | ||
| + | |||
| + | </ | ||
| + | |||
| + | ==== Avec analyse de la distribution : ==== | ||
| + | <code python 07_tkinter_random_walk_many_steps_1D-analysis.py> | ||
| + | # -*- coding: utf-8 -*- | ||
| + | |||
| + | from tkinter import * | ||
| + | from random import choice | ||
| + | import numpy as np | ||
| + | import matplotlib.pyplot as plt # http:// | ||
| + | import matplotlib.mlab as mlab # http:// | ||
| + | |||
| + | window = Tk() | ||
| + | sizex = 400 | ||
| + | sizey = 400 | ||
| + | canvas = Canvas(window, | ||
| + | canvas.pack() | ||
| + | x = 200 # initial left-most edge of first ball | ||
| + | y = 1 # initial top-most edge of first ball | ||
| + | r = 4 # ball diameter | ||
| + | depx = 1 # displacement at each move in x direction | ||
| + | depy = 0 | ||
| + | |||
| + | # create balls: | ||
| + | no_particles = 1000 | ||
| + | dy = (sizey-2.)/ | ||
| + | #print dy | ||
| + | ball_list=[] | ||
| + | for i in range(no_particles): | ||
| + | ball = canvas.create_oval(x, | ||
| + | y = y+dy | ||
| + | ball_list.append(ball) | ||
| + | |||
| + | #moves | ||
| + | no_moves = 400 | ||
| + | for j in range(no_moves): | ||
| + | for ball in ball_list: | ||
| + | canvas.move(ball, | ||
| + | canvas.after(1) | ||
| + | canvas.update() | ||
| + | |||
| + | #analysis - histogram | ||
| + | # see http:// | ||
| + | xpos = [] | ||
| + | for ball in ball_list: | ||
| + | posi = canvas.coords(ball) | ||
| + | xpos.append(posi[0]-x) | ||
| + | xh = np.array(xpos) | ||
| + | # compute the mean mu and sigma from xh (and/or theoretical value from random walk result) | ||
| + | mu = np.mean(xh) | ||
| + | sigma = np.std(xh) | ||
| + | fig = plt.figure() | ||
| + | ax = fig.add_subplot(111) | ||
| + | # print xh | ||
| + | n, bins, patches = ax.hist(xh, 20, facecolor=' | ||
| + | print(mu, sigma) | ||
| + | print(n, | ||
| + | # hist uses np.histogram to create ' | ||
| + | # np.histogram returns the bin edges, so there will be ii probability | ||
| + | # density values in n, ii+1 bin edges in bins and ii patches. | ||
| + | # everything lined up, we'll compute the bin centers | ||
| + | bincenters = 0.5*(bins[1: | ||
| + | # add a 'best fit' line for the normal PDF | ||
| + | yh = (bins[1]-bins[0])*no_particles*mlab.normpdf( bincenters, mu, sigma) | ||
| + | l = ax.plot(bincenters, | ||
| + | #print n | ||
| + | ax.set_xlabel(' | ||
| + | ax.set_ylabel(' | ||
| + | |||
| + | ax.grid(True) | ||
| + | |||
| + | plt.show() | ||
| + | |||
| + | # | ||
| + | </code> | ||