From 61bbb2c5d2404702b16ba630365bb7f4cab07131 Mon Sep 17 00:00:00 2001 From: steve Date: Mon, 9 Jan 2017 15:25:02 -0600 Subject: [PATCH] experimentation ... --- test/data.csv | 13 +++++++++++++ test/demo.py | 42 ++++++++++++++++++++++-------------------- 2 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 test/data.csv diff --git a/test/data.csv b/test/data.csv new file mode 100644 index 0000000..14b618f --- /dev/null +++ b/test/data.csv @@ -0,0 +1,13 @@ +0.0,4.5 +0.0,4.5 +11.6,4.4 +12.2,4.3 +1.4,3.9 +1.4,3.9 +2.5,3.8 +0.1,3.8 +0.5,5.1 +0.7,5.2 +0.7,5.1 +0.0,4.6 +0.0,4.6 diff --git a/test/demo.py b/test/demo.py index d0d6791..058ae42 100644 --- a/test/demo.py +++ b/test/demo.py @@ -1,25 +1,27 @@ import numpy as np m = [[0.0, 4.5], [0.0, 4.5], [11.6, 4.4], [12.2, 4.3], [1.4, 3.9], [1.4, 3.9], [2.5, 3.8], [0.1, 3.8], [0.5, 5.1], [0.7, 5.2], [0.7, 5.1], [0.0, 4.6], [0.0, 4.6]] +m = np.transpose(np.array(m)) +xu_ = np.mean(m[1,:]) +yu_ = np.mean(m[0,:]) -m_ = np.array(m) -x_ = np.mean(m_[:,0]) -y_ = np.mean(m_[:,1]) -u = np.array([x_,y_]) -r = [np.sqrt(np.var(m_[:,0])),np.sqrt(np.var(m_[:,1]))] -x__ = (m_[:,0] - x_ )/r[0] -y__ = (m_[:,1] - y_ )/r[1] - -nm = np.matrix([x__,y__]) - - -cx = np.cov(nm) -print cx.shape -x = np.array([1.9,3]) -n = 2 -a = 1/ np.sqrt((2*np.pi**k)*np.det(cx)) -b = np.exp(() ) -#from scipy.stats import multivariate_normal -#print multivariate_normal.pdf(x,u,cx) - +xr_ = np.sqrt(np.var(m[0,:])) +yr_ = np.sqrt(np.var(m[1,:])) +# +# -- normalizing the matrix before computing covariance +# +mn = np.array([list( (m[0,:]-xu_)/xr_),list( (m[1,:]-yu_)/yr_)]) +cx = np.cov(mn) +n = m.shape[0] +x = np.array([2.5,3.1]) +u = np.array([xu_,yu_]) +d = np.matrix(x - u) +d.shape = (n,1) +a = (2*(np.pi)**(n/2))*np.linalg.det(cx)**0.5 +b = np.exp(-0.5*np.transpose(d) * (cx**-1)*d) +from scipy.stats import multivariate_normal +xo= multivariate_normal.pdf(x,u,cx) +yo= (b/a)[0,0] +for row in np.transpose(m): + print ",".join([str(value) for value in row]) #-- We are ready to perform anomaly detection ...