Thanks for your help with the previous error! I’ve made some debugging changes and encountered a new error:
def fit_tvp_pvar(self, num_iterations=10000, burn=5000, tune=5000, cores=1, delay=1):
imputation_methods = [‘kalman’, ‘iterative’, ‘knn’]
imputed = False
for method in imputation_methods:
try:
if method == 'kalman':
# Kalman Filter Imputation
data = self.kalman_imputation(self.data[self.vars].values.astype('float32'))
print("Imputation successful with Kalman filter method.")
elif method == 'iterative':
# Iterative Imputer
imputer = IterativeImputer(max_iter=100, random_state=self.seed)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=ConvergenceWarning)
data = imputer.fit_transform(self.data[self.vars].values.astype('float32'))
print("Imputation successful with iterative method.")
elif method == 'knn':
# KNN Imputer
imputer = KNNImputer()
data = imputer.fit_transform(self.data[self.vars].values.astype('float32'))
print("Imputation successful with KNN method.")
if not np.any(np.isnan(data)) and not np.any(np.isinf(data)):
imputed = True
self.n_vars = data.shape[1]
break
time.sleep(delay) # Add 1 second delay between iterations
except Exception as e:
print(f"{method} imputation failed: {e}")
print("Trying the next method...")
if not imputed:
raise ValueError("All imputation methods failed.")
# Convert to a clean numpy array with the correct dtype
data = np.array(data, dtype=np.float32)
try:
with pm.Model() as tvp_pvar_model:
# Priors
sd_dist = pm.HalfNormal.dist(sigma=1.0)
log_sd_vals = pm.Normal("log_sd_vals", mu=0, sigma=1, shape=(self.n_vars,))
exp_log_sd_vals = pm.math.exp(log_sd_vals)
sd_vals = pm.Deterministic("sd_vals", exp_log_sd_vals)
alphas = pm.Normal("alphas", mu=0, sigma=1, shape=(self.n_obs, self.n_vars))
# Initial state for hidden variables (replace with appropriate values based on your data)
initial_state = np.zeros((1, self.p * self.n_vars))
# State transitions using loop
state_vars = []
for t in range(self.n_obs):
prev_state = state_vars[t - 1] if t > 0 else initial_state
shocks = pm.Normal("shocks_t{}".format(t), mu=0.0, sigma=sd_vals, shape=(self.n_vars,))
new_state = np.zeros_like(prev_state)
# Loop through previous states and shocks to accumulate state updates
for lag in range(self.p):
new_state += pm.math.dot(alphas[t - lag - 1], shocks[t - lag])
state_vars.append(new_state)
# Likelihood
etas = pm.Normal("etas", mu=pm.math.dot(state_vars[0], pm.math.repmat(np.arange(self.p)[::-1], self.n_obs, 1)),
sigma=sd_vals, observed=data)
# MCMC sampling
trace = pm.sample(draws=num_iterations, tune=tune, cores=cores)
self.trace = trace
except RecursionError as e:
print(f"RecursionError encountered: {e}. Increasing recursion limit.")
sys.setrecursionlimit(10000)
self.fit_tvp_pvar(num_iterations=num_iterations, burn=burn, tune=tune, cores=cores, delay=delay)
except Exception as e:
import traceback
print(traceback.format_exc())
print(f"Error during model fitting: {e}")
i got
Traceback (most recent call last):
File “/Users/dimitri/anaconda3/lib/python3.11/site-packages/pytensor/tensor/type.py”, line 293, in dtype_specs
return self.dtype_specs_map[self.dtype]
~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: ‘object’
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/var/folders/p_/4shh5wgx7vn_sp4_77tp6c7m0000gn/T/ipykernel_5819/1811677506.py”, line 95, in fit_tvp_pvar
new_state += pm.math.dot(alphas[t - lag - 1], shocks[t - lag])
File “/Users/dimitri/anaconda3/lib/python3.11/site-packages/pytensor/tensor/variable.py”, line 201, in radd
return pt.math.add(other, self)
^^^^^^^^^^^^^^^^^^^^^^^^
File “/Users/dimitri/anaconda3/lib/python3.11/site-packages/pytensor/graph/op.py”, line 292, in call
node = self.make_node(*inputs, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Users/dimitri/anaconda3/lib/python3.11/site-packages/pytensor/tensor/elemwise.py”, line 481, in make_node
inputs = [as_tensor_variable(i) for i in inputs]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Users/dimitri/anaconda3/lib/python3.11/site-packages/pytensor/tensor/elemwise.py”, line 481, in
inputs = [as_tensor_variable(i) for i in inputs]
^^^^^^^^^^^^^^^^^^^^^
File “/Users/dimitri/anaconda3/lib/python3.11/site-packages/pytensor/tensor/init.py”, line 50, in as_tensor_variable
return as_tensor_variable(x, name, ndim, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Users/dimitri/anaconda3/lib/python3.11/functools.py”, line 909, in wrapper
return dispatch(args[0].class)(*args, **kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Users/dimitri/anaconda3/lib/python3.11/site-packages/pytensor/tensor/basic.py”, line 185, in as_tensor_numbers
return constant(x, name=name, ndim=ndim, dtype=dtype)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Users/dimitri/anaconda3/lib/python3.11/site-packages/pytensor/tensor/basic.py”, line 238, in constant
ttype = TensorType(dtype=x.dtype, shape=x.shape)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Users/dimitri/anaconda3/lib/python3.11/site-packages/pytensor/tensor/type.py”, line 122, in init
self.dtype_specs() # error checking is done there
^^^^^^^^^^^^^^^^^^
File “/Users/dimitri/anaconda3/lib/python3.11/site-packages/pytensor/tensor/type.py”, line 295, in dtype_specs
raise TypeError(
TypeError: Unsupported dtype for TensorType: object
Error during model fitting: Unsupported dtype for TensorType: object
Then , looking deeper I updated the version of the fit_tvp_pvar
method with the debugging steps moved to the beginning of the method:
def fit_tvp_pvar(self, num_iterations=10000, burn=5000, tune=5000, cores=1, delay=1):
imputation_methods = ['kalman', 'iterative', 'knn']
imputed = False
for method in imputation_methods:
try:
if method == 'kalman':
# Kalman Filter Imputation
data = self.kalman_imputation(self.data[self.vars].values.astype('float32'))
print("Imputation successful with Kalman filter method.")
elif method == 'iterative':
# Iterative Imputer
imputer = IterativeImputer(max_iter=100, random_state=self.seed)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=ConvergenceWarning)
data = imputer.fit_transform(self.data[self.vars].values.astype('float32'))
print("Imputation successful with iterative method.")
elif method == 'knn':
# KNN Imputer
imputer = KNNImputer()
data = imputer.fit_transform(self.data[self.vars].values.astype('float32'))
print("Imputation successful with KNN method.")
if not np.any(np.isnan(data)) and not np.any(np.isinf(data)):
imputed = True
self.n_vars = data.shape[1]
break
time.sleep(delay) # Add 1 second delay between iterations
except Exception as e:
print(f"{method} imputation failed: {e}")
print("Trying the next method...")
if not imputed:
raise ValueError("All imputation methods failed.")
# Convert to a clean numpy array with the correct dtype
data = np.array(data, dtype=np.float32)
# Check for non-numeric values in the data
print(f"Non-numeric values in data: {np.any(~np.isfinite(data))}")
try:
with pm.Model() as tvp_pvar_model:
# Print data types before model creation
print(f"data dtype: {data.dtype}")
# Priors
sd_dist = pm.HalfNormal.dist(sigma=1.0)
log_sd_vals = pm.Normal("log_sd_vals", mu=0, sigma=1, shape=(self.n_vars,))
exp_log_sd_vals = pm.math.exp(log_sd_vals)
sd_vals = pm.Deterministic("sd_vals", exp_log_sd_vals)
alphas = pm.Normal("alphas", mu=0, sigma=1, shape=(self.n_obs, self.n_vars))
# Initial state for hidden variables (replace with appropriate values based on your data)
initial_state = np.zeros((1, self.p * self.n_vars))
# State transitions using loop
state_vars = []
for t in range(self.n_obs):
prev_state = state_vars[t - 1] if t > 0 else initial_state
shocks = pm.Normal("shocks_t{}".format(t), mu=0.0, sigma=sd_vals, shape=(self.n_vars,))
new_state = np.zeros_like(prev_state)
# Loop through previous states and shocks to accumulate state updates
for lag in range(self.p):
# Check data types of alphas and shocks
print(f"alphas dtype: {alphas.dtype}")
print(f"shocks dtype: {shocks.dtype}")
# Check for non-numeric values in alphas and shocks
print(f"Non-numeric values in alphas: {np.any(~np.isfinite(alphas))}")
print(f"Non-numeric values in shocks: {np.any(~np.isfinite(shocks))}")
# Convert alphas and shocks to numerical arrays
alphas = np.array(alphas, dtype=np.float32)
shocks = np.array(shocks, dtype=np.float32)
new_state += pm.math.dot(alphas[t - lag - 1], shocks[t - lag])
state_vars.append(new_state)
# Likelihood
etas = pm.Normal("etas", mu=pm.math.dot(state_vars[0], pm.math.repmat(np.arange(self.p)[::-1], self.n_obs, 1)),
sigma=sd_vals, observed=data)
# MCMC sampling
trace = pm.sample(draws=num_iterations, tune=tune, cores=cores)
self.trace = trace
except RecursionError as e:
print(f"RecursionError encountered: {e}. Increasing recursion limit.")
sys.setrecursionlimit(10000)
self.fit_tvp_pvar(num_iterations=num_iterations, burn=burn, tune=tune, cores=cores, delay=delay)
except Exception as e:
import traceback
print(traceback.format_exc())
print(f"Error during model fitting: {e}")
I’ve moved the debugging steps to the beginning of the try
block, right after the with pm.Model()
line. This way, the print statements will be executed before any PyMC operations are performed, and you should see the printed output regardless of where the error occurs within the with
block.
Additionally, I’ve added a new print statement to check the data type of the data
variable before creating the PyMC model:
print(f"data dtype: {data.dtype}")
where I got
kalman imputation failed: array must not contain infs or NaNs
Trying the next method…
Imputation successful with iterative method.
Non-numeric values in data: False
data dtype: float32
alphas dtype: float64
shocks dtype: float64
Traceback (most recent call last):
File “/var/folders/p_/4shh5wgx7vn_sp4_77tp6c7m0000gn/T/ipykernel_6927/1672946134.py”, line 106, in fit_tvp_pvar
print(f"Non-numeric values in alphas: {np.any(~np.isfinite(alphas))}")
^^^^^^^^^^^^^^^^^^^
TypeError: ufunc ‘isfinite’ not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ‘‘safe’’
Error during model fitting: ufunc ‘isfinite’ not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ‘‘safe’’
kalman imputation failed: array must not contain infs or NaNs
Trying the next method…
Imputation successful with iterative method.
Non-numeric values in data: False
data dtype: float32
alphas dtype: float64
shocks dtype: float64
Traceback (most recent call last):
File “/var/folders/p_/4shh5wgx7vn_sp4_77tp6c7m0000gn/T/ipykernel_6746/1672946134.py”, line 106, in fit_tvp_pvar
print(f"Non-numeric values in alphas: {np.any(~np.isfinite(alphas))}")
^^^^^^^^^^^^^^^^^^^
TypeError: ufunc ‘isfinite’ not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ‘‘safe’’
Error during model fitting: ufunc ‘isfinite’ not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ‘‘safe’’
So far as you can see
Debugging Steps Taken:
- Moved Debugging Statements: I moved the printing of data types and checks for non-numeric values to the beginning of the
try
block, ensuring they run before any PyMC operations.
- Checked Data Type: I added a print statement to verify the data type after conversion (
data.dtype
).
Current Observations:
- The imputation seems successful (no NaNs or infs).
- Data is converted to
float32
.
alphas
and shocks
data types are float64
.
Possible Causes and Solutions:
Based on the error message and observations, it seems the issue might be related to the data types of alphas
and shocks
. Although the data itself is float32
, PyMC might be creating these variables with a different default dtype (float64
in this case). This mismatch could cause problems with the isfinite
function.
Additional Notes:
- The
kalman
imputation method seems to be failing. Consider checking its implementation or trying alternative imputation techniques if necessary.
I appreciate any insights or suggestions you might have to resolve this new error.
Thanks,
Dimitri