import numpy as np
import pandas as pd
from scipy import stats

# 从表1.6中提取的数据
x1_data = [9.0, 8.5, 13.0, 10.0, 7.0, 15.5, 22.5, 5.5, 25.0, 15.0,
           12.5, 17.0, 16.0, 20.0, 12.0, 22.0, 17.0, 16.0, 21.0, 13.0,
           21.0, 21.0, 13.5, 5.0, 16.0, 14.5, 10.0, 11.0, 10.5, 15.0,
           9.0, 23.0, 14.0, 16.0, 16.5, 16.0, 12.0, 9.0, 12.0, 5.0,
           17.0, 16.0, 17.5, 11.5, 4.0, 17.5, 9.5, 26.0, 15.0, 19.0]

x2_data = [12.0, 15.0, 19.0, 7.0, 13.0, 28.5, 20.0, 8.5, 35.0, 19.0,
           20.0, 19.5, 17.5, 20.0, 17.0, 20.0, 28.0, 18.0, 27.5, 14.0,
           13.0, 6.0, 6.5, 7.5, 20.0, 14.5, 23.0, 13.0, 12.0, 15.5,
           12.5, 24.0, 21.0, 11.0, 17.0, 15.0, 15.5, 4.0, 6.0, 14.0,
           15.0, 11.0, 18.0, 15.0, 3.0, 15.0, 11.5, 38.0, 13.0, 12.0]

x3_data = [3.0, 3.0, 3.0, 4.0, 2.5, 5.0, 4.5, 3.0, 6.5, 4.0,
           3.0, 5.0, 6.0, 7.5, 4.0, 6.0, 5.5, 3.0, 6.0, 4.0,
           9.0, 3.5, 3.5, 3.5, 5.5, 4.0, 6.0, 6.0, 3.5, 3.0,
           5.0, 6.5, 6.5, 3.0, 4.0, 3.0, 3.5, 2.0, 5.0, 3.0,
           4.5, 3.0, 3.0, 3.0, 2.0, 4.5, 2.5, 4.0, 4.5, 3.0]

# 将数据转换为DataFrame
data = pd.DataFrame({
    'X1': x1_data,
    'X2': x2_data,
    'X3': x3_data
})

# 计算Pearson相关系数和对应的p值
pearson_corr = np.zeros((3, 3))
pearson_p = np.zeros((3, 3))

# 计算Spearman相关系数和对应的p值
spearman_corr = np.zeros((3, 3))
spearman_p = np.zeros((3, 3))

variables = ['X1', 'X2', 'X3']

# 计算每对变量之间的相关系数和p值
for i, var1 in enumerate(variables):
    for j, var2 in enumerate(variables):
        # Pearson相关系数及p值
        pearson_result = stats.pearsonr(data[var1], data[var2])
        pearson_corr[i, j] = pearson_result[0]
        pearson_p[i, j] = pearson_result[1]
        
        # Spearman相关系数及p值
        spearman_result = stats.spearmanr(data[var1], data[var2])
        spearman_corr[i, j] = spearman_result[0]
        spearman_p[i, j] = spearman_result[1]

# 创建DataFrame以便于查看结果
pearson_corr_df = pd.DataFrame(pearson_corr, index=variables, columns=variables)
pearson_p_df = pd.DataFrame(pearson_p, index=variables, columns=variables)

spearman_corr_df = pd.DataFrame(spearman_corr, index=variables, columns=variables)
spearman_p_df = pd.DataFrame(spearman_p, index=variables, columns=variables)

# 显示结果
print("Pearson相关系数矩阵R:")
print(pearson_corr_df)
print("\nPearson相关系数对应的p值:")
print(pearson_p_df)

print("\nSpearman相关系数矩阵Q:")
print(spearman_corr_df)
print("\nSpearman相关系数对应的p值:")
print(spearman_p_df)

# 以格式化的方式展示结果
print("\n各元素相关性及显著性检验结果:")
for i, var1 in enumerate(variables):
    for j, var2 in enumerate(variables):
        if i != j:  # 不显示自身相关
            print(f"{var1}与{var2}的Pearson相关系数: {pearson_corr[i, j]:.4f}, p值: {pearson_p[i, j]:.6f}")
            print(f"{var1}与{var2}的Spearman相关系数: {spearman_corr[i, j]:.4f}, p值: {spearman_p[i, j]:.6f}")
            
            # 判断显著性
            if pearson_p[i, j] < 0.05:
                print(f"  Pearson相关性检验: 在0.05水平上显著")
            if pearson_p[i, j] < 0.01:
                print(f"  Pearson相关性检验: 在0.01水平上显著")
                
            if spearman_p[i, j] < 0.05:
                print(f"  Spearman相关性检验: 在0.05水平上显著")
            if spearman_p[i, j] < 0.01:
                print(f"  Spearman相关性检验: 在0.01水平上显著")
            print()