1 
  2 /**
  3  * 
  4  * A TimeSeries is a set of x and y values.
  5  * 
  6  * @class
  7  * @param x
  8  * @param y
  9  * @returns
 10  */
 11 function TimeSeries(name, x, y){
 12 	this.name = name;
 13 	this.interpolation = Interpolation.LINEAR;
 14 	if(y != null && x == null){
 15 		x = new Array();
 16 		for(var i = 0; i < y.length; i++){
 17 			x.push(i);
 18 		}
 19 	}
 20 	if(x == null){
 21 		x = new Array();
 22 	}
 23 	if(y == null){
 24 		y = new Array();
 25 	}
 26 	
 27 	var result = BSpline.evaluate(x, y);
 28 	this.x = result.x;
 29 	this.y = result.y;
 30 }
 31 
 32 ///**
 33 // * 
 34 // */
 35 //TimeSeries.prototype.valuesInRange = function(start, end, numSamples){
 36 //	var values = new Array(numSamples);
 37 //	
 38 //	for(var i = 0; i <= numSamples; i++){
 39 //		values[i] = this.valueAt(start+(end-start)*(i/numSamples));
 40 //	}
 41 //	
 42 //	return values;
 43 //};
 44 
 45 /**
 46  * evaluates f(x) with a given interpolation or approximation
 47  * 
 48  * @param x
 49  * @returns
 50  */
 51 TimeSeries.prototype.valueAt = function(x){
 52 	if(x < Math.min.apply(null, this.x) || x > Math.max.apply(null, this.x)){
 53 		return 0;
 54 	}
 55 	var value = null;
 56 	
 57 	if(this.interpolation == Interpolation.NEAREST){
 58 		var nearestIndex = 0;
 59 		for(var i = 0; i < this.x.length; i++){
 60 			if(Math.abs(this.x[i] - x) < Math.abs(this.x[nearestIndex] - x)){
 61 				nearestIndex = i;
 62 			}
 63 		}
 64 		value = this.y[nearestIndex];
 65 	}else if(this.interpolation == Interpolation.LINEAR){
 66 		var pos = 0;
 67 		for(var i = 0; i < this.x.length-1; i++){
 68 			if(x >= this.x[i] && x < this.x[i+1]){
 69 				pos = i;
 70 			}
 71 		}
 72 		
 73 		if(x == this.end){
 74 			value = this.y[this.y.length -1];
 75 		}else{
 76 			var width = this.x[pos+1] - this.x[pos];
 77 			var u = (x - this.x[pos]) / width;
 78 			value = (1-u)*this.y[pos] + u*this.y[pos+1];
 79 		}
 80 	}
 81 	
 82 	return value;
 83 };
 84 
 85 Object.defineProperty(TimeSeries.prototype, "start", {
 86 	get: function(){
 87 		return Math.min.apply(null, this.x);
 88 	}
 89 });
 90 
 91 Object.defineProperty(TimeSeries.prototype, "end", {
 92 	get: function(){
 93 		return  Math.max.apply(null, this.x);
 94 	}
 95 });
 96 
 97 Object.defineProperty(TimeSeries.prototype, "max", {
 98 	get: function(){
 99 		 return Math.max.apply(null, this.y);
100 	}
101 });
102 
103 Object.defineProperty(TimeSeries.prototype, "min", {
104 	get: function(){
105 		return Math.min.apply(null, this.y);
106 	}
107 });
108 
109 Object.defineProperty(TimeSeries.prototype, "average", {
110 	get: function(){
111 		var sum = 0;
112 		for(var i = 0; i < this.y.length; i++){
113 			sum += this.y[i];
114 		}
115 		
116 		return sum/this.y.length;
117 	}
118 });
119 
120 Object.defineProperty(TimeSeries.prototype, "variance", {
121 	get: function(){
122 		var sum = 0;
123 		var average = this.average;
124 		for(var i = 0; i < this.y.length; i++){
125 			sum += Math.pow(this.y[i] - average, 2);
126 		}
127 		
128 		return sum/(this.y.length-1);
129 	}
130 });
131 
132 /**
133  * create a time series with #count random samples between 0 and 1
134  * 
135  * @class
136  * @param name
137  * @param count
138  * @augments TimeSeries
139  * @returns
140  */
141 function RandomTimeSeries(name, count){
142 	var x = new Array();
143 	var y = new Array();
144 	for(var i = 0; i < count; i++){
145 		x.push(i);
146 		y.push(Math.random());
147 	}
148 	
149 	TimeSeries.call(this, name, x, y);
150 }
151 RandomTimeSeries.prototype = new TimeSeries();
152 
153 /**
154  * create a TimeSeries reflecting a PoisonDistribution with the given parameters
155  * 
156  * @class
157  * @param lambda
158  * @param numSamples
159  * @param start
160  * @param end
161  * @param offset
162  * @augments TimeSeries
163  * @returns
164  */
165 function PoisonTimeSeries(name, lambda, numSamples, start, end, offset){
166 	var x = new Array();
167 	var y = new Array();
168 	for(var i = 0; i <= numSamples; i++){
169 		var px = start + (end-start)*(i/numSamples);
170 		var fx = poisonDistribution(lambda, px);
171 		x.push(px - offset);
172 		y.push(fx);
173 	}
174 	
175 	TimeSeries.call(this, name, x, y);
176 }
177 PoisonTimeSeries.prototype = new TimeSeries();
178 
179 
180