Spaces:
Runtime error
Runtime error
| import yfinance as yf | |
| import numpy as np | |
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| import gradio as gr | |
| def get_data(symbol, start_date, end_date): | |
| data = yf.download(symbol, start=start_date, end=end_date) | |
| return data['Close'] | |
| def monte_carlo_simulation(data, num_simulations, num_days): | |
| log_returns = np.log(1 + data.pct_change()) | |
| mean = log_returns.mean() | |
| variance = log_returns.var() | |
| drift = mean - (0.5 * variance) | |
| stdev = log_returns.std() | |
| daily_returns = np.exp(drift + stdev * np.random.normal(size=(num_days, num_simulations))) | |
| price_paths = np.zeros_like(daily_returns) | |
| price_paths[0] = data.iloc[-1] | |
| for t in range(1, num_days): | |
| price_paths[t] = price_paths[t - 1] * daily_returns[t] | |
| return price_paths | |
| def sigma_confidence_interval(price_paths, confidence_level=0.95): | |
| final_prices = price_paths[-1] | |
| lower_bound = np.percentile(final_prices, (1 - confidence_level) / 2 * 100) | |
| upper_bound = np.percentile(final_prices, (1 + confidence_level) / 2 * 100) | |
| return lower_bound, upper_bound | |
| def plot_simulation(price_paths, title): | |
| plt.figure(figsize=(10, 5)) | |
| plt.plot(price_paths) | |
| plt.title(title) | |
| plt.xlabel('Days') | |
| plt.ylabel('Price') | |
| plt.grid(True) | |
| plt.show() | |
| def plot_correlation(nifty_data, sensex_data): | |
| correlation = nifty_data.corr(sensex_data) | |
| plt.figure(figsize=(10, 5)) | |
| plt.scatter(nifty_data, sensex_data) | |
| plt.title(f'Correlation between NIFTY and SENSEX: {correlation:.2f}') | |
| plt.xlabel('NIFTY') | |
| plt.ylabel('SENSEX') | |
| plt.grid(True) | |
| plt.show() | |
| def simulate(start_date, end_date, num_simulations, num_days): | |
| nifty_data = get_data('^NSEI', start_date, end_date) | |
| sensex_data = get_data('^BSESN', start_date, end_date) | |
| nifty_paths = monte_carlo_simulation(nifty_data, num_simulations, num_days) | |
| sensex_paths = monte_carlo_simulation(sensex_data, num_simulations, num_days) | |
| nifty_lower_bound, nifty_upper_bound = sigma_confidence_interval(nifty_paths) | |
| sensex_lower_bound, sensex_upper_bound = sigma_confidence_interval(sensex_paths) | |
| fig, axs = plt.subplots(1, 3, figsize=(40, 10)) | |
| for path in nifty_paths.T: | |
| axs[0].plot(path) | |
| axs[0].set_title('Monte Carlo Simulation - NIFTY') | |
| axs[0].set_xlabel('Days') | |
| axs[0].set_ylabel('Price') | |
| axs[0].grid(True) | |
| for path in sensex_paths.T: | |
| axs[1].plot(path) | |
| axs[1].set_title('Monte Carlo Simulation - SENSEX') | |
| axs[1].set_xlabel('Days') | |
| axs[1].set_ylabel('Price') | |
| axs[1].grid(True) | |
| correlation = nifty_data.corr(sensex_data) | |
| axs[2].scatter(nifty_data, sensex_data) | |
| axs[2].set_title(f'Correlation between NIFTY and SENSEX: {correlation:.2f}') | |
| axs[2].set_xlabel('NIFTY') | |
| axs[2].set_ylabel('SENSEX') | |
| axs[2].grid(True) | |
| plt.tight_layout() | |
| 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}]' | |
| def gradio_app(start_date, end_date, num_simulations, num_days): | |
| fig, interval = simulate(start_date, end_date, num_simulations, num_days) | |
| return fig, interval | |
| iface = gr.Interface( | |
| fn=gradio_app, | |
| inputs=[ | |
| gr.Textbox(placeholder="YYYY-MM-DD", label="Start Date"), | |
| gr.Textbox(placeholder="YYYY-MM-DD", label="End Date"), | |
| gr.Slider(minimum=100, maximum=10000, step=100, label="Number of Simulations"), | |
| gr.Slider(minimum=10, maximum=365, step=10, label="Number of Days for Forecast") | |
| ], | |
| outputs=[gr.Plot(label="Simulation Plots and Correlation Plot"), gr.Textbox(label="Sigma Confidence Interval")], | |
| title="Monte Carlo Simulation for NIFTY and SENSEX" | |
| ) | |
| iface.launch() | |