Copulas
1
%matplotlib inline
2
3
import seaborn as sns
4
from scipy import stats
Copied!

Random Variable Transformation

1
# uniform sampling
2
x = stats.uniform(0, 1).rvs(10000)
3
sns.distplot(x, kde=False, norm_hist=True)
Copied!
png
1
# inverse CDF
2
norm = stats.norm()
3
x_trans = norm.ppf(x)
4
sns.distplot(x_trans)
Copied!
png
1
h = sns.jointplot(x, x_trans, stat_func=None)
2
h.set_axis_labels('original', 'transformed', fontsize=16)
Copied!
png
1
# same work for beta
2
beta = stats.beta(a=10, b=3)
3
x_trans = beta.ppf(x)
4
h = sns.jointplot(x, x_trans, stat_func=None)
5
h.set_axis_labels('original', 'transformed', fontsize=16)
Copied!
png
1
# same work for Gumbel
2
gumbel = stats.gumbel_l()
3
x_trans = gumbel.ppf(x)
4
h = sns.jointplot(x, x_trans, stat_func=None)
5
h.set_axis_labels('original', 'transformed', fontsize=16)
Copied!
png
1
# convert an arbitrary distribution to the uniform (0, 1): CDF
2
x_trans_trans = gumbel.cdf(x_trans)
3
h = sns.jointplot(x_trans, x_trans_trans, stat_func=None)
4
h.set_axis_labels('original', 'transformed', fontsize=16)
Copied!
png

Adding correlation with Gaussian copulas

1
mvnorm = stats.multivariate_normal(mean = [0, 0], cov = [[1., 0.5], [0.5, 1.]])
2
x = mvnorm.rvs(100000)
Copied!
1
h = sns.jointplot(x[:, 0], x[:, 1], kind = 'kde', stat_func = None)
2
h.set_axis_labels('x1', 'x2', fontsize=16)
Copied!
png
1
norm = stats.norm()
2
x_unif = norm.cdf(x)
3
h = sns.jointplot(x_unif[:, 0], x_unif[:, 1], kind='hex', stat_func=None)
4
h.set_axis_labels('Y1', 'Y2', fontsize=16)
Copied!
png
1
# transform the marginal
2
m1 = stats.gumbel_l()
3
m2 = stats.beta(a=10, b=2)
4
5
x1_trans = m1.ppf(x_unif[:, 0])
6
x2_trans = m2.ppf(x_unif[:, 1])
7
8
h = sns.jointplot(x1_trans, x2_trans, kind='kde', xlim=(-6, 2), ylim=(.6, 1.0), stat_func=None)
9
h.set_axis_labels('Maximum river level', 'Probability of flooding', fontsize=16)
Copied!
png
1
# compare with the joint distribution without correlation
2
x1 = m1.rvs(10000)
3
x2 = m2.rvs(10000)
4
5
h = sns.jointplot(x1, x2, kind='kde', xlim=(-6, 2), ylim=(.6, 1.0), stat_func=None)
6
h.set_axis_labels('Maximum river level', 'Probability of flooding', fontsize=16)
Copied!
png
1
Copied!
Last modified 2yr ago