Created
January 15, 2021 20:32
-
-
Save matthiasdv/03430b5d1609857d7b482c6e26386516 to your computer and use it in GitHub Desktop.
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
import panel | |
import pandas as pd | |
from matplotlib.figure import Figure | |
from matplotlib.backends.backend_agg import FigureCanvas | |
returns_opts = [ | |
("0/100", 0.013), | |
("20/80", 0.017), | |
("30/70", 0.019), | |
("40/60", 0.022), | |
("50/50", 0.024), | |
("60/40", 0.026), | |
("70/30", 0.028), | |
("80/20", 0.030), | |
("100/0", 0.035), | |
] | |
savings_opts = [0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50] | |
def get_return_series(deposit, rate, period): | |
deposit = [deposit] * period | |
rate = [rate] * period | |
df = pd.DataFrame({"deposit": deposit, "rate": rate}) | |
df["interest"] = df.deposit * df.rate | |
# does not account for initial balance | |
# initial balance always 0 | |
df["total"] = ( | |
df["deposit"] * df["rate"].shift().add(1).cumprod().fillna(1) | |
).cumsum() | |
return df | |
def mpl_plot(df_a, df_b): | |
fig = Figure() | |
FigureCanvas(fig) # not needed in mpl >= 3.1 | |
ax = fig.add_subplot() | |
df_a.plot(ax=ax, label="alice") | |
df_b.plot(ax=ax, label="bob") | |
fig.legend(loc="upper right") | |
return fig | |
def calculate_data( | |
view_fn=mpl_plot, | |
savings_alice=0.15, | |
returns_alice=0.04, | |
savings_bob=0.05, | |
returns_bob=0.04, | |
period=20, | |
): | |
df_one = pd.DataFrame(get_return_series(savings_alice, returns_alice, period)) | |
df_two = pd.DataFrame(get_return_series(savings_bob, returns_bob, period)) | |
return view_fn(df_one["total"], df_two["total"]) | |
savings_alice = panel.widgets.FloatSlider(name="savings") | |
w_alice_deposit = panel.widgets.FloatSlider( | |
name="Savings", start=0.05, end=3, step=0.05, value=0.25 | |
) | |
w_alice_rate = panel.widgets.DiscreteSlider( | |
name="Return", options=dict(returns_opts), value=0.035 | |
) | |
box_alice = panel.WidgetBox("# Alice", w_alice_deposit, w_alice_rate) | |
w_bob_deposit = panel.widgets.FloatSlider( | |
name="Savings", start=0.05, end=3, step=0.05, value=0.15 | |
) | |
w_bob_rate = panel.widgets.DiscreteSlider( | |
name="Return", options=dict(returns_opts), value=0.035 | |
) | |
box_bob = panel.WidgetBox("# Bob", w_bob_deposit, w_bob_rate) | |
w_period = panel.widgets.IntSlider(name="Period", start=5, end=100, step=1, value=25) | |
def mpl_plot_mod(df_a, df_b): | |
fig = Figure() | |
FigureCanvas(fig) # not needed in mpl >= 3.1 | |
ax = fig.add_subplot() | |
df_a.plot(ax=ax, label="alice") | |
df_b.plot(ax=ax, label="bob") | |
ax.axhline(y=25, color="red", label="25x savings") | |
fig.legend(loc="upper right") | |
return fig | |
@panel.depends( | |
w_alice_deposit.param.value, | |
w_alice_rate.param.value, | |
w_bob_deposit.param.value, | |
w_bob_rate.param.value, | |
w_period.param.value, | |
) | |
def calculate_data_mod( | |
savings_alice, returns_alice, savings_bob, returns_bob, period, view_fn=mpl_plot_mod | |
): | |
df_one = pd.DataFrame(get_return_series(savings_alice, returns_alice, period)) | |
df_two = pd.DataFrame(get_return_series(savings_bob, returns_bob, period)) | |
return view_fn(df_one["total"], df_two["total"]) | |
dashboard = panel.Row( | |
panel.Column( | |
panel.Row(box_alice), | |
panel.Row(box_bob), | |
panel.Row(w_period), | |
), | |
panel.Column( | |
calculate_data_mod, | |
), | |
) | |
panel.serve(dashboard) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment