Created
April 6, 2024 09:24
-
-
Save Kouhei-Takagi/6efaa2dd834a1ff9aea3138a50fc9b15 to your computer and use it in GitHub Desktop.
Nasdaq vs Cacao * (Copper + Silver + Gold) - Sugar * Coffee (Corr. 0.8305)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import yfinance as yf | |
import pandas as pd | |
from scipy.optimize import minimize | |
from scipy.stats import pearsonr | |
class Data: | |
def __init__(self, symbol, start_date, end_date): | |
self.symbol = symbol | |
self.start_date = start_date | |
self.end_date = end_date | |
def get_data(self): | |
data = yf.download(self.symbol, start=self.start_date, end=self.end_date) | |
return data | |
# カカオのデータの取得 | |
cacao_instance = Data("CC=F", "1990-01-01", "2024-03-31") | |
cacao_data = cacao_instance.get_data() | |
# 銅のデータの取得 | |
copper_instance = Data("HG=F", "1990-01-01", "2024-03-31") | |
copper_data = copper_instance.get_data() | |
# 銀のデータの取得 | |
silver_instance = Data("SI=F", "1990-01-01", "2024-03-31") | |
silver_data = silver_instance.get_data() | |
# 金のデータの取得 | |
gold_instance = Data("GC=F", "1990-01-01", "2024-03-31") | |
gold_data = gold_instance.get_data() | |
# 砂糖のデータの取得 | |
sugar_instance = Data("SB=F", "1990-01-01", "2024-03-31") | |
sugar_data = sugar_instance.get_data() | |
# コーヒーのデータの取得 | |
coffee_instance = Data("KC=F", "1990-01-01", "2024-03-31") | |
coffee_data = coffee_instance.get_data() | |
# ナスダックのデータの取得 | |
nasdaq_instance = Data("^IXIC", "1990-01-01", "2024-03-31") | |
nasdaq_data = nasdaq_instance.get_data() | |
# 終値の列のみを抽出してデータフレームに結合 | |
merged_data = pd.concat([ | |
cacao_data["Close"].rename("Cacao_Close"), | |
copper_data["Close"].rename("Copper_Close"), | |
silver_data["Close"].rename("Silver_Close"), | |
gold_data["Close"].rename("Gold_Close"), | |
sugar_data["Close"].rename("Sugar_Close"), | |
coffee_data["Close"].rename("Coffee_Close"), | |
nasdaq_data["Close"].rename("Nasdaq_Close") | |
], axis=1).dropna() | |
# 列名を変数として定義 | |
cacao_close_col = "Cacao_Close" | |
copper_close_col = "Copper_Close" | |
silver_close_col = "Silver_Close" | |
gold_close_col = "Gold_Close" | |
sugar_close_col = "Sugar_Close" | |
coffee_close_col = "Coffee_Close" | |
nasdaq_close_col = "Nasdaq_Close" | |
# 終値の列のみを抽出してデータフレームに結合 | |
merged_data = pd.concat([ | |
cacao_data["Close"].rename(cacao_close_col), | |
copper_data["Close"].rename(copper_close_col), | |
silver_data["Close"].rename(silver_close_col), | |
gold_data["Close"].rename(gold_close_col), | |
sugar_data["Close"].rename(sugar_close_col), | |
coffee_data["Close"].rename(coffee_close_col), | |
nasdaq_data["Close"].rename(nasdaq_close_col) | |
], axis=1).dropna() | |
# 目的関数の定義: 相関係数を最大化 | |
def objective(params): | |
a, p_a, b, p_b, c, p_c, d, p_d, e, p_e, f, p_f = params # 各商品価格に対する係数と冪乗 | |
# 混合要素の計算 | |
mixed_close = (merged_data['Cacao_Close'] * a) ** p_a * \ | |
((merged_data['Copper_Close'] * b) ** p_b + \ | |
(merged_data['Silver_Close'] * c) ** p_c + \ | |
(merged_data['Gold_Close'] * d) ** p_d) - \ | |
(merged_data['Sugar_Close'] * e) ** p_e * \ | |
(merged_data["Coffee_Close"] * f) ** p_f | |
# ナスダック終値との相関係数 | |
corr, _ = pearsonr(mixed_close, merged_data['Nasdaq_Close']) | |
return -corr # 最大化のための負の相関係数 | |
# 初期係数と冪乗 | |
initial_params = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] # 係数と冪乗の初期値 | |
# 最適化の実行 | |
result = minimize(objective, initial_params, method='BFGS') | |
# 結果の表示 | |
print(f"Optimized Coefficients and Powers: {result.x}") | |
print(f"Maximum Correlation: {-result.fun}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment