Particle filter
Particle filter class
- class pyvib.ParticleFilter.ParticleFilter(N, R, Q, model, nStates, nParameters, measuredStates, resampleAlways=False, resampleDebug=False)
A particle filter class
- Parameters
N (int) – Number of particles
R (float or array_like) – Variance of measured states len(R) == len(measuredStates)
Q (float or array_like) – Variance of actuation error Part of model
model (function(u, states, parameters, Q)) –
Model that generates next step of states using previous states, parameters and Q statesDerivative can be used as a placeholder for the derivative Example:
- def model(u, states, parameters, statesDerivative, Q):
m = parameters[:, 0] k = parameters[:, 1] c = parameters[:, 2] dt = 1.0 statesDerivative[:, 0] = states[:, 1] statesDerivative[:, 1] = 1.0/m*(-k*states[:, 0] - c*states[:, 1] + (u + randn(states.shape[0])*np.sqrt(Q)) states[:, 0] += statesDerivative[:, 0]*dt states[:, 1] += statesDerivative[:, 1]*dt
nStates (int) – Number of states in the system
nParameters (int) – Number of parameters in the system
measuredStates (int or array_like) – Which state number are measured Could be a single number or multiple in a list. Observation (z) must have the same length.
Methods
createGaussianParticles
(mean, var)Create gaussian distributed particles
createUniformParticles
(ranges)Create uniformly distributed particles
estimate
()Estimates true value and variance of states and parameters Results are saved in ParticleFilter.meanList and -.varList
Get meanlist and varlist Mean and var
getPercentile
(per)Get the percentile of values
Return the parameters of particles
Return the states of particles
plotHistogram
(column)Plot histogram of a state or parameter
predict
(u)Predict state of next time step using control input
resample
([thrScale])Resamples particles IF necessary
simulateTrend
(iterations, u[, percs])Simulate the trend moving forward using the particles parameters and state
update
(z[, debug])Update the weights based on measurements and observation noise
- createGaussianParticles(mean, var)
Create gaussian distributed particles
- Parameters
mean (array_like) – Mean value of gaussian distributed guess len(mean) = nStates + nParameters
std (array_like) – Variation of gaussian distributed guess len(var) = nStates + nParameters
- createUniformParticles(ranges)
Create uniformly distributed particles
- Parameters
ranges (2D numpy.ndarray) – The uniform range of starting guess Shaped as [nStates + nParamteres, 2]
- estimate()
Estimates true value and variance of states and parameters Results are saved in ParticleFilter.meanList and -.varList
- getMeanAndVariance()
Get meanlist and varlist Mean and var
- Returns
meanList (list of float 1D array) – Mean of each state for each time step
varList (list of float 1D array) – Variance of each state for each time step
- getPercentile(per)
Get the percentile of values
- Parameters
per (float) – Percentile <0.0, 1.0>
- Returns
percentile – Percentiles
- Return type
float 1D array
- get_parameters()
Return the parameters of particles
- Returns
parameters – Parameters of particles
- Return type
float 2D array
- get_states()
Return the states of particles
- Returns
states – States of particles
- Return type
float 2D array
- plotHistogram(column)
Plot histogram of a state or parameter
- Parameters
column (int) – Which column of self.particles should be pltted
- predict(u)
Predict state of next time step using control input
- Parameters
u (float or array_like) – The control input. Must follow rules of model function
- resample(thrScale=0.5)
Resamples particles IF necessary
- Parameters
thrScale (float, optional) – Thresholds for resampling scaled by number of particles
- simulateTrend(iterations, u, percs=[0.5])
Simulate the trend moving forward using the particles parameters and state
- Parameters
iterations (int) – Number of iterations to simulate
u (Data type defined by user in self.model) – Control input
percs (list of floats, optional) – Which percentile of parameters and states to simulate as true values
- Returns
output – Simulation results output[i, j, k] gives the following i - the iteration j - the percentile k - the state number
- Return type
float 3D array
- update(z, debug=False)
Update the weights based on measurements and observation noise
- zfloat or array_like:
The observation len(z) == len(measuredStates)