<jupyter_start><jupyter_text>확률변수의 공분산과 상관계수  - $Cov[X,Y] = E[(X-E[X])(Y-E[Y])]$   - $\rho[X,Y] = \frac{Cov[X,Y]}{\sqrt{Var[X]\cdot Var[Y]}}$          : 이론적인 상관계수 ( *$-1 \leq \rho \leq 1$* )              - $\rho = 1$ : 완전선형 상관관계        - $\rho = 0$ : 무상관       - $\rho = -1$ : 완전선형 반상관관계<jupyter_code>np.random.seed(0)

corrs = [1, 0.7, 0.3, 0., -0.3, -0.7, -1]

plt.figure(figsize=(len(corrs), 2))
for i, r in enumerate(corrs):
    x, y = np.random.multivariate_normal([0, 0], [[1, r], [r, 1]], 1000).T
    plt.subplot(1, len(corrs), i+1)
    plt.plot(x, y, 'ro', ms=1)
    plt.axis('equal')
    plt.yticks([])
    plt.xticks([])
    plt.title(r"$\rho = {}$".format(r))

plt.tight_layout()
plt.show()<jupyter_output><empty_output><jupyter_text>비선형 상관관계 - 선형이 아닌 상관관계  @@ 피어슨 상관계수는 두 확률변수의 관계가 선형적일 때만 상관관계를 제대로 계산할 수 있음<jupyter_code>n = 500
np.random.seed(1)

plt.subplot(221)
x1 = np.random.uniform(-1, 1, n)
y1 = 2*x1**2 + np.random.uniform(-0.1, 0.1, n)
plt.scatter(x1, y1)
r1 = sp.stats.pearsonr(x1, y1)[0]
plt.title(r'Nonlinear relationships 1: r= P{:3.1f}'.format(r1))

plt.subplot(222)
x2 = np.random.uniform(-1, 1, n)
y2 = 4*(x2 ** 2 - 0.5)**2 + 0.1*np.random.uniform(-1, 1, n)
plt.scatter(x2, y2)
r2 = sp.stats.pearsonr(x2, y2)[0]
plt.title(r'Nonlinear relationships 2: r= P{:3.1f}'.format(r2))

plt.subplot(223)
x3 = np.random.uniform(-1, 1, n)
y3 = np.cos(x3 * np.pi) + np.random.uniform(0, 1/8, n)
x3 = np.sin(x3 * np.pi) + np.random.uniform(0, 1/8, n)
plt.scatter(x3, y3)
r3 = sp.stats.pearsonr(x3, y3)[0]
plt.title(r'Nonlinear relationships 3: r= P{:3.1f}'.format(r3))

plt.subplot(224)
x4 = np.random.uniform(-1, 1, n)
y4 = (x4 ** 2 + np.random.uniform(0, 0.1, n)) *\
    np.array([-1, 1])[np.random.random_integers(0, 1, size=n)]
plt.scatter(x4, y4)
r4 = sp.stats.pearsonr(x4, y4)[0]
plt.title(r'Nonlinear relationships 4: r= P{:3.1f}'.format(r4))
plt.tight_layout()
plt.show()<jupyter_output>/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:28: DeprecationWarning: This function is deprecated. Please call randint(0, 1 + 1) instead<jupyter_text>앤스콤 데이터 - 프랭크 앤스콤의 논문<jupyter_code>data = sm.datasets.get_rdataset("anscombe")

df = data.data
df[["x1", "y1", "x2", "y2", "x3", "y3", "x4", "y4"]]
df
plt.subplot(221)
sns.regplot(x='x1', y='y1', data=df)
plt.subplot(222)
sns.regplot(x='x2', y='y2', data=df)
plt.subplot(223)
sns.regplot(x='x3', y='y3', data=df)
plt.subplot(224)
sns.regplot(x='x4', y='y4', data=df)
plt.tight_layout()
plt.subplots_adjust(top=0.9)
plt.suptitle("anscombe data")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2296875, 'fraction_numerical': 0.06796875, 'mean_word_length': 4.142570281124498, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '99278', 'license': 'MIT', 'n_tokens_mistral': 1438, 'n_tokens_neox': 1444, 'n_words': 226, 'path': 'MATH/24_Covariance_Correlation .ipynb', 'repo_name': 'CATERINA-SEUL/Data-Science-School', 'size': '2560'}