/
desmitry
/
market-data-analysis
Обзор
Документация
Войти
/
desmitry
/
market-data-analysis
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/visualizer.py
57 строк
2 KB
desmitry
Create project
25 ноя 2025, 09:14
25 ноя 2025, 09:14
c2a71d5
Код
Авторство
О чём код?
import matplotlib.pyplot as plt import numpy as np from matplotlib.ticker import FuncFormatter, MultipleLocator from scipy.stats import gaussian_kde def plot_weekday_analysis(df, symbol): """Generates the KDE plot for weekdays (from your notebook).""" weekdays = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday") plt.style.use("dark_background") fig = plt.figure(figsize=(20, 15)) for weekday in range(5): data = df.loc[df.index.weekday == weekday] high_data = data["High Return"] low_data = np.abs(data["Low Return"]) # Create subplots dynamically ax_high = plt.subplot2grid(shape=(5, 2), loc=(weekday, 0)) ax_low = plt.subplot2grid(shape=(5, 2), loc=(weekday, 1)) for name, ax, returns in zip(["High", "Low"], [ax_high, ax_low], [high_data, low_data]): _setup_axis(ax) # Plot KDE positive_vals = [i for i in returns if i > 0] if len(positive_vals) > 5: p_zero = (len(returns) - len(positive_vals)) / len(returns) log_vals = np.log(positive_vals) kde = gaussian_kde(log_vals, bw_method="scott") support = np.linspace(1e-5, returns.max(), 1000) log_support = np.log(support) cdf = np.array([kde.integrate_box_1d(-np.inf, x) for x in log_support]) combined_cdf = p_zero + (1 - p_zero) * cdf # Plot CDF full_x = np.concatenate([[0], support]) full_y = np.concatenate([[p_zero], combined_cdf]) ax.plot(full_x, full_y, color="cyan") mean = np.mean(returns) std = np.std(returns) ax.axvline(mean, color="blue", linestyle=":", label="Mean") ax.axvline(mean + std, color="green", linestyle=":", label="1 Std") ax.set_title(f"{symbol} {weekdays[weekday]} {name} CDF") plt.tight_layout() plt.savefig("weekday_analysis.png") print("Analysis saved to weekday_analysis.png") def _setup_axis(ax): ax.xaxis.set_major_formatter(FuncFormatter(lambda x, pos: f"{x * 100:.2f}%")) ax.set_xlim(-0.001, 0.055) ax.set_ylim(0, 1) ax.grid(which="major", alpha=0.5)