Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pull samples with a tweedie distribution using numpy

I'm trying to plot a CDF of random samples to compare to a target within a dataset that follows a tweedie distribution. I know the following code will pull random samples along a poisson distribution:

import numpy as np
import matplotlib.pyplot as plt

x_r = np.random.poisson(lam = coll_df['pure_premium'].mean(), size = len(coll_df['pure_premium'])).sort()

y_r = np.arange(1, len(x)+1)/len(x)

_ = plt.plot(x, y_r, color = 'red')

_ = plt.xlabel('Percent of Pure Premium')

_ = plt.ylabel('ECDF')

However, there is no tweedie distribution option on the random sampling. Anyone know how to hack this together?

like image 598
Jordan Avatar asked Sep 02 '25 10:09

Jordan


1 Answers

PyPI has a tweedie package. A minimal example drawing a sample would be:

import tweedie, seaborn as sns, matplotlib.pyplot as plt

tvs = tweedie.tweedie(mu=10, p=1.5, phi=20).rvs(100000)

sns.distplot(tvs)
plt.show()

The package's GitHub pages have a more fancy example. The package implements rv_continuous, so one gets a bunch of other functionality besides rvs(). Also, while there seems no nice online docs, help(tweedie.tweedie) gives lots of detail.

like image 154
Ulrich Stern Avatar answered Sep 04 '25 10:09

Ulrich Stern