I'm trying this simple whitening function in python in R
Python
def svd_whiten(X):
U, s, Vt = np.linalg.svd(X, full_matrices=False)
#print(U)
#print(Vt)
# U and Vt are the singular matrices, and s contains the singular values.
# Since the rows of both U and Vt are orthonormal vectors, then U * Vt
# will be white
X_white = np.dot(U, Vt)
return X_white
Read Python Data
df = pd.read_csv("https://raw.githubusercontent.com/thistleknot/Python-Stock/master/data/raw/states.csv")
pd.DataFrame(svd_whiten(df.iloc[:,2:]))
R
ZCA_svd <- function(x)
{
internal <- svd(x)
U = internal$u
#print(U)
Vt = internal$v
#print(Vt)
s = internal$d
#U, s, Vt = np.linalg.svd(X, full_matrices=False)
# U and Vt are the singular matrices, and s contains the singular values.
# Since the rows of both U and Vt are orthonormal vectors, then U * Vt
# will be white
#dot(U,Vt)
X_white = U%*%Vt
#np$dot(U,Vt)
#
return(X_white)
}
R Data
x_ = read.csv(file="https://raw.githubusercontent.com/thistleknot/Python-Stock/master/data/raw/states.csv",header =TRUE,row.names = 1)
x = x_[,2:ncol(x_)]
ZCA_svd(x)
If I print the values of U and Vt in either R or Python, they are the same, but when multiplied, the results are different between R and Python.
To add to the fun, if I use reticulate and import numpy via np$dot(U, Vt). The results are the same as U%*%Vt. As a result. I'm not sure which is the "correct" version to use.