Skip to content

Instantly share code, notes, and snippets.

@mick001
Created August 29, 2015 11:21
Show Gist options
  • Save mick001/de1e3e81fd67139d4e1b to your computer and use it in GitHub Desktop.
Save mick001/de1e3e81fd67139d4e1b to your computer and use it in GitHub Desktop.
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)
# Data and histograms
def plotData():
global x,y
fig = plt.figure()
fig.add_subplot(2,2,1)
plt.hist(x,bins=20,color='green',alpha=0.8,align='mid')
plt.title('X variable distribution')
fig.add_subplot(2,2,3)
plt.scatter(x,y,marker="o",alpha=0.8)
fig.add_subplot(2,2,4)
plt.title('Joint X,Y')
plt.hist(y,bins=20,orientation='horizontal',color='red',alpha=0.8,align='mid')
plt.title('Y variable distribution')
plt.show()
def generateCopulas():
global x,y
fig = plt.figure()
frank = Copula(x,y,family='frank')
uf,vf = frank.generate_uv(1000)
fig.add_subplot(2,2,1)
plt.scatter(uf,vf,marker='.',color='blue')
plt.ylim(0,1)
plt.xlim(0,1)
plt.title('Frank copula')
clayton = Copula(x,y,family='clayton')
uc,vc = clayton.generate_uv(1000)
fig.add_subplot(2,2,2)
plt.scatter(uc,vc,marker='.',color='red')
plt.ylim(0,1)
plt.xlim(0,1)
plt.title('Clayton copula')
gumbel = Copula(x,y,family='gumbel')
ug,vg = gumbel.generate_uv(1000)
fig.add_subplot(2,2,3)
plt.scatter(ug,vg,marker='.',color='green')
plt.ylim(0,1)
plt.xlim(0,1)
plt.title('Gumbel copula')
plt.show()
#-------------------------------------------------------------------------------
# Run the functions
generateData()
plotData()
generateCopulas()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment