Skip to content

Instantly share code, notes, and snippets.

@pavlin-policar
Created April 30, 2018 18:45
Show Gist options
  • Save pavlin-policar/579af1203cb772aca756a5c32469686f to your computer and use it in GitHub Desktop.
Save pavlin-policar/579af1203cb772aca756a5c32469686f to your computer and use it in GitHub Desktop.
Ridge regression on circulant matrices
import numpy as np
# Initialize the generating vector
x = np.array([1, 2, 3, 4, 5])
y = np.array([5, 4, 3, 2, 1])
# Initialize the permutation matrix
P = np.array([
[0, 0, 0, 0, 1],
[1, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 0],
])
# Form the circulant matrix using the generating vector
X = np.vstack((np.dot(np.linalg.matrix_power(P, i), x) for i in range(len(x))))
# Set the regularization coefficient
alpha = 1
# Solve closed form solution
w_spatial = np.linalg.inv(X.T.dot(X) + np.eye(len(x)) * alpha).dot(X.T).dot(y)
# Solve in frequency domain
x_freq = np.fft.fft(x)
y_freq = np.fft.fft(y)
w_freq = (np.conj(x_freq) * y_freq) / (np.conj(x_freq) * x_freq + alpha)
w_fft = np.real(np.fft.ifft(w_freq))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment