# How to Build a Market Simulator Using Markov Chains and Python

I am modeling how new users behave within 30 days of using Credit Sesame’s website.new user data on a particular dayNext, we segment our customers into different categories or states..1 = inactive user, 2 = active user, 3 = very active user.After segmenting first day data, you pick a time-frame..After 30 days, users would have had the opportunity to shift to and from each segment: very active users may become inactive, moderately active users may become very active, and so on.Apply segmentation to this post-30-day data..Make sure you account for the time frame (for example, average your engagement score for the 30 days).segmentation applied to 30-day dataLet’s visualize the results:As expected, the number of users who became inactive rose over the 30 days and the number of users who stayed active and very active decreased.Onto applying the Markov frameworkMarkov Chains GroundworkMarkov chains are simply mathematical systems that model state-to-state movement using certain probabilistic rules and fixed assumptions.To put it more simply, when you have a system with fixed states (or segments), and agents/users who can move between those states with a certain fixed probability, you can model it using a Markov chain.But let us first see if our system satisfies the assumptions of a Markov model:ASSUMPTION 1: There are a finite set of states..While my system does take into account hundreds of thousands of user data points, it is easy to believe the variance of probabilities for different 30 day time frames shouldn’t be too large..The end state is also a 3×1 vector that shows the number of users in each segment after the first month (after we multiply the initial state vector with the probabilities matrix)..The transition probability matrix is a 3×3 matrix that represents the fixed probabilities of moving to and from different customer segments.So how do we calculate these fixed probabilities?From our recorded segment movements..We look at how users from each segment on day 1 moved to various segments after 30 days and calculate the probabilities accordingly (equivalent to proportions).0.89 (in the picture) refers to the probability that someone in segment 1 on day 1 stays in the same segment after 30 days i.e..This is a type of action that will affect the number of users in each segment consistently..In order to account this type of change we change the probabilities in the transition matrix. You can calculate the changed probabilities either by A/B testing the new website on a subsample of users, or, you can change them manually using your own heuristics and assumptions.Assuming probabilities stay fixed, we can see how the customer flow changes with the new transition matrix.Predicting other metricsYou can predict several other user metrics such as Customer Lifetime Value, revenue per user etc..For example, to predict revenue changes, calculate the average revenue for each segment, calculate the output state, and multiply the revenue with the number of users in their respective segment.ConclusionIf you’ve gotten this far, I commend you.. More details