import pandas as pd
import quantecon as qe
import warnings
warnings.filterwarnings('ignore')fp = "../data/stochastic_matrix_coffee_price-regime-R-8.csv"
df = pd.read_csv(fp, usecols = ["L", "M", "H"])
col_order = ["L", "M", "H"]
df = df[col_order]sm = df.values
smfrom quantecon import MarkovChain
mc = qe.MarkovChain(sm, ("L", "M", "H"))mc.is_irreduciblemc.communication_classesmc.is_aperiodicmc.stationary_distributionsimport plotly.express as px
fig = px.imshow(sm, text_auto=True,  labels=dict(x="Previous Month Price", y="Current Month Price"),
                x=['Low', 'Medium', 'High'],
                y=['Low', 'Medium', 'High'])
fig.update_layout(
    title={
        'text': "Stochastic Matrix for Region 8",
        'y':.95,
        'x':0.5,
        'xanchor': 'center',
        'yanchor': 'top'})
fig.show()d = mc.stationary_distributions.flatten().tolist()
stationary_dist = {"Low": d[0], "Medium": d[1], "High": d[2]}
dfp = pd.DataFrame.from_dict(stationary_dist, orient='index').round(3)
dfp = dfp.reset_index()
dfp.columns = ["Price", "Probability"]
dfp