Advertisement
Danila_lipatov

kop-7

Apr 17th, 2024
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. import pandas as pd
  2. import yfinance as yf
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. from scipy.interpolate import interp1d
  6. from sklearn.linear_model import LinearRegression
  7.  
  8. # Функция Вейерштрасса-Мандельброта C(t)
  9. def C(t, b=0.5, D=1.8):
  10.     j_max = 100
  11.     result = 0
  12.     for j in range(-j_max, j_max + 1):
  13.         result += (1 - np.cos(b ** j * t)) / (b ** (j * (2 - D)))
  14.     return result
  15.  
  16. # Создаем временной ряд t с шагом h
  17. N = 600
  18. t = np.linspace(0, 1, N)
  19. # Загружаем данные из CSV файла
  20. currency_df = pd.read_csv('/Users/danilalipatov/Downloads/Прошлые данные - AFLT.csv', index_col=0, parse_dates=True)
  21.  
  22. # Интерполируем курс валюты для получения N точек
  23. for ind in currency_df['Макс.'].index:
  24.     currency_df.at[ind, 'Макс.'] = float(currency_df['Макс.'][ind].replace(',', '.'))
  25. f = interp1d(np.linspace(0, 1, len(currency_df)), currency_df['Макс.'])
  26. interpolated_currency = f(np.linspace(0, 1, N))
  27.  
  28. # Нормируем значения курса валюты
  29. normalized_currency = (interpolated_currency - np.min(interpolated_currency)) / (np.max(interpolated_currency) - np.min(interpolated_currency))
  30.  
  31. # Применяем линейную регрессию для выделения линейного тренда курса валюты
  32. reg = LinearRegression().fit(np.linspace(0, 1, N).reshape(-1, 1), normalized_currency.reshape(-1, 1))
  33. trend_currency = reg.predict(np.linspace(0, 1, N).reshape(-1, 1)).reshape(-1)
  34.  
  35. # Убираем линейный тренд из курса валюты
  36. detrended_currency = normalized_currency - trend_currency
  37. b = 3.5
  38. D = 1.4
  39. # Вычисляем значения функции C(t)
  40. C_values = np.array([C(t_i, b, D) for t_i in t])
  41.  
  42. # Нормируем значения функции C(t)
  43. C_values_normalized = (C_values - np.min(C_values)) / (np.max(C_values) - np.min(C_values))
  44.  
  45. # Применяем линейную регрессию для выделения линейного тренда функции C(t)
  46. reg = LinearRegression().fit(t.reshape(-1, 1), C_values.reshape(-1, 1))
  47. trend_C = reg.predict(t.reshape(-1, 1)).reshape(-1)
  48.  
  49. # Убираем линейный тренд из функции C(t)
  50. detrended_C = C_values - trend_C
  51.  
  52. #  b   D
  53. # 1.1 1.1
  54. # 1.1 1.95
  55. # Строим график курса EUR/USD без тренда и функции C(t) без тренда на одном рисунке
  56. plt.figure(figsize=(10, 6))
  57. plt.plot(t, detrended_currency, label='Detrended Aeroflot')
  58. plt.plot(t, detrended_C, label='Detrended C(t)')
  59. plt.title('Detrended Aeroflot Exchange Rate and Function C(t)')
  60. plt.xlabel('Time')
  61. plt.ylabel('Detrended Normalized Value')
  62. plt.legend()
  63. plt.grid(True)
  64. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement