-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from EconomicSL/master
Non-prpoportional
- Loading branch information
Showing
9 changed files
with
274 additions
and
11 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,39 @@ | ||
import scipy.stats | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from distributiontruncated import TruncatedDistWrapper | ||
from distributionreinsurance import ReinsuranceDistWrapper | ||
import pdb | ||
|
||
non_truncated_dist = scipy.stats.pareto(b=2, loc=0, scale=0.5) | ||
truncated_dist = TruncatedDistWrapper(lower_bound=0.6, upper_bound=1., dist=non_truncated_dist) | ||
reinsurance_dist = ReinsuranceDistWrapper(lower_bound=0.85, upper_bound=0.95, dist=truncated_dist) | ||
|
||
x1 = np.linspace(non_truncated_dist.ppf(0.01), non_truncated_dist.ppf(0.99), 100) | ||
x2 = np.linspace(truncated_dist.ppf(0.01), truncated_dist.ppf(1.), 100) | ||
x3 = np.linspace(reinsurance_dist.ppf(0.01), reinsurance_dist.ppf(1.), 100) | ||
x_val_1 = reinsurance_dist.lower_bound | ||
x_val_2 = truncated_dist.upper_bound - (reinsurance_dist.upper_bound - reinsurance_dist.lower_bound) | ||
x_val_3 = reinsurance_dist.upper_bound | ||
x_val_4 = truncated_dist.upper_bound | ||
|
||
fig, ax = plt.subplots(1, 1) | ||
ax.plot(x1, non_truncated_dist.pdf(x1), 'k-', lw=2, label='non-truncated pdf') | ||
ax.plot(x1, non_truncated_dist.cdf(x1), 'g-', lw=2, label='non-truncated cdf') | ||
ax.plot(x2, truncated_dist.pdf(x2), 'r-', lw=2, label='truncated pdf') | ||
ax.plot(x2, truncated_dist.cdf(x2), 'm-', lw=2, label='truncated cdf') | ||
ax.plot(x3, reinsurance_dist.pdf(x3), 'b-', lw=2, label='reinsurance pdf') | ||
ax.plot(x3, reinsurance_dist.cdf(x3), 'c-', lw=2, label='reinsurance cdf') | ||
ax.set_xlim(0.45, 1.25) | ||
ax.set_ylim(0, 5) | ||
ax.arrow(x_val_1, reinsurance_dist.cdf(x_val_1), x_val_3 - x_val_1, truncated_dist.cdf(x_val_3) - reinsurance_dist.cdf(x_val_1), head_width=0, head_length=0, fc='m', ec='m', ls=':') | ||
ax.arrow(x_val_2, reinsurance_dist.cdf(x_val_2), x_val_4 - x_val_2, truncated_dist.cdf(x_val_4) - reinsurance_dist.cdf(x_val_2), head_width=0, head_length=0, fc='m', ec='m', ls=':') | ||
ax.arrow(x_val_1, reinsurance_dist.pdf(x_val_1+0.00001), x_val_3 - x_val_1, truncated_dist.pdf(x_val_3) - reinsurance_dist.pdf(x_val_1+0.00001), head_width=0, head_length=0, fc='r', ec='r', ls=':') | ||
ax.arrow(x_val_2, reinsurance_dist.pdf(x_val_2), x_val_4 - x_val_2, truncated_dist.pdf(x_val_4) - reinsurance_dist.pdf(x_val_2), head_width=0, head_length=0, fc='r', ec='r', ls=':') | ||
sample = reinsurance_dist.rvs(size=100000) | ||
#sample = sample[sample < scipy.percentile(sample, 90)] | ||
ax.hist(sample, normed=True, histtype='stepfilled', alpha=0.4) | ||
ax.legend(loc='best', frameon=False) | ||
plt.show() | ||
|
||
pdb.set_trace() |
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,85 @@ | ||
import scipy.stats | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from math import ceil | ||
import scipy | ||
import pdb | ||
|
||
class ReinsuranceDistWrapper(): | ||
def __init__(self, dist, lower_bound=None, upper_bound=None): | ||
assert lower_bound is not None or upper_bound is not None | ||
self.dist = dist | ||
self.lower_bound = lower_bound | ||
self.upper_bound = upper_bound | ||
if lower_bound is None: | ||
self.lower_bound = -np.inf | ||
elif upper_bound is None: | ||
self.upper_bound = np.inf | ||
assert self.upper_bound > self.lower_bound | ||
self.redistributed_share = dist.cdf(upper_bound) - dist.cdf(lower_bound) | ||
|
||
|
||
def pdf(self, x): | ||
x = np.array(x, ndmin=1) | ||
r = map(lambda Y: self.dist.pdf(Y) if Y < self.lower_bound \ | ||
else np.inf if Y==self.lower_bound \ | ||
else self.dist.pdf(Y + self.upper_bound - self.lower_bound), x) | ||
r = np.array(list(r)) | ||
if len(r.flatten()) == 1: | ||
r = float(r) | ||
return r | ||
|
||
def cdf(self, x): | ||
x = np.array(x, ndmin=1) | ||
r = map(lambda Y: self.dist.cdf(Y) if Y < self.lower_bound \ | ||
else self.dist.cdf(Y + self.upper_bound - self.lower_bound), x) | ||
r = np.array(list(r)) | ||
if len(r.flatten()) == 1: | ||
r = float(r) | ||
return r | ||
|
||
def ppf(self, x): | ||
x = np.array(x, ndmin=1) | ||
assert (x >= 0).all() and (x <= 1).all() | ||
r = map(lambda Y: self.dist.ppf(Y) if Y <= self.dist.cdf(self.lower_bound) \ | ||
else self.dist.ppf(self.dist.cdf(self.lower_bound)) if Y <= self.dist.cdf(self.upper_bound) \ | ||
else self.dist.ppf(Y) - self.upper_bound + self.lower_bound, x) | ||
r = np.array(list(r)) | ||
if len(r.flatten()) == 1: | ||
r = float(r) | ||
return r | ||
|
||
def rvs(self, size=1): | ||
sample = self.dist.rvs(size=size) | ||
sample1 = sample[sample<=self.lower_bound] | ||
sample2 = sample[sample>self.lower_bound] | ||
sample3 = sample2[sample2>=self.upper_bound] | ||
sample2 = sample2[sample2<self.upper_bound] | ||
|
||
sample2 = np.ones(len(sample2)) * self.lower_bound | ||
sample3 = sample3 -self.upper_bound + self.lower_bound | ||
|
||
sample = np.append(np.append(sample1,sample2),sample3) | ||
return sample[:size] | ||
|
||
|
||
if __name__ == "__main__": | ||
non_truncated = scipy.stats.pareto(b=2, loc=0, scale=0.5) | ||
#truncated = ReinsuranceDistWrapper(lower_bound=0, upper_bound=1, dist=non_truncated) | ||
truncated = ReinsuranceDistWrapper(lower_bound=0.9, upper_bound=1.1, dist=non_truncated) | ||
|
||
x = np.linspace(non_truncated.ppf(0.01), non_truncated.ppf(0.99), 100) | ||
x2 = np.linspace(truncated.ppf(0.01), truncated.ppf(0.99), 100) | ||
|
||
fig, ax = plt.subplots(1, 1) | ||
ax.plot(x, non_truncated.pdf(x), 'k-', lw=2, label='non-truncated pdf') | ||
ax.plot(x2, truncated.pdf(x2), 'r-', lw=2, label='truncated pdf') | ||
ax.plot(x, non_truncated.cdf(x), 'b-', lw=2, label='non-truncated cdf') | ||
ax.plot(x2, truncated.cdf(x2), 'g-', lw=2, label='truncated cdf') | ||
sample = truncated.rvs(size=1000) | ||
sample = sample[sample < scipy.percentile(sample, 90)] | ||
ax.hist(sample, normed=True, histtype='stepfilled', alpha=0.4) | ||
ax.legend(loc='best', frameon=False) | ||
plt.show() | ||
|
||
#pdb.set_trace() |
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,68 @@ | ||
import scipy.stats | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from math import ceil | ||
import scipy.integrate | ||
|
||
class TruncatedDistWrapper(): | ||
def __init__(self, dist, lower_bound=0, upper_bound=1): | ||
self.dist = dist | ||
self.normalizing_factor = dist.cdf(upper_bound) - dist.cdf(lower_bound) | ||
self.lower_bound = lower_bound | ||
self.upper_bound = upper_bound | ||
assert self.upper_bound > self.lower_bound | ||
|
||
def pdf(self, x): | ||
x = np.array(x, ndmin=1) | ||
r = map(lambda Y: self.dist.pdf(Y) / self.normalizing_factor \ | ||
if (Y >= self.lower_bound and Y <= self.upper_bound) else 0, x) | ||
r = np.array(list(r)) | ||
if len(r.flatten()) == 1: | ||
r = float(r) | ||
return r | ||
|
||
def cdf(self, x): | ||
x = np.array(x, ndmin=1) | ||
r = map(lambda Y: 0 if Y < self.lower_bound else 1 if Y > self.upper_bound \ | ||
else (self.dist.cdf(Y) - self.dist.cdf(self.lower_bound))/ self.normalizing_factor, x) | ||
r = np.array(list(r)) | ||
if len(r.flatten()) == 1: | ||
r = float(r) | ||
return r | ||
|
||
def ppf(self, x): | ||
x = np.array(x, ndmin=1) | ||
assert (x >= 0).all() and (x <= 1).all() | ||
return self.dist.ppf(x * self.normalizing_factor + self.dist.cdf(self.lower_bound)) | ||
|
||
def rvs(self, size=1): | ||
init_sample_size = int(ceil(size / self.normalizing_factor * 1.1)) | ||
sample = self.dist.rvs(size=init_sample_size) | ||
sample = sample[sample>=self.lower_bound] | ||
sample = sample[sample<=self.upper_bound] | ||
while len(sample) < size: | ||
sample = np.append(sample, self.rvs(size - len(sample))) | ||
return sample[:size] | ||
|
||
def mean(self): | ||
mean_estimate, mean_error = scipy.integrate.quad(lambda Y: Y*self.pdf(Y), self.lower_bound, self.upper_bound) | ||
return mean_estimate | ||
|
||
if __name__ == "__main__": | ||
non_truncated = scipy.stats.pareto(b=2, loc=0, scale=0.5) | ||
truncated = TruncatedDistWrapper(lower_bound=0.55, upper_bound=1., dist=non_truncated) | ||
|
||
x = np.linspace(non_truncated.ppf(0.01), non_truncated.ppf(0.99), 100) | ||
x2 = np.linspace(truncated.ppf(0.01), truncated.ppf(0.99), 100) | ||
|
||
fig, ax = plt.subplots(1, 1) | ||
ax.plot(x, non_truncated.pdf(x), 'k-', lw=2, label='non-truncated pdf') | ||
ax.plot(x2, truncated.pdf(x2), 'r-', lw=2, label='truncated pdf') | ||
ax.plot(x, non_truncated.cdf(x), 'b-', lw=2, label='non-truncated cdf') | ||
ax.plot(x2, truncated.cdf(x2), 'g-', lw=2, label='truncated cdf') | ||
sample = truncated.rvs(size=1000) | ||
ax.hist(sample, normed=True, histtype='stepfilled', alpha=0.4) | ||
ax.legend(loc='best', frameon=False) | ||
plt.show() | ||
|
||
print(truncated.mean()) |
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
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
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
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
Oops, something went wrong.