<TensorType(float64, scalar)

11 views
Skip to first unread message

D. K

unread,
Jun 14, 2024, 10:32:57 AMJun 14
to pystatsmodels
Hello,

I run a high dimensional tvp pvar with numerous endogenous variables . I get the error below. I have look at many places, but I did not find a solution. I would much appreciate it if you could help me


David




/Users/eva/anaconda3/lib/python3.11/site-packages/sklearn/impute/_iterative.py:801: ConvergenceWarning: [IterativeImputer] Early stopping criterion not reached.

warnings.warn(

Auto-assigning NUTS sampler...

Initializing NUTS using jitter+adapt_diag...


You can find the C code in this temporary file: /var/folders/p_/4shh5wgx7vn_sp4_77tp6c7m0000gn/T/theano_compilation_error_0s9f3yfr

Traceback (most recent call last):



File ~/anaconda3/lib/python3.11/site-packages/theano/tensor/basic.py:184 in as_tensor_variable

x = [extract_constants(i) for i in x]


File ~/anaconda3/lib/python3.11/site-packages/theano/tensor/basic.py:184 in <listcomp>

x = [extract_constants(i) for i in x]


File ~/anaconda3/lib/python3.11/site-packages/theano/tensor/basic.py:179 in extract_constants

raise TypeError


TypeError



During handling of the above exception, another exception occurred:


Traceback (most recent call last):


Cell In[2], line 1

model.fit_tvp_pvar(num_iterations=50000, burn=20000, tune=10000, cores=4, imputation_method='iterative', imputer_max_iter=1000)


Cell In[1], line 130 in fit_tvp_pvar

trace = pm.sample(num_iterations, tune=tune, cores=cores, return_inferencedata=True)


File ~/anaconda3/lib/python3.11/site-packages/pymc3/sampling.py:496 in sample

start_, step = init_nuts(


File ~/anaconda3/lib/python3.11/site-packages/pymc3/sampling.py:2187 in init_nuts

step = pm.NUTS(potential=potential, model=model, **kwargs)


File ~/anaconda3/lib/python3.11/site-packages/pymc3/step_methods/hmc/nuts.py:168 in __init__

super().__init__(vars, **kwargs)


File ~/anaconda3/lib/python3.11/site-packages/pymc3/step_methods/hmc/base_hmc.py:88 in __init__

super().__init__(vars, blocked=blocked, model=model, dtype=dtype, **theano_kwargs)


File ~/anaconda3/lib/python3.11/site-packages/pymc3/step_methods/arraystep.py:254 in __init__

func = model.logp_dlogp_function(vars, dtype=dtype, **theano_kwargs)


File ~/anaconda3/lib/python3.11/site-packages/pymc3/model.py:1001 in logp_dlogp_function

costs = [self.logpt]


File ~/anaconda3/lib/python3.11/site-packages/pymc3/model.py:1011 in logpt

logp = tt.sum([tt.sum(factor) for factor in factors])


File ~/anaconda3/lib/python3.11/site-packages/theano/tensor/basic.py:3221 in sum

out = elemwise.Sum(axis=axis, dtype=dtype, acc_dtype=acc_dtype)(input)


File ~/anaconda3/lib/python3.11/site-packages/theano/graph/op.py:250 in __call__

node = self.make_node(*inputs, **kwargs)


File ~/anaconda3/lib/python3.11/site-packages/theano/tensor/elemwise.py:1925 in make_node

input = as_tensor_variable(input)


File ~/anaconda3/lib/python3.11/site-packages/theano/tensor/basic.py:194 in as_tensor_variable

return theano.tensor.opt.MakeVector(dtype)(*x)


File ~/anaconda3/lib/python3.11/site-packages/theano/graph/op.py:253 in __call__

compute_test_value(node)


File ~/anaconda3/lib/python3.11/site-packages/theano/graph/op.py:126 in compute_test_value

thunk = node.op.make_thunk(node, storage_map, compute_map, no_recycling=[])


File ~/anaconda3/lib/python3.11/site-packages/theano/graph/op.py:634 in make_thunk

return self.make_c_thunk(node, storage_map, compute_map, no_recycling)


File ~/anaconda3/lib/python3.11/site-packages/theano/graph/op.py:600 in make_c_thunk

outputs = cl.make_thunk(


File ~/anaconda3/lib/python3.11/site-packages/theano/link/c/basic.py:1203 in make_thunk

cthunk, module, in_storage, out_storage, error_storage = self.__compile__(


File ~/anaconda3/lib/python3.11/site-packages/theano/link/c/basic.py:1138 in __compile__

thunk, module = self.cthunk_factory(


File ~/anaconda3/lib/python3.11/site-packages/theano/link/c/basic.py:1634 in cthunk_factory

module = get_module_cache().module_from_key(key=key, lnk=self)


File ~/anaconda3/lib/python3.11/site-packages/theano/link/c/cmodule.py:1191 in module_from_key

module = lnk.compile_cmodule(location)


File ~/anaconda3/lib/python3.11/site-packages/theano/link/c/basic.py:1543 in compile_cmodule

module = c_compiler.compile_str(


File ~/anaconda3/lib/python3.11/site-packages/theano/link/c/cmodule.py:2546 in compile_str

raise Exception(


Exception: ('Compilation failed (return status=1): /Users/ev/.theano/compiledir_macOS-14.5-arm64-arm-64bit-arm-3.11.8-64/tmpenzneiyk/mod.cpp:72300:32: fatal error: bracket nesting level exceeded maximum of 256. if (!PyErr_Occurred()) {. ^. /Users/ev/.theano/compiledir_macOS-14.5-arm64-arm-64bit-arm-3.11.8-64/tmpenzneiyk/mod.cpp:72300:32: note: use -fbracket-depth=N to increase maximum nesting level. 1 error generated.. ', "FunctionGraph(MakeVector{dtype='float64'}(<TensorType(float64, scalar)>, <TensorType(float64, scalar)>, <TensorType(float64, scalar)>, <TensorType(float64, scalar)>, <TensorType(float64, scalar)>, <TensorType(float64, scalar)>, <TensorType(float64, scalar)>, <TensorType(float64, scalar)>, <TensorType(float64, scalar)>, <TensorType(float64, scalar)>, <TensorType(float64, scalar)>, <TensorType(float64, scalar)>, <TensorType(float64, scalar)>, <TensorType(float64, scalar)>, <TensorType(float64, scalar)>, <TensorType(float64, scalar)>, <TensorType(float64, scalar)>, <TensorType(float64, scalar)>, <TensorType(float64, scalar)>, <TensorType(float64,


my code is 

 def fit_tvp_pvar(self, num_iterations=1000, burn=500, tune=500, cores=1, delay=1, imputation_method='iterative', imputer_max_iter=100):
        """Fit the TVP-PVAR model using NUTS sampling"""
        data = self.data[self.vars].values
       
        if imputation_method == 'iterative':
            # Iterative Imputer
            imputer = IterativeImputer(max_iter=imputer_max_iter, random_state=self.seed)
            data = imputer.fit_transform(data)
        elif imputation_method == 'kalman':
            # Kalman Filter Imputation
            data = kalman_imputation(data, max_iter=imputer_max_iter, seed=self.seed)
        elif imputation_method == 'knn':
            # KNN Imputer
            imputer = KNNImputer()
            data = imputer.fit_transform(data)

        valid_mask = ~np.isnan(data) & ~np.isinf(data)  # Mask for valid values
        self.n_vars = data.shape[1]  # Set self.n_vars based on data
        self.n_obs = data.shape[0]

        with pm.Model() as tvp_pvar_model:
            sd_dist = pm.HalfNormal.dist(sigma=1.0)  # Standard deviation distribution
            sd_vals = pm.HalfNormal('sd_vals', sigma=1, shape=(self.n_vars,))
            alphas = pm.Normal('alphas', mu=0, sigma=1, shape=(self.n_obs, self.n_vars)) + 1e-6  # Add a small constant
            phis = horseshoe_prior('phis', shape=(self.n_obs - self.p, self.p, self.n_vars, self.n_vars)) + 1e-6  # Add a small constant

            # Define a Cholesky-factor covariance matrix
            cov_matrix_chol = pm.LKJCholeskyCov('cov_matrix_chol', n=self.n_vars, eta=2, sd_dist=sd_dist)

            # Compute diagonal matrix from sd_vals
            diag_sd_vals = tt.diag(sd_vals)

            # Compute covariance matrix
            cov_matrix = diag_sd_vals.dot(cov_matrix_chol.dot(cov_matrix_chol.T)).dot(diag_sd_vals)
            chol_cov = pm.Deterministic('chol_cov', tt.slinalg.cholesky(cov_matrix))

            # Likelihood
            for t in range(self.p, self.n_obs):
                y_t = data[t]
                y_lags = np.column_stack([data[t-i-1] for i in range(self.p)])  # Adjust index for lagged values
                y_lags = y_lags.T.reshape(self.p, self.n_vars, 1)  # Reshape y_lags for correct multiplication
                mean_t = alphas[t] + tt.sum(phis[t-self.p] * y_lags, axis=1).T  # Corrected matrix multiplication

                delta = y_t - mean_t
                valid_obs = valid_mask[t, :]  # Check valid observations at time t

                # Create valid_indices for indexing
                valid_indices = valid_obs.nonzero()[0]

                # Ensure chol_cov is not flattened unnecessarily
                chol_cov_reshaped = tt.reshape(chol_cov, (self.n_vars, self.n_vars))

                # Ensure delta is not reshaped unnecessarily
                pm.MvNormal(f'y_{t}', mu=mean_t[valid_indices], chol=chol_cov_reshaped[valid_indices][:, valid_indices], observed=delta[valid_indices])

            # Introduce a delay between iterations to avoid timeout issues
            iteration_delay = 1  # Delay in seconds
            for iteration in range(num_iterations):
                time.sleep(iteration_delay)

            trace = pm.sample(num_iterations, tune=tune, cores=cores, return_inferencedata=True)

            trace = az.sel(trace, draw=slice(burn, None))

            self.trace = trace
Reply all
Reply to author
Forward
0 new messages