{"id":182,"date":"2012-07-09T12:54:25","date_gmt":"2012-07-09T17:54:25","guid":{"rendered":"https:\/\/blogs.mathworks.com\/cleve\/?p=182"},"modified":"2013-05-02T10:05:15","modified_gmt":"2013-05-02T15:05:15","slug":"friday-the-13th","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/cleve\/2012\/07\/09\/friday-the-13th\/","title":{"rendered":"Friday the 13th"},"content":{"rendered":"<!DOCTYPE html\r\n  PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\">\r\n<style type=\"text\/css\">\r\n\r\nh1 { font-size:18pt; }\r\nh2.titlebg { font-size:13pt; }\r\nh3 { color:#4A4F55; padding:0px; margin:5px 0px 5px; font-family:Arial, Helvetica, sans-serif; font-size:11pt; font-weight:bold; line-height:140%; border-bottom:1px solid #d6d4d4; display:block; }\r\nh4 { color:#4A4F55; padding:0px; margin:0px 0px 5px; font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-weight:bold; line-height:140%; border-bottom:1px solid #d6d4d4; display:block; }\r\n   \r\np { padding:0px; margin:0px 0px 20px; }\r\nimg { padding:0px; margin:0px 0px 20px; border:none; }\r\np img, pre img, tt img, li img { margin-bottom:0px; } \r\n\r\nul { padding:0px; margin:0px 0px 20px 23px; list-style:square; }\r\nul li { padding:0px; margin:0px 0px 7px 0px; background:none; }\r\nul li ul { padding:5px 0px 0px; margin:0px 0px 7px 23px; }\r\nul li ol li { list-style:decimal; }\r\nol { padding:0px; margin:0px 0px 20px 0px; list-style:decimal; }\r\nol li { padding:0px; margin:0px 0px 7px 23px; list-style-type:decimal; }\r\nol li ol { padding:5px 0px 0px; margin:0px 0px 7px 0px; }\r\nol li ol li { list-style-type:lower-alpha; }\r\nol li ul { padding-top:7px; }\r\nol li ul li { list-style:square; }\r\n\r\npre, tt, code { font-size:12px; }\r\npre { margin:0px 0px 20px; }\r\npre.error { color:red; }\r\npre.codeinput { padding:10px; border:1px solid #d3d3d3; background:#f7f7f7; }\r\npre.codeoutput { padding:10px 11px; margin:0px 0px 20px; color:#4c4c4c; }\r\n\r\n@media print { pre.codeinput, pre.codeoutput { word-wrap:break-word; width:100%; } }\r\n\r\nspan.keyword { color:#0000FF }\r\nspan.comment { color:#228B22 }\r\nspan.string { color:#A020F0 }\r\nspan.untermstring { color:#B20000 }\r\nspan.syscmd { color:#B28C00 }\r\n\r\n.footer { width:auto; padding:10px 0px; margin:25px 0px 0px; border-top:1px dotted #878787; font-size:0.8em; line-height:140%; font-style:italic; color:#878787; text-align:left; float:none; }\r\n.footer p { margin:0px; }\r\n\r\n  <\/style><div class=\"content\"><!--introduction--><p>We all know that Friday the 13th is unlucky, but is it unlikely?<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#9ca96c73-540e-49f7-8d54-1abea47bc429\">Year 2012<\/a><\/li><li><a href=\"#8540c465-5c77-4329-b1a2-d9764fa2a4e6\">Friday the 13th<\/a><\/li><li><a href=\"#05ae1a83-a2e7-41b1-b434-e7993cd178a1\">Calendars and Leap Years<\/a><\/li><li><a href=\"#19044af7-c60c-4fde-8ebb-1b8c1bc55485\">Clock<\/a><\/li><li><a href=\"#5d934110-9bcd-40ee-9814-9fe4918e4942\">Datenum<\/a><\/li><li><a href=\"#1ad2f751-be3f-4535-abf3-0e19de4f3bc4\">Calendar number<\/a><\/li><li><a href=\"#112a8910-3d33-4b1d-bc5d-d69f32f49773\">Friday the 13th is likely<\/a><\/li><\/ul><\/div><h4>Year 2012<a name=\"9ca96c73-540e-49f7-8d54-1abea47bc429\"><\/a><\/h4><p>I plan to post this article during the second week of July, 2012. The Friday in this week is a Friday the 13th, the third we've had so far this year. There were also ones in January and April. That seems like a lot. How often do we have three Friday the 13ths in the first seven months of a year? Well, it's not all that often. It usually happens only once every 28 years. The next time will be the year 2040. But sometimes, around the turn of centuries, it happens twice in 12 years. I mention all this to establish that our calendar does not have a simple periodic behavior. By the way, not to worry, after this week, it will be 14 months until the next Friday the 13th, in September, 2013.<\/p><h4>Friday the 13th<a name=\"8540c465-5c77-4329-b1a2-d9764fa2a4e6\"><\/a><\/h4><p>Which brings us to the central topic of this post:<\/p><div><ul><li><i>What is the probability that the 13th of a month falls on a Friday?<\/i><\/li><\/ul><\/div><p>An obvious response is<\/p><div><ul><li><i>Easy question, the probability is 1\/7.<\/i><\/li><\/ul><\/div><p>After all, there are seven days in a week and the 13th of a month is equally likely to fall on any one of them. Well, as we shall see, that's close, but not exactly right.<\/p><h4>Calendars and Leap Years<a name=\"05ae1a83-a2e7-41b1-b434-e7993cd178a1\"><\/a><\/h4><p>Leap years make our calendar a nontrivial mathematical object. The leap year rule can be implemented by this anonymous function.<\/p><pre class=\"codeinput\">leapyear = @(y) mod(y,4)==0 &amp; mod(y,100)~=0 | mod(y,400)==0;\r\n<\/pre><p>This says that leap years happen every four years, except the turn of a century not divisible by 400 is skipped. Try a few year numbers.<\/p><pre class=\"codeinput\">y = [2012 2013 2000 2100]';\r\ndisp([y leapyear(y)])\r\n<\/pre><pre class=\"codeoutput\">        2012           1\r\n        2013           0\r\n        2000           1\r\n        2100           0\r\n\r\n<\/pre><p>So, this year is a leap year, next year is not, 2000 was a leap year, 2100 is not.<\/p><p>The leap year rule implies that our calendar has a period of 400 years. The calendar from 1601 to 2000 is being reused from 2001 to 2400. (Except the Gregorian calendar had not been invented in 1601, so I'm talking about the calendar that would have been used back then if they could have used today's calendar, but never mind.)<\/p><p>In a 400 year period, there are 97 leap years, 4800 months, 20871 weeks, and 146097 days. So the average number of days in a calendar year is not 365.25, but<\/p><pre class=\"codeinput\">format <span class=\"string\">short<\/span>\r\ndpy = 365+97\/400\r\n<\/pre><pre class=\"codeoutput\">\r\ndpy =\r\n\r\n  365.2425\r\n\r\n<\/pre><p>We can compute the probability that the 13th of a month is a Friday by counting how many times that happens in 4800 months. The correct probability is then that count divided by 4800. Since 4800 is not divisible by 7, the probability does not reduce to 1\/7.<\/p><h4>Clock<a name=\"19044af7-c60c-4fde-8ebb-1b8c1bc55485\"><\/a><\/h4><p>MATLAB has a number of functions for doing computations involving calendars and dates. Many of these functions are in the MATLAB Toolbox, but some of the more specialized ones are in the Finance Toolbox. We encountered a few of these functions in my blog about biorhythms. The basis for all the functions is <tt>clock<\/tt>, which reads the system's clock and returns a 6-element vector<\/p><pre>  [year, month, date, hour, minute, seconds]<\/pre><p>The first five elements have integer values. The sixth element has a fractional part whose accuracy depends upon the computer's internal clock. Here is the output generated when I <tt>publish<\/tt> this blog.<\/p><pre class=\"codeinput\">c = clock;\r\nfprintf(<span class=\"string\">'clock = [ %4d %4d %5d %5d %5d %8.3f ]\\n'<\/span>,c)\r\n<\/pre><pre class=\"codeoutput\">clock = [ 2012    7     5    11    53   14.258 ]\r\n<\/pre><h4>Datenum<a name=\"5d934110-9bcd-40ee-9814-9fe4918e4942\"><\/a><\/h4><p>The <tt>datenum<\/tt> function facilitates computations involving calendars by collapsing the <tt>clock<\/tt> vector into one value, the <i>serial date number<\/i>. This value is the number of days, and fractions of a day, since a reference time 20 centuries ago when <tt>clock<\/tt> would have been all zeros. Here are a couple of examples of the use of <tt>datenum<\/tt>. If you run this code yourself, your results should be different.<\/p><pre class=\"codeinput\">t = now;\r\nfprintf(<span class=\"string\">'current_date_number = %10.3f\\n'<\/span>,t)\r\ndate_string = datestr(t)\r\ntday = fix(t)\r\ntday_string = datestr(tday)\r\n[week_day,week_day_name] = weekday(tday)\r\n<\/pre><pre class=\"codeoutput\">current_date_number = 735055.495\r\n\r\ndate_string =\r\n\r\n05-Jul-2012 11:53:14\r\n\r\n\r\ntday =\r\n\r\n      735055\r\n\r\n\r\ntday_string =\r\n\r\n05-Jul-2012\r\n\r\n\r\nweek_day =\r\n\r\n     5\r\n\r\n\r\nweek_day_name =\r\n\r\nThu\r\n\r\n<\/pre><h4>Calendar number<a name=\"1ad2f751-be3f-4535-abf3-0e19de4f3bc4\"><\/a><\/h4><p>The calendar for any year is determined by two pieces of information, the weekday of January 1st and whether or not the year is a leap year. So we need only 14 calendars. We could number all possible calendars, with the units digit specifying the starting week day and the tens digits indicating leap years.  The 14 numbers would be <tt>[1:7 11:17]<\/tt>.<\/p><pre class=\"codeinput\">calendar_number = @(y) weekday(datenum(y,1,1)) + 10*leapyear(y);\r\n<\/pre><p>If the calendar industry used this numbering scheme, here are the calendars you would need for the next 21 years.<\/p><pre class=\"codeinput\">y = (2012:2032)';\r\ndisp([y calendar_number(y)])\r\n<\/pre><pre class=\"codeoutput\">        2012          11\r\n        2013           3\r\n        2014           4\r\n        2015           5\r\n        2016          16\r\n        2017           1\r\n        2018           2\r\n        2019           3\r\n        2020          14\r\n        2021           6\r\n        2022           7\r\n        2023           1\r\n        2024          12\r\n        2025           4\r\n        2026           5\r\n        2027           6\r\n        2028          17\r\n        2029           2\r\n        2030           3\r\n        2031           4\r\n        2032          15\r\n\r\n<\/pre><h4>Friday the 13th is likely<a name=\"112a8910-3d33-4b1d-bc5d-d69f32f49773\"><\/a><\/h4><p>We are now ready to use the <tt>weekday<\/tt> function to count the number of times in a 400-year calendar cycle that the 13th of a month occurs on each of the various days of the week.<\/p><pre class=\"codeinput\">c = zeros(1,7);\r\n<span class=\"keyword\">for<\/span> y = 1601:2000\r\n   <span class=\"keyword\">for<\/span> m = 1:12\r\n      d = datenum([y,m,13]);\r\n      w = weekday(d);\r\n      c(w) = c(w) + 1;\r\n   <span class=\"keyword\">end<\/span>\r\n<span class=\"keyword\">end<\/span>\r\nc\r\n<\/pre><pre class=\"codeoutput\">\r\nc =\r\n\r\n   687   685   685   687   684   688   684\r\n\r\n<\/pre><p>A bar graph, with a line at a probability of 1\/7, and week day axis labels.<\/p><pre class=\"codeinput\">bar(c)\r\naxis([0 8 680 690])\r\navg = 4800\/7;\r\nline([0 8], [avg avg],<span class=\"string\">'linewidth'<\/span>,4,<span class=\"string\">'color'<\/span>,<span class=\"string\">'black'<\/span>)\r\nset(gca,<span class=\"string\">'xticklabel'<\/span>,{<span class=\"string\">'Su'<\/span>,<span class=\"string\">'M'<\/span>,<span class=\"string\">'Tu'<\/span>,<span class=\"string\">'W'<\/span>,<span class=\"string\">'Th'<\/span>,<span class=\"string\">'F'<\/span>,<span class=\"string\">'Sa'<\/span>})\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/cleve\/friday13_01.png\" alt=\"\"> <p>The probability for Friday is<\/p><pre class=\"codeinput\">p = c(6)\/4800;\r\nfprintf(<span class=\"string\">'p = %8.6f\\n'<\/span>,p)\r\nfprintf(<span class=\"string\">'1\/7 = %8.6f\\n'<\/span>,1\/7)\r\n<\/pre><pre class=\"codeoutput\">p = 0.143333\r\n1\/7 = 0.142857\r\n<\/pre><p>So, the 13th of a month is more likely to occur on Friday that any other day of the week. Only slightly more likely, I admit, but still ...<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_fd6ae558417948438028a104b0f9c011() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='fd6ae558417948438028a104b0f9c011 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' fd6ae558417948438028a104b0f9c011';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2012 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_fd6ae558417948438028a104b0f9c011()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.14<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; 7.14<br><\/p><\/div><!--\r\nfd6ae558417948438028a104b0f9c011 ##### SOURCE BEGIN #####\r\n%% Friday the 13th\r\n% We all know that Friday the 13th is unlucky, but is it unlikely?\r\n\r\n%% Year 2012\r\n% I plan to post this article during the second week of July, 2012.\r\n% The Friday in this week is a Friday the 13th,\r\n% the third we've had so far this year.\r\n% There were also ones in January and April.\r\n% That seems like a lot.\r\n% How often do we have three Friday the 13ths in the first seven\r\n% months of a year?\r\n% Well, it's not all that often.\r\n% It usually happens only once every 28 years.\r\n% The next time will be the year 2040.\r\n% But sometimes, around the turn of centuries, it happens twice in 12 years.\r\n% I mention all this to establish that our calendar\r\n% does not have a simple periodic behavior.\r\n% By the way, not to worry, after this week, it will be 14 months until\r\n% the next Friday the 13th, in September, 2013.\r\n\r\n%% Friday the 13th\r\n% Which brings us to the central topic of this post:\r\n%\r\n% * _What is the probability that the 13th of a month falls on a Friday?_\r\n%\r\n% An obvious response is\r\n%\r\n% * _Easy question, the probability is 1\/7._\r\n%\r\n% After all, there are seven days in a week and the 13th of a month\r\n% is equally likely to fall on any one of them.\r\n% Well, as we shall see, that's close, but not exactly right.\r\n\r\n%% Calendars and Leap Years\r\n% Leap years make our calendar a nontrivial mathematical object.\r\n% The leap year rule can be implemented by this anonymous function.\r\n\r\nleapyear = @(y) mod(y,4)==0 & mod(y,100)~=0 | mod(y,400)==0;\r\n\r\n%%\r\n% This says that leap years happen every four years,\r\n% except the turn of a century not divisible by 400 is skipped.\r\n% Try a few year numbers.\r\n\r\ny = [2012 2013 2000 2100]';\r\ndisp([y leapyear(y)])\r\n\r\n%% \r\n% So, this year is a leap year, next year is not, 2000 was a leap year,\r\n% 2100 is not.\r\n\r\n%% \r\n% The leap year rule implies that our calendar has a period of 400 years.\r\n% The calendar from 1601 to 2000 is being reused from 2001 to 2400.\r\n% (Except the Gregorian calendar had not been invented in 1601,\r\n% so I'm talking about the calendar that would have been used back then\r\n% if they could have used today's calendar, but never mind.)\r\n\r\n%%\r\n% In a 400 year period, there are 97 leap years, 4800 months,\r\n% 20871 weeks, and 146097 days.\r\n% So the average number of days in a calendar year is not 365.25, but\r\n\r\nformat short\r\ndpy = 365+97\/400 \r\n\r\n%%\r\n% We can compute the probability that the 13th of a month is a Friday\r\n% by counting how many times that happens in 4800 months.\r\n% The correct probability is then that count divided by 4800.\r\n% Since 4800 is not divisible by 7, the probability does not reduce to 1\/7.\r\n\r\n%% Clock\r\n% MATLAB has a number of functions for doing computations involving\r\n% calendars and dates.\r\n% Many of these functions are in the MATLAB Toolbox,\r\n% but some of the more specialized ones are in the Finance Toolbox.\r\n% We encountered a few of these functions in my blog about biorhythms.\r\n% The basis for all the functions is |clock|,\r\n% which reads the system's clock and returns a 6-element vector\r\n%\r\n%    [year, month, date, hour, minute, seconds]\r\n%\r\n% The first five elements have integer values.\r\n% The sixth element has a fractional part whose accuracy depends upon\r\n% the computer's internal clock.\r\n% Here is the output generated when I |publish| this blog.\r\n\r\nc = clock;\r\nfprintf('clock = [ %4d %4d %5d %5d %5d %8.3f ]\\n',c)\r\n\r\n%% Datenum\r\n% The |datenum| function facilitates computations involving calendars\r\n% by collapsing the |clock| vector into one value, the _serial date number_.\r\n% This value is the number of days, and fractions of a day, since a reference\r\n% time 20 centuries ago when |clock| would have been all zeros. \r\n% Here are a couple of examples of the use of |datenum|.\r\n% If you run this code yourself, your results should be different.\r\n\r\nt = now;\r\nfprintf('current_date_number = %10.3f\\n',t)\r\ndate_string = datestr(t)\r\ntday = fix(t)\r\ntday_string = datestr(tday)\r\n[week_day,week_day_name] = weekday(tday)\r\n\r\n%% Calendar number\r\n% The calendar for any year is determined by two pieces of information,\r\n% the weekday of January 1st and whether or not the year is a leap year.\r\n% So we need only 14 calendars.\r\n% We could number all possible calendars, with the units digit\r\n% specifying the starting week day and the tens digits indicating\r\n% leap years.  The 14 numbers would be |[1:7 11:17]|.\r\n\r\ncalendar_number = @(y) weekday(datenum(y,1,1)) + 10*leapyear(y);\r\n\r\n%%\r\n% If the calendar industry used this numbering scheme, here are the\r\n% calendars you would need for the next 21 years.\r\n\r\ny = (2012:2032)';\r\ndisp([y calendar_number(y)])\r\n\r\n%% Friday the 13th is likely\r\n% We are now ready to use the |weekday| function to count the number of\r\n% times in a 400-year calendar cycle that the 13th of a month occurs\r\n% on each of the various days of the week.\r\n\r\nc = zeros(1,7);\r\nfor y = 1601:2000\r\n   for m = 1:12\r\n      d = datenum([y,m,13]);\r\n      w = weekday(d);\r\n      c(w) = c(w) + 1;\r\n   end\r\nend\r\nc\r\n\r\n%%\r\n% A bar graph, with a line at a probability of 1\/7, and week day axis labels.\r\n\r\nbar(c)\r\naxis([0 8 680 690])\r\navg = 4800\/7;\r\nline([0 8], [avg avg],'linewidth',4,'color','black')\r\nset(gca,'xticklabel',{'Su','M','Tu','W','Th','F','Sa'})\r\n\r\n%%\r\n% The probability for Friday is\r\n\r\np = c(6)\/4800;\r\nfprintf('p = %8.6f\\n',p)\r\nfprintf('1\/7 = %8.6f\\n',1\/7)\r\n\r\n%%\r\n% So, the 13th of a month is more likely to occur on Friday that any\r\n% other day of the week.\r\n% Only slightly more likely, I admit, but still ...\r\n\r\n##### SOURCE END ##### fd6ae558417948438028a104b0f9c011\r\n-->","protected":false},"excerpt":{"rendered":"<!--introduction--><p>We all know that Friday the 13th is unlucky, but is it unlikely?... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/cleve\/2012\/07\/09\/friday-the-13th\/\">read more >><\/a><\/p>","protected":false},"author":78,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/182"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/users\/78"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/comments?post=182"}],"version-history":[{"count":12,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/182\/revisions"}],"predecessor-version":[{"id":195,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/182\/revisions\/195"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/media?parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/categories?post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/tags?post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}