class_docstr: This implements the ensemble Kalman filter (EnKF). The EnKF uses
  an ensemble of hundreds to thousands of state vectors that are randomly
  sampled around the estimate, and adds perturbations at each update and
  predict step. It is useful for extremely large systems such as found
  in hydrophysics. As such, this class is admittedly a toy as it is far
  too slow with large N.
  
  There are many versions of this sort of this filter. This formulation is
  due to Crassidis and Junkins [1]. It works with both linear and nonlinear
  systems.
  
  Parameters
  ----------
  
  x : np.array(dim_x)
      state mean
  
  P : np.array((dim_x, dim_x))
      covariance of the state
  
  dim_z : int
      Number of of measurement inputs. For example, if the sensor
      provides you with position in (x,y), dim_z would be 2.
  
  dt : float
      time step in seconds
  
  N : int
      number of sigma points (ensembles). Must be greater than 1.
  
  K : np.array
      Kalman gain
  
  hx : function hx(x)
      Measurement function. May be linear or nonlinear - converts state
      x into a measurement. Return must be an np.array of the same
      dimensionality as the measurement vector.
  
  fx : function fx(x, dt)
      State transition function. May be linear or nonlinear. Projects
      state x into the next time period. Returns the projected state x.
  
  
  Attributes
  ----------
  x : numpy.array(dim_x, 1)
      State estimate
  
  P : numpy.array(dim_x, dim_x)
      State covariance matrix
  
  x_prior : numpy.array(dim_x, 1)
      Prior (predicted) state estimate. The *_prior and *_post attributes
      are for convienence; they store the  prior and posterior of the
      current epoch. Read Only.
  
  P_prior : numpy.array(dim_x, dim_x)
      Prior (predicted) state covariance matrix. Read Only.
  
  x_post : numpy.array(dim_x, 1)
      Posterior (updated) state estimate. Read Only.
  
  P_post : numpy.array(dim_x, dim_x)
      Posterior (updated) state covariance matrix. Read Only.
  
  z : numpy.array
      Last measurement used in update(). Read only.
  
  R : numpy.array(dim_z, dim_z)
      Measurement noise matrix
  
  Q : numpy.array(dim_x, dim_x)
      Process noise matrix
  
  fx : callable (x, dt)
      State transition function
  
  hx : callable (x)
      Measurement function. Convert state `x` into a measurement
  
  K : numpy.array(dim_x, dim_z)
      Kalman gain of the update step. Read only.
  
  inv : function, default numpy.linalg.inv
      If you prefer another inverse function, such as the Moore-Penrose
      pseudo inverse, set it to that instead: kf.inv = np.linalg.pinv
  
  Examples
  --------
  
  .. code-block:: Python
  
      def hx(x):
         return np.array([x[0]])
  
      F = np.array([[1., 1.],
                    [0., 1.]])
      def fx(x, dt):
          return np.dot(F, x)
  
      x = np.array([0., 1.])
      P = np.eye(2) * 100.
      dt = 0.1
      f = EnsembleKalmanFilter(x=x, P=P, dim_z=1, dt=dt,
                               N=8, hx=hx, fx=fx)
  
      std_noise = 3.
      f.R *= std_noise**2
      f.Q = Q_discrete_white_noise(2, dt, .01)
  
      while True:
          z = read_sensor()
          f.predict()
          f.update(np.asarray([z]))
  
  See my book Kalman and Bayesian Filters in Python
  https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python
  
  References
  ----------
  
  - [1] John L Crassidis and John L. Junkins. "Optimal Estimation of
    Dynamic Systems. CRC Press, second edition. 2012. pp, 257-9.
class_docstr_tok: ["This","implements","the","ensemble","Kalman","filter","(","EnKF",")",".","The","EnKF","uses","an","ensemble","of","hundreds","to","thousands","of","state","vectors","that","are","randomly","sampled","around","the","estimate",",","and","adds","perturbations","at","each","update","and","predict","step",".","It","is","useful","for","extremely","large","systems","such","as","found","in","hydrophysics",".","As","such",",","this","class","is","admittedly","a","toy","as","it","is","far","too","slow","with","large","N.","There","are","many","versions","of","this","sort","of","this","filter",".","This","formulation","is","due","to","Crassidis","and","Junkins","[","1","]",".","It","works","with","both","linear","and","nonlinear","systems",".","Parameters","--","--","--","--","--","x",":","np.array","(","dim_x",")","state","mean","P",":","np.array","(","(","dim_x",",","dim_x",")",")","covariance","of","the","state","dim_z",":","int","Number","of","of","measurement","inputs",".","For","example",",","if","the","sensor","provides","you","with","position","in","(","x",",","y",")",",","dim_z","would","be","2.","dt",":","float","time","step","in","seconds","N",":","int","number","of","sigma","points","(","ensembles",")",".","Must","be","greater","than","1",".","K",":","np.array","Kalman","gain","hx",":","function","hx","(","x",")","Measurement","function",".","May","be","linear","or","nonlinear","-","converts","state","x","into","a","measurement",".","Return","must","be","an","np.array","of","the","same","dimensionality","as","the","measurement","vector",".","fx",":","function","fx","(","x",",","dt",")","State","transition","function",".","May","be","linear","or","nonlinear",".","Projects","state","x","into","the","next","time","period",".","Returns","the","projected","state","x",".","Attributes","--","--","--","--","--","x",":","numpy.array","(","dim_x",",","1",")","State","estimate","P",":","numpy.array","(","dim_x",",","dim_x",")","State","covariance","matrix","x_prior",":","numpy.array","(","dim_x",",","1",")","Prior","(","predicted",")","state","estimate",".","The","*","_prior","and","*","_post","attributes","are","for","convienence",";","they","store","the","prior","and","posterior","of","the","current","epoch",".","Read","Only",".","P_prior",":","numpy.array","(","dim_x",",","dim_x",")","Prior","(","predicted",")","state","covariance","matrix",".","Read","Only",".","x_post",":","numpy.array","(","dim_x",",","1",")","Posterior","(","updated",")","state","estimate",".","Read","Only",".","P_post",":","numpy.array","(","dim_x",",","dim_x",")","Posterior","(","updated",")","state","covariance","matrix",".","Read","Only",".","z",":","numpy.array","Last","measurement","used","in","update","(",")",".","Read","only",".","R",":","numpy.array","(","dim_z",",","dim_z",")","Measurement","noise","matrix","Q",":","numpy.array","(","dim_x",",","dim_x",")","Process","noise","matrix","fx",":","callable","(","x",",","dt",")","State","transition","function","hx",":","callable","(","x",")","Measurement","function",".","Convert","state","`","x","`","into","a","measurement","K",":","numpy.array","(","dim_x",",","dim_z",")","Kalman","gain","of","the","update","step",".","Read","only",".","inv",":","function",",","default","numpy.linalg.inv","If","you","prefer","another","inverse","function",",","such","as","the","Moore-Penrose","pseudo","inverse",",","set","it","to","that","instead",":","kf.inv","=","np.linalg.pinv","Examples","--","--","--","--","..","code-block",":",":","Python","def","hx","(","x",")",":","return","np.array","(","[","x","[","0","]","]",")","F","=","np.array","(","[","[","1.",",","1",".","]",",","[","0.",",","1",".","]","]",")","def","fx","(","x",",","dt",")",":","return","np.dot","(","F",",","x",")","x","=","np.array","(","[","0.",",","1",".","]",")","P","=","np.eye","(","2",")","*","100.","dt","=","0.1","f","=","EnsembleKalmanFilter","(","x=x",",","P=P",",","dim_z=1",",","dt=dt",",","N=8",",","hx=hx",",","fx=fx",")","std_noise","=","3.","f.R","*","=","std_noise","*","*","2","f.Q","=","Q_discrete_white_noise","(","2",",","dt",",",".01",")","while","True",":","z","=","read_sensor","(",")","f.predict","(",")","f.update","(","np.asarray","(","[","z","]",")",")","See","my","book","Kalman","and","Bayesian","Filters","in","Python","https",":","\/\/github.com\/rlabbe\/Kalman-and-Bayesian-Filters-in-Python","References","--","--","--","--","--","-","[","1","]","John","L","Crassidis","and","John","L.","Junkins",".","``","Optimal","Estimation","of","Dynamic","Systems",".","CRC","Press",",","second","edition",".","2012.","pp",",","257-9","."]
code: def __init__(self, x, P, dim_z, dt, N, hx, fx):
      if dim_z <= 0:
          raise ValueError("dim_z must be greater than zero")
  
      if N <= 0:
          raise ValueError("N must be greater than zero")
  
      dim_x = len(x)
      self.dim_x = dim_x
      self.dim_z = dim_z
      self.dt = dt
      self.N = N
      self.hx = hx
      self.fx = fx
      self.K = zeros((dim_x, dim_z))
      self.z = array([[None] * self.dim_z]).T
      self.S = zeros((dim_z, dim_z))  # system uncertainty
      self.SI = zeros((dim_z, dim_z))  # inverse system uncertainty
  
      self.initialize(x, P)
      self.Q = eye(dim_x)  # process uncertainty
      self.R = eye(dim_z)  # state uncertainty
      self.inv = np.linalg.inv
  
      # used to create error terms centered at 0 mean for
      # state and measurement
      self._mean = zeros(dim_x)
      self._mean_z = zeros(dim_z)
  
file_path: filterpy/filterpy/kalman/ensemble_kalman_filter.py
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  from copy import deepcopy
  import numpy
  from numpy import array, zeros, eye, dot
  from numpy.random import multivariate_normal
  from filterpy.common import pretty_str, outer_product_sum
label_desc: Use image node_id 1 to create a new EnsembleKalmanFilter object from inherited base classes: object with example: obj = EnsembleKalmanFilter(x, P, dim_z, dt, N, hx, fx)