52 KiB
52 KiB
In [1]:
import numpy as np
# SVD inversion
def SVDinv(A):
''' Takes as input a numpy matrix A and returns inv(A) based on singular value decomposition (SVD).
SVD is numerically more stable than the inversion algorithms provided by
numpy and scipy.linalg at the cost of being slower.
'''
U, s, VT = np.linalg.svd(A)
# print('test U')
# print( (np.transpose(U) @ U - U @np.transpose(U)))
# print('test VT')
# print( (np.transpose(VT) @ VT - VT @np.transpose(VT)))
print(U)
print(s)
print(VT)
D = np.zeros((len(U),len(VT)))
for i in range(0,len(VT)):
D[i,i]=s[i]
UT = np.transpose(U); V = np.transpose(VT); invD = np.linalg.inv(D)
return np.matmul(V,np.matmul(invD,UT))
X = np.array([ [1.0, -1.0, 2.0], [1.0, 0.0, 1.0], [1.0, 2.0, -1.0], [1.0, 1.0, 0.0] ])
print(X)
A = np.transpose(X) @ X
print(A)
# Brute force inversion of super-collinear matrix
#B = np.linalg.inv(A)
#print(B)
C = SVDinv(A)
print(C)[[ 1. -1. 2.] [ 1. 0. 1.] [ 1. 2. -1.] [ 1. 1. 0.]] [[ 4. 2. 2.] [ 2. 6. -4.] [ 2. -4. 6.]] [[-1.18404906e-16 8.16496581e-01 -5.77350269e-01] [-7.07106781e-01 4.08248290e-01 5.77350269e-01] [ 7.07106781e-01 4.08248290e-01 5.77350269e-01]] [1.00000000e+01 6.00000000e+00 9.10898112e-32] [[ 3.33066907e-17 -7.07106781e-01 7.07106781e-01] [ 8.16496581e-01 4.08248290e-01 4.08248290e-01] [ 5.77350269e-01 -5.77350269e-01 -5.77350269e-01]] [[-3.65939208e+30 3.65939208e+30 3.65939208e+30] [ 3.65939208e+30 -3.65939208e+30 -3.65939208e+30] [ 3.65939208e+30 -3.65939208e+30 -3.65939208e+30]]
In [2]:
# Importing various packages
import numpy as np
n = 100
x = np.random.normal(size=n)
print(np.mean(x))
y = 4+3*x+np.random.normal(size=n)
print(np.mean(y))
W = np.vstack((x, y))
C = np.cov(W)
print(C)0.062127739929490035 4.226441441217558 [[0.95977893 2.78652875] [2.78652875 9.09409124]]
In [3]:
import numpy as np
n = 100
# define two vectors
x = np.random.random(size=n)
y = 4+3*x+np.random.normal(size=n)
#scaling the x and y vectors
x = x - np.mean(x)
y = y - np.mean(y)
variance_x = np.sum(x@x)/n
variance_y = np.sum(y@y)/n
print(variance_x)
print(variance_y)
cov_xy = np.sum(x@y)/n
cov_xx = np.sum(x@x)/n
cov_yy = np.sum(y@y)/n
C = np.zeros((2,2))
C[0,0]= cov_xx/variance_x
C[1,1]= cov_yy/variance_y
C[0,1]= cov_xy/np.sqrt(variance_y*variance_x)
C[1,0]= C[0,1]
print(C)0.09053391104887817 1.9755272664385481 [[1. 0.64723729] [0.64723729 1. ]]
In [4]:
import numpy as np
import pandas as pd
n = 10
x = np.random.normal(size=n)
x = x - np.mean(x)
y = 4+3*x+np.random.normal(size=n)
y = y - np.mean(y)
X = (np.vstack((x, y))).T
print(X)
Xpd = pd.DataFrame(X)
print(Xpd)
correlation_matrix = Xpd.corr()
print(correlation_matrix)[[ 0.52374318 0.7528421 ]
[-1.07892554 -3.5697027 ]
[ 0.65536057 2.84854 ]
[-0.9936011 -1.75368597]
[-0.22233456 -1.63866932]
[ 1.10799046 4.51410028]
[ 1.30401938 5.04686521]
[ 0.70055962 1.28566384]
[-1.69423925 -6.23684061]
[-0.30257276 -1.24911284]]
0 1
0 0.523743 0.752842
1 -1.078926 -3.569703
2 0.655361 2.848540
3 -0.993601 -1.753686
4 -0.222335 -1.638669
5 1.107990 4.514100
6 1.304019 5.046865
7 0.700560 1.285664
8 -1.694239 -6.236841
9 -0.302573 -1.249113
0 1
0 1.000000 0.967871
1 0.967871 1.000000
Warning:
Output truncated. This notebook contains too many cells to display efficiently.