Building distance matrices.
Sys.setenv(RETICULATE_PYTHON = "C:/Users/brent/anaconda3/envs/STOCK_MASTER/python.exe")
::use_condaenv(condaenv = 'STOCK_MASTER', required = TRUE)
reticulate::opts_chunk$set(echo = TRUE, cache = TRUE)
knitrlibrary('reticulate')
import numpy as np
=2, linewidth=150)
np.set_printoptions(precisionfrom scipy.spatial import distance
= np.array([[6,1,7],[10,9,4],[13,9,3],[10,8,15],[14,4,1]])
X1 = np.array([6,1,7])
X2 print(X1, "\n\n", X2)
## [[ 6 1 7]
## [10 9 4]
## [13 9 3]
## [10 8 15]
## [14 4 1]]
##
## [6 1 7]
def ed_sc(x):
= x[:,None] if x.ndim == 1 else x
x = distance.pdist(x, metric='euclidean')
D_sp = distance.squareform(D_sp, force='no', checks=True)
Dmat return Dmat
= ed_sc(X1)
a = ed_sc(X2)
b print(a, "\n\n", b)
## [[ 0. 9.43 11.36 11.36 10.44]
## [ 9.43 0. 3.16 11.05 7.07]
## [11.36 3.16 0. 12.41 5.48]
## [11.36 11.05 12.41 0. 15.1 ]
## [10.44 7.07 5.48 15.1 0. ]]
##
## [[0. 5. 1.]
## [5. 0. 6.]
## [1. 6. 0.]]
SO reference
def ed_np1(x):
= x[:,None] if x.ndim == 1 else x
x = np.sum(x * x, 1)
a = np.repeat(a[:, None], x.shape[0], axis=1)
b = b + b.T -2 * np.dot(x, x.T)
D = np.sqrt(D)
Dmat return Dmat
= ed_np1(X1)
a = ed_np1(X2)
b print(a, "\n\n", b)
## [[ 0. 9.43 11.36 11.36 10.44]
## [ 9.43 0. 3.16 11.05 7.07]
## [11.36 3.16 0. 12.41 5.48]
## [11.36 11.05 12.41 0. 15.1 ]
## [10.44 7.07 5.48 15.1 0. ]]
##
## [[0. 5. 1.]
## [5. 0. 6.]
## [1. 6. 0.]]
Code adapted from here
def ed_np2(x):
= x[:,None] if x.ndim == 1 else x
x = np.sum(x**2, 1).reshape(-1, 1) \
D + np.sum(x**2, 1) \
- 2 * np.dot(x, x.T)
= np.sqrt(D)
Dmat return Dmat
= ed_np2(X1)
a = ed_np2(X2)
b print(a, "\n\n", b)
## [[ 0. 9.43 11.36 11.36 10.44]
## [ 9.43 0. 3.16 11.05 7.07]
## [11.36 3.16 0. 12.41 5.48]
## [11.36 11.05 12.41 0. 15.1 ]
## [10.44 7.07 5.48 15.1 0. ]]
##
## [[0. 5. 1.]
## [5. 0. 6.]
## [1. 6. 0.]]
SO reference
def ed_np3(x):
= x[:,None] if x.ndim == 1 else x
x = np.sqrt(((x[:, :, None] - x[:, :, None].T) ** 2).sum(1))
Dmat return Dmat
= ed_np3(X1)
a = ed_np3(X2)
b print(a, "\n\n", b)
## [[ 0. 9.43 11.36 11.36 10.44]
## [ 9.43 0. 3.16 11.05 7.07]
## [11.36 3.16 0. 12.41 5.48]
## [11.36 11.05 12.41 0. 15.1 ]
## [10.44 7.07 5.48 15.1 0. ]]
##
## [[0. 5. 1.]
## [5. 0. 6.]
## [1. 6. 0.]]
def md_sp(x):
= x[:,None] if x.ndim == 1 else x
x = distance.pdist(x, metric='mahalanobis')
D_sp = distance.squareform(D_sp, force='no', checks=True)
Dmat return Dmat
= md_sp(X1)
a = md_sp(X2)
b print(a, "\n\n", b)
## [[0. 2.39 2.63 2.76 2.6 ]
## [2.39 0. 1.22 2.68 2.8 ]
## [2.63 1.22 0. 2.3 1.85]
## [2.76 2.68 2.3 0. 2.78]
## [2.6 2.8 1.85 2.78 0. ]]
##
## [[0. 1.56 0.31]
## [1.56 0. 1.87]
## [0.31 1.87 0. ]]
def md_np(x):
= np.linalg.cholesky(np.cov(x, rowvar=False))
dec = np.linalg.solve(dec, x.T)
tmp = tmp.T
tmp1 return ed_np3(tmp1)
print(md_np(X1))
## [[0. 2.39 2.63 2.76 2.6 ]
## [2.39 0. 1.22 2.68 2.8 ]
## [2.63 1.22 0. 2.3 1.85]
## [2.76 2.68 2.3 0. 2.78]
## [2.6 2.8 1.85 2.78 0. ]]
<- function(x) {
md_r <- chol( cov(x) )
dec <- forwardsolve(t(dec), t(x) )
tmp0 <- t(tmp0)
tmp1 <- dist(tmp1)
lower_tri <- matrix(NA, nrow(x), nrow(x))
m lower.tri(m)] <- lower_tri
m[upper.tri(m)] <- t(m)[upper.tri(m)]
m[diag(m) <- 0
m
}
<- matrix(c(6,1,7,10,9,4,13,9,3,10,8,15,14,4,1), ncol = 3, byrow= TRUE)
d md_r(d)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.000000 2.388123 2.631810 2.762811 2.603338
## [2,] 2.388123 0.000000 1.220222 2.678094 2.798580
## [3,] 2.631810 1.220222 0.000000 2.303122 1.847275
## [4,] 2.762811 2.678094 2.303122 0.000000 2.783878
## [5,] 2.603338 2.798580 1.847275 2.783878 0.000000