I was using PyMC and try to follow blog by Dr. Juan Camilo Orduz, and a blog by Dr. Robert Kübler. In the blog he use GaussianRandomWalk
to model time varying coefficient. However when I try it on my own dataset, the random walk coefficients overfit, and cause other prior not to converge (r_hat > 2
). So instead of modeling time-varying coefficient as pure GaussianRandomWalk
, I think transforming the input using signal filtering method like savgol_filter
would be nice
However if I apply the savgol_filter
directly it give me
ValueError: setting an array element with a sequence.
which is somewhat expected as the input variable is a pytensor, but savgol_filter
need a numpy input
Another way I can think of is to apply convolution
to the signal, however the document seems to be missing. I opened an issue here just in case
I’ve tried implement convolution operation, but my code is very slow
def pt_conv1d(a, v, sig_sz=None, mode="full"):
# Based on https://stackoverflow.com/questions/71309358/what-is-the-best-way-to-implement-1d-convolution-in-python
assert mode in ["full", "valid", "same"]
kernel = v[::-1]
if sig_sz is None:
sig_sz = a.shape[0]
ker_sz = v.shape[0]
if len(kernel) == 1:
mode = "full"
if mode == "full":
min_idx = 0
max_idx = sig_sz + ker_sz - 1
elif mode == "valid":
min_idx = ker_sz - 1
max_idx = -1*(ker_sz - 1)
else:
min_idx = ker_sz//2 - 1
max_idx = sig_sz + ker_sz - min_idx - 2
return pt.as_tensor_variable([
pm.math.dot(
a[max(0, i): min(i+ker_sz, sig_sz)],
kernel[max(-i,0): sig_sz-i*(sig_sz-ker_sz<i)],
) for i in range(1 - ker_sz, sig_sz)
][min_idx: max_idx])
So is there a recommend way to apply signal filtering?