Created
August 29, 2015 11:21
-
-
Save mick001/de1e3e81fd67139d4e1b to your computer and use it in GitHub Desktop.
Copulalib: How to use copulas in Python. Full article at http://www.firsttimeprogrammer.blogspot.com/2015/02/copulalib-how-to-use-copulas-in-python.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
my spyder can't recognize the copulalib?
my real problem is that "No module named 'copulalib'"