```
%matplotlib inline
```

```
import numpy as np
import matplotlib.pyplot as plt
import random as rm
from scipy.stats import norm
import itertools
import matplotlib.patches as mpatches
```

# Stochastic Processes and Applications¶

## Author: Stefan Panev¶

### Abstract¶

This notebook is a basic introduction into Stochastic Processes. It is meant for the general reader that is not very math savvy, like the course participants in the Math Concepts for Developers in SoftUni.

There is a basic definition. Some examples of the most popular types of processes like Random Walk, Brownian Motion or Weiner Process, Poisson Process and Markov chains have been given. Their basic characteristics and examples for some possible applications are stated. For all the examples there are simulations in Python, some are visualized.

The following packages have been used:

- nympy
- matplotlib.pyplot
- random
- scipy.stats
- itertools
- matplotlib.patches

### Introduction¶

A Stochastic Process is a collection of random variables over a period of time. It can be either discrete or continuous type. Techniques from calculus and probability theory are used to study the processes. There are numerous kids of stochastic processes. Several kinds of Stochastic processes are going to be given as an example in this notebook.

Lets define the stochastic process as a collection of random variables in defined on a common probability space $$(\Omega, \mathcal{F}, P)$$
,where omega is the the sample space or all the possible outcomes, mathematical F is the sigma-algebra where each set contains zero or several possible outcomes and P is the probability for the realization of the outcomes.

We define a function with an index t (representing the time) that maps a variable from the set T to a random variable from the state space S or: $$X_{t} : T \to S$$

### Random Walk¶

The first basic process we will look at is the random walk. It is defined as the path created by the steps which have no rule determining their development. The term 'random walk' was coined by the mathematician Karl Pearson (1857 - 1936) in 1905. The process can either be represented as one or several dimensions.

There are numerous applications in finance for modeling stocks and price movements (for further reading https://www.investopedia.com/terms/r/randomwalktheory.asp), the net worth of a gambler, movement of people in the market (such as some agent based modeling), movements of molecules and particles or changes in gene in the genome.

There are different kinds of random walks, if the steps follow the normal distribution it is said that it is a gaussian type. Other variants of a random walk are self-interacting walks, correlated walks, maximal entropy random walk and others.

```
# Initialization
np.random.seed(99)
# Initialize all_walks
all_walks = []
# Simulate random walk 10 times
for i in range(10) :
random_walk = [0]
for x in range(100) :
step = random_walk[-1]
dice = np.random.randint(1,7)
if dice <= 2:
step = step - 1
elif dice <= 5:
step = step + 1
else:
step = step + np.random.randint(1,5)
random_walk.append(step)
# Append random_walk to all_walks
all_walks.append(random_walk)
#print(all_walks)
# Plot random_walk
plt.figure(figsize=(20,10))
plt.plot(random_walk)
# Show the plot
plt.show()
```

We generate a simulate 100 random walks. The np.random.seed of 99 is being used that let the reader reproduce the same results. Because we are using the gaussian random generator of integers the walk will be gaussian in nature.

To make thing more interesting we create a basic algorithm, where we though a dice and depending on the result we move with a particular number of steps. We get the number of step from the previous iteration of the for cycle. The steps are decreased with 1 if the dice is less or equal then 2, if there are between 3 and 5 we increase the number of steps with 1, and if we get 6 we increase the number of steps with a random number between 1 and 4. Finally the values of the random walk are printed (optionally, it is commented out) and plotted.

We seem mostly a increasing trend.

```
np.random.seed(99)
all_walks = []
for i in range(20) :
random_walk = [0]
for x in range(100) :
step = random_walk[-1]
dice = np.random.randint(1,7)
if dice <= 2:
step = step - 1
elif dice <= 5 and dice > 2:
step = step + 1
else:
step = step + np.random.randint(1,5)
random_walk.append(step)
all_walks.append(random_walk)
# Convert all_walks to Numpy array: np_aw
np_aw = np.array(all_walks)
# Plot np_aw and show
plt.figure(figsize=(20,10))
plt.plot(np_aw)
plt.show()
# Clear the figure
plt.clf()
# Transpose np_aw: np_aw_t
np_aw_t = np.transpose(np_aw)
# Plot np_aw_t and show
plt.figure(figsize=(20,10))
plt.plot(np_aw_t)
plt.show()
```