Commit 12ca79e1 authored by Matthieu Boileau's avatar Matthieu Boileau
Browse files

Updates for nbcourse

parent 1717a530
Pipeline #7758 passed with stages
in 1 minute and 7 seconds
title: Master Class in Probability
slug_title: proba-mc2020
subtitle: IRMA, Strasbourg, 2020
subtitle: IRMA, Strasbourg, Jan 2020
favicon: fig/favicon.ico
book:
file: proba-mc2020.pdf
authors:
- name: Matthieu Boileau
- name: Sébastien Martineau
- name: Marielle Simon
- name: Dario Trevisan
- name: Xiaolin Zeng
email: matthieu.boileau@math.unistra.fr
- Sébastien Martineau
- Marielle Simon
- Dario Trevisan
- Xiaolin Zeng
chapter_preview_only:
- 3
links:
manual:
title: Notice
target: manual.html
binder:
title: Exécuter
target: https://mybinder.org/v2/gh/fitzinger/cours-python/master
target: https://mybinder.org/v2/git/https%3A%2F%2Fgitlab.math.unistra.fr%2Fboileau%2Fproba-mc2020/master
book:
title: Version pdf
target: cours-python.pdf
gitlab:
title: Sources
target: https://gitlab.math.unistra.fr/fitzinger/cours-python
target: https://gitlab.math.unistra.fr/boileau/proba-mc2020
archive:
title: Archive complète
target: cours-python.zip
license:
text: Contenu mis à disposition sous licence
target: https://creativecommons.org/licenses/by-sa/4.0/
......
%% Cell type:markdown id: tags:
# Course of Marielle Simon: Simple random walk
# Simple random walk
> *Course of Marielle Simon*
%% Cell type:markdown id: tags:
First, some python initializations.
%% Cell type:code id: tags:
``` python
%matplotlib inline
from matplotlib import rcParams
from matplotlib import pyplot as plt
from ipywidgets import interact, IntSlider
rcParams['figure.figsize'] = (8., 6.) # Enlarge figure
rcParams['animation.html'] = 'html5' # to render animation in notebook
import srw # Import simple random walk module from srw.py
```
%% Cell type:markdown id: tags:
## Random walk on $\mathbb{Z}^2$
%% Cell type:markdown id: tags:
Create and play a matplotlib animation for a $nstep$-step random walk starting at $(x, y) = (0, 0)$.
%% Cell type:code id: tags:
``` python
walk = srw.Walk2D(nstep=100) # Create a 100-step random walk
anim = walk.generate_animation()
plt.close(anim._fig) # Close the initial figure to display only the animation figure
anim # Now play
```
%% Cell type:markdown id: tags:
Plot entire path for various $nstep$ values.
%% Cell type:code id: tags:
``` python
def plot_walk(nstep: int):
srw.Walk2D(nstep).plot()
slider = IntSlider(min=100, max=20000, step=100, value=5000, continuous_update=False)
interact(plot_walk, nstep=slider);
```
%% Cell type:markdown id: tags:
### Some quantities as a function of the number of steps
Compute average **final distance** over 1000 random walks.
%% Cell type:code id: tags:
``` python
srw.FinalDistance(nwalk=1000).plot()
```
%% Cell type:markdown id: tags:
Compute average **maximum distance** over 1000 random walks.
%% Cell type:code id: tags:
``` python
srw.MaxDistance(nwalk=1000).plot()
```
%% Cell type:markdown id: tags:
Compute the **number of times** the walk goes back to starting point (average over 1000 random walks).
%% Cell type:code id: tags:
``` python
srw.BackToStart(nwalk=10000, nstepmax=10000).plot()
```
%% Cell type:markdown id: tags:
## Random walk on $\mathbb{Z}$
%% Cell type:markdown id: tags:
Consider the random walk on $\mathbb{Z}$ with $0 < p < 1$, denoted by $(S_n)$. The chain is supposed to start from state 0.
1\. Implement a function `random_walk_z` simulating the behaviour of the random walk for $n_{\max}$ steps, and represent it on a graph. Ensure that the function `random_walk_z`also returns: #
- the number of times the chain is returned to the initial state;
- the largest state reached by the chain.
%% Cell type:code id: tags:
``` python
import numpy as np
from sklearn.utils import check_random_state
from scipy.special import binom
import multiprocessing as mp
mp.set_start_method('spawn', True) # see https://github.com/microsoft/ptvsd/issues/1443
from numba import jit
@jit(nopython=True)
def count_first(item: int, vec: np.ndarray):
"""
Find the index of the first element in the array `vec` equal to the element `item`.
"""
c = 0
for i in range(len(vec)):
if item == vec[i]:
c += 1
return c
def random_walk_z(p, n_max, random_state):
""" Simulate a simple 1D random walk in Z.
:returns:
- Ti (:py:class:`int`) - number of returns to the initial state
- state_max (:py:class:`int`) - farthest state reached by the chain (w.r.t the initial state)
"""
rng = check_random_state(random_state)
Z = 2*rng.binomial(1, p, size=(n_max)) - 1
X = np.empty(shape=(n_max+1), dtype=float)
X[0] = 0
X[1:] = np.cumsum(Z)
Ti = count_first(0, X[1:])
id = np.argmax(np.abs(X))
state_max = X[id]
t = np.arange(0, n_max+1, 1)
plt.plot(t, X)
plt.show()
return Ti, state_max
```
%% Cell type:code id: tags:
``` python
random_walk_z(0.5, 1000, 500)
```
%% Cell type:markdown id: tags:
2\. Assume now that two players $A$ and $B$ play heads or tails, where heads occur with probability $p$. Player $A$ bets $1$ euro on heads at each toss, and $B$ bets $1$ euro on tails. Assume that:
- the initial fortune of $A$ is $a \in \mathbb{N}$;
- the initial fortune of $B$ is $b\in\mathbb{N}$;
- the gain ends when a player is ruined.
Implement a function which returns the empirical frequency of winning for $A$, and compare it with the theoretical probability computed in the lecture.
%% Cell type:code id: tags:
``` python
# Exercise
```
......
%% Cell type:markdown id: tags:
# Course of Sébastien Martineau: Percolation
# Percolation
> *Course of Sébastien Martineau*
%% Cell type:markdown id: tags:
First, some python initializations.
%% Cell type:code id: tags:
``` python
%matplotlib inline
from matplotlib import rcParams
from matplotlib import pyplot as plt
from ipywidgets import interact, FloatSlider, RadioButtons
rcParams['figure.figsize'] = (8., 6.) # Enlarge figure
sliderd = dict(min=0., max=1., step=0.1, value=0.5, continuous_update=False)
from percolation import PercolationRect, PercolationHex, percolation_vs_p, PercolationRectDual
```
%% Cell type:markdown id: tags:
## Rectangular lattice
%% Cell type:code id: tags:
``` python
percorect = PercolationRect(20, 10)
interact(percorect.plot, p=FloatSlider(**sliderd));
```
%% Cell type:markdown id: tags:
## Hexagonal lattice
%% Cell type:code id: tags:
``` python
percohex = PercolationHex(5, 5)
percohex.compute_clusters(0.2)
percohex.plot_clusters(add_cluster_id=True)
```
%% Cell type:code id: tags:
``` python
percohex15 = PercolationHex(30, 30)
interact(percohex15.plot, p=FloatSlider(**sliderd));
```
%% Cell type:markdown id: tags:
## Probability of crossing as a function of $p$
Based on 300 simulations on a $25 \times 25$ lattice.
%% Cell type:code id: tags:
``` python
percolation_vs_p(25, 25, nsim=300)
```
%% Cell type:markdown id: tags:
## Initial and dual graph for a rectangular percolation
%% Cell type:code id: tags:
``` python
perco = PercolationRectDual(5)
interact(perco.plot_graph, p=FloatSlider(**sliderd),
graph_type=RadioButtons(options=['initial', 'dual', 'both']));
```
%% Cell type:markdown id: tags:
## Todo
- Standard coupling on honeycomb lattice,
- Duality of honycomb lattice
- Standard coupling on square lattice and its dual #
- Monte Carlo for crossing probabilities on square lattice, threshold phenomena #
- Monte Carlo for crossing probabilities on honeycomb lattice
- Percolation on 4 regular trees
- Percolation on free group with 2 generators #
- Percolation on the dual of seven triangular tilling
hex cells:
<https://stackoverflow.com/questions/46525981/how-to-plot-x-y-z-coordinates-in-the-shape-of-a-hexagonal-grid>
graphs: networkx
......
%% Cell type:markdown id: tags:
# Course of Dario Trevisan: Optimal transport
# Optimal transport
> *Course of Dario Trevisan*
%% Cell type:markdown id: tags:
We solve the earth movers problem using the [POT](https://pot.readthedocs.io/en/stable/index.html) library.
First, some python initializations.
%% Cell type:code id: tags:
``` python
%matplotlib inline
from matplotlib import rcParams
from ipywidgets import interact, fixed, FloatSlider, RadioButtons
rcParams['figure.figsize'] = (8., 6.) # Enlarge figure
# A slider for p
sliderd = dict(min=-1., max=2., step=0.05, value=1.1, continuous_update=False)
from earth_movers import EarthMovers1D, EarthMovers2D
```
%% Cell type:markdown id: tags:
## 1D case
Solve the earth movers problem for 50-position samples.
%% Cell type:code id: tags:
``` python
em1D = EarthMovers1D(50)
interact(em1D.plot_ot, p=FloatSlider(**sliderd), plot_points=fixed(True));
```
%% Cell type:markdown id: tags:
## 2D case
Solve the earth movers problem for 50-position samples.
%% Cell type:code id: tags:
``` python
em2D = EarthMovers2D(100)
interact(em2D.plot_ot, p=FloatSlider(**sliderd), plot_points=fixed(True));
```
%% Cell type:markdown id: tags:
Solve the earth movers problem for 1000-position samples.
The darker the line, the longer the distance.
%% Cell type:code id: tags:
``` python
em2D_large = EarthMovers2D(1000)
interact(em2D_large.plot_ot, p=FloatSlider(**sliderd), plot_points=fixed(False));
```
%% Cell type:markdown id: tags:
## Histogram of distance
%% Cell type:markdown id: tags:
Plot the histogram of distance for 2000-position samples on the 1D and 2D problems.
%% Cell type:code id: tags:
``` python
def plot_histogram(dimension=2, p=1.1):
nsim = 2000
em = EarthMovers1D(nsim) if dimension == 1 else EarthMovers2D(nsim)
return em.plot_distance_histogram(p, bins=20)
interact(plot_histogram, dimension=RadioButtons(options=[1, 2], value=2), p=FloatSlider(**sliderd));
```
%% Cell type:markdown id: tags:
## Todo
- Uniform random blue and red points on a square #
- Its optimal mathching, with p=1, n=500 #
- Histogram of matching length in d=1,2,3 #
- one dimensional matching for p=1.1 and p=0.9, comparison
- The scaling algorithm for local optimal matching
PoT: <https://pot.readthedocs.io/en/stable/auto_examples/plot_OT_2D_samples.html>
......
......@@ -68,9 +68,16 @@
<table id="auteurs">
{% for author in authors %}
<tr>
<td>{{ author.name }}</td>
{% if author.email %}
<td><a href="mailto: {{ author.email }}"><img style="display:inline" width="20" src="img/email.svg"></a></td>
<td><a href="mailto: {{ author.email }}"><img style="display:inline" width="20" src="img/email.svg"></a></td>
<td>{{ author.name }}</td>
{% else %}
<td><span style="display:none"></span></td>
{% if author is mapping %}
<td>{{ author.name }}</td>
{% else %}
<td>{{ author }}</td>
{% endif %}
{% endif %}
</tr>
{% endfor %}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment