from __future__ import division
import numpy as np
from utils.ml import AnomalyDetection
mo = [[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(mo))
xu_ = np.mean(m[0,:])
yu_ = np.mean(m[1,:])

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]
test=[2.4,3.1]
x = np.array(test)
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)) * (np.linalg.inv(cx)*d))
print u.shape
print cx.shape

from scipy.stats import multivariate_normal
xo= multivariate_normal.pdf(x,u,cx)
yo= (b/a)[0,0]
e= np.float64(0.05)
print [yo,yo < e]
print [xo,xo < e]
ml = AnomalyDetection()
end = int(len(mo)*.7)
mu,sigma = ml.gParameters(mo)
r =  ml.gPx(mu,sigma,[test],0.05)
for i in range(0,len(r)) :
	print ' *** ', mo[(i+end)],r[i]


#for row in np.transpose(m):
#	print ",".join([str(value) for value in row])
#-- We are ready to perform anomaly detection ...
