Usage
Installation
To use pygbm, first install the repo:
(.venv) ~$ git clone https://github.com/Dario-Zela/pygbm.git
Then install it:
(.venv) ~$ cd pygbm
(.venv) ~/pygbm$ pip install -e .
Run a simulation in the CLI
To create a simulation in the CLI, the following methods are used:
- pygbm.cli.main()
Main function for the CLI tool.
- pygbm.cli.simulate_analytical(y0, mu, sigma, T, N, output)
Runs the simulation on the input given by the user using analytical solution and saves the resulting simulation in the output file
- Parameters:
y0 (float) – Starting location of the simulation.
mu (float) – The drift of the simulation.
sigma (float) – The diffusion of the simulation.
T (float or None) – The lenght of time should the program simulate.
N (int or None) – The number of simualtion steps.
output (str or None) – The location the file will be saved in.
- pygbm.cli.simulate_euler(y0, mu, sigma, T, N, output)
Runs the simulation on the input given by the user using Euler-Maruyama approximation and saves the resulting simulation in the output file
- Parameters:
y0 (float) – Starting location of the simulation.
mu (float) – The drift of the simulation.
sigma (float) – The diffusion of the simulation.
T (float or None) – The lenght of time should the program simulate.
N (int or None) – The number of simualtion steps.
output (str or None) – The location the file will be saved in.
- pygbm.cli.simulate_milstein(y0, mu, sigma, T, N, output)
Runs the simulation on the input given by the user using Milstein approximation and saves the resulting simulation in the output file
- Parameters:
y0 (float) – Starting location of the simulation.
mu (float) – The drift of the simulation.
sigma (float) – The diffusion of the simulation.
T (float or None) – The lenght of time should the program simulate.
N (int or None) – The number of simualtion steps.
output (str or None) – The location the file will be saved in.
In the console this can be used via:
(.venv) $ pygbm euler --y0 1.0 --mu 0.05 --sigma 0.2 --T 1.0 --N 100 --output gbm_plot.png
The –y0, –mu, and –sigma arguments are required, while the program will default a –T to 10, –N to 100 and output to “output.png”
Run a simulation
To create a simulation in a python script the GBMSimulator class must be used:
- class pygbm.gbm_simulator.GBMSimulator(y0, mu, sigma)
The class used to encapsulate the parameters for the simulation and solve the GBM equation.
- browninan(dt, N_steps)
Creates the browninan noise used in the simulation
- Parameters:
dt (float) – The length of time between time steps.
N_steps (int) – The total number of time steps.
- simulate_path(T_Final, N_steps)
Runs the simulation on the input given by the user, using analytical solution
- Parameters:
T_Final (float) – The lenght of time should the program simulate.
N_steps (int) – The number of simualtion steps.
- simulate_path_euler(T_Final, N_steps)
Runs the simulation on the input given by the user, using Euler-Maruyama method
- Parameters:
T_Final (float) – The lenght of time should the program simulate.
N_steps (int) – The number of simualtion steps.
- simulate_path_milstein(T_Final, N_steps)
Runs the simulation on the input given by the user, using Milstein method
- Parameters:
T_Final (float) – The lenght of time should the program simulate.
N_steps (int) – The number of simualtion steps.
For example:
from pygbm.gbm_simulator import GBMSimulator
import matplotlib.pyplot as plt
# Parameters for GBM
y0 = 1.0
mu = 0.05
sigma = 0.2
T = 1.0
N = 100
# Initialize simulator
simulator = GBMSimulator(y0, mu, sigma)
# Simulate path
t_values, y_values = simulator.simulate_path_analytical(T, N)
# Plot the simulated path
plt.plot(t_values , y_values , label ="GBM Path")
plt.xlabel(" Time ")
plt.ylabel("Y(t)")
plt.title("Simulated Geometric Brownian Motion Path")
plt.legend()
plt.show()