pymc.exceptions.SamplingError: Initial evaluation of model at starting point failed!

as the topic name, I use spatial climate data to model tick counts in each county.
I have used CAR model to model spatial error.
here’s part of the code,

kommun_names = gdf_2023["kom_name"].tolist()  # Store municipality names
for name in kommun_names:
    G.add_node(name)
for idx1, row1 in gdf_2023.iterrows():
    for idx2, row2 in gdf_2023.iterrows():
        if idx1 != idx2 and row1.geometry.touches(row2.geometry):
            G.add_edge(row1["kom_name"], row2["kom_name"])
adj_matrix = nx.to_numpy_array(G, nodelist=kommun_names)
adj_matrix += np.eye(adj_matrix.shape[0])
logE = 0
with pm.Model(coords={"area_idx": np.arange(len(tick))}) as fixed_spatial_model:
    beta0 = pm.Normal("beta0", mu=0.0, tau=1.0e-5)
    beta1 = pm.Normal("beta1", mu=0.0, tau=1.0e-5,shape=(df_X.shape[1],))
    # variance parameter of the independent random effect
    tau_ind = pm.Gamma("tau_ind", alpha=3.2761, beta=1.81)
    # variance parameter of the spatially dependent random effects
    tau_spat = pm.Gamma("tau_spat", alpha=1.0, beta=1.0)
    alpha = pm.Beta("alpha", alpha=1, beta=1)

    # area-specific model parameters
    # independent random effect
    theta = pm.Normal("theta", mu=0, tau=tau_ind, dims="area_idx")
    # spatially dependent random effect, alpha fixed
    phi = pm.CAR("phi", mu=np.zeros(len(tick)), tau=tau_spat, alpha=alpha, W=adj_matrix, dims="area_idx")

    # exponential of the linear predictor -> the mean of the likelihood
    mu = pm.Deterministic("mu", pt.exp(logE + beta0 + pt.dot(df_X, beta1) + theta + phi), dims="area_idx")
    overdisp = pm.Gamma("overdisp", alpha=2, beta=2)
    # likelihood of the observed data
    y_i = pm.NegativeBinomial("y_i", alpha=overdisp,mu=mu, observed=tick, dims="area_idx")

    # saving the residual between the observation and the mean response for the area
    res = pm.Deterministic("res", tick - mu, dims="area_idx")

    initial_beta0 = np.log(np.mean(tick) + 1e-3)
    start = {
    "beta0": initial_beta0,
    "beta1": np.zeros(df_X.shape[1]),  # Vector of zeros, shape (16,)
    "tau_ind": 1.0,                    # Reasonable starting value for tau_ind
    "tau_spat": 1.0,                   # Reasonable starting value for tau_spat
    "theta": np.zeros(len(tick)),       # Zero random effect for each area
    "phi": np.zeros(len(tick))          # Zero spatial effect for each area
    }
    # sampling the model
    fixed_spatial_idata = pm.sample(2000, tune=2000)

and log:

pymc.exceptions.SamplingError: Initial evaluation of model at starting point failed!
Starting values:
{'beta0': array(-0.00864732), 'beta1': array([ 0.43360899,  0.0165883 , -0.6106348 ,  0.04969502, -0.41653248,
        0.56580465, -0.35319304, -0.62928801, -0.44185885, -0.26512687,
       -0.46520866, -0.84918115, -0.23743283,  0.77291029, -0.99620026,
       -0.73819431]), 'tau_ind_log__': array(1.35830089), 'tau_spat_log__': array(0.6427573), 'alpha_logodds__': array(0.87986205), 'theta': array([-7.10863289e-01, -8.46685731e-01, -2.67034749e-01, -7.63368436e-01,
        2.13772277e-01, -1.79841608e-01,  5.36627419e-01,  9.81370606e-01,
       -1.64072779e-01,  8.53645923e-01, -3.51788069e-02,  9.35610493e-01,
       -3.79340553e-01, -8.41653009e-01, -9.43500808e-01, -8.44590253e-01,
       -3.92255764e-01,  3.79685438e-01,  1.19541806e-01,  6.23484639e-02,
       -2.53743306e-01, -7.99894642e-01,  8.03709733e-01,  1.23673422e-01,
       -2.85492520e-01, -6.94460965e-01, -3.88325775e-01,  2.49700341e-01,
        3.47907648e-02,  4.20344774e-01,  1.04205670e-01, -5.80052477e-01,
       -7.47368981e-01, -9.48149021e-01, -4.55731107e-01, -9.29916407e-01,
        7.84803087e-02,  1.15684808e-01, -9.18264358e-01,  8.31368477e-01,
       -3.12784061e-01,  8.56856787e-01,  7.62389847e-01, -2.32316300e-01,
        2.98574838e-02,  1.06699308e-01,  7.56680807e-01, -1.98072243e-01,
       -9.46697729e-01,  6.60479855e-01, -3.87679928e-01,  4.13408669e-01,
       -9.88388002e-01,  4.05846765e-01,  8.10699128e-01,  3.19388098e-01,
        2.15462037e-01,  5.83588554e-01,  5.59829158e-01, -3.40258797e-01,
        4.73939726e-01,  5.16610032e-01,  1.34523144e-01, -8.42233221e-01,
       -1.22724802e-01, -7.45316116e-01,  6.90664459e-01, -4.90238604e-01,
        4.23428302e-01,  3.88861165e-01,  6.12916231e-01, -4.98974419e-01,
        7.34336530e-01, -8.62088596e-01, -2.24952692e-01,  2.07326223e-01,
       -8.26567436e-01,  6.07487452e-01, -9.79233743e-01, -7.70530276e-01,
        2.68830946e-01,  9.57323078e-01,  2.93910783e-01,  5.77207607e-01,
       -4.76076908e-01, -9.23011124e-01, -7.71460729e-01,  6.66846881e-02,
       -5.03878195e-01,  7.32548857e-01,  4.02989309e-01,  4.29904498e-04,
       -3.17847570e-01, -9.08848442e-01, -2.54292932e-01,  2.10937621e-01,
       -5.62049618e-01,  7.83572578e-01,  6.57108504e-01, -4.55702902e-01,
        7.15250377e-01, -6.54441482e-01,  5.70377959e-01,  7.89976236e-01,
        2.33803443e-01, -8.27199102e-01, -2.19441584e-01,  8.65994713e-01,
       -5.68984791e-01, -1.67678994e-01,  7.11931588e-01,  2.19587863e-01,
        4.56918323e-01,  4.87816704e-01, -7.76713858e-01, -6.55889465e-02,
       -4.65990371e-01, -9.88101477e-01, -7.50260244e-01, -4.54339536e-02,
        6.35833631e-01,  4.33119192e-01, -6.68254018e-01,  8.78275820e-01,
        2.57356251e-01, -2.87677613e-01,  3.26292075e-01, -1.70668190e-01,
        3.98223405e-02,  9.79514877e-01,  9.63254733e-02, -2.03770822e-01,
       -9.70525958e-01, -7.22812392e-01,  4.62016485e-01, -9.96137704e-01,
       -5.13315468e-01, -5.50886245e-01,  6.62024926e-01,  6.45351254e-01,
       -8.58433633e-01,  5.07402594e-01, -4.16033152e-01, -5.27381980e-01,
       -5.45683284e-01,  7.76614955e-01,  4.67730409e-01,  5.16838791e-01,
       -9.17591289e-01,  6.62864575e-01,  7.36093648e-01, -5.42331794e-01,
        2.87389222e-01, -9.76483003e-01, -6.63277217e-01, -8.38417818e-01,
       -2.06687352e-01, -3.15423448e-02, -3.79137744e-01, -5.57938155e-01,
       -5.04897008e-01, -9.82481034e-01, -8.73830464e-01,  1.55200069e-01,
        4.65371738e-01,  4.16484183e-01,  7.55403652e-01,  3.47573709e-01,
        5.55178408e-01, -8.24218380e-01, -5.65349557e-01,  8.02577733e-01,
       -7.88991632e-01,  9.75207886e-01,  6.12757436e-01,  4.17461636e-01,
       -4.04074302e-01,  1.23902094e-01, -7.48305030e-01, -6.45460730e-01,
        5.13732704e-01,  2.99730485e-01,  9.09731005e-01, -6.75957381e-01,
        7.78418599e-01, -2.22541704e-01,  9.01611134e-01,  4.24282447e-01,
       -8.65176680e-01, -7.44117973e-01, -2.38228462e-01, -3.93960920e-01,
        8.00469400e-01, -6.06510971e-01,  8.79300336e-01,  9.11228721e-01,
        1.02201253e-01,  2.69162745e-02,  6.93016806e-01, -7.39762605e-01,
       -4.82273592e-01,  5.78204841e-01, -4.65894968e-01,  6.01137370e-01,
        8.66744326e-03,  3.10289426e-01, -9.71243446e-01,  6.46843264e-01,
       -7.97369150e-02, -8.73870972e-02, -8.22393356e-01, -2.20903444e-01,
        6.76711648e-01,  7.25753064e-01,  2.87681214e-01,  9.48960980e-02,
       -6.45735633e-02, -2.10954865e-01, -8.76034999e-01, -6.83751422e-01,
       -6.48329738e-01, -9.64329742e-01,  9.13175008e-01, -4.02108618e-01,
       -7.95119286e-01, -3.43749207e-01,  7.35463652e-02,  2.14837728e-01,
       -8.17131875e-01, -6.35506029e-01,  2.13593083e-01,  1.51365308e-01,
        7.31152532e-02,  7.70387167e-01, -3.19583048e-01,  6.97322629e-01,
       -9.61876225e-02, -2.39554324e-01, -9.87295963e-02,  7.70311536e-01,
        8.96699675e-01, -3.83261893e-01,  4.69129760e-01,  9.11320170e-01,
        3.38881943e-01, -1.63808491e-01, -3.41986236e-01, -2.96818773e-01,
        7.91074802e-01, -8.75000551e-01,  6.94344559e-01, -8.00034251e-01,
       -5.26506274e-01, -2.03856672e-01,  6.19386239e-01, -9.49493720e-01,
       -8.69909069e-01, -5.86641005e-01,  2.01515592e-01,  6.45753707e-01,
       -2.18822248e-02, -8.06206979e-01,  9.62297679e-01, -6.95209516e-01,
        8.81279639e-01, -3.55280811e-01, -2.27319624e-01,  5.34975994e-01,
       -9.14484425e-01,  7.94410075e-01, -3.31201886e-01,  7.32272368e-01,
       -5.07596731e-02,  4.87088048e-01,  9.83990312e-02,  8.75457316e-01,
        3.55855522e-01, -3.55755824e-01, -7.08255359e-01, -9.18634468e-02,
        8.62354471e-02, -8.19084118e-01,  4.20687280e-01,  8.09632524e-01]), 'phi': array([-0.36749515, -0.25228655, -0.41279353,  0.23529318,  0.83040083,
        0.721619  , -0.97123151,  0.20656592, -0.81014624,  0.24381084,
       -0.85738268, -0.12960058,  0.28655509, -0.6333417 ,  0.57363079,
        0.48886406,  0.5034794 , -0.46573459,  0.17768361, -0.96274086,
        0.29681098,  0.42258715,  0.08450794,  0.58829174, -0.95505799,
       -0.65378787, -0.11387985,  0.95144923, -0.08581248, -0.19648482,
        0.29615487,  0.14145148, -0.69303887, -0.50522472,  0.65199347,
        0.21217768,  0.70297474, -0.41166968,  0.72959326, -0.92365276,
       -0.37223593,  0.06603676,  0.69865331,  0.79301209,  0.78907393,
        0.43388815,  0.5697557 , -0.53660381, -0.90472876,  0.62684301,
        0.63779169,  0.89209868, -0.63772774,  0.42287976,  0.18702984,
       -0.06583077, -0.1281155 ,  0.51605076,  0.63498346, -0.0443403 ,
       -0.69191234, -0.11623554, -0.00794979,  0.44118913,  0.63233087,
       -0.12586375, -0.35131094, -0.74158757, -0.81692212,  0.39125052,
        0.52955764,  0.44282385,  0.55954917, -0.3955081 , -0.79306125,
       -0.5877738 , -0.58604528,  0.15788597,  0.65757851,  0.57301809,
        0.78133529, -0.37470345,  0.14236327,  0.34746368, -0.61516443,
        0.77206688,  0.67218304, -0.08530151, -0.51088269,  0.20267586,
        0.20145047,  0.37600009, -0.19525985,  0.1535937 , -0.26492596,
       -0.93605477, -0.08641725,  0.52402583,  0.16862045,  0.965194  ,
       -0.75804481, -0.16984531, -0.44185105, -0.58455455,  0.18293709,
        0.80606776, -0.07134133, -0.68833603,  0.58134663,  0.33255703,
       -0.81989438, -0.41969141,  0.60810154, -0.42603102, -0.00623414,
        0.34983757, -0.91840823,  0.02016399,  0.03128757, -0.4880488 ,
        0.23494367, -0.53595766, -0.68877394, -0.47939872, -0.19120311,
        0.27380027, -0.02920806, -0.31270902, -0.47043286, -0.59803599,
        0.10463104,  0.41632529, -0.79734768, -0.0448739 ,  0.75797268,
       -0.2458588 , -0.86741529, -0.15749839,  0.78766469, -0.76537116,
       -0.49444332,  0.58869899, -0.99537279, -0.95045426, -0.08873041,
        0.2060658 ,  0.45997991,  0.84540488, -0.72075211, -0.38646285,
       -0.46542311,  0.35396381, -0.02437356,  0.92872498,  0.30825975,
       -0.0862608 ,  0.89358482,  0.81943533,  0.72146539,  0.90752149,
        0.63177385,  0.81539517, -0.11157401, -0.83466183,  0.15524435,
       -0.12473567,  0.77726261,  0.76387357, -0.67829283, -0.8496076 ,
        0.55082502, -0.65742021, -0.12510556,  0.31358648,  0.52863958,
        0.36243215,  0.11900989, -0.30362865, -0.07284159, -0.51836067,
       -0.62888581,  0.40478359, -0.24493837, -0.88599264, -0.97415141,
        0.89211457, -0.57238328,  0.27731109,  0.73585204, -0.42012191,
       -0.87814805,  0.89391973,  0.0154215 ,  0.79403342, -0.81891675,
        0.19436527,  0.64927774, -0.91320475,  0.66601825, -0.32789018,
        0.99110277,  0.56965857, -0.12560556, -0.18295556,  0.10730121,
        0.48162176,  0.04191296,  0.41585245,  0.78544897,  0.87223387,
       -0.62988823, -0.1396164 ,  0.17946756,  0.25118082,  0.31968201,
       -0.74079086, -0.773411  ,  0.72754406,  0.50882104,  0.83806812,
        0.09531416, -0.3747751 ,  0.68148027,  0.14271777,  0.37259425,
       -0.03694566, -0.97117391, -0.55229874,  0.968437  , -0.96946345,
       -0.7764898 ,  0.25791538, -0.24835041, -0.26435427,  0.07162122,
       -0.33230622, -0.18017198,  0.14384816, -0.26318731,  0.17035749,
        0.62105363, -0.21098449,  0.90492739,  0.67496524, -0.64527571,
       -0.22890743, -0.63534356,  0.13811335,  0.41053294,  0.48354426,
       -0.86662109, -0.96164225,  0.22912047,  0.32853203, -0.30130884,
       -0.70256889,  0.46708991,  0.94426573, -0.90734315,  0.68684104,
        0.58721229,  0.93276838,  0.50091519,  0.466484  ,  0.4400111 ,
        0.01937702,  0.73641042,  0.94306467, -0.66624168, -0.16059987,
       -0.97796473,  0.59679433, -0.89686034,  0.95193678, -0.66723861,
        0.89939715, -0.08903039,  0.29408017, -0.70036316, -0.79831698,
        0.27048195,  0.75352811, -0.60084966,  0.65452421]), 'overdisp_log__': array(0.85248135)}

Logp initial evaluation results:
{'beta0': -6.68, 'beta1': -106.81, 'tau_ind': -1.61, 'tau_spat': -1.26, 'alpha': -1.57, 'theta': -266.91, 'phi': -433.78, 'overdisp': -1.6, 'y_i': -inf}
You can call `model.debug()` for more details.

Could you help me point out what’s wrong with my code? Thank you

Hi @christ, it is hard to be certain without being able to have a runnable example but I suspect the issue is with the priors set on the over-dispersion parameter. Take a look at this resource, I think it might help you with picking a better prior Prior Choice Recommendations · stan-dev/stan Wiki · GitHub

Definitely check priors. I’ll add in that when working with exponential link models, it is extremely easy to get numerical overflows. The maximum value you can have inside the exp is something like 700, so the scale of the data is extremely important.

For count distributions, I’ve been using softplus as a link function instead of exp recently, because it has much nicer numerical properties.

1 Like

Did you try it?

Yes, it shows no problem found

Thank you seems this link function works,

Sorry, maybe I also ask another question, which is also a problem.
I used 2023 data as training, 2024 for test. but in 2023, there’s like 10 regions don’t have tick counts. in 2024, there are different regions but more than 10 regions without tick counts. In the code I posted here, I just simply remove the regions that don’t have tick counts to build adjacency matrix, otherwise the dimension is mismatch. Is this correct? But in test as phi dimension is different from 2023, which also dim mismatch again. How to solve it?