{"id":1455,"date":"2022-04-29T23:30:00","date_gmt":"2022-04-30T03:30:00","guid":{"rendered":"http:\/\/aristotle2digital.blogwyrm.com\/?p=1455"},"modified":"2022-04-01T13:52:58","modified_gmt":"2022-04-01T17:52:58","slug":"sunny-day-markov-chain","status":"publish","type":"post","link":"https:\/\/aristotle2digital.blogwyrm.com\/?p=1455","title":{"rendered":"Sunny Day Markov Chain"},"content":{"rendered":"\n<p>In the last post, we demonstrated that not all conditional probabilities are conceptually equal even if they are linguistically equal.&nbsp; The specific case discussed at the end \u2013 the sunny day case \u2013 doesn\u2019t fit the usual mold.&nbsp; To better understand why it doesn\u2019t fit let\u2019s looks back at the textbook case.<\/p>\n\n\n\n<p>Ordinarily, we calculate a conditional probability by fixing one of the variables from a joint probability distribution and renormalizing so that \u2018the probability of $B$ given $A$\u2019, denoted by $P(B|A)$ obeys all the axioms of probability theory.&nbsp; For simplicity, the joint probability distribution in the previous analysis was presented as a table (e.g. color and life of a bulb or snack preference based on sex) but any mathematically multi-variate function capable of being normalized serves.<\/p>\n\n\n\n<p>But the sunny day case is different.&nbsp; In that case, we define transition probabilities that express what the chance that the weather tomorrow will be like given the weather today, where there were only two possible outcomes: \u2018Sunny\u2019 (denoted as $S$) and \u2018Rainy\u2019 (denoted as $R$).&nbsp; The conditional probabilities were:<\/p>\n\n\n\n<p>\\[ P(S|S) = 0.9 \\; \\]<\/p>\n\n\n\n<p>and<\/p>\n\n\n\n<p>\\[ P(R|S) = 0.1 \\; , \\]<\/p>\n\n\n\n<p>for the weather tomorrow based on today\u2019s weather being sunny.&nbsp; Likewise,<\/p>\n\n\n\n<p>\\[ P(S|R) = 0.5 = P(R|R) \\; ,\\]<\/p>\n\n\n\n<p>&nbsp;given that today is rainy.<\/p>\n\n\n\n<p>It isn\u2019t all clear how many actual variables we have in the joint probability distribution.&nbsp; Is it 2, one for today and one for tomorrow?&nbsp; Clearly that\u2019s wrong since in any given span of days, say the month of September, taken in isolation, there are 28 days that serve as both a today and a tomorrow and 1 day each that is either a tomorrow or a today but not both.&nbsp; As we\u2019ll see below, in a real sense there are an infinite number of variables in the joint distribution since there are limitless todays and tomorrows (ignoring the obvious conflict with modern cosmology).<\/p>\n\n\n\n<p>The question left hanging was just how could we determine the probability that, given an $N$-day long span of time, the weather on any given day was sunny.<\/p>\n\n\n\n<p>This type of problem is called a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Markov_chain\">Markov chain<\/a> problem defined as the type of problem where the transition probabilities only depend on the current state and not something further back.&nbsp; This type of system is said to have no memory.<\/p>\n\n\n\n<p>There are two equally general methods for computing the long term probability of any given day being sunny or rainy but they differ in convenience and scope.&nbsp; The first is the analytic method.<\/p>\n\n\n\n<p>In the analytic method, we express the state of the system as a vector ${\\bar S}$ with two components stating the probability of getting a sunny or rainy day. &nbsp;The transition rules express themselves nicely in matrix form according to<\/p>\n\n\n\n<p>\\[ {\\bar S}_{tomorrow} = M {\\bar S}_{today} \\; ,\\]<\/p>\n\n\n\n<p>which has the explicit form<\/p>\n\n\n\n<p>\\[ \\left[ \\begin{array}{c} S \\\\ R \\end{array} \\right]_{tomorrow} = \\left[ \\begin{array}{cc} 0.9 &amp; 0.5 \\\\ 0.1 &amp; 0.5 \\end{array} \\right] \\left[ \\begin{array}{c} S \\\\ R \\end{array} \\right]_{today} \\; . \\]<\/p>\n\n\n\n<p>The explicit form of $M$ can be checked against an initial, known state of either ${\\bar S}^{(0)} = [1,0]^{T}$ for sunny or ${\\bar S}^{(0)} = [0,1]^{T}$ for rainy.<\/p>\n\n\n\n<p>The matrix $M$ links the weather state today to that of tomorrow and we can think of it as an adjacency matrix for the following graph<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"857\" height=\"715\" src=\"http:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2022\/04\/Sunny_days_transitions.png\" alt=\"\" class=\"wp-image-1453\" srcset=\"https:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2022\/04\/Sunny_days_transitions.png 857w, https:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2022\/04\/Sunny_days_transitions-300x250.png 300w, https:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2022\/04\/Sunny_days_transitions-768x641.png 768w, https:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2022\/04\/Sunny_days_transitions-810x676.png 810w\" sizes=\"auto, (max-width: 857px) 100vw, 857px\" \/><\/figure><\/div>\n\n\n\n<p>with the interpretation that the edge weightings are the conditional transition probabilities.<\/p>\n\n\n\n<p>Now lets place an index on any given day just to keep track.&nbsp; The state of the n<sup>th<\/sup> day is related to the initial state by<\/p>\n\n\n\n<p>\\[ {\\bar S}^{(n)} = M {\\bar S}^{(n-1)} = M^n {\\bar S}^{0} \\; .\\]<\/p>\n\n\n\n<p>Since we would like to know the probability of picking a sunny day from a collection of days, we are looking for a steady state solution, one which is immune to random fluctuations associated with a string of good or bad luck.&nbsp; This special state ${\\bar S}^{*}$ must correspond to $n \\rightarrow \\infty$ and so it must remain unchanged by successive applications of $M$ (otherwise it doesn\u2019t represent a steady state).&nbsp; Mathematically, this condition can be expressed as<\/p>\n\n\n\n<p>\\[ {\\bar S}^{*} = M {\\bar S}^{*} &nbsp;\\; , \\]<\/p>\n\n\n\n<p>or, in words, ${\\bar S}^{*}$ is an eigenvector of $M$. Computing the eigenvectors of $M$ is easy with numpy and the results are<\/p>\n\n\n\n<p>\\[ {\\bar S}^{*}_{\\lambda_1} = [0.98058068, 0.19611614]^{T} \\; \\]<\/p>\n\n\n\n<p>and<\/p>\n\n\n\n<p>\\[ {\\bar S}^{*}_{\\lambda_2} = [-0.70710678, 0.70710678]^{T} \\; \\]<\/p>\n\n\n\n<p>for $\\lambda_1 = 1$ and $\\lambda_2 = 0.4$.&nbsp; We reject the vector corresponding to $\\lambda_2$ because it has negative entries which can\u2019t be interpreted as probabilities.&nbsp; All that is left is to scale ${\\bar S}^{*}_{\\lambda_1}$ so that the sum of the entries is 1.&nbsp; Doing so gives,<\/p>\n\n\n\n<p>\\[ {\\bar S}^{*} = [0.8333{\\bar 3},0.16666{\\bar 6}]^{T} \\; ,\\]<\/p>\n\n\n\n<p>where the subscript has been dropped as it serves no purpose.<\/p>\n\n\n\n<p>While mathematically correct, one might feel queasy about $\\lim_{n\\rightarrow \\infty} M^n$ converging to something meaningful. &nbsp;&nbsp;But a few numerical results will help support this argument. Consider the following 3 powers of $M$<\/p>\n\n\n\n<p>\\[ M^2 = \\left[ \\begin{array}{cc} 0.86 &amp; 0.70 \\\\ 0.14 &amp; 0.3 \\end{array} \\right] \\; ,\\]<\/p>\n\n\n\n<p>\\[ M^{10} = \\left[ \\begin{array}{cc} 0.83335081 &amp; 0.83324595 \\\\ 0.16664919 &amp; 0.16675405 \\end{array} \\right] \\; ,\\]<\/p>\n\n\n\n<p>and<\/p>\n\n\n\n<p>\\[ M^{20} = \\left[ \\begin{array}{cc} 0.83333334 &amp; 0.83333332 \\\\ 0.16666666 &amp; 0.16666668 \\end{array} \\right] \\; .\\]<\/p>\n\n\n\n<p>Clearly, repeated powers are causing the matrix to converge and the elements that obtain in the limit are the very probabilities calculated above and, by the time $n=20$ we have essentially reached steady state.<\/p>\n\n\n\n<p>The second method is numerical simulation where we \u2018propagate\u2019 an initial state forward in time according to the following rules:<\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\"><li>draw a uniform random number in the range $P \\in [0,1]$ (standard range)<\/li><li>if the current state is $S$ then<ol><li>set the new state to $S$ if $P \\leq 0.9$<\/li><\/ol><ol><li>else set the new state to $R$<\/li><\/ol><\/li><li>if the current state is $R$ then<ol><li>set the new state to $S$ is $P \\leq 0.5$<\/li><\/ol><ol><li>else set the new state to $R$<\/li><\/ol><\/li><li>Record the new state and repeat<\/li><\/ol>\n\n\n\n<p>An alternative way of writing the same thing reads<\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\"><li>draw a uniform random number in the range $P \\in [0,1]$ (standard range)<\/li><li>if the current state is $S$ then<ol><li>set the new state to $R$ if $P \\leq 0.1$<\/li><\/ol><ol><li>else set the new state to $S$<\/li><\/ol><\/li><li>if the current state is $R$ then<ol><li>set the new state to $R$ is $P \\leq 0.5$<\/li><\/ol><ol><li>else set the new state to $R$<\/li><\/ol><\/li><li>Record the new state and repeat<\/li><\/ol>\n\n\n\n<p>Of course, hybridizations of these rule sets where block 2 in the first set if matched with block 3 in the second and vice versa also give the same results.&nbsp; It is a matter of taste.<\/p>\n\n\n\n<p>In any event, these rules are easy to implement in python and the following plot shows the fraction of $S$\u2019s as a function of the number of times the rule has been implemented<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"480\" src=\"http:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2022\/04\/Sunny_day_probs.png\" alt=\"\" class=\"wp-image-1454\" srcset=\"https:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2022\/04\/Sunny_day_probs.png 640w, https:\/\/aristotle2digital.blogwyrm.com\/wp-content\/uploads\/2022\/04\/Sunny_day_probs-300x225.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure><\/div>\n\n\n\n<p>The dotted line shows the value for the probability of a sunny day obtained from the first method and &nbsp;clearly the simulations shows that it converges to that value.&nbsp; This sunny\/rainy day simulation is an example of Markov Chain Monte Carlo, which will be the subject of upcoming blogs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last post, we demonstrated that not all conditional probabilities are conceptually equal even if they are linguistically equal.&nbsp; The specific case discussed at the end \u2013 the sunny&#8230; <a class=\"read-more-button\" href=\"https:\/\/aristotle2digital.blogwyrm.com\/?p=1455\">Read more &gt;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1455","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/aristotle2digital.blogwyrm.com\/index.php?rest_route=\/wp\/v2\/posts\/1455","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aristotle2digital.blogwyrm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aristotle2digital.blogwyrm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aristotle2digital.blogwyrm.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/aristotle2digital.blogwyrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1455"}],"version-history":[{"count":0,"href":"https:\/\/aristotle2digital.blogwyrm.com\/index.php?rest_route=\/wp\/v2\/posts\/1455\/revisions"}],"wp:attachment":[{"href":"https:\/\/aristotle2digital.blogwyrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aristotle2digital.blogwyrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aristotle2digital.blogwyrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}