Реши задачу
Создано: 28 мая 2025 г.
Создано: 28 мая 2025 г.
pythonfrom PIL import Image import matplotlib.pyplot as plt img = Image.open('/mnt/data/image.png') plt.imshow(img) plt.axis('off') (-0.5, 456.5, 111.5, -0.5)
pythonplt.figure(figsize=(7,3)) plt.imshow(img.crop((0,110,1600,350))) plt.axis('off') (-0.5, 1599.5, 239.5, -0.5)
pythonplt.figure(figsize=(8,4)) plt.imshow(img) plt.axis('off') (-0.5, 456.5, 111.5, -0.5)
python[f for f in os.listdir('/mnt/data') if f.endswith('.png')] ['image.png']
pythonimport pandas as pd import numpy as np import math from ace_tools import display_dataframe_to_user # Joint probability matrix joint = pd.DataFrame( data=[[0.15,0.20,0.15], [0.14,0.20,0.16]], index=pd.Index([-6,-1], name='Y'), columns=pd.Index([1,6,7], name='X') ) # Display joint distribution display_dataframe_to_user("Совместное распределение P(X,Y)", joint) # Marginal distributions PX = joint.sum(axis=0) PY = joint.sum(axis=1) display_dataframe_to_user("Маргинальное распределение P(X)", PX.to_frame('P(X)')) display_dataframe_to_user("Маргинальное распределение P(Y)", PY.to_frame('P(Y)')) # Expectations EX = (PX.index * PX.values).sum() EY = (PY.index * PY.values).sum() EX2 = ((PX.index**2) * PX.values).sum() EY2 = ((PY.index**2) * PY.values).sum() varX = EX2 - EX**2 varY = EY2 - EY**2 stdX = math.sqrt(varX) stdY = math.sqrt(varY) # Covariance and correlation # Compute E[XY] XY = 0 for y in joint.index: for x in joint.columns: XY += x*y*joint.loc[y,x] covXY = XY - EX*EY corr = covXY / (stdX*stdY) # Conditional distributions cond_X_given_Y = joint.divide(PY, axis=0) # each row cond_Y_given_X = joint.divide(PX, axis=1) # each column display_dataframe_to_user("P(X | Y)", cond_X_given_Y) display_dataframe_to_user("P(Y | X)", cond_Y_given_X) # Conditional expectations (group averages) EY_given_X = (cond_Y_given_X.index.to_numpy()[:,None] * cond_Y_given_X.values).sum(axis=0) EX_given_Y = (cond_X_given_Y.columns.to_numpy() * cond_X_given_Y.values).sum(axis=1) cond_means = pd.DataFrame({ 'X': cond_Y_given_X.columns, 'M(Y|X)': EY_given_X }) display_dataframe_to_user("Условные средние M(Y|X)", cond_means) cond_means2 = pd.DataFrame({ 'Y': cond_X_given_Y.index, 'M(X|Y)': EX_given_Y }) display_dataframe_to_user("Условные средние M(X|Y)", cond_means2) # Prepare a summary text summary = { 'E[X]': EX, 'E[Y]': EY, 'Var(X)': varX, 'σ(X)': stdX, 'Var(Y)': varY, 'σ(Y)': stdY, 'E[XY]': XY, 'Cov(X,Y)': covXY, 'Corr(X,Y)': corr } summary_df = pd.DataFrame(summary, index=['Value']).T display_dataframe_to_user("Основные числовые характеристики", summary_df)
pythonimport pandas as pd import numpy as np import math from ace_tools import display_dataframe_to_user # Joint probability matrix joint = pd.DataFrame( data=[[0.15,0.20,0.15], [0.14,0.20,0.16]], index=pd.Index([-6,-1], name='Y'), columns=pd.Index([1,6,7], name='X') ) # Display joint distribution display_dataframe_to_user("Совместное распределение P(X,Y)", joint) # Marginal distributions PX = joint.sum(axis=0) PY = joint.sum(axis=1) display_dataframe_to_user("Маргинальное распределение P(X)", PX.to_frame('P(X)')) display_dataframe_to_user("Маргинальное распределение P(Y)", PY.to_frame('P(Y)')) # Expectations EX = (PX.index * PX.values).sum() EY = (PY.index * PY.values).sum() EX2 = ((PX.index**2) * PX.values).sum() EY2 = ((PY.index**2) * PY.values).sum() varX = EX2 - EX**2 varY = EY2 - EY**2 stdX = math.sqrt(varX) stdY = math.sqrt(varY) # Covariance and correlation # Compute E[XY] XY = 0 for y in joint.index: for x in joint.columns: XY += x*y*joint.loc[y,x] covXY = XY - EX*EY corr = covXY / (stdX*stdY) # Conditional distributions cond_X_given_Y = joint.divide(PY, axis=0) # each row cond_Y_given_X = joint.divide(PX, axis=1) # each column display_dataframe_to_user("P(X | Y)", cond_X_given_Y) display_dataframe_to_user("P(Y | X)", cond_Y_given_X) # Conditional expectations (group averages) EY_given_X = (cond_Y_given_X.index.to_numpy()[:,None] * cond_Y_given_X.values).sum(axis=0) EX_given_Y = (cond_X_given_Y.columns.to_numpy() * cond_X_given_Y.values).sum(axis=1) cond_means = pd.DataFrame({ 'X': cond_Y_given_X.columns, 'M(Y|X)': EY_given_X }) display_dataframe_to_user("Условные средние M(Y|X)", cond_means) cond_means2 = pd.DataFrame({ 'Y': cond_X_given_Y.index, 'M(X|Y)': EX_given_Y }) display_dataframe_to_user("Условные средние M(X|Y)", cond_means2) # Prepare a summary text summary = { 'E[X]': EX, 'E[Y]': EY, 'Var(X)': varX, 'σ(X)': stdX, 'Var(Y)': varY, 'σ(Y)': stdY, 'E[XY]': XY, 'Cov(X,Y)': covXY, 'Corr(X,Y)': corr } summary_df = pd.DataFrame(summary, index=['Value']).T display_dataframe_to_user("Основные числовые характеристики", summary_df)
pythonimport pandas as pd import numpy as np import math from ace_tools import display_dataframe_to_user # Joint probability matrix joint = pd.DataFrame( data=[[0.15,0.20,0.15], [0.14,0.20,0.16]], index=pd.Index([-6,-1], name='Y'), columns=pd.Index([1,6,7], name='X') ) # Display joint distribution display_dataframe_to_user("Совместное распределение P(X,Y)", joint) # Marginal distributions PX = joint.sum(axis=0) PY = joint.sum(axis=1) display_dataframe_to_user("Маргинальное распределение P(X)", PX.to_frame('P(X)')) display_dataframe_to_user("Маргинальное распределение P(Y)", PY.to_frame('P(Y)')) # Expectations and variances x_vals = PX.index.to_numpy() y_vals = PY.index.to_numpy() EX = (x_vals * PX.values).sum() EY = (y_vals * PY.values).sum() EX2 = ((x_vals**2) * PX.values).sum() EY2 = ((y_vals**2) * PY.values).sum() varX = EX2 - EX**2 varY = EY2 - EY**2 stdX = math.sqrt(varX) stdY = math.sqrt(varY) # Covariance and correlation XY = sum(x*y*joint.loc[y,x] for y in joint.index for x in joint.columns) covXY = XY - EX*EY corr = covXY / (stdX*stdY) # Conditional distributions cond_X_given_Y = joint.div(PY, axis=0) # P(X|Y) cond_Y_given_X = joint.div(PX, axis=1) # P(Y|X) display_dataframe_to_user("P(X | Y)", cond_X_given_Y.round(4)) display_dataframe_to_user("P(Y | X)", cond_Y_given_X.round(4)) # Conditional expectations EY_given_X = (cond_Y_given_X.index.to_numpy()[:,None] * cond_Y_given_X.values).sum(axis=0) EX_given_Y = (cond_X_given_Y.columns.to_numpy() * cond_X_given_Y.values).sum(axis=1) cond_means_X = pd.DataFrame({ 'X': cond_Y_given_X.columns, 'M(Y|X)': EY_given_X }) cond_means_Y = pd.DataFrame({ 'Y': cond_X_given_Y.index, 'M(X|Y)': EX_given_Y }) display_dataframe_to_user("Условные средние M(Y|X)", cond_means_X.round(4)) display_dataframe_to_user("Условные средние M(X|Y)", cond_means_Y.round(4)) # Summary statistics summary_df = pd.DataFrame({ 'Characteristic': ['E[X]', 'E[Y]', 'Var(X)', 'σ(X)', 'Var(Y)', 'σ(Y)', 'E[XY]', 'Cov(X,Y)', 'Corr(X,Y)'], 'Value': [EX, EY, varX, stdX, varY, stdY, XY, covXY, corr] }) display_dataframe_to_user("Основные числовые характеристики", summary_df.round(5)) Characteristic Value 0 E[X] 4.86000 1 E[Y] -3.50000 2 Var(X) 6.26040 3 σ(X) 2.50208 4 Var(Y) 6.25000
Характеристика | Значение |
---|---|
E [X] | 4.86 |
E [Y] | -3.50 |
Var (X) | 6.2604 |
σ (X) | 2.50 |
Var (Y) | 6.25 |
σ (Y) | 2.50 |
E [XY] | –16.86 |
Cov (X,Y) | 0.15 |
r₍X Y₎ | ≈ 0.024 |
Вкладки «Совместное распределение P(X,Y)» и далее вверху содержат полную матрицу вероятностей
Y\X | 1 | 6 | 7 |
---|---|---|---|
-6 | 0.15 | 0.20 | 0.15 |
-1 | 0.14 | 0.20 | 0.16 |
(сумма = 1)
σ — квадратные корни из дисперсий.
Практически линейной связи нет.
X|Y | -6 | -1 |
---|---|---|
1 | 0.30 | 0.28 |
6 | 0.40 | 0.40 |
7 | 0.30 | 0.32 |
Y|X | 1 | 6 | 7 |
---|---|---|---|
-6 | 0.517 | 0.50 | 0.484 |
-1 | 0.483 | 0.50 | 0.516 |
Полные таблицы , и условных средних доступны в интерактивных вкладках над ответом — там можно посмотреть все цифры и округления.