Skip to content

Instantly share code, notes, and snippets.

View IlievskiV's full-sized avatar
🐻‍❄️

Vladimir Ilievski IlievskiV

🐻‍❄️
View GitHub Profile
# import libraries
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# ignore warnings
import warnings
warnings.filterwarnings("ignore")
def brownian_motion(N, T, h, seed=42):
import numpy as np
def drifted_brownian_motion(mu, sigma, N, T, seed=42):
"""Simulates a Brownian Motion with drift.
:param float mu: drift coefficient
:param float sigma: volatility coefficient
:param int N : number of discrete steps
:param int T: number of continuous time steps
:param int seed: initial seed of the random generator
def geometric_brownian_motion(G0, mu, sigma, N, T):
"""Simulates a Geometric Brownian Motion.
:param float G0: initial value
:param float mu: drift coefficient
:param float sigma: diffusion coefficient
:param int N: number of discrete steps
:param int T: number of continuous time steps
:return list: the geometric Brownian Motion
"""
import numpy as np
def gbm_mean(G0, mu, sigma, N, T):
"""Simulates the mean of the Geometric Brownian Motion, which is:
E(t) = G0*e^{(mu + sigma^{2}/2)*t}
:param float G0: initial value
:param float mu: drift coefficient
:param float sigma: diffusion coefficient
:param int N: number of discrete steps
:param int T: number of continuous time steps
def calculate_integral(f, a, b, n):
'''Calculates the integral based on the composite trapezoidal rule
relying on the Riemann Sums.
:param function f: the integrand function
:param int a: lower bound of the integral
:param int b: upper bound of theintergal
:param int n: number of trapezoids of equal width
:return float: the integral of the function f between a and b
'''
@IlievskiV
IlievskiV / pi_geometric_approx_quarter.ipynb
Created June 5, 2020 21:06
Approximating Pi using Monte Carlo Method
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@IlievskiV
IlievskiV / reflection_principle_bm.ipynb
Created June 5, 2020 21:22
Demonstration of the reflection principle of the Brownian Motion
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@IlievskiV
IlievskiV / calculate_circle_area.ipynb
Created June 7, 2020 23:52
How to numerically calculate the area of a circle
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
def mandelbrot(x, y, threshold):
"""Calculates whether the number c = x + i*y belongs to the
Mandelbrot set. In order to belong, the sequence z[i + 1] = z[i]**2 + c
must not diverge after 'threshold' number of steps. The sequence diverges
if the absolute value of z[i+1] is greater than 4.
:param float x: the x component of the initial complex number
:param float y: the y component of the initial complex number
:param int threshold: the number of iterations to considered it converged
"""
@IlievskiV
IlievskiV / animate_mandelbrot_set.py
Created September 6, 2020 10:02
How to animate the convergence of the Mandelbrot set in one interesting lattice of the complex plane
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
x_start, y_start = -2, -1.5 # an interesting region starts here
width, height = 3, 3 # for 3 units up and right
density_per_unit = 250 # how many pixles per unit
# real and imaginary axis
re = np.linspace(x_start, x_start + width, width * density_per_unit )