{"id":375,"date":"2020-08-13T09:38:52","date_gmt":"2020-08-13T09:38:52","guid":{"rendered":"https:\/\/data-science.gotoauthority.com\/2020\/08\/13\/markov-chain-monte-carlo-simulation-for-airport-queuing-network\/"},"modified":"2020-08-13T09:38:52","modified_gmt":"2020-08-13T09:38:52","slug":"markov-chain-monte-carlo-simulation-for-airport-queuing-network","status":"publish","type":"post","link":"https:\/\/wealthrevelation.com\/data-science\/2020\/08\/13\/markov-chain-monte-carlo-simulation-for-airport-queuing-network\/","title":{"rendered":"Markov Chain Monte Carlo Simulation For Airport Queuing Network"},"content":{"rendered":"<div id=\"tve_editor\" data-post-id=\"4717\">\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173e641986c\"><span class=\"tve_image_frame\"><img loading=\"lazy\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/1-Markov-chain-monte-carlo-simulation.png?resize=613%2C394&amp;ssl=1\" class=\"tve_image wp-image-4722\" alt=\"Markov chain monte carlo simulation\" data-id=\"4722\" width=\"613\" data-init-width=\"700\" height=\"394\" data-init-height=\"450\" title=\"Markov chain monte carlo simulation\" data-width=\"613\" data-height=\"394\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4722\" alt=\"Markov chain monte carlo simulation\" data-id=\"4722\" width=\"613\" data-init-width=\"700\" height=\"394\" data-init-height=\"450\" title=\"Markov chain monte carlo simulation\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/1-Markov-chain-monte-carlo-simulation.png?resize=613%2C394&amp;ssl=1\" data-width=\"613\" data-height=\"394\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p dir=\"ltr\">Today we\u2019ll introduce a new set of algorithms called Markov Chain Monte Carlo which won&#8217;t fall in <a href=\"https:\/\/dataaspirant.com\/supervised-and-unsupervised-learning\/\" target=\"_blank\" rel=\"noopener noreferrer\">supervised learning algorithms<\/a>. In this blog post we will walk through, what Markov Chains are and where we can use it.<\/p>\n<p dir=\"ltr\">We will introduce the main family of algorithms, known collectively as Markov Chain Monte Carlo (MCMC), that allows us to approximate the posterior distribution as calculated by <strong>Bayes&#8217; Theorem<\/strong>.\u00a0<\/p>\n<p dir=\"ltr\">In particular, we consider the <strong>Metropolis Algorithm<\/strong>, which is easily stated and relatively straightforward to understand. It serves as a useful starting point when learning about MCMC before delving into more sophisticated algorithms such as Metropolis-Hastings, Gibbs Samplers, and Hamiltonian Monte Carlo.<\/p>\n<p dir=\"ltr\">Once we have described how MCMC works, we will carry it out using the open-source <strong>PyMC3 library<\/strong>, which takes care of many of the underlying implementation details, allowing us to concentrate on Bayesian modeling.<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_tw_qs tve_clearfix\" data-url=\"https:\/\/twitter.com\/intent\/tweet\" data-via=\"\">\n<div class=\"thrv_tw_qs_container\">\n<div class=\"thrv_tw_quote\">\n<p>Markov Chain Monte Carlo Simulation For Airport Queuing Network<\/p>\n<\/div>\n<p>\n\t\t\t<span><br \/>\n\t\t\t\t<i><\/i><br \/>\n\t\t\t\t<span class=\"thrv_tw_qs_button_text  thrv-inline-text tve_editable\">Click to Tweet<\/span><br \/>\n\t\t\t<\/span>\n\t\t<\/p>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p dir=\"ltr\">Before we drive further let\u2019s look at what you are going to learn by the end of the article.<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element tve-froala fr-box fr-basic\">\n<h2 id=\"t-1597295730171\" class=\"\">Markov chain Monte Carlo analogy<\/h2>\n<p dir=\"ltr\">Before getting started we\u2019ll try to understand the analogy behind Markov Chains. When we are getting into a learning curve in the field of analytics we have various divisions like first we\u2019ll start with forecasting and then linear regression after we\u2019ll get into <a href=\"https:\/\/dataaspirant.com\/classification-clustering-alogrithms\/\" target=\"_blank\" rel=\"noopener noreferrer\">classification algorithms<\/a> which are non-parametric models.\u00a0<\/p>\n<p dir=\"ltr\">After this curve,\u00a0 we\u2019ll get into Neural Networks like CNN, R-CNN, Auto-encoders so on, and so forth.<\/p>\n<p dir=\"ltr\">Once these are done now we will get into the stage of Markov Chains(MC) and Hidden Markov Chains (HMC) which are purely stochastic models in order to make a statement on random predictions.\u00a0<\/p>\n<p dir=\"ltr\">Let\u2019s understand it by example,<\/p>\n<p dir=\"ltr\"><a href=\"https:\/\/dataaspirant.com\/decision-tree-algorithm-python-with-scikit-learn\/\" target=\"_blank\" class=\"tve-froala\" rel=\"noopener noreferrer\">Decision tree algorithms<\/a> can say whether they are going to buy it or not. <a href=\"https:\/\/dataaspirant.com\/random-forest-classifier-python-scikit-learn\/\" target=\"_blank\" class=\"tve-froala\" rel=\"noopener noreferrer\">Random forest<\/a> says what are the various conditions need to be satisfied in order to make a statement. The <a href=\"https:\/\/dataaspirant.com\/how-logistic-regression-model-works\/\" target=\"_blank\" rel=\"noopener noreferrer\">logistic algorithm<\/a> can say yes\/no statements using sigmoid equations. When getting into CNN they can recognize the images and by using RNN they can do sequential tasking.<\/p>\n<h2 id=\"t-1597295730172\" class=\"\">Where we use Markov chains<\/h2>\n<p dir=\"ltr\">All the above set of algorithms categories are intended to predict the feature using the historical data, But if we want to predict like if you\u2019re sitting in a restaurant and you\u2019re waiting for the waiter in order to take up the order right.<\/p>\n<p dir=\"ltr\">So which algorithm be used in order to make the statement?.<\/p>\n<blockquote class=\"\"><p>Whether <strong>Virat Kohli<\/strong> going to hit a six or not,\u00a0<\/p><\/blockquote>\n<p dir=\"ltr\">which algorithm we need to use? In these scenarios we can\u2019t go into deep learning algorithms, we can\u2019t do forecasting right. So we want to make a statement on the above statements based on the current event we want to make a prediction about the next ball.\u00a0<\/p>\n<p dir=\"ltr\">So for all these instant based predictions, the only route we have is <strong>Markov Chains and Hidden Markov Chains<\/strong>. Markov Chains are one of the powerful algorithms where we\u2019re able to extract or able to make a statement on random events.<\/p>\n<p dir=\"ltr\">For these kinds of events, we prefer Markov Chains. Before we learn about markov chains, we need to learn about bayes&#8217;s rule.<\/p>\n<p dir=\"ltr\">Let&#8217;s spend some time now.<\/p>\n<h2 id=\"t-1597295730173\" class=\"\">Bayes\u2019s Rule<\/h2>\n<p dir=\"ltr\">If we recall Bayes\u2019s Rule:<\/p>\n<p dir=\"ltr\"><a href=\"https:\/\/www.codecogs.com\/eqnedit.php?latex=P(%5Ctheta%20%7C%20D)%20%3D%20%5Cfrac%7BP(D%7C%5Ctheta)%20P(%5Ctheta)%7D%7BP(D)%7D#0\" class=\"tve-froala\"><img loading=\"lazy\" src=\"https:\/\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif\" data-lazy-src=\"https:\/\/lh3.googleusercontent.com\/IRi3Zfx8-UO_uNT0OII76-JPkwcjWwhv2LELKtNAigsIMAQT6-eCowQHVLI606N6i7xS5eOpqsc1Vy68SSFsmezqS_-UnBuvyLoc4-EW9hD8vNL6IFocSYDJlz08WvdAsoqw95Xq\" width=\"157\" height=\"39\"><img loading=\"lazy\" src=\"https:\/\/lh3.googleusercontent.com\/IRi3Zfx8-UO_uNT0OII76-JPkwcjWwhv2LELKtNAigsIMAQT6-eCowQHVLI606N6i7xS5eOpqsc1Vy68SSFsmezqS_-UnBuvyLoc4-EW9hD8vNL6IFocSYDJlz08WvdAsoqw95Xq\" width=\"157\" height=\"39\"><\/a><\/p>\n<p dir=\"ltr\"><strong>Where:<\/strong><\/p>\n<div class=\"\">\nP(D) is evidence. This is the probability of the data as determined by summing (or integrating) across all possible values of<br \/>\n<a href=\"https:\/\/www.codecogs.com\/eqnedit.php?latex=%5Ctheta#0\"><img loading=\"lazy\" src=\"https:\/\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif\" data-lazy-src=\"https:\/\/lh5.googleusercontent.com\/OR2Ar7BfoaTqT2AE1oK7qyOPwv5obRt-gr-LvLLOV38xTGDEU4iMvRfeGxDbEbqanRREWEELjjmpC1GJk32BbXb_kW65rjQkSwfc_WbMf7qz09P9SaPoCka-JNKFjYCpS3VkHR1b\" width=\"7\" height=\"12\"><img loading=\"lazy\" src=\"https:\/\/lh5.googleusercontent.com\/OR2Ar7BfoaTqT2AE1oK7qyOPwv5obRt-gr-LvLLOV38xTGDEU4iMvRfeGxDbEbqanRREWEELjjmpC1GJk32BbXb_kW65rjQkSwfc_WbMf7qz09P9SaPoCka-JNKFjYCpS3VkHR1b\" width=\"7\" height=\"12\"><\/a> , weighted by how strongly we believe in those particular values of<br \/>\n<a href=\"https:\/\/www.codecogs.com\/eqnedit.php?latex=%5Ctheta#0\" class=\"tve-froala\"><img loading=\"lazy\" src=\"https:\/\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif\" data-lazy-src=\"https:\/\/lh6.googleusercontent.com\/FWKnvGbjINOys1Zp_27EeWmPSYUWqX3-Nx5I76bXqokXJDUL30ESr-IyJZaQN3sUW3buoHUs3fH_2nP8bETUHfoWVwbSbhHkRbXk2Niyj0wJFbMGiAtIE-uXvOgCVin4vw6qO5uX\" width=\"7\" height=\"12\"><img loading=\"lazy\" src=\"https:\/\/lh6.googleusercontent.com\/FWKnvGbjINOys1Zp_27EeWmPSYUWqX3-Nx5I76bXqokXJDUL30ESr-IyJZaQN3sUW3buoHUs3fH_2nP8bETUHfoWVwbSbhHkRbXk2Niyj0wJFbMGiAtIE-uXvOgCVin4vw6qO5uX\" width=\"7\" height=\"12\"><\/a>.\n<\/div>\n<p dir=\"ltr\">We can see that we need to calculate the evidence P(D). In order to achieve this we need to evaluate the following integral, which integrates over all possible values of, the parameters:<\/p>\n<p dir=\"ltr\"><a href=\"https:\/\/www.codecogs.com\/eqnedit.php?latex=P(D)%20%3D%20%5Cint_%7B%5CTheta%7D%20P(D%2C%20%5Ctheta)%20%5Ctext%7Bd%7D%5Ctheta#0\"><img loading=\"lazy\" src=\"https:\/\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif\" data-lazy-src=\"https:\/\/lh4.googleusercontent.com\/LsxymHctpfTMYBV37YujsT61aA09hJ7RPhhKbRg0jFbvPBgC8HaxcdlothaBzMd-bBkrl9SCfP4mgwruC3D4UyrSxfRNjFlBCJ1BMRu-HtCeEwei9v5ihZLs85tEuU32KsaC7_5N\" width=\"152\" height=\"37\"><img loading=\"lazy\" src=\"https:\/\/lh4.googleusercontent.com\/LsxymHctpfTMYBV37YujsT61aA09hJ7RPhhKbRg0jFbvPBgC8HaxcdlothaBzMd-bBkrl9SCfP4mgwruC3D4UyrSxfRNjFlBCJ1BMRu-HtCeEwei9v5ihZLs85tEuU32KsaC7_5N\" width=\"152\" height=\"37\"><\/a><\/p>\n<p dir=\"ltr\">The fundamental problem is that we are often unable to evaluate this integral analytically and so we must turn to a numerical approximation method instead.\u00a0<\/p>\n<p dir=\"ltr\">An additional problem is that our models might require a large number of parameters. This means that our prior distributions could potentially have a large number of dimensions.<\/p>\n<p dir=\"ltr\">This in turn means that our posterior distributions will also be high dimensional. Hence, we are in a situation where we have to numerically evaluate an integral in a potentially very large dimensional space.<\/p>\n<p dir=\"ltr\">This means we are in a situation often described as the Curse of Dimensionality. Informally, this means that the volume of a high-dimensional space is so vast that any available data becomes extremely sparse within that space and hence leads to problems of statistical significance.<\/p>\n<p dir=\"ltr\">Practically, in order to gain any statistical significance, the volume of data needed must grow exponentially with the number of dimensions.<\/p>\n<p dir=\"ltr\">Such problems are often extremely difficult to tackle unless they are approached in an intelligent manner. The motivation behind Markov Chain Monte Carlo\u2019s methods is that they perform an intelligent search within a high dimensional space and thus Bayesian Models in high dimensions become easy to control.<\/p>\n<p dir=\"ltr\">The basic idea is to sample from the posterior distribution by combining a \u201c<strong>random search<\/strong>\u201d with a mechanism for intelligently \u201cjumping\u201d around, but in a manner that ultimately doesn\u2019t depend on where we started from.<\/p>\n<p dir=\"ltr\">Hence Markov Chain Monte Carlo methods are memoryless searches performed with intelligent jumps.<\/p>\n<h2 id=\"t-1597295730174\" class=\"\">The Metropolis Algorithm\u00a0<\/h2>\n<p dir=\"ltr\">There is a large family of Algorithms that perform MCMC. Most of these algorithms can be expressed at a high level as follows:\u00a0<\/p>\n<ol class=\"\">\n<li>Begin the algorithm at the current position in parameter space.\u00a0<\/li>\n<li>Propose a \u201cjump\u201d to a new position in parameter space.\u00a0<\/li>\n<li>Accept or reject the jump probabilistically using the prior information and available data.\u00a0<\/li>\n<li>If the jump is accepted, move to the new position and return to step 1.<\/li>\n<li>If the jumps are rejected, stay where you are and return to step 1.<\/li>\n<li>After a set of a number of jumps has occurred, return all accepted positions.\u00a0<\/li>\n<\/ol>\n<p dir=\"ltr\">The main difference between <strong>MCMC algorithms<\/strong> occurs in how you jump as well as how you decide whether to jump.\u00a0<\/p>\n<p dir=\"ltr\">The Metropolis algorithm uses a normal distribution to propose a jump. This normal distribution has a mean value<strong> \u03bc<\/strong> which is equal to the current position and takes a &#8220;proposal width&#8221; for its standard deviation<strong> \u03c3.<\/strong><\/p>\n<p dir=\"ltr\">A normal distribution is a good choice for such a proposal distribution (for continuous parameters), it is more likely to select points nearer to the current position than further away. However, it will occasionally choose points further away, allowing the space to be explored.<\/p>\n<p dir=\"ltr\">Once the jump has been proposed, we need to decide (in a <strong>probabilistic<\/strong> manner) whether it is a good move to jump to the new position. How do we do this? We calculate the ratio of the proposal distribution of the new position and the proposal distribution at the current position to determine the probability of moving, p:<\/p>\n<p><a href=\"https:\/\/www.codecogs.com\/eqnedit.php?latex=p%20%3D%20P(%5Ctheta_%7B%5Ctext%7Bnew%7D%7D)%2FP(%5Ctheta_%7B%5Ctext%7Bcurrent%7D%7D)#0\" class=\"tve-froala\"><img loading=\"lazy\" src=\"https:\/\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif\" data-lazy-src=\"https:\/\/lh3.googleusercontent.com\/HRoemsHNavKrHeFz9WfkB2sueJUNkzvHaQoBPrI65zSy5-G_QxZJ05ReJXpsrMzKNZc-bYCW1cPQSn9iShTJXMpxeW_pF_FWH5QO9ZeN1rVnOgRv3DzH-79SnbNVMctqQSBYmtAF\" width=\"160\" height=\"16\"><img loading=\"lazy\" src=\"https:\/\/lh3.googleusercontent.com\/HRoemsHNavKrHeFz9WfkB2sueJUNkzvHaQoBPrI65zSy5-G_QxZJ05ReJXpsrMzKNZc-bYCW1cPQSn9iShTJXMpxeW_pF_FWH5QO9ZeN1rVnOgRv3DzH-79SnbNVMctqQSBYmtAF\" width=\"160\" height=\"16\"><\/a><\/p>\n<h3 id=\"t-1597295730175\" class=\"\">About PyMC3<\/h3>\n<p dir=\"ltr\"><a href=\"https:\/\/docs.pymc.io\" target=\"_blank\" class=\"tve-froala\" rel=\"noopener noreferrer\">PyMC3<\/a> is a Python package for Bayesian statistical modeling and probabilistic machine learning which focuses on advanced Markov chain Monte Carlo and variational fitting algorithms. It is a rewrite from scratch of the previous version of the PyMC software.<\/p>\n<h3 id=\"t-1597295730176\" class=\"\">Simulation using PyMC3<\/h3>\n<p dir=\"ltr\">The example we want to model and simulate is based on this scenario: a daily flight from London to Rome has a scheduled departure time at 12:00 am, and a standard flight time of two hours.<\/p>\n<p dir=\"ltr\">We need to organize the operations at the destination airport, but we don&#8217;t want to allocate resources when the plane hasn&#8217;t landed yet. Therefore, we want to model the process using a Bayesian network and considering some common factors that can influence the arrival time.<\/p>\n<p dir=\"ltr\">In particular, we know that the onboarding process can be longer than expected, as well as the refueling one, even if they are carried out in parallel. London air traffic control can also impose a delay, and the same can happen when the plane is approaching Rome. We also know that the presence of rough weather can cause another delay due to a change of route.\u00a0<\/p>\n<p dir=\"ltr\">We can <strong>summarise<\/strong> this analysis with the following plot<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173e65288ed\"><span class=\"tve_image_frame\"><img loading=\"lazy\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/2-markov-chain-simulation.png?resize=613%2C393&amp;ssl=1\" class=\"tve_image wp-image-4738\" alt=\"markov chain simulation\" data-id=\"4738\" width=\"613\" data-init-width=\"1024\" height=\"393\" data-init-height=\"656\" title=\"markov chain simulation\" data-width=\"613\" data-height=\"393\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4738\" alt=\"markov chain simulation\" data-id=\"4738\" width=\"613\" data-init-width=\"1024\" height=\"393\" data-init-height=\"656\" title=\"markov chain simulation\" src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/2-markov-chain-simulation.png?resize=613%2C393&amp;ssl=1\" data-width=\"613\" data-height=\"393\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p dir=\"ltr\">Bayesian network representing the <strong>air traffic<\/strong> control problem<\/p>\n<p dir=\"ltr\">Considering our experience, we decide to model the random variables using the following distributions:<\/p>\n<ol class=\"\">\n<li>Passenger onboarding ~ Wald(\u00b5 = 0.5, \u03bb = 0.2)<\/li>\n<li>Refueling ~ Wald( \u00b5 = 0.25, \u03bb = 0.5)<\/li>\n<li>Departure traffic delay ~ Wald(\u00b5 = 0.1, \u03bb = 0.2)<\/li>\n<li>Arrival traffic delay ~ Wald(\u00b5 = 0.1, \u03bb = 0.2)<\/li>\n<li>Departure time = 12 + Departure traffic delay + max(Passenger onboarding, Refueling)<\/li>\n<li>Rough weather ~ Bernoulli(p =0.35)<\/li>\n<li>Flight time ~ Exponential(\u03bb = 0.5 &#8211; (0.1 . Rough weather))(The output of a Bernoulli distribution is 0 or 1 corresponding to False and True)<\/li>\n<li>Arrival time = Departure time + Flight time + Arrival traffic delay<\/li>\n<\/ol>\n<p dir=\"ltr\">Departure time and Arrival time are functions of random variables, and the parameter \u03bb of Flight time is also a function of Rough Weather.\u00a0<\/p>\n<h2 class=\"\" id=\"t-1597295730182\">Markov Chain Monte Carlo Simulation with PyMC3<\/h2>\n<p dir=\"ltr\">Even if the model is not very complex, the direct inference is rather inefficient, and therefore we want to simulate the process using PyMC3.<\/p>\n<p dir=\"ltr\">The first step is to create a model instance:<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173e653c116\"><span class=\"tve_image_frame\"><img loading=\"lazy\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/3-pymc3-import.png?resize=613%2C430&amp;ssl=1\" class=\"tve_image wp-image-4742\" alt=\"pymc3 import\" data-id=\"4742\" width=\"613\" data-init-width=\"684\" height=\"430\" data-init-height=\"480\" title=\"pymc3 import\" data-width=\"613\" data-height=\"430\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4742\" alt=\"pymc3 import\" data-id=\"4742\" width=\"613\" data-init-width=\"684\" height=\"430\" data-init-height=\"480\" title=\"pymc3 import\" src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/3-pymc3-import.png?resize=613%2C430&amp;ssl=1\" data-width=\"613\" data-height=\"430\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p dir=\"ltr\">From now on, all operations must be performed using the context manager provided by the model variable. We can now set up all the random variables of our Bayesian network<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173e65476dc\"><span class=\"tve_image_frame\"><img loading=\"lazy\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/4-Markov-chain-python-code.png?resize=613%2C844&amp;ssl=1\" class=\"tve_image wp-image-4745\" alt=\"Markov chain python code\" data-id=\"4745\" width=\"613\" data-init-width=\"1524\" height=\"844\" data-init-height=\"2100\" title=\"Markov chain python code\" data-width=\"613\" data-height=\"844\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4745\" alt=\"Markov chain python code\" data-id=\"4745\" width=\"613\" data-init-width=\"1524\" height=\"844\" data-init-height=\"2100\" title=\"Markov chain python code\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/4-Markov-chain-python-code.png?resize=613%2C844&amp;ssl=1\" data-width=\"613\" data-height=\"844\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p dir=\"ltr\">We have imported two namespaces, pymc3.distributions.continuous and pymc3.distributions.discrete because we are using both kinds of variables. <\/p>\n<p dir=\"ltr\">Wald and exponential are continuous distributions, while Bernoulli is discrete. In the first three rows, we declare the variables passenger_onboarding, refueling, and departure_traffic_delay. <\/p>\n<p dir=\"ltr\">The structure is always the same: we need to specify the class corresponding to the desired distribution, passing the name of the variable and all the required parameters.<\/p>\n<p dir=\"ltr\">The departure_time variable is declared as pm. Deterministic. In PyMC3, this means that, once all the random elements have been set, its value becomes completely determined. <\/p>\n<p dir=\"ltr\">Indeed, if we sample from departure_traffic_delay, passenger_onboarding, and refueling, we get a determined value for departure_time. In this declaration, we&#8217;ve also used the utility function pmm.switch, which operates a binary choice based on its first parameter (for example, if A &gt; B, return A, else return B).<\/p>\n<p dir=\"ltr\">The other variables are very similar, except for flight_time, which is an exponential variable with a parameter \u03bb, which is a function of another variable (rough_weather). As a Bernoulli variable outputs 1 with probability p and 0 with probability 1 &#8211; p, \u03bb = 0.4 if there&#8217;s rough weather, and 0.5 otherwise.<\/p>\n<p dir=\"ltr\">Once the model has been set up, it&#8217;s possible to simulate it through a sampling process. PyMC3 picks the best sampler automatically, according to the type of variables. As the model is not very complex, we can limit the process to 500 samples:<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173e6558817\"><span class=\"tve_image_frame\"><img loading=\"lazy\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/5-mark-chain-model-output.png?resize=613%2C230&amp;ssl=1\" class=\"tve_image wp-image-4748\" alt=\"mark chain model output\" data-id=\"4748\" width=\"613\" data-init-width=\"1420\" height=\"230\" data-init-height=\"534\" title=\"mark chain model output\" data-width=\"613\" data-height=\"230\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4748\" alt=\"mark chain model output\" data-id=\"4748\" width=\"613\" data-init-width=\"1420\" height=\"230\" data-init-height=\"534\" title=\"mark chain model output\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/5-mark-chain-model-output.png?resize=613%2C230&amp;ssl=1\" data-width=\"613\" data-height=\"230\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p dir=\"ltr\">The output can be analyzed using the built-in pm.traceplot() function, which generates the plots for each of the sample\u2019s variables. The following graph shows the detail of one of them:<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173e6562e40\"><span class=\"tve_image_frame\"><img loading=\"lazy\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/6-arrive-and-departurn-times.png?resize=613%2C198&amp;ssl=1\" class=\"tve_image wp-image-4750\" alt=\"arrive and departurn times\" data-id=\"4750\" width=\"613\" data-init-width=\"1262\" height=\"198\" data-init-height=\"408\" title=\"arrive and departurn times\" data-width=\"613\" data-height=\"198\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4750\" alt=\"arrive and departurn times\" data-id=\"4750\" width=\"613\" data-init-width=\"1262\" height=\"198\" data-init-height=\"408\" title=\"arrive and departurn times\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/6-arrive-and-departurn-times.png?resize=613%2C198&amp;ssl=1\" data-width=\"613\" data-height=\"198\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>Distribution and samples for the arrival time random variable<\/strong>\u00a0<\/p>\n<p dir=\"ltr\">PyMC3 provides a statistical summary that can help us in making the right decisions using <strong>pm.summary()<\/strong>. In the following snippet, the output containing the summary of a single variable is shown:<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173e657180e\"><span class=\"tve_image_frame\"><img loading=\"lazy\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/7-PyMC3-output.png?resize=613%2C162&amp;ssl=1\" class=\"tve_image wp-image-4752\" alt=\"PyMC3 output\" data-id=\"4752\" width=\"613\" data-init-width=\"1980\" height=\"162\" data-init-height=\"522\" title=\"PyMC3 output\" data-width=\"613\" data-height=\"162\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4752\" alt=\"PyMC3 output\" data-id=\"4752\" width=\"613\" data-init-width=\"1980\" height=\"162\" data-init-height=\"522\" title=\"PyMC3 output\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/7-PyMC3-output.png?resize=613%2C162&amp;ssl=1\" data-width=\"613\" data-height=\"162\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element tve-froala fr-box fr-basic\">\n<p dir=\"ltr\">For each variable, it contains mean, standard deviation, Monte Carlo error, <strong>95%<\/strong> highest posterior density interval, and the posterior quantiles. In our case, we know that the plane will land at about 15:10 (15.174).<\/p>\n<p dir=\"ltr\">This is only a very simple example to show the power of Bayesian networks.<\/p>\n<h2 id=\"t-1597295730177\" class=\"\">List of parametric and non-parametric Algorithms<\/h2>\n<p dir=\"ltr\">Machine learning algorithms can be classified as two distinct groups: parametric and non-parametric<\/p>\n<p dir=\"ltr\">We can classify algorithms as non-parametric when models become more complex if the number of samples in the training set increases. Vice versa, a model would be parametric if the model becomes stable when the number of examples in the training set increases.\u00a0<\/p>\n<p dir=\"ltr\">In simple terms, we can say parametric has a functional form while non-parametric has no functional form.\u00a0<\/p>\n<p dir=\"ltr\">Functional form comprises a simple formula like y = f(x). So if you input a value, you are to get a fixed output value. It means if the data set is changed or being changed there is not much variation in the results. But in non-parametric algorithms, a small change in data sets can result in a large change in results.<\/p>\n<ul class=\"\">\n<li>Non-parametric models\u00a0<\/li>\n<li class=\" dir=\">Parametric models<\/li>\n<\/ul>\n<h2 id=\"t-1597295730178\" class=\"\">List of methods that can perform MCMC<\/h2>\n<ul class=\"\">\n<li>The metropolis algorithm\u00a0<\/li>\n<li>The Metropolis-Hasting algorithm<\/li>\n<li>The Gibbs sampler\u00a0<\/li>\n<li>Hamiltonian Monte Carlo\u00a0<\/li>\n<li>No U-turn sampler (and several variants)<\/li>\n<\/ul>\n<h2 id=\"t-1597295730181\" class=\"\">Complete Code<\/h2>\n<p>Below is the complete code we have explained in the article, you clone the code in \u00a0our <a href=\"https:\/\/github.com\/saimadhu-polamuri\/DataAspirant_codes\/tree\/master\/markov_chains\" target=\"_blank\" rel=\"noopener noreferrer\">GitHub<\/a> repo too.<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h2 id=\"t-1597295730179\" class=\"\">Conclusion<\/h2>\n<p dir=\"ltr\">In this article, we learned the basics of markov chain monte carlo, one specific method known as the Metropolis algorithm, how to implement them using PyMC3.\u00a0<\/p>\n<h2 id=\"t-1597295730180\" class=\"\">Next Steps\u00a0<\/h2>\n<p dir=\"ltr\">In the coming articles, we\u2019ll further discuss sampling techniques such as Metropolis-Hastings, Gibbs Sampling, and Hamiltonian Monte Carlo.<\/p>\n<h4 class=\"\">Recommended Courses<\/h4>\n<\/div>\n<div class=\"thrv_wrapper thrv-page-section thrv-lp-block\" data-inherit-lp-settings=\"1\" data-css=\"tve-u-173e6ad7b44\">\n<div class=\"tve-page-section-in tve_empty_dropzone  \" data-css=\"tve-u-173e6ad7dc3\">\n<div class=\"thrv_wrapper thrv-columns dynamic-group-kbt3q0q7\" data-css=\"tve-u-173e6ad7b46\">\n<div class=\"tcb-flex-row v-2 tcb--cols--3 tcb-medium-no-wrap tcb-mobile-wrap m-edit\" data-css=\"tve-u-173e6ad7b47\">\n<div class=\"tcb-flex-col\">\n<div class=\"tcb-col dynamic-group-kbt3pyfd\" data-css=\"tve-u-173e6ad7b48\">\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad dynamic-group-kbt3pwhk\" data-css=\"tve-u-173e6ad7b49\">\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper tve_image_caption dynamic-group-kbt3pu4z\" data-css=\"tve-u-173e6ad7b54\"><span class=\"tve_image_frame\"><a href=\"https:\/\/dataaspirant.com\/recommends\/ds-courses\/educative-machine-learning-interveiw\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img loading=\"lazy\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/machine_learning_interview_prep.png?resize=172%2C86&amp;ssl=1\" class=\"tve_image wp-image-4799\" alt=\"machine learning interview prep\" data-id=\"4799\" width=\"172\" data-init-width=\"600\" height=\"86\" data-init-height=\"300\" title=\"machine_learning_interview_prep\" data-width=\"172\" data-height=\"86\" data-css=\"tve-u-173e6ad7b55\" data-link-wrap=\"true\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4799\" alt=\"machine learning interview prep\" data-id=\"4799\" width=\"172\" data-init-width=\"600\" height=\"86\" data-init-height=\"300\" title=\"machine_learning_interview_prep\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/machine_learning_interview_prep.png?resize=172%2C86&amp;ssl=1\" data-width=\"172\" data-height=\"86\" data-css=\"tve-u-173e6ad7b55\" data-link-wrap=\"true\" data-recalc-dims=\"1\"><\/a><span class=\"tve-image-overlay\"><\/span><\/span><\/div>\n<h4 class=\"\" data-css=\"tve-u-173e6ad7b57\">Machine Learning Interview Preparation<\/h4>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"tcb-flex-col\">\n<div class=\"tcb-col dynamic-group-kbt3pyfd\" data-css=\"tve-u-173e6ad7b48\">\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad dynamic-group-kbt3pwhk\" data-css=\"tve-u-173e6ad7b63\">\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper tve_image_caption dynamic-group-kbt3pu4z\" data-css=\"tve-u-173e6ad7b64\"><span class=\"tve_image_frame\"><a href=\"https:\/\/dataaspirant.com\/recommends\/ds-courses\/udemy-markov-chain-model\/\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"><img loading=\"lazy\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/markochains.jpg?resize=172%2C86&amp;ssl=1\" class=\"tve_image wp-image-4804\" alt=\"marko chains\" data-id=\"4804\" width=\"172\" data-init-width=\"480\" height=\"86\" data-init-height=\"270\" title=\"marko chains\" data-width=\"172\" data-height=\"86\" data-css=\"tve-u-173e6ad7b65\" data-link-wrap=\"true\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4804\" alt=\"marko chains\" data-id=\"4804\" width=\"172\" data-init-width=\"480\" height=\"86\" data-init-height=\"270\" title=\"marko chains\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/markochains.jpg?resize=172%2C86&amp;ssl=1\" data-width=\"172\" data-height=\"86\" data-css=\"tve-u-173e6ad7b65\" data-link-wrap=\"true\" data-recalc-dims=\"1\"><\/a><span class=\"tve-image-overlay\"><\/span><\/span><\/div>\n<h4 class=\"\" data-css=\"tve-u-173e6ad7b68\">Markov Chain Simulation in Python<\/h4>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"tcb-flex-col\">\n<div class=\"tcb-col dynamic-group-kbt3pyfd\" data-css=\"tve-u-173e6ad7b48\">\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad dynamic-group-kbt3pwhk\" data-css=\"tve-u-173e6ad7b73\">\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper tve_image_caption dynamic-group-kbt3pu4z\" data-css=\"tve-u-173e6ad7b74\"><span class=\"tve_image_frame\"><a href=\"https:\/\/dataaspirant.com\/recommends\/data-science-courses\/machine-learning-z-hands-python-r-data-science-course-udemy\/\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"><img loading=\"lazy\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/machine-learning-1.jpg?resize=172%2C86&amp;ssl=1\" class=\"tve_image wp-image-4302\" alt=\"Machine learning\" data-id=\"4302\" width=\"172\" data-init-width=\"750\" height=\"86\" data-init-height=\"422\" title=\"machine learning\" data-width=\"172\" data-height=\"86\" data-css=\"tve-u-173e6ad7b75\" data-link-wrap=\"true\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4302\" alt=\"Machine learning\" data-id=\"4302\" width=\"172\" data-init-width=\"750\" height=\"86\" data-init-height=\"422\" title=\"machine learning\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/machine-learning-1.jpg?resize=172%2C86&amp;ssl=1\" data-width=\"172\" data-height=\"86\" data-css=\"tve-u-173e6ad7b75\" data-link-wrap=\"true\" data-recalc-dims=\"1\"><\/a><span class=\"tve-image-overlay\"><\/span><\/span><\/div>\n<h4 class=\"\" data-css=\"tve-u-173e6ad7b77\">Machine Learing A to Z in Python course<\/h4>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/dataaspirant.com\/markov-chain-monte-carlo-simulation-airport-queuing-network\/<\/p>\n","protected":false},"author":0,"featured_media":376,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/posts\/375"}],"collection":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/comments?post=375"}],"version-history":[{"count":0,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/posts\/375\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/media\/376"}],"wp:attachment":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/media?parent=375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/categories?post=375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/tags?post=375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}