Hi , I am using a transition matrix of 3 X 3. I am beginner in PyMC and trying to use built-in methods to sample an generate events to develop a time series. For the followoing code , I tried but unable to understand the error. Any help/directions/ references in PyMC would be very helpful.
import numpy as np
import pymc as pm
# Define the transition matrix
transition_matrix = np.array([[0.1, 0.2, 0.7],
[0.09, 0.01, 0.9],
[0.2, 0.8, 0.0]])
# Define the state of interest
target_state = 2
# Number of iterations
num_iterations = 10000
# Define PyMC model
model = pm.Model()
# Counter for transition to target state
transition_count = 0
with model:
# Hidden state sequence
states = [pm.Categorical(f'state_{i}', p=transition_matrix[0]) for i in range(num_iterations)]
# Generate events based on transition matrix
for i in range(1, num_iterations):
states[i] = pm.Categorical(f'state_{i}', p=transition_matrix[states[i:-1]])
# Check if the transition leads to the target state
if states[i] == target_state:
transition_count +=1
# Sample from the model
with model:
trace = pm.sample(num_iterations, chains=1)
# Estimate the probability of transitioning to the target state
estimated_probability = transition_count.get_value() / num_iterations
print("Estimated probability of transitioning to state", target_state, ":", estimated_probability)
I am getting error at Categorical" IndexError: only integers, slices (:
), ellipsis (...
), numpy.newaxis (None
) and integer or boolean arrays are valid indices"