import numpy as np from numpy import sqrt x = np.matrix([[0.1,0,0,0,0.1],[0,0.4,0,0,0.4],[0,0,0.3,0,0.3],[0,0,0,0.2,0.2],[0.1,0.4,0.3,0.2,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_15=x[0,4];x_15 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_25=x[1,4];x_25 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 x_35=x[2,4];x_35 x_41=x[3,0];x_41 x_42=x[3,1];x_42 x_43=x[3,2];x_43 x_44=x[3,3];x_44 x_45=x[3,4];x_45 x_51=x[4,0];x_51 x_52=x[4,1];x_52 x_53=x[4,2];x_53 x_54=x[4,3];x_54 x_55=x[4,4];x_55 mu_x=5*x_15+7*x_25+(-4)*x_35+15*x_45 mu_x mu_y=(-1)*x_51+6*x_52+2*x_53+20*x_54 mu_y print("Mean of x is : \n", round(mu_x,6)) print("Mean of y is : \n", round(mu_y,6)) var_x=5**2*x_15+7**2*x_25+(-4)**2*x_35+15**2*x_45-mu_x**2 var_x var_y=(-1)**2*x_51+6**2*x_52+2**2*x_53+20**2*x_54-mu_y**2 var_y print("Variance of x is : \n", round(var_x,6)) print("Variance of y is : \n", round(var_y,2)) """ sum_p_xy = ((-5)*x_11+30*x_12+10*x_13+100*x_14 +(-7)*x_21+42*x_22+14*x_23+140*x_24 +(4)*x_31+(-24)*x_32+(-8)*x_33+(-80)*x_34 +(-15)*x_41+90*x_42+30*x_43+300*x_44) 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)) """ xx = np.matrix([[0.1,0,0,0],[0,0.4,0,0],[0,0,0.3,0],[0,0,0,0.2]]) p_xy = np.array(xx) yy= np.matrix([[-5,30,10,100],[-7,42,14,140],[4,-24,-8,-80],[-15,90,30,300]]) xy = np.array(yy) cov_xy = np.sum(p_xy*xy) - mu_x*mu_y cov_xy print("Covariance of x and y is :", round(cov_xy,2)) corr_xy = (cov_xy)/sqrt(var_x*var_y) corr_xy print("Correlation of x and y is :", round(corr_xy,3))