I found the trend by this code. This trend is from Japanese Workers' Pay Day(Every 25th day in a month). '''python import yfinance as yf import matplotlib . pyplot as plt import pandas as pd import seaborn as sns # 分析期間の設定 dateStart = "2015-01-01" dateEnd = "2026-05-23" ticker = "JPY=X" # ドル円 # yfinanceから取得(仕様変更の警告回避のため auto_adjust を明示) df = yf . download ( ticker , start = dateStart , end = dateEnd , auto_adjust = False , progress = False ) # yfinanceの仕様変更(MultiIndex化)に対応して終値を安全に抽出 if isinstance ( df . columns , pd . MultiIndex ): close = df [ "Close" ][ ticker ].dropna() else : close = df [ "Close" ]. dropna () # 前日比の変化率(%)を計算 # ドル円の場合、プラス = ドル買い/円売り、マイナス = ドル売り/円買い daily_return = close . pct_change () * 100 # 分析用のDataFrameを作成 analysis_df = pd . DataFrame ({ "Return" : daily_return }) analysis_df . index = pd . to_datetime ( analysis_df . index ...
To Functional Programming with Python
I rewrote the previous code to below as functional Python.
Gemini(Google AI) helped me, and it taught me important things.
To convert from procedural to functional is difficult for me.
The convertion needs a lot of my brain resouces.
So from the beginning, I will try to functional programinng with Python...
import numpy as np
import matplotlib.pyplot as plt
# データ生成
x = np.linspace(-3.15, 3.15, 100)
def plot_waves(x, wave_func, title):
"""
指定された関数に基づいて複数の波を生成し、描画する関数。
Args:
x (np.array): x軸のデータ。
wave_func (function): 使用する波の関数 (np.sin または np.cos)。
title (str): グラフのタイトル。
"""
plt.figure(figsize=(8, 6))
base_wave = wave_func(x)
# 振幅を半分にし、正負を反転させて波を生成
current_wave = base_wave
for i in range(10):
if i % 2 == 0:
# 偶数番目: 正の波
plt.plot(x, current_wave, label=f'Wave {i+1}')
else:
# 奇数番目: 負の波
plt.plot(x, -current_wave, label=f'Wave {i+1}')
# 次の波の振幅を半分にする
current_wave /= 2
plt.xlabel("x")
plt.ylabel("y")
plt.title(title)
plt.legend()
plt.grid(True)
plt.show()
# Sin Waves
plot_waves(x, np.sin, "Sin Waves")
# Cos Waves
plot_waves(x, np.cos, "Cos Waves")
Comments
Post a Comment