Mounted at /content/drive
Overall Pearson Correlation with Close_TAIEX:
Pearson Correlation P-value
RSI21 0.185104 2.388407e-245
RSI14 0.151384 6.581929e-165
CMO14 0.098249 4.022700e-70
Aroon Up 0.095180 3.432801e-65
RSI7 0.094756 9.565736e-66
CCI20 0.087107 3.717489e-55
Signal Line 0.083004 1.343307e-49
MACD Line 0.082689 1.358365e-49
%D 0.066708 3.890501e-33
WILLR14 0.063084 7.404786e-30
%K 0.063084 7.404786e-30
Market Return 0.053582 3.279364e-22
Adj_Close 0.040832 1.508200e-13
Lower Band 0.025671 4.182478e-06
Stock Return 0.024979 6.320338e-06
Low 0.022923 3.390795e-05
Close_MSCI 0.022754 3.870897e-05
MA100 0.022563 9.995018e-05
MA7 0.022436 5.204963e-05
MA50 0.022385 7.611907e-05
Open 0.022309 5.473519e-05
High 0.022106 6.395461e-05
Middle Band 0.021825 9.140844e-05
MA21 0.021806 9.338782e-05
Upper Band 0.018484 9.214753e-04
MACD Histogram 0.012701 2.371481e-02
Volume -0.014191 1.028059e-02
Band Width -0.022150 7.169296e-05
Aroon Down -0.084144 2.690046e-51
Beta_120 -0.161654 1.405573e-169
Beta_60 -0.183652 1.560559e-232
Stock Pearson Correlation with Close_TAIEX:
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-aa5f9275-976a-47b8-a5ea-f7568198c188.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-dfd2d46a-12d8-43be-a185-8ff134284987.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-7560edf8-db58-4980-9685-41954d59ad06.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-11c87185-6d9e-4760-b825-da40f4332857.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-c671e2a0-ce3b-43b2-ae54-843aed37ed38.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-b3c67fed-fc09-40c6-9fef-5b3d60e434a2.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-cb5c47ad-8836-4fd4-abc6-daed6bf0acef.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-8af3ca64-2dc6-4eec-8470-899b281f424f.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-08e55755-f228-4ac5-b251-a4984a6792ca.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-e739ffed-bdcc-48d6-929d-1d4f63737b43.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-99ee7f73-d268-4fd3-9035-f234b76f241f.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-9106267b-7404-4c27-b33f-5f3a7f6fcfef.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-5082e803-efe6-4a43-8f01-5189883ae8b4.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-4f3c73ce-5cf1-4279-b4e2-db5fbab32216.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-824cb7c9-17d0-4ec3-b772-83436a703ba9.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-0ae5735c-fd00-4891-a4b9-51f7e58e53a8.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-38a03a6a-b497-4ddc-aa31-3fe8ada1b0a6.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-3e6675b0-8def-4cb2-b6e2-1f6066a17554.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-f5d4f4ad-c236-4f6a-9d23-613f4da338b0.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-6bfa5e95-687e-47cb-ae07-597d20b27eab.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-a412648c-9d4d-4a34-a19e-02b3fa2d979b.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-81847450-1a72-4263-9dd8-d8cc7af66221.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-3961b5f2-b98b-44cf-87e3-1a75e24e66c3.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-4e389861-0965-4249-a8e9-ce06c77d3d79.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-246d59f7-4fc6-427d-b777-284e5240dcdb.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-98c05f29-7a95-4964-9519-0ee5399912af.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-7a2408e3-c04c-4fae-954d-f581bf2a93ff.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-f86d1baa-16c0-4afe-998e-5b1eee615cac.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-cb4d59c1-0792-4588-b64d-bf29d362ae4e.png)
![](https://thepearl.ghost.io/content/images/2024/06/data-src-image-951e4a49-818d-4d2d-9b28-e706117623fb.png)
Code
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import pearsonr
import statsmodels.api as sm
# Mount Google Drive
from google.colab import drive
drive.mount('/content/drive', force_remount=True)
# Load the data
file_path = '/content/drive/My Drive/MSCI_Taiwan_30_data.csv'
data = pd.read_csv(file_path)
# Convert 'Date' column to datetime
data['Date'] = pd.to_datetime(data['Date'], format='%Y/%m/%d')
# Define the target variable
target_variable = 'Close_TAIEX'
# Function to create scatter plot matrix by variable
def scatter_plot_matrix_by_variable():
variables = [col for col in data.columns if col not in ['Date', 'ST_Code', 'ST_Name', target_variable]]
significant_variables = []
# Identify significant variables
for variable in variables:
for stock_code in data['ST_Code'].unique():
stock_data = data[data['ST_Code'] == stock_code]
taiex_data = data[['Date', target_variable]].drop_duplicates()
aligned_data = pd.merge(stock_data, taiex_data, on='Date', suffixes=('', '_TAIEX'))
aligned_data = aligned_data.replace([np.inf, -np.inf], np.nan).dropna()
if aligned_data.shape[0] > 0 and variable in aligned_data.columns:
correlation, p_value = pearsonr(aligned_data[target_variable], aligned_data[variable])
if p_value <= 0.05:
significant_variables.append(variable)
break
significant_variables = list(set(significant_variables)) # Remove duplicates
# Generate scatter plot matrix for each significant variable
for variable in significant_variables:
plt.figure(figsize=(25, 25))
unique_stock_codes = data['ST_Code'].unique()
for i, stock_code in enumerate(unique_stock_codes):
stock_data = data[data['ST_Code'] == stock_code]
taiex_data = data[['Date', target_variable]].drop_duplicates()
aligned_data = pd.merge(stock_data, taiex_data, on='Date', suffixes=('', '_TAIEX'))
aligned_data = aligned_data.replace([np.inf, -np.inf], np.nan).dropna()
if aligned_data.shape[0] > 0 and variable in aligned_data.columns:
correlation, p_value = pearsonr(aligned_data[target_variable], aligned_data[variable])
plt.subplot((len(unique_stock_codes) + 4) // 5, 5, i + 1)
sns.scatterplot(x=aligned_data[target_variable], y=aligned_data[variable])
sns.regplot(x=aligned_data[target_variable], y=aligned_data[variable], scatter=False, color='red', ci=95, order=3, line_kws={'linestyle': 'dashed'})
stock_name = aligned_data['ST_Name'].iloc[0] # Get the stock name from the first row
plt.title(f'{stock_code} ({stock_name})\n(r={correlation:.2f}, p={p_value:.2e})')
plt.xlabel('TAIEX')
plt.ylabel(variable)
plt.suptitle(f'Scatter Plot Matrix for {variable}', fontsize=20)
plt.tight_layout(rect=[0, 0, 1, 0.95])
plt.savefig(f'/content/drive/My Drive/scatter_plot_matrix_{variable}.png')
plt.show()
# Generate scatter plot matrix by variable
scatter_plot_matrix_by_variable()