-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathgauss.min.js
4 lines (4 loc) · 18.7 KB
/
gauss.min.js
1
2
3
4
!function(){var Collection=function(values){"use strict";var collection=Array.isArray(values)?values:Array.prototype.slice.call(arguments);0===Array.prototype.slice.call(arguments).length&&(collection=[]);var result=function(value,callback){return callback?callback(value):value};return Object.defineProperty(collection,"extend",{value:function(methods,callback){for(var method in methods)Object.defineProperty(this,method,{value:methods[method],writable:!0,enumerable:!1});return result(this,callback)},writable:!0,enumerable:!1}),collection.extend({indexOf:function(element,callback){for(var index=-1,i=0;i<this.length;i++)if(this[i]===element){index=i;break}return result(index,callback)},indexBy:function(predicate,callback){for(var index=-1,i=0;i<this.length;i++)if(predicate(this[i])){index=i;break}return result(index,callback)},indicesOf:function(element,callback){var start=this.indexOf(element),end=this.lastIndexOf(element),indices=new Collection;if(start===end)indices.push(start);else for(var i=start;end>=i;i++)this[i]===element&&indices.push(i);return result(indices,callback)},indicesBy:function(predicate,callback){var start=0,end=this.length-1,indices=new Collection;if(start===end)indices.push(start);else for(var i=start;end>=i;i++)predicate(this[i])&&indices.push(i);return result(indices,callback)},lastIndexBy:function(predicate,callback){for(var index=-1,i=this.length-1;i>=0;i--)if(predicate(this[i])){index=i;break}return result(index,callback)},find:function(predicate,callback){var results=new Collection,comparator=function(e){return e===!0};if("object"==typeof predicate)for(var i=0;i<this.length;i++){var conditions=[];for(var key in predicate)conditions.push(predicate[key]===this[i][key]);conditions.every(comparator)&&results.push(this[i])}else for(var i=0;i<this.length;i++)predicate(this[i])&&results.push(this[i]);return result(results,callback)},findOne:function(predicate,callback){var element,comparator=function(e){return e===!0};if("object"==typeof predicate)for(var i=0;i<this.length;i++){var conditions=[];for(var key in predicate)conditions.push(predicate[key]===this[i][key]);if(conditions.every(comparator)){element=this[i];break}}else for(var i=0;i<this.length;i++)if(predicate(this[i])){element=this[i];break}return result(element,callback)},mode:function(callback){for(var map={},count=1,modes=new Collection,i=0;i<this.length;i++){var e=this[i];null==map[e]?map[e]=1:map[e]++,map[e]>count?(modes=new Collection(e),count=map[e]):map[e]==count&&(modes.push(e),count=map[e])}return 1===modes.length&&(modes=modes[0]),result(modes,callback)},frequency:function(element,callback){var freq=0;if(-1!==this.indexOf(element)){var buffer=this.copy().sort();freq=buffer.lastIndexOf(element)-buffer.indexOf(element)+1}return result(freq,callback)},distribution:function(format,callback){for(var array=this.copy().sort(),distribution={},i=0;i<array.length;)distribution[array[i]]="relative"===format?array.frequency(array[i])/array.length:array.frequency(array[i]),i=array.lastIndexOf(array[i])+1;return result(distribution,callback)},append:function(that,callback){return collection=this.push.apply(this,that),result(this,callback)},equal:function(that,callback){var equality=!(that>this||this>that);return result(equality,callback)},clone:function(callback){var object=Array.isArray(this)?[]:{};for(var i in this)"clone"!==i&&(this[i]&&"object"==typeof this[i]?object[i]=this[i].clone():object[i]=this[i]);return result(object,callback)},copy:function(callback){var copy=new Collection(this.slice());return result(copy,callback)},toArray:function(callback){var array=Array.prototype.slice.call(this);return result(array,callback)},concat:function(){var args=Array.prototype.slice.call(arguments),end=args[args.length-1];return"function"==typeof end?end("number"!=typeof args[0]?new Collection(this.toArray().concat(args[0])):new Collection(this.toArray().concat(args.slice(0,args.length-1)))):new Collection("number"!=typeof args[0]?this.toArray().concat(args[0]):this.toArray().concat(args))},slice:function(begin,end,callback){var args=Array.prototype.slice.call(arguments);return 3===args.length?callback(new Collection(this.toArray().slice(begin,end))):2===args.length?"function"==typeof args[1]?callback(new Collection(this.toArray().slice(begin))):new Collection(this.toArray().slice(begin,end)):new Collection(1===args.length?this.toArray().slice(begin):this.toArray().slice())},split:function(predicate,callback){var split=new Collection;return split.push(this.toArray().filter(function(e){return!predicate(e)})),split.push(this.toArray().filter(predicate)),result(split,callback)},unique:function(callback){for(var array=this.copy().sort(),unique=new Collection,i=0;i<array.length;)unique.push(array[i]),i=array.lastIndexOf(array[i])+1;return result(unique,callback)},filter:function(callback,next){var filter=new Collection(this.toArray().filter(callback));return result(filter,next)},every:function(callback,next){var every=this.toArray().every(callback);return result(every,next)},map:function(callback,next){var map=new Collection(this.toArray().map(callback));return result(map,next)},some:function(callback,next){var some=this.toArray().some(callback);return result(some,next)},reduce:function(callback,initialValue,next){var args=Array.prototype.slice.call(arguments);return 3===args.length?next(this.toArray().reduce(callback,initialValue)):2===args.length?"function"==typeof args[1]?next(this.toArray().reduce(callback)):this.toArray().reduce(callback,initialValue):this.toArray().reduce(callback)},reduceRight:function(callback,initialValue,next){var args=Array.prototype.slice.call(arguments);return 3===args.length?next(this.toArray().reduceRight(callback,initialValue)):2===args.length?"function"==typeof args[1]?next(this.toArray().reduceRight(callback)):this.toArray().reduceRight(callback,initialValue):this.toArray().reduceRight(callback)},union:function(that,callback){var union=this.append(that).unique();return result(union,callback)}}),collection};Array.prototype.toCollection=function(){return new Collection(this)},"undefined"!=typeof window?(window.gauss="undefined"==typeof window.gauss?{}:window.gauss,window.gauss.Collection=Collection):exports=module.exports=Collection}();
!function(){var Collection="undefined"==typeof window?require("./collection"):window.gauss.Collection;Collection=new Collection;var Vector=function(values){"use strict";var vector=Array.isArray(values)?values:Array.prototype.slice.call(arguments);0===Array.prototype.slice.call(arguments).length&&(vector=[]);var cache={sum:0,product:1,variance:0,sample:{variance:0},values:Array.prototype.slice.call(vector)};cache.sync=function(){cache.values=Array.prototype.slice.call(vector)};var asc=function(a,b){return a-b},extend=function(value,callback){return value=Array.isArray(value)?new Vector(value):value,callback?callback(value):value},result=function(value,callback){return callback?callback(value):value};return Object.defineProperty(vector,"extend",{value:function(methods,callback){return Collection.extend.bind(this),Collection.extend.apply(this,arguments)},writable:!0,enumerable:!1}),vector.extend({indexOf:Collection.indexOf,indexBy:Collection.indexBy,indicesOf:function(element,callback){return extend(Collection.indicesOf.apply(this,arguments),callback)},indicesBy:function(predicate,callback){return extend(Collection.indicesBy.apply(this,arguments),callback)},lastIndexBy:function(predicate,callback){return extend(Collection.lastIndexBy.apply(this,arguments),callback)},find:function(predicate,callback){return extend(Collection.find.apply(this,arguments),callback)},findOne:function(predicate,callback){return extend(Collection.findOne.apply(this,arguments),callback)},mode:function(callback){return extend(Collection.mode.apply(this),callback)},frequency:Collection.frequency,distribution:Collection.distribution,append:Collection.append,equal:Collection.equal,clone:function(callback){return extend(Collection.clone.apply(this),callback)},copy:function(callback){return extend(Collection.copy.apply(this),callback)},toArray:Collection.toArray,concat:function(){var args=Array.prototype.slice.call(arguments),end=args[args.length-1];return"function"==typeof end?extend(Collection.concat.apply(this,arguments),end):extend(Collection.concat.apply(this,arguments))},slice:function(begin,end,callback){return extend(Collection.slice.apply(this,arguments),callback)},split:function(predicate,callback){return extend(Collection.split.apply(this,arguments),callback)},unique:function(callback){return extend(Collection.unique.apply(this),callback)},filter:function(callback,next){return extend(Collection.filter.apply(this,arguments),next)},every:Collection.every,map:function(callback,next){return extend(Collection.map.apply(this,arguments),next)},some:Collection.some,reduce:function(callback,initialValue,next){return extend(Collection.reduce.apply(this,arguments),next)},reduceRight:function(callback,initialValue,next){return extend(Collection.reduceRight.apply(this,arguments),next)},union:function(that,callback){return extend(Collection.union.apply(this,arguments),callback)},sum:function(callback){var sum=0;if(this.equal(cache.values)&&0!==cache.sum)sum=cache.sum;else{for(var i=0;i<this.length;)sum+=this[i++];cache.sum=sum,cache.sync()}return result(sum,callback)},product:function(callback){var product=1;if(this.equal(cache.values)&&1!==cache.product)product=cache.product;else for(var i=0;i<this.length;)product*=this[i++],cache.product=product,cache.sync();return result(product,callback)},mean:function(callback){var mean=this.sum()/this.length;return result(mean,callback)},gmean:function(callback){var gmean=Math.pow(Math.abs(this.product()),1/this.length);return result(gmean,callback)},hmean:function(callback){function reciprocalSum(set){for(var i=0,sum=0;i<set.length;)sum+=1/Math.abs(set[i++]);return sum}var hmean=this.length/reciprocalSum(this);return result(hmean,callback)},qmean:function(callback){var qmean=Math.sqrt(this.pow(2).sum()/this.length);return result(qmean,callback)},pmean:function(p,callback){for(var pmean=0,i=0;i<this.length;)pmean+=Math.pow(this[i++],p);return pmean=Math.pow(pmean/this.length,1/p),result(pmean,callback)},median:function(callback){var buffer=this.copy();buffer.sort(asc);var median=this.length%2===0?(buffer[this.length/2-1]+buffer[this.length/2])/2:buffer[parseInt(this.length/2,10)];return result(median,callback)},range:function(callback){var range=this.max()-this.min();return result(range,callback)},variance:function(callback){var mean=0,variance=0;if(this.equal(cache.values)&&0!==cache.variance)variance=cache.variance;else{for(var i=0;i<this.length;i++){var _mean=mean;mean+=(this[i]-_mean)/(i+1),variance+=(this[i]-_mean)*(this[i]-mean)}variance/=this.length,cache.variance=variance,cache.sync()}return result(variance,callback)},stdev:function(percentile,callback){var stdev=0;return stdev=percentile?this.density(percentile).stdev():Math.sqrt(this.variance()),result(stdev,callback)},percentile:function(percent,callback){var buffer=this.copy();buffer.sort(asc);var percentile=buffer[0];return percent>0&&(percentile=buffer[Math.floor(this.length*percent)]),result(percentile,callback)},density:function(percent,callback){var slice,buffer=this.copy();if(buffer.sort(asc),1===percent)slice=buffer;else{var begin=Math.round(this.length*(.5-percent/2)-1),end=Math.round(this.length*(.5+percent/2)-1);slice=new Vector(buffer.slice(begin,end))}return result(slice,callback)},quantile:function(quantity,callback){var buffer=this.copy();buffer.sort(asc);var increment=1/quantity,results=new Vector;if(quantity>this.length)throw new RangeError("Subset quantity is greater than the Vector length");for(var i=increment;1>i;i+=increment){var index=Math.round(buffer.length*i)-1;index<buffer.length-1&&results.push(buffer[index])}return result(results,callback)},delta:function(callback){for(var delta=new Vector,i=1;i<this.length;i++)delta.push(this[i]-this[i-1]);return result(delta,callback)},sma:function(period,callback){var sma;if(1===period)sma=this;else{var sum=new Vector(this.slice(0,period)).sum();sma=new Vector([sum/period]);for(var i=1;i<this.length-period+1;i++)sum+=this[i+period-1]-this[i-1],sma.push(sum/period)}return result(sma,callback)},ema:function(options,callback){if("number"==typeof options){var length=options;options={period:length,ratio:function(n){return 2/(n+1)}}}for(var sum=new Vector(this.slice(0,options.period)).sum(),ema=new Vector([sum/options.period]),ratio=options.ratio(options.period),i=1;i<this.length-options.period+1;i++)ema.push(ratio*(this[i+options.period-1]-ema[i-1])+ema[i-1]);return result(ema,callback)},sample:{mean:function(callback){var mean=vector.sum()/(vector.length-1);return result(mean,callback)},gmean:function(callback){var gmean=Math.pow(Math.abs(vector.product()),1/(vector.length-1));return result(gmean,callback)},hmean:function(callback){function reciprocalSum(set){for(var i=0,sum=0;i<set.length;)sum+=1/Math.abs(set[i++]);return sum}var hmean=(vector.length-1)/reciprocalSum(vector);return result(hmean,callback)},qmean:function(callback){var qmean=Math.sqrt(vector.pow(2).sum()/(vector.length-1));return result(qmean,callback)},pmean:function(p,callback){for(var pmean=0,i=0;i<vector.length;)pmean+=Math.pow(vector[i++],p);return pmean=Math.pow(pmean/(vector.length-1),1/p),result(pmean,callback)},variance:function(callback){var mean=0,variance=0;if(vector.equal(cache.values)&&0!==cache.sample.variance)variance=cache.sample.variance;else{for(var i=0;i<vector.length;i++){var _mean=mean;mean+=(vector[i]-_mean)/(i+1),variance+=(vector[i]-_mean)*(vector[i]-mean)}variance/=vector.length-1,cache.sample.variance=variance,cache.sync()}return result(variance,callback)},stdev:function(callback){var stdev=Math.sqrt(vector.sample.variance());return result(stdev,callback)}},max:function(callback){var max=Math.max.apply({},this);return result(max,callback)},min:function(callback){var min=Math.min.apply({},this);return result(min,callback)},abs:function(callback){var abs=this.map(Math.abs);return result(abs,callback)},acos:function(callback){var acos=this.map(Math.acos);return result(acos,callback)},asin:function(callback){var asin=this.map(Math.asin);return result(asin,callback)},atan:function(callback){var atan=this.map(Math.atan);return result(atan,callback)},ceil:function(callback){var ceil=this.map(Math.ceil);return result(ceil,callback)},cos:function(callback){var cos=this.map(Math.cos);return result(cos,callback)},exp:function(callback){var exp=this.map(Math.exp);return result(exp,callback)},floor:function(callback){var floor=this.map(Math.floor);return result(floor,callback)},log:function(callback){var log=this.map(Math.log);return result(log,callback)},pow:function(exponent,callback){var pow=this.map(function(x){return Math.pow(x,exponent)});return result(pow,callback)},round:function(callback){var round=this.map(Math.round);return result(round,callback)},sin:function(callback){var sin=this.map(Math.sin);return result(sin,callback)},sqrt:function(callback){var sqrt=this.map(Math.sqrt);return result(sqrt,callback)},tan:function(callback){var tan=this.map(Math.tan);return result(tan,callback)},push:function(){var mean=0===this.length?0:cache.sum/this.length,variance=0===this.length?0:cache.variance*this.length,args=Array.prototype.slice.call(arguments),end=args[args.length-1],length=this.length;if("function"==typeof end){for(var i=0;i<args.length-1;i++){Array.prototype.push.call(this,args[i]),Array.prototype.push.call(cache.values,args[i]);var _mean=mean;mean+=(args[i]-_mean)/this.length,variance+=(args[i]-_mean)*(args[i]-mean),cache.sum+=args[i],cache.product*=args[i],length++}return variance/=this.length,cache.variance=variance,end(length)}for(var i=0;i<args.length;i++){Array.prototype.push.call(this,args[i]),Array.prototype.push.call(cache.values,args[i]);var _mean=mean;mean+=(args[i]-_mean)/this.length,variance+=(args[i]-_mean)*(args[i]-mean),cache.sum+=args[i],cache.product*=args[i],length++}return variance/=this.length,cache.variance=variance,length},add:function(that,callback){var add=new Vector;if(Array.isArray(that)){if(that.length!==this.length)throw new RangeError("Cannot add vectors of differing lengths");for(var i=0;i<this.length;i++)add[i]=this[i]+that[i]}else for(var j=0;j<this.length;j++)add[j]=this[j]+that;return result(add,callback)},subtract:function(that,callback){var subtract=new Vector;if(Array.isArray(that)){if(that.length!==this.length)throw new RangeError("Cannot subtract vectors of differing lengths");for(var i=0;i<this.length;i++)subtract[i]=this[i]-that[i]}else for(var j=0;j<this.length;j++)subtract[j]=this[j]-that;return result(subtract,callback)},multiply:function(that,callback){var multiply=new Vector;if(Array.isArray(that)){if(that.length!==this.length)throw new RangeError("Cannot multiply vectors of differing lengths");for(var i=0;i<this.length;i++)multiply[i]=this[i]*that[i]}else for(var j=0;j<this.length;j++)multiply[j]=this[j]*that;return result(multiply,callback)},divide:function(that,callback){var divide=new Vector;if(Array.isArray(that)){if(that.length!==this.length)throw new RangeError("Cannnot divide vectors of differing lengths");for(var i=0;i<this.length;i++)divide[i]=this[i]/that[i]}else for(var j=0;j<this.length;j++)divide[j]=this[j]/that;return result(divide,callback)}}),cache.sum=vector.sum(),cache.product=vector.product(),cache.variance=vector.variance(),vector};Array.prototype.toVector=function(){return new Vector(this)},"undefined"!=typeof window?(window.gauss="undefined"==typeof window.gauss?{}:window.gauss,window.gauss.Vector=Vector):exports=module.exports=Vector}();
!function(){var Collection="undefined"==typeof window?require("./collection"):window.gauss.Collection;Collection=new Collection;var Vector="undefined"==typeof window?require("./vector"):window.gauss.Vector,TimeSeries=function(values){"use strict";var timeseries=2===arguments.length?Array.prototype.slice.call(arguments):values;0===Array.prototype.slice.call(arguments).length&&(timeseries=[]);var result=function(value,callback){return callback?callback(value):value};return Object.defineProperty(timeseries,"extend",{value:function(methods,callback){return Collection.extend.bind(this),Collection.extend.apply(this,arguments)},writable:!0,enumerable:!1}),timeseries.extend({byValue:function(a,b){return a[1]<b[1]?-1:a[1]>b[1]?1:0},byDate:function(a,b){return a[0]<b[0]?-1:a[0]>b[0]?1:0},times:function(callback){var times=new Vector(Array.prototype.map.call(this,function(i){return i[0]}));return result(times,callback)},values:function(callback){var values=new Vector(Array.prototype.map.call(this,function(i){return i[1]}));return result(values,callback)}}),timeseries.extend({every:Collection.every,append:Collection.append}),timeseries};Array.prototype.toTimeSeries=function(){return new TimeSeries(this)},"undefined"!=typeof window?(window.gauss="undefined"==typeof window.gauss?{}:window.gauss,window.gauss.TimeSeries=TimeSeries):exports=module.exports=TimeSeries}();
!function(gauss){"function"==typeof define&&define.amd&&define("gauss",[],function(){return gauss})}(window.gauss);