Upload 3 files
Browse files- app.py +102 -0
- flips.pkl.gz +3 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
from matplotlib import pyplot as plt
|
4 |
+
import numpy as np
|
5 |
+
import pickle
|
6 |
+
import gzip
|
7 |
+
import seaborn as sns
|
8 |
+
sns.set_theme()
|
9 |
+
|
10 |
+
def flip(n, t, seed=13):
|
11 |
+
np.random.seed(seed)
|
12 |
+
flips = np.random.randint(0, 2, size=(n, t))
|
13 |
+
heads = flips.cumsum(axis=1)
|
14 |
+
return heads.astype(np.uint8)
|
15 |
+
|
16 |
+
def load(filename):
|
17 |
+
# Adapted from https://gist.github.com/thearn/5424244
|
18 |
+
file = gzip.GzipFile(filename, 'rb')
|
19 |
+
data = file.read()
|
20 |
+
object = pickle.loads(data)
|
21 |
+
file.close()
|
22 |
+
return object
|
23 |
+
|
24 |
+
def calc_outcome(p_win, win, loss, leverage):
|
25 |
+
n = 1000000
|
26 |
+
t = 60
|
27 |
+
base=100
|
28 |
+
# heads = flip(n, t)
|
29 |
+
heads = load('flips.pkl.gz')
|
30 |
+
tails = np.arange(t)+1 - heads
|
31 |
+
p_loss = 1 - p_win
|
32 |
+
log_win = np.log(1+leverage*win)
|
33 |
+
log_loss = np.log(1-leverage*loss)
|
34 |
+
log_outcome = heads*log_win + tails*log_loss
|
35 |
+
outcome = base*(np.exp(log_outcome))
|
36 |
+
return outcome
|
37 |
+
|
38 |
+
def process(p_win, win, loss, leverage):
|
39 |
+
outcome = calc_outcome(p_win/100., win/100., loss/100., leverage/100.)
|
40 |
+
ensemble = fig2img(plot_ensemble(outcome)[0])
|
41 |
+
time = fig2img(plot_time(outcome)[0])
|
42 |
+
richest = fig2img(plot_richest(outcome)[0])
|
43 |
+
return ensemble, time, richest
|
44 |
+
|
45 |
+
def fig2img(fig):
|
46 |
+
"""Convert a Matplotlib figure to a PIL Image and return it"""
|
47 |
+
import io
|
48 |
+
buf = io.BytesIO()
|
49 |
+
fig.savefig(buf)
|
50 |
+
buf.seek(0)
|
51 |
+
img = Image.open(buf)
|
52 |
+
return img
|
53 |
+
|
54 |
+
def plot_ensemble(outcome):
|
55 |
+
fig, ax = plt.subplots(1, 1, figsize=(4, 4))
|
56 |
+
ax.plot(outcome.mean(axis=0))
|
57 |
+
ax.set_xlabel('Time')
|
58 |
+
ax.set_ylabel('Average Wealth ($)')
|
59 |
+
ax.set_title('Ensemble (Collective) Perspective')
|
60 |
+
fig.tight_layout()
|
61 |
+
return fig, ax
|
62 |
+
|
63 |
+
def plot_richest(outcome):
|
64 |
+
fig, ax = plt.subplots(1, 1, figsize=(4, 4))
|
65 |
+
ax.plot(outcome.max(axis=0)/1e6)
|
66 |
+
ax.set_xlabel('Time')
|
67 |
+
ax.set_ylabel('Wealth ($ million)')
|
68 |
+
ax.set_title('Richest Individual')
|
69 |
+
fig.tight_layout()
|
70 |
+
return fig, ax
|
71 |
+
|
72 |
+
def plot_time(outcome):
|
73 |
+
fig, ax = plt.subplots(1, 1, figsize=(4, 4))
|
74 |
+
ax.plot(np.percentile(outcome, 50, axis=0))
|
75 |
+
ax.set_xlabel('Time')
|
76 |
+
ax.set_ylabel('Median Wealth ($)')
|
77 |
+
ax.set_title('Time (Individual) Perspective')
|
78 |
+
fig.tight_layout()
|
79 |
+
return fig, ax
|
80 |
+
|
81 |
+
css = ""
|
82 |
+
|
83 |
+
with gr.Blocks(css=css) as demo:
|
84 |
+
with gr.Column():
|
85 |
+
with gr.Row():
|
86 |
+
p_win = gr.Slider(0, 100, value=50, label='Win Prob.(%)', interactive=True)
|
87 |
+
with gr.Row():
|
88 |
+
win = gr.Number(value=50, label='Gain (%)', interactive=True)
|
89 |
+
loss = gr.Number(value=40, label='Loss (%)', interactive=True)
|
90 |
+
with gr.Row():
|
91 |
+
leverage = gr.Slider(0, 100, value=100, label='Leverage (%)', interactive=True)
|
92 |
+
with gr.Row():
|
93 |
+
btn = gr.Button('Go!')
|
94 |
+
with gr.Column(scale=1, min_width=900):
|
95 |
+
with gr.Row():
|
96 |
+
ensemble = gr.Image(label="Ensemble Perspective", shape=(300, 300), elem_id="plot_ensemble")
|
97 |
+
time = gr.Image(label="Time Perspective", shape=(300, 300), elem_id="plot_time")
|
98 |
+
richest = gr.Image(label="Richest Individual", shape=(300, 300), elem_id="plot_richest")
|
99 |
+
|
100 |
+
btn.click(process, [p_win, win, loss, leverage], [ensemble, time, richest])
|
101 |
+
|
102 |
+
demo.launch()
|
flips.pkl.gz
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e5db71f8aef5f135390eb0875b3b8a164755af1571d2d2990ed0dd598366c70c
|
3 |
+
size 1554099
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
pillow
|
3 |
+
matplotlib
|
4 |
+
numpy
|
5 |
+
seaborn
|