|
|
|
/**
|
|
|
|
return max ;
|
|
|
|
N = lxi.length ;
|
|
|
|
mean = jx.math.mean(lxi,lni) ;
|
|
|
|
|
|
|
|
sqr = [] ;
|
|
|
|
for(var i=0; i < lxi.length ;i++){
|
|
|
|
console.log(lxi[i])
|
|
|
|
sqr[i] = jx.math.pow((Number(lxi[i])-mean),2 ) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
total = jx.math.sum(sqr);
|
|
|
|
|
|
|
|
return jx.math.sqrt(total/(N-1)) ;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the factorial of a given value
|
|
|
|
*/
|
|
|
|
jx.math.factorial = function(value){
|
|
|
|
r =value;
|
|
|
|
for(var i =value-1; i > 0; i--){
|
|
|
|
r *= i ;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the fibonacci value of a given number using the golden ratio
|
|
|
|
*/
|
|
|
|
jx.math.fibonacci = function(value){
|
|
|
|
r = (jx.math.pow(jx.math.PHI,value)/jx.math.sqrt(5)) + 0.5 ;
|
|
|
|
return jx.math.floor(r) ;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* computes the absolute difference of values in a list of observations
|
|
|
|
*/
|
|
|
|
jx.math.diff = function(lxi){
|
|
|
|
var r = [] ;
|
|
|
|
var x,y;
|
|
|
|
for(var i=0; i < lxi.length-1; i++){
|
|
|
|
x = lxi[i] ;
|
|
|
|
y = lxi[i+1] ;
|
|
|
|
r.push(y-x)
|
|
|
|
}
|
|
|
|
return r ;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This section implements a few handlers based on sets
|
|
|
|
*/
|
|
|
|
jx.math.sets = {} ;
|
|
|
|
/**
|
|
|
|
* This function will perform a unique operation of values/objects
|
|
|
|
* @param list list/vector of values or objects
|
|
|
|
* @param equals operator to be used, only provide this for complex objects
|
|
|
|
*/
|
|
|
|
jx.math.sets.unique = jx.utils.unique ;
|
|
|
|
/**
|
|
|
|
* This function will perform the union of 2 sets (objects, or values)
|
|
|
|
* @param list1 list/vector of values or objects
|
|
|
|
* @param list2 list/vector of values or objects
|
|
|
|
* @param equals operator to be used to evaluate equality (use this for complex objects)
|
|
|
|
*/
|
|
|
|
jx.math.sets.union = function(list1,list2,equals){
|
|
|
|
runion = [] ;
|
|
|
|
runion = list1.concat(list2) ;
|
|
|
|
runion = jx.math.sets.unique(runion,equals)
|
|
|
|
return runion;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will normalize values within a vector
|
|
|
|
* By definition normalization is (x - u) / sd (assuming population parameters are known)
|
|
|
|
*/
|
|
|
|
jx.math.normalize = function(lvalues){
|
|
|
|
mean = jx.math.mean(lvalues) ;
|
|
|
|
sd = jx.math.sd(lvalues) ;
|
|
|
|
return jx.utils.patterns.visitor(lvalues,function(x){
|
|
|
|
return ((x - mean) / sd)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will scale a feature vector over it's range
|
|
|
|
*/
|
|
|
|
jx.math.scale = function(lvalues,percent){
|
|
|
|
max = jx.math.max(lvalues) ;
|
|
|
|
min = jx.math.min(lvalues) ;
|
|
|
|
return jx.utils.patterns.visitor(lvalues,function(x){
|
|
|
|
var value = (x - min ) / max ;
|
|
|
|
if(percent == true){
|
|
|
|
return (100*value).toFixed(2)
|
|
|
|
}else{
|
|
|
|
return value ;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* This is a lightweight map reduce infrastructure
|
|
|
|
*/
|
|
|
|
jx.mr = {} ;
|
|
|
|
/**
|
|
|
|
* This function will perform a map on a given id in rec, then will call emit with the
|
|
|
|
*/
|
|
|
|
jx.mr.map = null
|
|
|
|
/**
|
|
|
|
* @param keys
|
|
|
|
* @param values array of values that were mapped
|
|
|
|
*/
|
|
|
|
jx.mr.reduce = null;
|
|
|
|
jx.mr.mapreduce = function(data,fn_map,fn_reduce){
|
|
|
|
if (fn_map == null){
|
|
|
|
throw new "Map function is not defined"
|
|
|
|
}
|
|
|
|
map = {} ;
|
|
|
|
emit = function(id,values){
|
|
|
|
if(map[id] == null){
|
|
|
|
map[id] = []
|
|
|
|
}
|
|
|
|
map[id].push(values);
|
|
|
|
}
|
|
|
|
if(data.constructor != Array){
|
|
|
|
for (id in data){
|
|
|
|
//rec = data[id] ;
|
|
|
|
rec = {}
|
|
|
|
rec['__id'] = id;
|
|
|
|
rec['data'] = data[id] ;
|
|
|
|
fn_map(rec,emit)
|
|
|
|
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
for (var i=0; i < data.length; i++){
|
|
|
|
rec = data[i];
|
|
|
|
fn_map(rec,emit);
|
|
|
|
//if(i == 2)break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(fn_reduce != null){
|
|
|
|
keys = jx.utils.keys(map) ;
|
|
|
|
m = {}
|
|
|
|
for(var i=0; i < keys.length; i++){
|
|
|
|
id = keys[i] ;
|
|
|
|
values = map[id] ;
|
|
|
|
value = fn_reduce(id,values) ;
|
|
|
|
id = keys[i] ;
|
|
|
|
m[id] = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
map = m
|
|
|
|
|
|
|
|
}
|
|
|
|
return map ;
|
|
|
|
}
|