Skip to content

Instantly share code, notes, and snippets.

@quantra-go-algo
Last active October 11, 2023 16:55
Show Gist options
  • Save quantra-go-algo/0d7d673d87b93f8ce803fa7dc2ba3bde to your computer and use it in GitHub Desktop.
Save quantra-go-algo/0d7d673d87b93f8ce803fa7dc2ba3bde to your computer and use it in GitHub Desktop.
# Create an HMM object with two hidden states
model = hmm.GaussianHMM(n_components = 2, covariance_type = "diag", n_iter = 100, random_state = 42)
# Create an array input for the HMM model
X = data[['R']].values
# Estimate the HMM model
results = model.fit(X)
# Use the Viterbi algorithm to find the fitted hidden states
data['states'] = results.predict(X)
# Count the number of days in which we find the two states
data['states'].value_counts()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment