Created
June 12, 2020 10:35
-
-
Save lanthias/5a41c1e4b21ae274ddb95cf5ad10b4ac to your computer and use it in GitHub Desktop.
Altair Finance Plot
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 is a variable forecast for modelling different growth-rates. | |
# Initial growth rate is set to be the current growth rate | |
# This cannot be 0, as growth rate wouldn't work | |
start_revenue = INITIAL_REVENUE if INITIAL_REVENUE > 100 else 100 | |
scale = (-CURRENT_CASH_GBP, CURRENT_CASH_GBP * 2) | |
monthly_growth_percent = WEEKLY_GROWTH_PERCENT * 4 | |
slider = alt.binding_range(min=0, max=100, step=0.1, name='Monthly growth rate (%)') | |
selector = alt.selection_single(fields=['month_growth'], bind=slider, init={'month_growth': monthly_growth_percent}) | |
base = alt.Chart(df[['date', 'cash_0g']]).transform_window( | |
# Use count() to find out the current month we're on (i.e. the index), as we use that as the power for our growth rate | |
index='count()' | |
).transform_calculate( | |
# Transform a % growth rate (i.e. 7) into sometime useful (i.e. 1.07). | |
# Raise that to our current month (i.e. for month 2, 1.07 ** 2 = ~1.145), and multiply that by our revenue amount (~1.145 * 500) | |
forecast_revenue = ((1+(selector.month_growth / 100)) ** alt.datum.index) * start_revenue | |
).transform_window( | |
# Take what is in practice the cumulative product of revenue (i.e. total revenue earned so far) | |
forecast_total_revenue = 'sum(forecast_revenue)', frame=[None, 0] | |
).transform_calculate( | |
# Add cumprod to current cash to find cash standing (i.e. if we have £100k cash, and 10 months of £10k revenue, we have £200k now) | |
adj_cash = alt.datum.cash_0g + alt.datum.forecast_total_revenue | |
).encode(x='yearmonth(date):T') | |
cash = base.encode(y=alt.Y('adj_cash:Q', scale=alt.Scale(domain=scale))).mark_area(opacity=0.75) | |
revenue = base.encode(y='forecast_total_revenue:Q').mark_line(color='#f26522', size=5) | |
revenue_growth = base.encode(y='forecast_revenue:Q').mark_line(color='purple', size=5) | |
charts = cash + revenue + revenue_growth | |
chart_final = charts.add_selection(selector).interactive().properties(width=850) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment