CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download
Project: test
Views: 91872
1
# -*- coding: utf-8 -*-
2
"""
3
Created on Tue Apr 22 11:38:49 2014
4
5
@author: rlabbe
6
"""
7
from __future__ import division, print_function
8
import math
9
import matplotlib.pyplot as plt
10
import numpy.random as random
11
12
13
class gaussian(object):
14
def __init__(self, mean, variance):
15
try:
16
self.mean = float(mean)
17
self.variance = float(variance)
18
19
except:
20
self.mean = mean
21
self.variance = variance
22
23
def __add__ (a, b):
24
return gaussian (a.mean + b.mean, a.variance + b.variance)
25
26
def __mul__ (a, b):
27
m = (a.variance*b.mean + b.variance*a.mean) / (a.variance + b.variance)
28
v = 1. / (1./a.variance + 1./b.variance)
29
return gaussian (m, v)
30
31
def __call__(self, x):
32
""" Impl
33
"""
34
return math.exp (-0.5 * (x-self.mean)**2 / self.variance) / \
35
math.sqrt(2.*math.pi*self.variance)
36
37
38
def __str__(self):
39
return "(%f, %f)" %(self.mean, self.sigma)
40
41
def stddev(self):
42
return math.sqrt (self.variance)
43
44
def as_tuple(self):
45
return (self.mean, self.variance)
46
47
def __tuple__(self):
48
return (self.mean, self.variance)
49
50
def __getitem__ (self,index):
51
""" maybe silly, allows you to access obect as a tuple:
52
a = gaussian(3,4)
53
print (tuple(a))
54
"""
55
if index == 0: return self.mean
56
elif index == 1: return self.variance
57
else: raise StopIteration
58
59
class KF1D(object):
60
def __init__ (self, pos, sigma):
61
self.estimate = gaussian(pos,sigma)
62
63
def update(self, Z,var):
64
self.estimate = self.estimate * gaussian (Z,var)
65
66
def predict(self, U, var):
67
self.estimate = self.estimate + gaussian (U,var)
68
69
70
71
72
def mul2 (a, b):
73
m = (a['variance']*b['mean'] + b['variance']*a['mean']) / (a['variance'] + b['variance'])
74
v = 1. / (1./a['variance'] + 1./b['variance'])
75
return gaussian (m, v)
76
77
78
#varying_error_kf( noise_factor=100)
79
80