Skip to content

Instantly share code, notes, and snippets.

View mick001's full-sized avatar

Michy mick001

View GitHub Profile
@mick001
mick001 / crankshaft_out.py
Created August 29, 2015 11:09
Crankshaft connecting rod and piston mechanism simulation with Python (Output). Full article at http://www.firsttimeprogrammer.blogspot.com/2015/02/crankshaft-connecting-rod-and-piston.html
m = My_mechanism(10,20,1)
# m.animation_vp(0.01,100,400)
# m.animation_m()
m.animation_m_plus()
import numpy as np
import matplotlib.pyplot as plt
from copulalib.copulalib import Copula
plt.style.use('ggplot')
def generateData():
global x,y
x = np.random.normal(size=250)
y = 2.5*x + np.random.normal(size=250)
@mick001
mick001 / CopulaClass.py
Created August 29, 2015 11:28
CopulaClass a Python class for using copulas: a fitting example. Full article at http://www.firsttimeprogrammer.blogspot.com/2015/02/copulaclass-python-class-for-using.html
# Copula class
import numpy as np
import matplotlib.pyplot as plt
from copulalib.copulalib import Copula
from scipy.stats import norm
plt.style.use('ggplot')
class copulaClass(object):
@mick001
mick001 / CopulaClass_example.py
Created August 29, 2015 11:31
CopulaClass a Python class for using copulas: a fitting example (Output example). Full article at http://www.firsttimeprogrammer.blogspot.com/2015/02/copulaclass-python-class-for-using.html
#####################################################
# Correlation details:
# Correlation index range: [-1,1] [negative,positive]
# Kendall's tau: 0.4859437751
# Spearman's rho: 0.665549080785
# Parameter of the copula (theta): 5.48651123047
#####################################################
@mick001
mick001 / currency_plots.py
Created August 29, 2015 11:39
Some exercises with plots and matplotlib on currencies. Full article at http://www.firsttimeprogrammer.blogspot.com/2015/02/some-exercises-with-plots-and.html
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
currency_needed = ['EUR','CNY','THB','VND','MYR','KHR','XOF']
country = ['Europe','China','Thailand','Vietnam','Malesia','Cambodia','Senegal']
print('Available currencies:')
x <- read.table("x.txt")
y <- read.table("y.txt")
mat <- matrix(nrow=100,ncol=2)
for(i in 1:100)
{
mat[i,1] <- x[,1][i]
mat[i,2] <- y[,1][i]
}
# Normal copula
normal.cop <- normalCopula(dim=2)
fit.cop<- fitCopula(normal.cop,pobs(mat),method="ml")
# Coefficients
rho <- coef(fit.cop)
print(rho)
# 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")
# Plot data with histograms
xhist <- hist(mat[,1], breaks=30, plot=FALSE)
yhist <- hist(mat[,2], breaks=30, plot=FALSE)
top <- max(c(xhist$counts, yhist$counts))
xrange <- c(-4,4)
yrange <- c(-6,6)
nf <- layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(3,1), c(1,3), TRUE)
par(mar=c(3,3,1,1))
plot(mat[,1], mat[,2], xlim=xrange, ylim=yrange, xlab="", ylab="")
par(mar=c(0,3,1,1))
import numpy as np
import matplotlib.pyplot as plt
b = []
for i in range(50):
a = np.random.normal(5,i+1,10)
b.append(a)
c = np.array(b)