Created
February 8, 2017 11:10
-
-
Save jvlmdr/83100b60b5be8cdf35d91543009f5102 to your computer and use it in GitHub Desktop.
Generate smooth functions in numpy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def covar1(m, s): | |
s = float(s) | |
r = np.arange(m, dtype=np.float) | |
d = np.reshape(r, (m, 1)) - np.reshape(r, (1, m)) | |
k = np.exp(-(0.5/s**2) * (d * d)) | |
return k | |
def smooth1(m, s): | |
k = covar1(m, s) | |
w, v = sqrt(k) | |
z = np.random.randn(m) | |
# A = V diag(w) such that E(x x') = E(A z z' A') = A A' = K | |
x = np.dot(v, w * z) | |
return x | |
def smooth2(m, s): | |
m0, m1 = m | |
s = float(s) | |
k0 = covar1(m0, s) | |
k1 = covar1(m1, s) | |
w0, v0 = sqrt(k0) | |
w1, v1 = sqrt(k1) | |
z = np.random.randn(m0, m1) | |
wz = np.reshape(w0, (m0, 1)) * np.reshape(w1, (1, m1)) * z | |
x = np.dot(np.dot(v0, wz), v1.T) | |
return x | |
def sqrt(s): | |
w, v = np.linalg.eigh(s) | |
w = np.sqrt(np.maximum(0, w)) | |
return w, v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment