-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YusufCakan
committed
Jan 26, 2015
0 parents
commit 42428c7
Showing
4 changed files
with
883 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from __future__ import division | ||
from scipy.stats import kendalltau, pearsonr, spearmanr | ||
import numpy as np | ||
from scipy.integrate import quad | ||
from scipy.optmize import fmin | ||
from scipy.interpolate import interp1d | ||
import statistics as st | ||
|
||
|
||
class Copula(): | ||
""" | ||
This class estimates teh parameters of copula to generated joint | ||
random variables for the parameters.\ | ||
This class hsas teh following three copulas | ||
Clayton | ||
Frank | ||
Gumbell | ||
""" | ||
|
||
def __init__ (self, X, Y, family): | ||
|
||
# check dimensions of the input arrays | ||
if not ((X.nidims == 1) and (Y.ndims == 1)): | ||
raise ValueError("The dimensions of array should be one") | ||
|
||
|
||
# input arrays should have the same size | ||
if X.size is not Y.size: | ||
raise ValueError("The size of both Arrays shoudl be the same") | ||
|
||
|
||
#check if the name of the copula family is correct | ||
copula_family =['clayton', "frank", "gumbell"] | ||
if family not in copula_family: | ||
raise ValueError('The family should be clayton or frank or gumbell') | ||
|
||
self.X = X | ||
self.Y = Y | ||
self.family= family | ||
|
||
# estimate kendalls' rank correlation | ||
self.tau = kendalltau(self.X, self.Y)[0] | ||
|
||
# estimate person R and spearman R | ||
self.pr = pearsonr(self.X, self.Y)[0] | ||
self.pr = spearmanr(self.X, self.Y)[0] | ||
|
||
self._get_parameters() | ||
|
||
self.U = None | ||
self.V = None | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from __future__ import division\n", | ||
"from scipy.stats import kendalltau, pearsonr, spearmanr\n", | ||
"import numpy as np\n", | ||
"from scipy.integrate import quad\n", | ||
"from scipy.optimize import fmin\n", | ||
"import sys\n", | ||
"#import statistics as st\n", | ||
"from scipy.interpolate import interp1d\n", | ||
"#from stats import scoreatpercentis" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 13, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"class Copula():\n", | ||
" \"\"\"\n", | ||
" This class estimats parameter of copula generated joint \n", | ||
" random variate for the parameters.\n", | ||
" This class has the following three copulas\n", | ||
" Clayton\n", | ||
" Frank\n", | ||
" Gumbell\n", | ||
" \"\"\"\n", | ||
" \n", | ||
" def __init__(self, x, y, family):\n", | ||
" \"\"\"\n", | ||
" Initialise the class with x and Y\n", | ||
" Input:\n", | ||
" X: one dimesional numpy array\n", | ||
" Y: one dimensional mumpy array\n", | ||
" family: clayton or frank or gumbell\n", | ||
" \n", | ||
" Note that the size of x and Y should be the same\n", | ||
" \"\"\"\n", | ||
" \n", | ||
" if not((x.ndim==1) and (y.ndim==1)):\n", | ||
" raise ValueError(\"The dimensions of array should \\\n", | ||
" be one\")\n", | ||
" \n", | ||
" if x.size != y.size:\n", | ||
" raise ValueError(\"the size of both arrays \\\n", | ||
" should be the same\")\n", | ||
" \n", | ||
" copula_family =[\"clayton\", \"frank\", \"gumbel\"]\n", | ||
" if family not in copula_family:\n", | ||
" raise ValueError(\"The family should be clayton \\\n", | ||
" or frank of gumbell\")\n", | ||
" \n", | ||
" self.x= x\n", | ||
" self.y= y\n", | ||
" self.family = family\n", | ||
" \n", | ||
" tau = kendalltau(self.x,self.y)[0]\n", | ||
" self.tau = tau\n", | ||
" \n", | ||
" self.pr =pearsonr(self.x, self.y)[0]\n", | ||
" self.sr= spearman(self.x,self.y)[0]\n", | ||
" \n", | ||
" self._get_parameter()\n", | ||
" \n", | ||
" self.u = None\n", | ||
" self.v = None\n", | ||
" \n", | ||
" def _get_parameter(self):\n", | ||
" \"\"\"\n", | ||
" estimate the parameter (theta) of copula\n", | ||
" \"\"\"\n", | ||
" \n", | ||
" if self.family == \"clayton\":\n", | ||
" self.theta = 2*self.tau/(1-self.tau)\n", | ||
" \n", | ||
" elif self.family == \"frank\"\n", | ||
" self.theta= -fmin(self._frank_fun, -5, disp=False)[0]\n", | ||
" \n", | ||
" elif self.family == 'gumbel':\n", | ||
" self.theta = 1/(1-self.tau)\n", | ||
" \n", | ||
" def generate_uv(self.n=1000):\n", | ||
" \"\"\"\n", | ||
" generate random variables (u,v)\n", | ||
" input: n: number of random copula to be generated\n", | ||
" output: u and v: generated copula\n", | ||
" \"\"\"\n", | ||
" #Clayton copula\n", | ||
" \n", | ||
" if self.family == \"clayton\":\n", | ||
" u = np.random.uniform(size=n)\n", | ||
" w = np.random.uniform(size = n)\n", | ||
" \n", | ||
" if self.theta <= -1:\n", | ||
" raise ValueError(\"the parameter for clayton copula shoud be more than -1)\n", | ||
" if self.theta == 0:\n", | ||
" raise ValueError('The parameter for clayton copula should not be 0')\n", | ||
" \n", | ||
" if self.theta < sys.float_info.epsilon:\n", | ||
" v=w\n", | ||
" else:\n", | ||
" v=u*(w**(-self.theta/(1+self.theta))-1+u**self.theta)**(-1/self.theta)\n", | ||
" \n", | ||
" # Frank Copula \n", | ||
" elif self.family == \"frank\":\n", | ||
" u = random.uniform(size=n)\n", | ||
" w = random.uniform(size=n)\n", | ||
" \n", | ||
" if self.theta == 0:\n", | ||
" raise ValueError(\"the parameter for frank copula \\\n", | ||
" should not be 0\")\n", | ||
" \n", | ||
" if abs(self.theta) > np.log(sys.float_info.max):\n", | ||
" v= ( u < 0) + np.sign(self.theta)*U\n", | ||
" elif abs(self.theta) > np.sqrt(sys.float_info.epsilon):\n", | ||
" " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 2", | ||
"name": "python2" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 2 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython2", | ||
"version": "2.7.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
Oops, something went wrong.