示例#1
0
	def test_grad_SEE(self):
		data_file = 'curvefitting.txt'
		for order in xrange(10):
			[X, Y] = hw1.getData(data_file)
			phi = hw1.designMatrix(X, order)
			weights = hw1.regressionFit(X, Y, phi)
			delta = 1
			analytic = hw1.computeSEEGrad(X,Y, weights, order).flatten()
			approx = gd.gradient_approx_SEE(X, Y, weights, order, delta).flatten()
			for i in xrange(order):	
				self.assertAlmostEqual(analytic[i], approx[i])

			delta2 = .1
			analytic2 = hw1.computeSEEGrad(X,Y, weights, order).flatten()
			approx2 = gd.gradient_approx_SEE(X, Y, weights, order, delta2).flatten()
			for i in xrange(order):	
				self.assertAlmostEqual(analytic2[i], approx2[i])

			delta3 = .01
			analytic3 = hw1.computeSEEGrad(X,Y, weights, order).flatten()
			approx3 = gd.gradient_approx_SEE(X, Y, weights, order, delta3).flatten()
			for i in xrange(order):	
				self.assertAlmostEqual(analytic3[i], approx3[i])

			delta4 = .001
			analytic4 = hw1.computeSEEGrad(X,Y, weights, order).flatten()
			approx4 = gd.gradient_approx_SEE(X, Y, weights, order, delta4).flatten()
			for i in xrange(order):	
				self.assertAlmostEqual(analytic4[i], approx4[i])
示例#2
0
	def __init__(self, x0, step_size, eps, 
		verbose=False, data_file = 'curvefitting.txt', order=0):
		""" Specify the initial guess, the step size and the convergence criterion"""
		""" Verbose prints debug messages for checking functions and things """
		self.first = x0
		self.step_size = step_size 	# eta
		self.eps = eps
		self.verbose = verbose

		# May not be necessary
		[self.X, self.Y] = hw1.getData(data_file)
		self.phi = hw1.designMatrix(self.X, order)
		self.order = order
示例#3
0
    def test_compare_derivative(self):
        """ Verify the gradient using the numerical derivative code """
        """ Part of question 2.2 """
        order = 4
        [X,Y] = hw1.getData('curvefitting.txt')
        Phi_matrix=hw1.designMatrix(X,order)
        #print Phi_matrix.shape
        weight_vector=hw1.regressionFit(X,Y,Phi_matrix)
        SSEG=hw1.computeSEEGrad(X,Y,weight_vector,order)
        approx = gd.gradient_approx_SEE(X, Y, weight_vector, order, 1e-10)
        true_deriv = hw1.computeSEEGrad(X,Y, weight_vector, order)

        print "COMPARE approx", approx
        print "TRUE ", true_deriv
示例#4
0
 def test_computeSEE(self):
     order = 2
     [X,Y] = hw1.getData('curvefitting.txt')
     phi_matrix = hw1.designMatrix(X,order)
     weights = hw1.regressionFit(X,Y,phi_matrix)
     hw1.computeSEE(X,Y,weights,order)