-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKalmanEngine.js
More file actions
31 lines (26 loc) · 1.01 KB
/
Copy pathKalmanEngine.js
File metadata and controls
31 lines (26 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class PriceKalmanFilter{
constructor(R = 0.01, Q= 0.1){
this.R = R; // Measurement noise: how much "static" the DEX has
this.Q = Q; // Process noise: how much the "true price" actually varies
this.x = null; // Filttered price(state)
this.P = 1; // Estimation error covariance
}
filter(measurement){
if(this.x === null){
this.x = measurement; // Initialize with the first measurement
return this.x;
}
//1. Prediction phase
this.P = this.P + this.Q;
//2. Update phase
const K = this.P / (this.P + this.R); // Kalman Gain
this.x = this.x + K * (measurement - this.x); // Update estimate with measurement
this.P = (1 - K) * this.P; // Update error covariance
return {
filteredPrice: this.x,
kalmanGain: K,
innovation: Math.abs(measurement - this.x)// Difference between measurement and estimate(noise)
}
}
}
module.exports = PriceKalmanFilter;