Skip to content

Instantly share code, notes, and snippets.

@atduskgreg
Last active August 29, 2015 13:59
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 atduskgreg/10738360 to your computer and use it in GitHub Desktop.
Save atduskgreg/10738360 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# 1. Generate 100 points x uniformly distributed between 0 and 1, and let y = 2+3x+ζ,
# where ζ is a Gaussian random variable with a standard deviation of 0.5. Use an
# SVD to fit y = a + bx to this data set, finding a and b.
import numpy as np
# x is uniformly distributed
x = np.random.random(100)
for i in x:
# zeta is gaussian with sd = 0.5
zeta = np.random.normal(0, 0.5)
y = 2 + 3*x + zeta
array = np.array([y])
U,s,V = np.linalg.svd(array)
#
print("U:")
print(U.shape)
print(U)
print("s: ")
print(s.shape)
print(s)
print("V: ")
print(V.shape)
print(V)
pinv_svd = np.dot(np.dot(V.T,np.linalg.inv(np.diag(s))),U.T)
# ValueError: matrices are not aligned
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment