Thank you!
Setting the alpha_zero’s as starting values via init or initval is throwing this error:
TypeError: can’t turn [Subtensor{int64}.0] and {} into a dict. Length of Subtensor{int64}.0 cannot be determined
How can I set the starting point of the time-varying process for the RandomGaussianWalk?
Also I am assuming I don’t need to specify the expected value as the previous alpha, given this is the whole point of the RGW?
I am trying to model the following time-varying process:
\theta_{t} = invlogit(\vec{\alpha_t} * x_{t}+ \beta + Ha)
with values:
\alpha_0 \sim N(0,2), \alpha_t \sim N(\alpha_{t-1},2)
\beta \sim N(0,2), Ha \sim N(0,2)
where \alpha_t are time-varying coefficients, \beta and Ha are constants.
This is my latest code.
def home_intensity_model(data, sample_shape=None):
with pm.Model(coords={"time": data["timedelta"]}) as model:
# how often do we update alpha?
# set this to every timedelta/row
interval = len(data["timedelta"])
# instantiate stochastic RV for global parameters
beta = pm.Normal('beta',mu=0,sigma=np.sqrt(2))
ha = pm.Normal('Ha',mu=0,sigma=np.sqrt(2))
#vector of time varying parameters as random walk
alpha_zero = pm.Normal("alpha_zero",mu=0,sigma=np.sqrt(2), shape=(sample_shape,1))
alpha= pm.GaussianRandomWalk("alpha",init=alpha_zero,sigma=np.sqrt(2),shape=(sample_shape,interval))
# Finally, define our deterministic prior variable theta
#To ensure that deterministic variables' values are accumulated during sampling, they should be instantiated
#using the named deterministic interface; this uses the Deterministic function to create the variable.
#Two things happen when a variable is created this way:
# 1- The variable is given a name (passed as the first argument)
# 2- The variable is appended to the model's list of random variables, which ensures that its values are tallied.
theta = pm.Deterministic("theta",pm.invlogit(pm.math.dot(data[pred_cols], alpha)+beta+ha))
# finally we define our likelihood, i.e. our sampling distribution of values we are trying to fit
# our observed thetas are between 0 and 1 but tilted towards 0
# try to fit Gamma distribution instead. Note that expected value of Gamma is alpha/beta, so set alpha as our theta and beta=1
#now define our likelihood
Y_obs =pm.Gamma("Y_obs", alpha=theta, beta =1 , observed= data[y])
# Can sample and look at distribution of our priors and thetas
trace = pm.sample(2, chains=1, cores=4)
pm.traceplot(trace)
plt.show()
return model