BayesianCurveFitting.py
Go to the documentation of this file.
00001 #coding:utf-8
00002 import numpy, pylab
00003 
00004 # http://aidiary.hatenablog.com/entry/20100404/1270359720
00005 
00006 class BayesianCurveFitting(object):
00007     def __init__(self, M, ALPHA, BETA, xlist, tlist):
00008         self.M = M
00009         self.ALPHA = ALPHA
00010         self.BETA = BETA
00011         self.xlist = xlist
00012         self.tlist = tlist
00013 
00014     def y(self, x, wlist):
00015         ret = wlist[0]
00016         for i in range(1, self.M + 1):
00017             ret += wlist[i] * (x ** i)
00018         return ret
00019 
00020     def phi(self, x):
00021         data = []
00022         for i in range(0, self.M + 1):
00023             data.append(x**i)
00024         ret = numpy.matrix(data).reshape((self.M + 1, 1))
00025         return ret
00026 
00027     # eq (1.70)
00028     def mean(self, x, S):
00029         sums = pylab.matrix(pylab.zeros((self.M + 1, 1)))
00030         for n in range(len(self.xlist)):
00031             sums += self.phi(self.xlist[n]) * self.tlist[n]
00032         ret = self.BETA * self.phi(x).transpose() * S * sums
00033         return ret
00034 
00035     # eq (1.71)
00036     def variance(self, x, S):
00037         ret = 1.0 / self.BETA + self.phi(x).transpose() * S * self.phi(x)
00038         return ret
00039 
00040     def main(self):
00041         sums = pylab.matrix(pylab.zeros((self.M + 1, self.M + 1)))
00042         for n in range(len(self.xlist)):
00043             sums += self.phi(self.xlist[n]) * self.phi(self.xlist[n]).transpose()
00044         I = pylab.matrix(numpy.identity(self.M + 1))
00045         S_inv = self.ALPHA * I + self.BETA * sums
00046         S = S_inv.getI()
00047 
00048         xs = numpy.linspace(min(self.xlist), max(self.xlist), 500)
00049         means = []
00050         uppers = []
00051         lowers = []
00052         for x in xs:
00053             m = self.mean(x, S)[0, 0]
00054             s = numpy.sqrt(self.variance(x, S)[0, 0])
00055             u = m + s
00056             l = m - s
00057             means.append(m)
00058             uppers.append(u)
00059             lowers.append(l)
00060         return([xs, means, uppers, lowers])


jsk_pcl_ros
Author(s): Yohei Kakiuchi
autogenerated on Wed Sep 16 2015 04:36:47