File: plot_random_walk_process.py

package info (click to toggle)
openturns 1.26-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,708 kB
  • sloc: cpp: 261,605; python: 67,030; ansic: 4,378; javascript: 406; sh: 185; xml: 164; makefile: 101
file content (86 lines) | stat: -rw-r--r-- 2,358 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
Create a random walk process
============================
"""

# %%
# This example details first how to create and manipulate a random walk.
#
# A random walk :math:`X: \Omega \times \mathcal{D} \rightarrow \mathbb{R}^d` is a process
# where :math:`\mathcal{D}=\mathbb{R}` discretized on the time grid :math:`(t_i)_{i \geq 0}` such
# that:
#
# .. math::
#    \begin{aligned}
#      X_{t_0} & = & \vect{x}_{t_0} \\
#      \forall n>0,\: X_{t_n} & = & X_{t_{n-1}} + \varepsilon_{t_n}
#    \end{aligned}
#
# where :math:`\vect{x}_0 \in \mathbb{R}^d` and :math:`\varepsilon` is a white noise of
# dimension :math:`d`.
#
# The library proposes to model it through the object :class:`~openturns.RandomWalk` defined
# thanks to the origin, the distribution of the white noise and the time
# grid.

# %%
import openturns as ot
import openturns.viewer as otv

# %%
# Define the origin
origin = [0.0]

# %%
# Define an 1-d mesh
tgrid = ot.RegularGrid(0.0, 1.0, 500)

# %%
# 1-d random walk and discrete distribution
dist = ot.UserDefined([[-1], [10]], [0.9, 0.1])
process = ot.RandomWalk(origin, dist, tgrid)
sample = process.getSample(5)
graph = sample.drawMarginal(0)
graph.setTitle("1D Random Walk with discrete steps")
view = otv.View(graph)

# %%
# 1-d random walk and continuous distribution
dist = ot.Normal(0.0, 1.0)
process = ot.RandomWalk(origin, dist, tgrid)
sample = process.getSample(5)
graph = sample.drawMarginal(0)
graph.setTitle("1D Random Walk with continuous steps")
view = otv.View(graph)

# %%
# Define the origin
origin = [0.0] * 2

# %%
# Color palette
pal = ["red", "cyan", "blue", "yellow", "green"]

# %%
# 2-d random walk and discrete distribution
dist = ot.UserDefined([[-1.0, -2.0], [1.0, 3.0]], [0.5, 0.5])
process = ot.RandomWalk(origin, dist, tgrid)
sample = process.getSample(5)
graph = ot.Graph("2D Random Walk with discrete steps", "X1", "X2", True)
for i in range(5):
    graph.add(ot.Curve(sample[i], pal[i % len(pal)], "solid"))
view = otv.View(graph)

# %%
# 2-d random walk and continuous distribution
dist = ot.Normal(2)
process = ot.RandomWalk(origin, dist, tgrid)
sample = process.getSample(5)
graph = ot.Graph("2D Random Walk with continuous steps", "X1", "X2", True)
for i in range(5):
    graph.add(ot.Curve(sample[i], pal[i % len(pal)], "solid"))
view = otv.View(graph)

# %%
# Display all figures
otv.View.ShowAll()