python 3.x - Particle filter probability density not as expected -


playing bit particle filter wonder why probability density doesn't i'm expecting:

i tried implement simple model $x_k = x_k-1 + noise$ , measurement $z = x_k + noise$ measurement values switching (toggling) between 0 , 1.

my expectations:

  • mean = 0.5 --- works expected
  • probability density function (normal distributed) peaks @ 0 , 1 , rest 0 --- works not @ all

the resulting probability density normal distribution around 0.5: enter image description here

so distribution correct or there bug in code?
need change in code binary distribution i'm expecting?

#!/usr/bin/python3  import math import numpy np import matplotlib.pyplot plt import matplotlib.animation animation  xmin = -1.15 xmax =  2.15 fig = plt.figure() ax = fig.add_axes([0, 0, 1, 1], frameon=true, xlim=( xmin, xmax ), ylim=( -0.1, 0.5 ) ) color = 'k' ims = []  stdmodel = 0.05 stdmeasure = 0.15  # number of particles n = 1000 x_particles = np.random.uniform( xmin, xmax, size=n ) x_weightsln = np.ones(n) * math.log(1/n)  in range( 100 ):     measure = i%2 # toggle between 0 , 1      # predict:     # stationary model: x_k = x_k-1 + noise     x_particles[:] += np.random.randn(n) * stdmodel      ### calculate , display probability density @ point     x_particlessortindices = np.argsort( x_particles )     x_particlessort = x_particles[x_particlessortindices]     x_weightssort = np.exp( x_weightsln[x_particlessortindices] )     x_weightssortcumsum = np.cumsum( x_weightssort )     samplepos = np.linspace( xmin, xmax, 201 )     samplevalindices = np.minimum( np.searchsorted( x_particlessort, samplepos ), n-1 )     sampleval = x_weightssortcumsum[samplevalindices]     sampleval = sampleval[1:] - sampleval[:-1]     samplepos = samplepos[1:]     sampleval /= sum( sampleval )     thisplot = ax.plot(         samplepos,sampleval,'-'+color+'',         x_particles,np.random.uniform( -0.09, -0.01, size=n),'k.',         [measure], 0, 'bx'     )     ims.append( thisplot )     ###      # measure:     # direct measurement: z = z + noise     z_particles = x_particles + np.random.randn(n) * stdmeasure     # normal gauss:     #x_weights *= (1/math.sqrt(2*math.pi*stdmeasure)) * np.exp( -(measure-z_particles)**2/(2*stdmeasure) )     # logarithmic version, ignoring prefactor normalisation rid of anyway     x_weightsln += -(measure-z_particles)**2/(2*stdmeasure)     x_weightsln -= np.log(sum(np.exp(x_weightsln))) # normalize      # resample:     doresample = (1. / np.sum(np.exp(2*x_weightsln))) < n/2     if doresample:         # stratified_resample         positions = (np.random.random(n) + range(n)) / n         indexes = np.zeros(n, 'i')         cumulative_sum = np.cumsum(np.exp(x_weightsln))         i, j = 0, 0         while < n:             if positions[i] < cumulative_sum[j]:                 indexes[i] = j                 += 1             else:                 j += 1         x_particles[:] = x_particles[indexes]         x_weightsln.fill(math.log(1.0 / n))     if doresample:         if 'k' == color:             color = 'r'         else:             color = 'k'  im_ani = animation.artistanimation(fig, ims, interval=50, blit=true ) plt.show() 


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -