Finite Markov Chain

Hello, I am a very beginner in Python. I am reading the Lecture on quant Econ website, on the part “Finite Markov chain”, there are some code I am still confusing.

  1. why we need to set P=np.asarray, when P is already a matrix (stochastic matrix).
    And why is must be “asarray”, not array?
    And I don’t understand the purpose of it “to allocate memory”, i don’t understand we need to allocate memory for what?

The questions might be silly, but I am just started and everything seem fancy to me. I tried to google but could not find appropriate answer.

Really appreciate any help!

Thanks

def mc_sample_path(P, init=0, sample_size=1000):

=== make sure P is a NumPy array ===

P = np.asarray§

=== allocate memory ===

X = np.empty(sample_size, dtype=int)
X[0] = init

=== convert each row of P into a distribution ===

In particular, P_dist[i] = the distribution corresponding to P[i,:]

n = len§
P_dist = [qe.DiscreteRV(P[i,:]) for i in range(n)]

Is it this lecture you are talking about? If yes, then:

It could be that the P passed to the function is not in the NumPy array format yet. In that case, this ensures that it will be.

If P already is a NumPy array, then using np.asarray instead of np.array makes sure that no new (redundant) copies are made. You could see this lecture on NumPy and search that page for the string “asarray”. The first hit provides explanation.

Memory is allocated for the object X returned by mc_sample_path. If you know the size of the object that the function will return, then it is usually sensible to pre-allocate the required memory instead of letting the array grow at every step in the for-loop, because it prevents the creation of intermediary objects in memory. Here this size is known (from sample_size) so it is easy to pre-allocate.

2 Likes

Got it. Thanks for your help!