Skip to content

Instantly share code, notes, and snippets.

View mick001's full-sized avatar

Michy mick001

View GitHub Profile
import math
from bigfloat import *
import matplotlib.pyplot as plt
from visual import *
# A class to handle the time ranges
class timeHoursSeconds(object):
def __init__(self,s,h,d,y):
self.s = s
self.h = h
import numpy as np
# R matrix
R = np.matrix([ [-1,-1,-1,-1,0,-1],
[-1,-1,-1,0,-1,100],
[-1,-1,-1,0,-1,-1],
[-1,0,0,-1,0,-1],
[-1,0,0,-1,-1,100],
[-1,0,-1,-1,0,100] ])
@mick001
mick001 / taylor_series_improved.py
Last active March 5, 2022 07:29
Revised and improved version of Taylor series with Python and Sympy. You can find the original post at http://firsttimeprogrammer.blogspot.com/2015/03/taylor-series-with-python-and-sympy.html
import sympy as sy
import numpy as np
from sympy.functions import sin, cos, ln
import matplotlib.pyplot as plt
plt.style.use("ggplot")
# Factorial function
def factorial(n):
if n <= 0:
return 1
@mick001
mick001 / Option_pricing_KDE_estimate.py
Last active February 7, 2022 15:13
Call option pricing using KDE estimate instead of the normality assumption
"""
MONTE CARLO PLAIN VANILLA OPTION PRICING
This script is used to estimate the price of a plain vanilla
option using the Monte Carlo method and assuming that returns
can be simulated using an estimated probability density (KDE estimate)
Call option quotations are available at:
http://www.google.com/finance/option_chain?q=NASDAQ%3AAAPL&ei=fNHBVaicDsbtsAHa7K-QDQ
# Pseudo observations
p_obs <- pobs(mat)
plot(p_obs[,1],p_obs[,2],main="Pseudo/simulated observations: BLUE/RED",xlab="u",ylab="v",col="blue")
# Simulate data
set.seed(100)
u1 = rCopula(500,normalCopula(coef(fit.cop),dim=2))
points(u1[,1],u1[,2],col="red")
@mick001
mick001 / magnetic_field.py
Last active November 1, 2021 06:44
Biot-Savart law: magnetic field of a straight wire, Python simulation. Full article at: http://www.firsttimeprogrammer.blogspot.com/2015/05/biot-savart-law-magnetic-field-of.html
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-4,4,10)
y = np.linspace(-4,4,10)
z = np.linspace(-4,4,10)
x,y,z = np.meshgrid(x,y,z)
@mick001
mick001 / poly_reg.R
Last active October 22, 2021 02:20
Polynomial regression in R. Full article available at: http://datascienceplus.com/fitting-polynomial-regression-r/
# Example 1
p <- 0.5
q <- seq(0,100,1)
y <- p*q
plot(q,y,type='l',col='red',main='Linear relationship')
# Example 2
y <- 450 + p*(q-10)^3
plot(q,y,type='l',col='navy',main='Nonlinear relationship',lwd=3)
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
l = 0.0229 #Inductance (H)
r = 3.34 #Resistance (Ohm)
v = 5 #Voltage (V) DC
i = v/r #Peak current (A)
tau = l/r #Tau time constant
@mick001
mick001 / earthMoon.py
Created August 29, 2015 12:54
Earth Moon system orbiting around the Sun and VPython. Full article at http://www.firsttimeprogrammer.blogspot.com/2014/12/earth-moon-system-orbiting-around-sun.html
import math
from visual import *
# Data in units according to the International System of Units
G = 6.67 * math.pow(10,-11)
# Mass of the Earth
ME = 5.973 * math.pow(10,24)
# Mass of the Moon
MM = 7.347 * math.pow(10,22)
@mick001
mick001 / slope_field_py.py
Last active July 16, 2021 09:26
Generate slope fields in R and Python (Python code). Full article at http://firsttimeprogrammer.blogspot.com/2014/09/generate-slope-fields-in-r-and-python.html
import numpy as np
from matplotlib import pyplot as plt
# Differential equation
# diff = y'= y/x (or say x+y)
def diff(x,y):
return y/x # try also x+y
x = np.linspace(-10,10,50)
y = np.linspace(-10,10,50)