Skip to content

Instantly share code, notes, and snippets.

@effigies
Created August 12, 2014 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save effigies/fa69f8b9a9c72fe128c1 to your computer and use it in GitHub Desktop.
Save effigies/fa69f8b9a9c72fe128c1 to your computer and use it in GitHub Desktop.
Turner Least Squares
# Based off of Mumford et al. (2011)
# Derived from a figure, as there was no actual math presented :\
#
# It's a sort of minimal-assumption regression regularization method
import numpy as np
def turnerLeastSquares(designMatrix, samples):
nuisance = np.sum(designMatrix, axis=1).reshape((designMatrix.shape[0], 1))
fitted = np.zeros((designMatrix.shape[1], samples.shape[1]))
for j in range(designMatrix.shape[1]):
column = designMatrix[:, [j]]
# Create a design matrix with the current column and the rest
# of the matrix collapsed into a single column
turnerMatrix = np.hstack((column, nuisance - column))
fits = np.linalg.lstsq(turnerMatrix, samples)[0]
# Keep only the parameters of interest
fitted[j, :] = fits[0, :]
return fitted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment