Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
Project: test
Views: 91872# -*- coding: utf-8 -*-1"""2Created on Tue Apr 22 11:38:49 201434@author: rlabbe5"""6from __future__ import division, print_function7import math8import matplotlib.pyplot as plt9import numpy.random as random101112class gaussian(object):13def __init__(self, mean, variance):14try:15self.mean = float(mean)16self.variance = float(variance)1718except:19self.mean = mean20self.variance = variance2122def __add__ (a, b):23return gaussian (a.mean + b.mean, a.variance + b.variance)2425def __mul__ (a, b):26m = (a.variance*b.mean + b.variance*a.mean) / (a.variance + b.variance)27v = 1. / (1./a.variance + 1./b.variance)28return gaussian (m, v)2930def __call__(self, x):31""" Impl32"""33return math.exp (-0.5 * (x-self.mean)**2 / self.variance) / \34math.sqrt(2.*math.pi*self.variance)353637def __str__(self):38return "(%f, %f)" %(self.mean, self.sigma)3940def stddev(self):41return math.sqrt (self.variance)4243def as_tuple(self):44return (self.mean, self.variance)4546def __tuple__(self):47return (self.mean, self.variance)4849def __getitem__ (self,index):50""" maybe silly, allows you to access obect as a tuple:51a = gaussian(3,4)52print (tuple(a))53"""54if index == 0: return self.mean55elif index == 1: return self.variance56else: raise StopIteration5758class KF1D(object):59def __init__ (self, pos, sigma):60self.estimate = gaussian(pos,sigma)6162def update(self, Z,var):63self.estimate = self.estimate * gaussian (Z,var)6465def predict(self, U, var):66self.estimate = self.estimate + gaussian (U,var)6768697071def mul2 (a, b):72m = (a['variance']*b['mean'] + b['variance']*a['mean']) / (a['variance'] + b['variance'])73v = 1. / (1./a['variance'] + 1./b['variance'])74return gaussian (m, v)757677#varying_error_kf( noise_factor=100)787980