BayesianCurveFitting.py
Go to the documentation of this file.
1 #coding:utf-8
2 import numpy, pylab
3 
4 # http://aidiary.hatenablog.com/entry/20100404/1270359720
5 
7  def __init__(self, M, ALPHA, BETA, xlist, tlist):
8  self.M = M
9  self.ALPHA = ALPHA
10  self.BETA = BETA
11  self.xlist = xlist
12  self.tlist = tlist
13 
14  def y(self, x, wlist):
15  ret = wlist[0]
16  for i in range(1, self.M + 1):
17  ret += wlist[i] * (x ** i)
18  return ret
19 
20  def phi(self, x):
21  data = []
22  for i in range(0, self.M + 1):
23  data.append(x**i)
24  ret = numpy.matrix(data).reshape((self.M + 1, 1))
25  return ret
26 
27  # eq (1.70)
28  def mean(self, x, S):
29  sums = pylab.matrix(pylab.zeros((self.M + 1, 1)))
30  for n in range(len(self.xlist)):
31  sums += self.phi(self.xlist[n]) * self.tlist[n]
32  ret = self.BETA * self.phi(x).transpose() * S * sums
33  return ret
34 
35  # eq (1.71)
36  def variance(self, x, S):
37  ret = 1.0 / self.BETA + self.phi(x).transpose() * S * self.phi(x)
38  return ret
39 
40  def main(self):
41  sums = pylab.matrix(pylab.zeros((self.M + 1, self.M + 1)))
42  for n in range(len(self.xlist)):
43  sums += self.phi(self.xlist[n]) * self.phi(self.xlist[n]).transpose()
44  I = pylab.matrix(numpy.identity(self.M + 1))
45  S_inv = self.ALPHA * I + self.BETA * sums
46  S = S_inv.getI()
47 
48  xs = numpy.linspace(min(self.xlist), max(self.xlist), 500)
49  means = []
50  uppers = []
51  lowers = []
52  for x in xs:
53  m = self.mean(x, S)[0, 0]
54  s = numpy.sqrt(self.variance(x, S)[0, 0])
55  u = m + s
56  l = m - s
57  means.append(m)
58  uppers.append(u)
59  lowers.append(l)
60  return([xs, means, uppers, lowers])
double min(const OneDataStat &d)
wrapper function for min method for boost::function
Definition: one_data_stat.h:94
double max(const OneDataStat &d)
wrapper function for max method for boost::function
def __init__(self, M, ALPHA, BETA, xlist, tlist)


jsk_pcl_ros
Author(s): Yohei Kakiuchi
autogenerated on Mon May 3 2021 03:03:46