In [1]:
"""
IPython Notebook v4.0 para python 2.7
Librerías adicionales: numpy, matplotlib
Contenido bajo licencia CC-BY 4.0. Código bajo licencia MIT. (c) Sebastian Flores.
"""
# Configuracion para recargar módulos y librerías
%reload_ext autoreload
%autoreload 2
%matplotlib inline
from IPython.core.display import HTML
HTML(open("style/mat281.css", "r").read())
Out[1]:
Ilustraremos el funcionamiento del método con datos sintéticos: $$ y(x) = 5 \cos \Big( \frac{\pi}{4} x \Big) + \mathcal{N}\Big(0,1\Big)$$
Buscaremos ajustar un modelo del tipo $$ y(x) = a \cos \Big( b x + c\Big) + d$$ minimizando el error cuadrático.
El error predictivo del modelo será calculado utilizando RMSE: $$ E(o,p) = \sqrt{ \frac{1}{N}\sum_{i=1}^N (o_i - p_i)^2 }$$ El RMSE corresponde a la desviación estándar de los residuos.
In [ ]:
import numpy as np
from mat281_code import model
# Parameters
M = 5 # particiones
# Load data
data = model.load_data("data/dataN5000.txt") # Change here
N = data.shape[0]
testing_size = int(1./M * N)
# Permute the data
np.random.seed(23) # Change here
data = np.random.permutation(data)
# Create vector to store the prediction error
prediction_error = np.zeros(M)
# Perform Cross Validation
for i in range(M):
index = np.arange(N)
testing_index = np.logical_and(i*testing_size < index,
index < (i+1)*testing_size)
# Do the split
testing_data = data[testing_index,:]
training_data = data[np.logical_not(testing_index),:]
# Train model excluding the holdout set
training_params = model.get_params(training_data)
# Test with the holdout set
prediction_error[i] = model.get_error(training_params, testing_data)
print "Prediction error estimated on ", prediction_error[i], "\n"
# Train model with all the data
all_data_params = model.get_params(data)
# Report
print "Average of prediction error", prediction_error.mean()
print "Standard Deviation of prediction error", prediction_error.std()
# Plot the model
model.plot(training_data, testing_data, training_params, all_data_params)
In [ ]:
import numpy as np
from mat281_code import model
# Parameters
M = 200 # Muestras
# Load data
data = model.load_data("data/dataN10.txt") # Change here
N = data.shape[0]
split = int(0.7*N) # Change here
# Create vector to store the prediction error
prediction_error = np.zeros(M)
for i in range(M):
# Permute the data
np.random.seed(i) # HERE IS THE MAIN POINT #
data = np.random.permutation(data)
# Do the split
training_data = data[:split,:]
testing_data = data[split:,:]
# Train model excluding the holdout set
training_params = model.get_params(training_data)
# Test with the holdout set
prediction_error[i] = model.get_error(training_params, testing_data)
print "Prediction error estimated on ", prediction_error[i], "\n"
# Train model with all the data
all_data_params = model.get_params(data)
# Report
print "Average of prediction error", prediction_error.mean()
print "Standard Deviation of prediction error", prediction_error.std()
# Plot the model
model.plot(training_data, testing_data, training_params, all_data_params)
In [ ]:
import numpy as np
from mat281_code import model
# Parameter free (M=N)
# Load data
data = model.load_data("data/dataN20.txt") # Change here
N = data.shape[0]
# Create vector to store the prediction error
prediction_error = np.zeros(N)
for i in range(N):
testing_index = np.zeros(N, dtype=bool)
testing_index[i] = True
# Do the split
testing_data = data[testing_index,:]
training_data = data[np.logical_not(testing_index),:]
# Train model excluding the holdout set
training_params = model.get_params(training_data)
# Test with the holdout set
prediction_error[i] = model.get_error(training_params, testing_data)
print "Prediction error estimated on ", prediction_error[i]
# Train model with all the data
all_data_params = model.get_params(data)
# Report
print "Average of prediction error", prediction_error.mean()
print "Standard Deviation of prediction error", prediction_error.std()
# Plot the model
model.plot(training_data, testing_data, training_params, all_data_params)
In [ ]:
from scipy.misc import comb
N = 10
split = int(0.7*N)
print comb(N,split)
In [ ]:
import numpy as np
from mat281_code import model
import itertools # Library to get the permutations
from scipy.misc import comb
# Load data
data = model.load_data("data/dataN20.txt") # Change here
N = data.shape[0]
split = int(0.7*N)
M = int(comb(N,split))
# Create vector to store the prediction error
prediction_error = np.zeros(M)
index = np.arange(N)
for i, training_index in enumerate(itertools.combinations(index,split)):
# Do the split
training_data = data[np.array(training_index),:]
testing_index = list(set(range(N))-set(training_index))
testing_data = data[np.array(testing_index),:]
# Train model excluding the holdout set
training_params = model.get_params(training_data)
# Test with the holdout set
prediction_error[i] = model.get_error(training_params, testing_data)
# Train model with all the data
all_data_params = model.get_params(data)
# Report
print "Average of prediction error", prediction_error.mean()
print "Standard Deviation of prediction error", prediction_error.std()
# Plot the model
model.plot(training_data, testing_data, training_params, all_data_params)
In [ ]:
import numpy as np
from numpy import linalg
def norm_1_very_wrong(x):
norm1 = 0
for i in range(len(x)):
norm1 = norm1 + abs(x[i])
return norm1
def norm_1_wrong(x):
norm1 = 0
for xi in x: # Dont use the index if possible
norm1 += abs(xi)
return norm1
def norm_1_better(x):
return np.abs(x).sum()
def norm_1(x):
return linalg.norm(x,1)
In [ ]:
my_big_vector = np.random.rand(100000)
%timeit norm_1_very_wrong(my_big_vector)
%timeit norm_1_wrong(my_big_vector)
%timeit norm_1_better(my_big_vector)
%timeit norm_1(my_big_vector)
In [ ]:
import numpy as np
from numpy import linalg
def norm_2_very_wrong(x):
norm2 = 0
for i in range(len(x)):
norm2 = norm2 + x[i]**2
return norm2**0.5
def norm_2_wrong(x):
norm2 = 0
for xi in x:
norm2 += xi**2
return np.sqrt(norm2)
def norm_2_better(x):
return np.sqrt((x**2).sum())
def norm_2(x):
return linalg.norm(x,2)
In [ ]:
my_big_vector = np.random.rand(100000)
%timeit norm_2_very_wrong(my_big_vector)
%timeit norm_2_wrong(my_big_vector)
%timeit norm_2_better(my_big_vector)
%timeit norm_2(my_big_vector)
In [ ]:
import numpy as np
from numpy import linalg
def norm_inf_very_wrong(x):
norminf = 0
for i in range(len(x)):
norminf = max(norminf, abs(x[i]))
return norminf
def norm_inf_wrong(x):
norminf = 0
for xi in x:
norminf = max(norminf, abs(xi))
return norminf
def norm_inf_better(x):
return np.abs(x).max()
def norm_inf(x):
return linalg.norm(x,np.inf)
In [ ]:
my_big_vector = np.random.rand(100000)
%timeit norm_inf_very_wrong(my_big_vector)
%timeit norm_inf_wrong(my_big_vector)
%timeit norm_inf_better(my_big_vector)
%timeit norm_inf(my_big_vector)
In [ ]:
linalg.norm?
En general (demo ): $$ ||x||_p \leq n^{\frac{1}{p}-\frac{1}{q}} ||x||_q$$
Consideremos $R^2$ y $F(x) = F (x_1 , x_2 ) = (x_1 + 1)^2 + (x_2 + 1)^2$ . Tenemos que claramente $$ \max_{||x||_1 \leq 1} F(x) \neq \max_{||x||_2 \leq 1} F(x) \neq \max_{||x||_{\infty} \leq 1} F(x)$$ y $$ \textrm{argmax}_{||x||_1 \leq 1} F(x) \neq \textrm{argmax}_{||x||_2 \leq 1} F(x) \neq \textrm{argmax}_{||x||_{\infty} \leq 1} F(x)$$
Pero profe, claro, su problema era trivial...
Cómo estaba minimizando sobre espacios distintos era lógico que llega a valores distintos.
Fijemos el espacio de minimización y pongamos la norma en el funcional a minimizar
Consideremos $m, b ∈ R$ y sean ciertos pares de datos $$(x_1 , y_1 ), (x_2 , y_2 ), ..., (x_n , y_n )$$ que deseamos modelar utilizando $y = mx + b$.
Recordemos que la minimización en norma 2 de cada grupo del cuadrteto de Anscombe daba los mismos parámetros. ¿Que sucede con las otras normas?