Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,7 +35,6 @@ def calculate_percentiles(simulations):
|
|
| 35 |
percentiles = np.percentile(simulations, [2.5, 16, 50, 84, 97.5], axis=0)
|
| 36 |
return percentiles
|
| 37 |
|
| 38 |
-
# Plot distributions
|
| 39 |
def plot_distributions(bootstrap_simulations, data, thresholds, bootstrap_probabilities):
|
| 40 |
final_bootstrap_prices = bootstrap_simulations[:, -1]
|
| 41 |
|
|
@@ -50,37 +49,54 @@ def plot_distributions(bootstrap_simulations, data, thresholds, bootstrap_probab
|
|
| 50 |
# Plot for Bootstrapping
|
| 51 |
fig.add_trace(go.Histogram(x=final_bootstrap_prices, nbinsx=50, name='Simulated Final Prices',
|
| 52 |
marker_color='blue', opacity=0.7))
|
| 53 |
-
fig.add_vline(x=mean_bootstrap_price, line=dict(color='red', dash='dash'), name=f'Mean: {mean_bootstrap_price:.2f}')
|
| 54 |
-
fig.add_vline(x=median_bootstrap_price, line=dict(color='orange', dash='dash'), name=f'Median: {median_bootstrap_price:.2f}')
|
| 55 |
-
fig.add_vline(x=latest_price, line=dict(color='green', dash='dash'), name=f'Latest Price: {latest_price:.2f}')
|
| 56 |
-
fig.add_vrect(x0=ci_68_bootstrap[0], x1=ci_68_bootstrap[1], fillcolor='yellow', opacity=0.2, layer="below", line_width=0, annotation_text="68% CI", annotation_position="top left")
|
| 57 |
-
fig.add_vrect(x0=ci_95_bootstrap[0], x1=ci_95_bootstrap[1], fillcolor='grey', opacity=0.2, layer="below", line_width=0, annotation_text="95% CI", annotation_position="top left")
|
| 58 |
-
|
| 59 |
-
max_freq = np.histogram(final_bootstrap_prices, bins=50)[0].max()
|
| 60 |
-
|
| 61 |
-
# Calculate positions based on a fraction of the max frequency
|
| 62 |
-
mean_y_pos = max_freq * 0.9
|
| 63 |
-
median_y_pos = max_freq * 0.7
|
| 64 |
-
latest_y_pos = max_freq * 0.5
|
| 65 |
-
|
| 66 |
-
# Annotations for the vertical lines
|
| 67 |
-
fig.add_annotation(x=mean_bootstrap_price, y=mean_y_pos, text=f'Mean: {mean_bootstrap_price:.2f}', showarrow=False)
|
| 68 |
-
fig.add_annotation(x=median_bootstrap_price, y=median_y_pos, text=f'Median: {median_bootstrap_price:.2f}', showarrow=False)
|
| 69 |
-
fig.add_annotation(x=latest_price, y=latest_y_pos, text=f'Latest: {latest_price:.2f}', showarrow=False)
|
| 70 |
-
|
| 71 |
-
fig.add_vline(x=mean_bootstrap_price, line=dict(color='red', dash='dash'), name='Mean', showlegend=True)
|
| 72 |
-
fig.add_vline(x=median_bootstrap_price, line=dict(color='orange', dash='dash'), name='Median', showlegend=True)
|
| 73 |
-
fig.add_vline(x=latest_price, line=dict(color='green', dash='dash'), name='Latest Price', showlegend=True)
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
textstr = f'P(>{thresholds[1]:.2f}): {bootstrap_probabilities["above"]:.2%}<br>' + \
|
| 76 |
f'P(<{thresholds[0]:.2f}): {bootstrap_probabilities["below"]:.2%}<br>' + \
|
| 77 |
f'P({thresholds[0]:.2f} - {thresholds[1]:.2f}): {bootstrap_probabilities["between"]:.2%}'
|
| 78 |
-
fig.add_annotation(xref='paper', yref='paper', x=0.98, y=0.02, text=textstr, showarrow=False,
|
| 79 |
bordercolor="black", borderwidth=1, borderpad=4, bgcolor="white", opacity=0.4, font=dict(color="black"))
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
return fig, mean_bootstrap_price, median_bootstrap_price, ci_68_bootstrap, ci_95_bootstrap, latest_price
|
| 83 |
|
|
|
|
| 84 |
# Plot price data with simulation cones
|
| 85 |
def plot_price_with_cones(data, bootstrap_percentiles, days, thresholds, bootstrap_probabilities):
|
| 86 |
last_date = data.index[-1]
|
|
|
|
| 35 |
percentiles = np.percentile(simulations, [2.5, 16, 50, 84, 97.5], axis=0)
|
| 36 |
return percentiles
|
| 37 |
|
|
|
|
| 38 |
def plot_distributions(bootstrap_simulations, data, thresholds, bootstrap_probabilities):
|
| 39 |
final_bootstrap_prices = bootstrap_simulations[:, -1]
|
| 40 |
|
|
|
|
| 49 |
# Plot for Bootstrapping
|
| 50 |
fig.add_trace(go.Histogram(x=final_bootstrap_prices, nbinsx=50, name='Simulated Final Prices',
|
| 51 |
marker_color='blue', opacity=0.7))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
# Replace add_vline with add_trace for vertical lines with legends
|
| 54 |
+
fig.add_trace(go.Scatter(
|
| 55 |
+
x=[mean_bootstrap_price, mean_bootstrap_price],
|
| 56 |
+
y=[0, np.histogram(final_bootstrap_prices, bins=50)[0].max()],
|
| 57 |
+
mode='lines',
|
| 58 |
+
line=dict(color='red', dash='dash'),
|
| 59 |
+
name=f'Mean: {mean_bootstrap_price:.2f}'
|
| 60 |
+
))
|
| 61 |
+
|
| 62 |
+
fig.add_trace(go.Scatter(
|
| 63 |
+
x=[median_bootstrap_price, median_bootstrap_price],
|
| 64 |
+
y=[0, np.histogram(final_bootstrap_prices, bins=50)[0].max()],
|
| 65 |
+
mode='lines',
|
| 66 |
+
line=dict(color='orange', dash='dash'),
|
| 67 |
+
name=f'Median: {median_bootstrap_price:.2f}'
|
| 68 |
+
))
|
| 69 |
+
|
| 70 |
+
fig.add_trace(go.Scatter(
|
| 71 |
+
x=[latest_price, latest_price],
|
| 72 |
+
y=[0, np.histogram(final_bootstrap_prices, bins=50)[0].max()],
|
| 73 |
+
mode='lines',
|
| 74 |
+
line=dict(color='green', dash='dash'),
|
| 75 |
+
name=f'Latest Price: {latest_price:.2f}'
|
| 76 |
+
))
|
| 77 |
+
|
| 78 |
+
# Add confidence intervals as vertical shaded regions
|
| 79 |
+
fig.add_vrect(x0=ci_68_bootstrap[0], x1=ci_68_bootstrap[1], fillcolor='yellow', opacity=0.2, layer="below", line_width=0)
|
| 80 |
+
fig.add_vrect(x0=ci_95_bootstrap[0], x1=ci_95_bootstrap[1], fillcolor='grey', opacity=0.2, layer="below", line_width=0)
|
| 81 |
+
|
| 82 |
+
# Annotations for probabilities
|
| 83 |
textstr = f'P(>{thresholds[1]:.2f}): {bootstrap_probabilities["above"]:.2%}<br>' + \
|
| 84 |
f'P(<{thresholds[0]:.2f}): {bootstrap_probabilities["below"]:.2%}<br>' + \
|
| 85 |
f'P({thresholds[0]:.2f} - {thresholds[1]:.2f}): {bootstrap_probabilities["between"]:.2%}'
|
| 86 |
+
fig.add_annotation(xref='paper', yref='paper', x=0.98, y=0.02, text=textstr, showarrow=False,
|
| 87 |
bordercolor="black", borderwidth=1, borderpad=4, bgcolor="white", opacity=0.4, font=dict(color="black"))
|
| 88 |
|
| 89 |
+
# Update layout
|
| 90 |
+
fig.update_layout(
|
| 91 |
+
title='Bootstrapping Simulation',
|
| 92 |
+
xaxis_title='Final Price',
|
| 93 |
+
yaxis_title='Frequency',
|
| 94 |
+
showlegend=True
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
return fig, mean_bootstrap_price, median_bootstrap_price, ci_68_bootstrap, ci_95_bootstrap, latest_price
|
| 98 |
|
| 99 |
+
|
| 100 |
# Plot price data with simulation cones
|
| 101 |
def plot_price_with_cones(data, bootstrap_percentiles, days, thresholds, bootstrap_probabilities):
|
| 102 |
last_date = data.index[-1]
|