b22ee075 commited on
Commit
a723fe7
·
verified ·
1 Parent(s): 98cf48c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -0
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import yfinance as yf
2
+ import numpy as np
3
+ import pandas as pd
4
+ import matplotlib.pyplot as plt
5
+ import gradio as gr
6
+
7
+ def get_data(symbol, start_date, end_date):
8
+ data = yf.download(symbol, start=start_date, end=end_date)
9
+ return data['Close']
10
+
11
+ def monte_carlo_simulation(data, num_simulations, num_days):
12
+ log_returns = np.log(1 + data.pct_change())
13
+ mean = log_returns.mean()
14
+ variance = log_returns.var()
15
+ drift = mean - (0.5 * variance)
16
+ stdev = log_returns.std()
17
+
18
+ daily_returns = np.exp(drift + stdev * np.random.normal(size=(num_days, num_simulations)))
19
+ price_paths = np.zeros_like(daily_returns)
20
+ price_paths[0] = data.iloc[-1]
21
+
22
+ for t in range(1, num_days):
23
+ price_paths[t] = price_paths[t - 1] * daily_returns[t]
24
+
25
+ return price_paths
26
+
27
+ def sigma_confidence_interval(price_paths, confidence_level=0.95):
28
+ final_prices = price_paths[-1]
29
+ lower_bound = np.percentile(final_prices, (1 - confidence_level) / 2 * 100)
30
+ upper_bound = np.percentile(final_prices, (1 + confidence_level) / 2 * 100)
31
+ return lower_bound, upper_bound
32
+
33
+ def plot_simulation(price_paths, title):
34
+ plt.figure(figsize=(10, 5))
35
+ plt.plot(price_paths)
36
+ plt.title(title)
37
+ plt.xlabel('Days')
38
+ plt.ylabel('Price')
39
+ plt.grid(True)
40
+ plt.show()
41
+
42
+ def plot_correlation(nifty_data, sensex_data):
43
+ correlation = nifty_data.corr(sensex_data)
44
+ plt.figure(figsize=(10, 5))
45
+ plt.scatter(nifty_data, sensex_data)
46
+ plt.title(f'Correlation between NIFTY and SENSEX: {correlation:.2f}')
47
+ plt.xlabel('NIFTY')
48
+ plt.ylabel('SENSEX')
49
+ plt.grid(True)
50
+ plt.show()
51
+
52
+ def simulate(start_date, end_date, num_simulations, num_days):
53
+ nifty_data = get_data('^NSEI', start_date, end_date)
54
+ sensex_data = get_data('^BSESN', start_date, end_date)
55
+
56
+ nifty_paths = monte_carlo_simulation(nifty_data, num_simulations, num_days)
57
+ sensex_paths = monte_carlo_simulation(sensex_data, num_simulations, num_days)
58
+ nifty_lower_bound, nifty_upper_bound = sigma_confidence_interval(nifty_paths)
59
+ sensex_lower_bound, sensex_upper_bound = sigma_confidence_interval(sensex_paths)
60
+
61
+ fig, axs = plt.subplots(1, 3, figsize=(40, 10))
62
+
63
+ for path in nifty_paths.T:
64
+ axs[0].plot(path)
65
+ axs[0].set_title('Monte Carlo Simulation - NIFTY')
66
+ axs[0].set_xlabel('Days')
67
+ axs[0].set_ylabel('Price')
68
+ axs[0].grid(True)
69
+
70
+ for path in sensex_paths.T:
71
+ axs[1].plot(path)
72
+ axs[1].set_title('Monte Carlo Simulation - SENSEX')
73
+ axs[1].set_xlabel('Days')
74
+ axs[1].set_ylabel('Price')
75
+ axs[1].grid(True)
76
+
77
+ correlation = nifty_data.corr(sensex_data)
78
+ axs[2].scatter(nifty_data, sensex_data)
79
+ axs[2].set_title(f'Correlation between NIFTY and SENSEX: {correlation:.2f}')
80
+ axs[2].set_xlabel('NIFTY')
81
+ axs[2].set_ylabel('SENSEX')
82
+ axs[2].grid(True)
83
+
84
+ plt.tight_layout()
85
+ return fig, f'NIFTY Sigma Confidence Interval at 0.95: [{nifty_lower_bound:.1f} : {nifty_upper_bound:.1f}], SENSEX Sigma Confidence Interval at 0.95: [{sensex_lower_bound:.1f} : {sensex_upper_bound:.1f}]'
86
+
87
+ def gradio_app(start_date, end_date, num_simulations, num_days):
88
+ fig, interval = simulate(start_date, end_date, num_simulations, num_days)
89
+ return fig, interval
90
+
91
+ iface = gr.Interface(
92
+ fn=gradio_app,
93
+ inputs=[
94
+ gr.Textbox(placeholder="YYYY-MM-DD", label="Start Date"),
95
+ gr.Textbox(placeholder="YYYY-MM-DD", label="End Date"),
96
+ gr.Slider(minimum=100, maximum=10000, step=100, label="Number of Simulations"),
97
+ gr.Slider(minimum=10, maximum=365, step=10, label="Number of Days for Forecast")
98
+ ],
99
+ outputs=[gr.Plot(label="Simulation Plots and Correlation Plot"), gr.Textbox(label="Sigma Confidence Interval")],
100
+ title="Monte Carlo Simulation for NIFTY and SENSEX"
101
+ )
102
+
103
+ iface.launch()