Created
May 3, 2025 07:30
-
-
Save Kouhei-Takagi/32391553dd7f0044c52e8bf59597bda7 to your computer and use it in GitHub Desktop.
correlation-features-USA4(corr=0.9645)
This file contains hidden or 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 pandas_datareader.data as web | |
| from datetime import datetime | |
| import pandas as pd | |
| start = datetime(2015, 6, 1) | |
| end = datetime(2025, 5, 1) | |
| # 月次データをFREDから取得 | |
| gold = web.DataReader("IR14270", "fred", start, end) | |
| zinc = web.DataReader("PZINCUSDM", "fred", start, end) | |
| copper = web.DataReader("PCOPPUSDM", "fred", start, end) | |
| wheat = web.DataReader("PWHEAMTUSDM", "fred", start, end) | |
| corn = web.DataReader("PMAIZMTUSDM", "fred", start, end) | |
| oil = web.DataReader("DCOILWTICO", "fred", start, end) | |
| uranium = web.DataReader("PURANUSDM", "fred", start, end) | |
| djia = web.DataReader("DJIA", "fred", start, end) | |
| # データ結合(列名変更) | |
| df = pd.concat([gold, zinc, copper, wheat, corn, oil, uranium, djia], axis=1).dropna() | |
| df.columns = ["Gold", "Zinc", "Copper", "Wheat", "Corn", "Oil", "Uranium", "DJIA"] | |
| # 合成価格を作成(単純合計) | |
| df["Combined_Price"] = df["Gold"] * 10 + (df["Copper"] - df["Zinc"]) * 0.75 - (df["Wheat"] + df["Corn"]) * 2.5 + (df["Oil"] - df["Uranium"]) * 30 | |
| # 相関係数を計算 | |
| corr = df["Combined_Price"].corr(df["DJIA"]) | |
| print("相関係数(合成商品価格 × DJIA):", corr) | |
| # 表示 | |
| print(df[["Combined_Price", "DJIA"]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment