Git Product home page Git Product logo

markov-chain-simulation's Introduction

Markov-chain-simulation


Introduction

Simulation 100 customers

This project aims at simulating customers behaviour in a supermarket. Customers are generated from a Markov chain modelling with transition probabilities. Transition probabilities are simply the probabilities of staying in the same state or moving to a different state given the current state. The probability transition matrix used is available at data/Q_matrix.csv and has been estimated from one week data (dataset not available in this repository). The possible states considered in the supermarket are: ['entrance', 'drinks', 'dairy', 'fruit', 'spices', 'checkout'] and will be defined per minute. The code can easily be adapted to generate the Markov chain for different transition/states/probabilities.

Probability transition matrix

One day simulation

In this section, let's simulate one full day of customers with their states per minute.

For this, simply run python simulation_one_day.py.

This will simulate customers behaviour minute per minute and save it at demo/one_day_simulation.csv . To obtain a realistic arrival time every minute, the program is using an hour count distribution available at data/customer_per_hour.csv . Then, the arrivals are randomly distributed for all the minutes within each hour.

The program uses a class Customer defined in customer_tools.py. This class simulates a Markov chain for one customer based on Markov states and the transition probability matrix defined above.

The function arrival_time_from_hour_distribution in simulation_tools.py, enables to generate a randomly sampled arrival time per minute to simulate arrival time of customers in the shop for one day, respecting the count per hour distribution.

Entrance distribution per hour

The function simulate_customers in simulation_tools.py enables to generates a Markov chain for each new customer arrival time and return a dataframe with minute per minute states of all customers for one day in the supermarket. An example generated can be seen at demo/one_day_simulation.csv.

To understand better the dataset simulated, an EDA (exploratory data analysis) is performed in the jupyter notebook EDA_one_day_simulation.ipynb as a demo. We can see for example, the number of new entrance per hour, the repartition of customers for each section, the time spent in the supermarket, the total revenue per hour and the most profitable sections.

Table simulated

Make Markov diagram

A diagram of the Markov chain can be drawn with python make_markov_diagram.py

It uses the probability transition matrix and the possible states.

Markov chain diagram

Make a gif simulating 100 customers

Run python make_gif_simulation.py

It will create a gif of all states of simulated customers in the order of occurrences (no overlap between customers in the display).

It simulate the behaviour of 100 customers per default but can easily change to any number.

In a first step the function save_each_state from make_gif_tools.py simulates all the customers, assigns a random color for each of them and saves all states (per minute) in the supermarket as png pictures. In a second step, the function make_gif_from_states from make_gif_tools.py reads all customers states in the order of occurrences and make a gif of all states (and delete the png pictures from disk).

How to use?

Clone this repository
Make sure you have installed the packages in requirements.txt (pip install -r requirements.txt)

License

License

markov-chain-simulation's People

Contributors

vbelz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.