import numpy as np from numpy import sqrt x = np.matrix([[1/6, 1/12, 1/12, 1/3], [1/12, 1/2, 1/12, 2/3], [1/4, 7/12, 1/6, 1]]) x print("Matrix is : \n", x) print(type(x)) x_11=x[0,0];x_11 x_12=x[0,1];x_12 x_13=x[0,2];x_13 x_14=x[0,3];x_14 x_21=x[1,0];x_21 x_22=x[1,1];x_22 x_23=x[1,2];x_23 x_24=x[1,3];x_24 x_31=x[2,0];x_31 x_32=x[2,1];x_32 x_33=x[2,2];x_33 x_34=x[2,3];x_34 mu_x=1*x_14+3*x_24 mu_x mu_y=0*x_31+1*x_32+2*x_33 mu_y print("Mean of x is : \n", round(mu_x,6)) print("Mean of y is : \n", round(mu_y,6)) var_x=1**2*x_14+3**2*x_24-mu_x**2 var_x var_y=0**2*x_31+1**2*x_32+2**2*x_33-mu_y**2 var_y print("Variance of x is : \n", round(var_x,6)) print("Variance of y is : \n", round(var_y,6)) sum_p_xy = 0*x_11+1*x_12+2*x_13+0*x_21+3*x_22+6*x_23 sum_p_xy print("Sum of multiplication of p_xy and xy is : \n", sum_p_xy) cov_xy = sum_p_xy - mu_x*mu_y cov_xy print("Covariance of x and y is :", round(cov_xy,6)) corr_xy = (cov_xy)/sqrt(var_x*var_y) corr_xy print("Correlation of x and y is :", round(corr_xy,6))