Skip to content

Instantly share code, notes, and snippets.

@chaoxu
Last active July 21, 2020 02:21
Show Gist options
  • Save chaoxu/d0f0a42b06e06fcac63c3ec26052e25e to your computer and use it in GitHub Desktop.
Save chaoxu/d0f0a42b06e06fcac63c3ec26052e25e to your computer and use it in GitHub Desktop.
Yotta quantiles
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
import math
N = 400 # number of tickets
i = 100000 # number of trials
k = 52 # number of weeks
rate = 0.002 # standard apr
M = N*25
elements = [0.1,0.15,0.75,10.0,0.4,15.0,0.0]
probabilities = [1.0/45.0, 1.0/73.0, 1.0/345.0,1.0/3935.0,1.0/165.0, 1/4518.0]
probabilities.append(1.0-sum(probabilities))
normalized_rate = (7.0*k)/365.0*rate
t = []
for i in range(i):
t.append(sum(np.random.choice(elements, N*k, p=probabilities)))
a = np.array([x/M + normalized_rate for x in t])
print(np.percentile(a, 1))
print(np.percentile(a, 10))
print(np.percentile(a, 50))
print(np.percentile(a, 90))
print(np.percentile(a, 99))
def to_percent(y, position):
s = str(math.floor(10000 * y)/100.0)
if plt.rcParams['text.usetex'] is True:
return s + r'$\%$'
else:
return s + '%'
# Generate data on commute times.
commutes = pd.Series(a)
commutes.plot.hist(grid=True, bins=30, rwidth=0.9,
color='#607c8e')
plt.title('APR for 100000 Trials')
formatter = FuncFormatter(to_percent)
plt.gca().xaxis.set_major_formatter(formatter)
plt.xlabel('APR')
plt.ylabel('Count')
plt.grid(axis='y', alpha=0)
plt.grid(axis='x', alpha=0)
plt.show()
plt.savefig('out.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment