Ejemplo n.º 1
0
## Let's define some true values for the w vector, which
## later we will try to estimate from the noisy data.
## Note that w_true is a vertical vector

w_true = np.array([[1], \
                   [2]])

(X, Y_true) = rdu.generate_linear_relation_dataset(w_true, n_samples)

w_hat_list = [
]  ## This is where we are going to store all the estimatec w_hat's
runs_so_far = 0
while (runs_so_far < n_runs):

    ## Add gaussian noise to all points
    Y = rdu.add_gaussian_noise(Y_true, 0, 1, 1)
    w_hat = ols.solve_ols(X, Y)  ## w_hat is a vertical vector
    w_hat_list.append(w_hat)
    runs_so_far += 1
    if runs_so_far % 1000 == 0:
        print "Executed ", runs_so_far, "runs so far..."

## Now let's plot the distributions of w_0 and w_1

w_matrix = pu.list_of_vertical_vectors_to_matrix(w_hat_list, w_true.shape[0])
print "Plotting distribution of w_0..."
pu.plot_histogram(w_matrix[0,:], \
                  "Distribution of values of $\hat{w}_0$ (" + str(n_runs) + " runs). True $w_0$ = 1", \
                  'green', \
                  '$w_0$', \
                  'nr. runs', \
Ejemplo n.º 2
0
## later we will try to estimate from the noisy data.
## Note that w_true is a vertical vector

w_true = np.array([[1], \
                   [2]])

(X, Y_true) = rdu.generate_linear_relation_dataset(w_true, n_samples)

w_hat_list = [
]  ## This is where we are going to store all the estimated w_hat's
w_hat_outliers_list = []  ## and this is for the case of outliers
runs_so_far = 0
while (runs_so_far < n_runs):

    ## Add gaussian noise to all points
    Y = rdu.add_gaussian_noise(Y_true, 0, 1, 1)
    ## Add 1% of outliers (gaussian noise with much higher variance!)
    Y_outliers = rdu.add_gaussian_noise(Y, 0, 5, p_outliers)
    w_hat = lad.solve_lad_soft(X, Y, 1000)  ## w_hat is a vertical vector
    w_hat_list.append(w_hat)
    w_hat_outliers = lad.solve_lad_soft(X, Y_outliers,
                                        1000)  ## w_hat is a vertical vector
    w_hat_outliers_list.append(w_hat_outliers)
    runs_so_far += 1
    if runs_so_far % 100 == 0:
        print "Executed ", runs_so_far, "runs so far..."

## Now let's plot the distributions of w_0 and w_1
percent_outliers = str(int(100 * p_outliers))
w_matrix = pu.list_of_vertical_vectors_to_matrix(w_hat_list, w_true.shape[0])
w_matrix_outliers = pu.list_of_vertical_vectors_to_matrix(
Ejemplo n.º 3
0
import numpy as np
import sys

sys.path.append('../utils')
import lad as lad
import random_dataset_utils as rdu

n_x_dimensions = 1
n_y_dimensions = 1
n_runs_left = 10000
n_samples = 500

## Let's define some true values for the w vector, which
## later we will try to estimate from the noisy data.
linear_weigths = np.array([[1], [2]])

(X, Y) = rdu.generate_linear_relation_dataset(linear_weigths, n_samples)

## Add a moderate amount of gaussion noise to all points
Y = rdu.add_gaussian_noise(Y, 0, 1, 1)

w_hat = lad.solve_lad_soft(X, Y, 1000000)

print w_hat