def test_irls(self):
		for i in range(5, TESTING_ITERATIONS):

			NOISE = 0

			columns = random.randint(2,i)

			A,x,y,initial_vector,initial_scale = generate_random.gen_noise(i,columns, NOISE)

			y_gui = copy.deepcopy(y)
			a_gui = copy.deepcopy(A)

			y_marta = copy.deepcopy(y.reshape(-1,1))
			a_marta = copy.deepcopy(A)

			# random float clipping between 0 and 10
			clipping_single = random.uniform(0.1, 4.0)

			test_kind='M'

			lamb=0.1

			'''
			xhat_marta = inv.irls(
				y=y_marta,
				a=a_marta,
				kind=test_kind,
				lossfunction='huber',
				regularization='l1',
				lmbd=lamb,
				initialx=initial_vector.reshape(-1,1),
				initialscale=initial_scale,
				clipping=clipping_single)[0][:,0]

			print "Marta's xhat for irls = ", xhat_marta
			'''

			def custom_reg_function(a,y,lamb):
				return lp.least_squares(a,y)

			xhat_linvpy = lp.irls(
				matrix_a=a_gui,
				vector_y=y_gui,
				loss_function=lp.psi_huber,
				clipping=clipping_single,
				scale=initial_scale,
				lamb=lamb,
				initial_x=initial_vector,
				kind=test_kind,
				regularization=custom_reg_function)

			print "LinvPy's xhat for irls = ", xhat_linvpy
			print "real xhat = ", x
			print "=================================="

			# only tests this if noise is zero otherwise it fails all the time
			# because values are not EXACTLY the same
			if NOISE == 0 :
				np.testing.assert_array_almost_equal(x, xhat_linvpy, decimal=5)
예제 #2
0
    def test_irls(self):
        for i in range(5, TESTING_ITERATIONS):

            NOISE = 0

            columns = random.randint(2, i)

            A, x, y, initial_vector, initial_scale = generate_random.gen_noise(
                i, columns, NOISE)

            y_gui = copy.deepcopy(y)
            a_gui = copy.deepcopy(A)

            y_marta = copy.deepcopy(y.reshape(-1, 1))
            a_marta = copy.deepcopy(A)

            # random float clipping between 0 and 10
            clipping_single = random.uniform(0.1, 4.0)

            test_kind = 'M'

            lamb = 0.1
            '''
			xhat_marta = inv.irls(
				y=y_marta,
				a=a_marta,
				kind=test_kind,
				lossfunction='huber',
				regularization='l1',
				lmbd=lamb,
				initialx=initial_vector.reshape(-1,1),
				initialscale=initial_scale,
				clipping=clipping_single)[0][:,0]

			print "Marta's xhat for irls = ", xhat_marta
			'''
            def custom_reg_function(a, y, lamb):
                return lp.least_squares(a, y)

            xhat_linvpy = lp.irls(matrix_a=a_gui,
                                  vector_y=y_gui,
                                  loss_function=lp.psi_huber,
                                  clipping=clipping_single,
                                  scale=initial_scale,
                                  lamb=lamb,
                                  initial_x=initial_vector,
                                  kind=test_kind,
                                  regularization=custom_reg_function)

            print "LinvPy's xhat for irls = ", xhat_linvpy
            print "real xhat = ", x
            print "=================================="

            # only tests this if noise is zero otherwise it fails all the time
            # because values are not EXACTLY the same
            if NOISE == 0:
                np.testing.assert_array_almost_equal(x, xhat_linvpy, decimal=5)
예제 #3
0
	def test_irls(self):
		for i in range(3, TESTING_ITERATIONS):

			NOISE = 0

			columns = random.randint(2,i)

			A, x, y, initial_x, scale = generate_random.gen_noise(i,columns,NOISE)

			y_gui = copy.deepcopy(y)
			a_gui = copy.deepcopy(A)

			y_marta = copy.deepcopy(y.reshape(-1,1))
			a_marta = copy.deepcopy(A)

			# random float clipping between 0 and 10
			clipping_tau = (random.uniform(0.1, 4.0), random.uniform(0.1, 4.0))

			test_kind='tau'

			lambda_parameter = 0

			xhat_marta = inv.irls(
				y=y_marta,
				a=a_marta,
				kind=test_kind,
				lossfunction='optimal',
				regularization='none',
				lmbd=lambda_parameter,
				initialx=initial_x.reshape(-1,1),
				initialscale=scale.reshape(-1,1),
				clipping=clipping_tau)[0][:,0].reshape(-1)

			print "Marta's xhat for tau irls = ", xhat_marta

			xhat_linvpy = lp.irls(
				matrix_a=a_gui,
				vector_y=y_gui,
				loss_function=lp.rho_optimal,
				clipping=clipping_tau,
				scale=scale,
				lamb=lambda_parameter,
				initial_x=initial_x,
				kind=test_kind)

			print "LinvPy's xhat for tau irls = ", xhat_linvpy
			print "real xhat = ", x
			print "=================================="

			# only tests this if noise is zero otherwise it fails all the time
			# because values are not EXACTLY the same
			if NOISE == 0 :
				np.testing.assert_array_almost_equal(x, xhat_linvpy, decimal=5)
예제 #4
0
	def test_fast_tau(self):
		for i in range(4, TESTING_ITERATIONS):

			NOISE = 0

			columns = random.randint(2,i)

			A, x, y, initial_x, scale = generate_random.gen_noise(i,columns,NOISE)

			y_gui = copy.deepcopy(y)
			a_gui = copy.deepcopy(A)

			y_marta = copy.deepcopy(y.reshape(-1,1))
			a_marta = copy.deepcopy(A)

			# random float clipping between 0 and 10
			clipping_tau = (random.uniform(0.1, 4.0), random.uniform(0.1, 4.0))

			# define parameters necessary for basic tau...
			lossfunction = 'optimal'

			# how many initial solutions do we want to try
			n_initial_solutions = random.randint(1,20)
			n_initial_solutions = 15

			xfinal_marta, tscalefinal_marta = inv.fasttau(
			  y=y_marta,
			  a=a_marta,
			  lossfunction=lossfunction,
			  clipping=clipping_tau,
			  ninitialx=n_initial_solutions
			)

			xfinal_lp, tscalefinal_lp = lp.fasttau(
			  y=y_gui,
			  a=a_gui,
			  loss_function=lp.rho_optimal,
			  clipping=clipping_tau,
			  ninitialx=n_initial_solutions
			)

			print "MARTA's fastau : ", xfinal_marta.reshape(-1), tscalefinal_marta			
			print "LinvPy's fastau : ", xfinal_lp.reshape(-1), tscalefinal_lp
			print "Real xhat = ", x
			print "==================="

			# only tests this if noise is zero otherwise it fails all the time
			# because values are not EXACTLY the same
			if NOISE == 0 :
				np.testing.assert_array_almost_equal(x, xfinal_lp.reshape(-1), decimal=5)
예제 #5
0
    def test_tau(self):
        for i in range(4, TESTING_ITERATIONS):

            NOISE = 0

            columns = random.randint(2, i)

            A, x, y, initial_x, scale = generate_random.gen_noise(
                i, columns, NOISE)

            y_gui = copy.deepcopy(y)
            a_gui = copy.deepcopy(A)

            y_marta = copy.deepcopy(y.reshape(-1, 1))
            a_marta = copy.deepcopy(A)

            # random float clipping between 0 and 10
            clipping_tau = (random.uniform(0.1, 4.0), random.uniform(0.1, 4.0))

            # define parameters necessary for basic tau...
            lossfunction = 'optimal'

            # how many initial solutions do we want to try
            n_initial_solutions = random.randint(1, 20)
            n_initial_solutions = 15

            # how many solutions do we keep
            n_best = random.randint(1, 20)
            n_best = 1
            '''
			# calls Marta's basic tau estimator
			xhat_marta, shat_marta = inv.basictau(
			  y=y_marta,
			  a=a_marta,
			  lossfunction=lossfunction,
			  clipping=clipping_tau,
			  ninitialx=n_initial_solutions,
			  nbest=n_best
			)
			'''

            # calls linvpy's basic tau estimator
            xhat_lp, shat_lp = lp.basictau(
                a=a_gui,
                y=y_gui,
                loss_function=lp.rho_optimal,
                clipping=clipping_tau,
                ninitialx=n_initial_solutions,
                nbest=n_best,
                regularization=lp.tikhonov_regularization,
                lamb=0)

            #print "Marta's output for tau-estimator = ", xhat_marta.reshape(-1)
            print "LinvPy's output for tau-estimator = ", xhat_lp.reshape(-1)
            print "Real xhat = ", x
            print "==================="

            # only tests this if noise is zero otherwise it fails all the time
            # because values are not EXACTLY the same
            if NOISE == 0:
                np.testing.assert_array_almost_equal(x,
                                                     xhat_lp.reshape(-1),
                                                     decimal=5)
예제 #6
0
import linvpy as lp
import mestimator_marta as marta
import generate_random as gen
import numpy as np
import matplotlib.pyplot as plt
import optimal as opt
from scipy.sparse.linalg import lsmr
import toolboxutilities as util
import toolboxinverse as inv
import copy
import random



gen.gen_noise(2,3)



#genA, geny = gen.generate_random(4,5)
#print "test =", lp.irls(genA, geny, lp.psi_huber)
'''
#28.04.16
last_x = np.array([1, 1])
last_a = np.random.rand(5, 2)
last_y = np.dot(last_a, last_x)
last_y = np.reshape(last_y, (5, 1))
y_noise = 0.5 * np.random.rand(5, 1) + last_y

print "My m-estimator = ", lp.irls(last_a, y_noise, lp.psi_huber, clipping=1.5, lamb=0, scale=2)

'''
예제 #7
0
import linvpy as lp
import mestimator_marta as marta
import generate_random as gen
import numpy as np
import matplotlib.pyplot as plt
import optimal as opt
from scipy.sparse.linalg import lsmr
import toolboxutilities as util
import toolboxinverse as inv
import copy
import random

gen.gen_noise(2, 3)

#genA, geny = gen.generate_random(4,5)
#print "test =", lp.irls(genA, geny, lp.psi_huber)
'''
#28.04.16
last_x = np.array([1, 1])
last_a = np.random.rand(5, 2)
last_y = np.dot(last_a, last_x)
last_y = np.reshape(last_y, (5, 1))
y_noise = 0.5 * np.random.rand(5, 1) + last_y

print "My m-estimator = ", lp.irls(last_a, y_noise, lp.psi_huber, clipping=1.5, lamb=0, scale=2)

'''

# I fix here the number of measurements of vector y
nmeasurements = 15
	def test_tau(self):
		for i in range(4, TESTING_ITERATIONS):

			NOISE = 0

			columns = random.randint(2,i)

			A, x, y, initial_x, scale = generate_random.gen_noise(i,columns,NOISE)

			y_gui = copy.deepcopy(y)
			a_gui = copy.deepcopy(A)

			y_marta = copy.deepcopy(y.reshape(-1,1))
			a_marta = copy.deepcopy(A)

			# random float clipping between 0 and 10
			clipping_tau = (random.uniform(0.1, 4.0), random.uniform(0.1, 4.0))

			# define parameters necessary for basic tau...
			lossfunction = 'optimal'

			# how many initial solutions do we want to try
			n_initial_solutions = random.randint(1,20)
			n_initial_solutions = 15

			# how many solutions do we keep
			n_best = random.randint(1,20)
			n_best = 1

			'''
			# calls Marta's basic tau estimator
			xhat_marta, shat_marta = inv.basictau(
			  y=y_marta,
			  a=a_marta,
			  lossfunction=lossfunction,
			  clipping=clipping_tau,
			  ninitialx=n_initial_solutions,
			  nbest=n_best
			)
			'''

			# calls linvpy's basic tau estimator
			xhat_lp, shat_lp = lp.basictau(
			  a=a_gui,
			  y=y_gui,
			  loss_function=lp.rho_optimal,
			  clipping=clipping_tau,
			  ninitialx=n_initial_solutions,
			  nbest=n_best,
			  regularization=lp.tikhonov_regularization,
			  lamb=0
			)

			#print "Marta's output for tau-estimator = ", xhat_marta.reshape(-1)
			print "LinvPy's output for tau-estimator = ", xhat_lp.reshape(-1)
			print "Real xhat = ", x
			print "==================="

			# only tests this if noise is zero otherwise it fails all the time
			# because values are not EXACTLY the same
			if NOISE == 0 :
				np.testing.assert_array_almost_equal(x, xhat_lp.reshape(-1), decimal=5)