Hello,
I am a new pymc and pytensor user and I am facing a problem with the following fit.
Here “data” is a 100 size array containing the data points with some np.nan elements and “total_cov” is the corresponding 100x100 covariance matrix, which contains np.nan elements when “data” is np.nan.
import numpy as np
import pytensor.tensor as pt
import pymc as pm
r = np.linspace(0, 1000, len(data))
#remove the NaNs from data and covariance matrix
w_cov = np.where(np.isfinite(total_cov))
total_cov = total_cov[w_cov]
w_not = np.where(np.isfinite(data))[0]
data = data[w_not]
def gnfw(x, p0, c500, a, b, c):
"""
Compute the gNFW profile at x
"""
gnfw_profile = p0*(x*c500)**(-c)*(1+(x*c500)**a)**((c-b)/a)
return gnfw_profile
with pm.Model() as model :
#gNFW params
p0 = pm.Uniform("p0", lower=0, upper=20)
c500 = 1.177
a = pm.Uniform("a", lower=0, upper=10)
b = pm.Uniform("b", lower=0, upper=20)
c = 0.3081
#multiplicative factor
eta = pm.Uniform("eta", lower=0., upper=4.)
#compute the model profile
prof = gnfw(r, p0, c500, a, b, c)
#multiply last bins by "eta"
mu = pt.subtensor.set_subtensor(prof[-50:], eta*prof[-50:])
#consider only the positions at which "data" is not np.nan
mu = mu[w_not]
ll = pm.MvNormal("likelihood", mu=mu, cov=total_cov, observed=data)
with model as model:
inference_data = pm.sample(chains=30, return_inferencedata=True, draws=1000, tune=1000, target_accept=0.95)
The fit works perfectly well when there is no np.nan in my data, so there’s no need to select the “w_not” elements in “mu”. On the contrary, when I need to select the “w_not” elements, this approach doesn’t seem to work.
I was not able to find a way to select some elements of “mu” with PyTensor functionalities. Also, I think that the problem is related to “subtensor.set_subtensor”. Is there a way to do what I would like to?