1
2
3
4
5 import numpy.linalg
6
8 """
9 Defines a cost function with a quadratic cost
10 """
12 """
13 Creates the function :
14 - x is the parameters for the function
15 - y is the data to approximate
16 - f is an object function defining the gradient and the Hessian if needed
17 It takes two parameters when called, the values x where the function should be computed and an array params that contains the parameters. The gradient of the function is the gradient for the parameters
18 """
19 self.x = x
20 self.y = y
21 self.f = f
22
24 """
25 Computes the cost for the specified parameters
26 """
27 return numpy.sum((self.y-self.f(self.x, params))**2)
28
30 """
31 Computes the gradient of the function
32 """
33 inter = -2 * self.f.gradient(self.x, params) * (self.y-self.f(self.x, params))[..., numpy.newaxis]
34 inter.shape=(-1, inter.shape[-1])
35 return numpy.sum(inter, axis = 0)
36
38 """
39 Compute sthe hessian of the fit function
40 """
41 inter = -2 * self.f.hessian(self.x, params) * (self.y-self.f(self.x, params))[..., numpy.newaxis, numpy.newaxis] + 2 * self.f.gradient(self.x, params)[..., numpy.newaxis] * self.f.gradient(self.x, params)[..., numpy.newaxis, :]
42 shape = inter.shape[-2], inter.shape[-1]
43 inter.shape = (-1, inter.shape[-2] * inter.shape[-1])
44 temp = numpy.sum(inter, axis = 0)
45 temp.shape = shape
46 return temp
47