http://wiki.math.uwaterloo.ca/statwiki/api.php?action=feedcontributions&user=Zhchong&feedformat=atom statwiki - User contributions [US] 2022-05-28T19:53:39Z User contributions MediaWiki 1.28.3 http://wiki.math.uwaterloo.ca/statwiki/index.php?title=stat340s13&diff=22691 stat340s13 2013-07-31T04:42:40Z <p>Zhchong: /* Gibbs Sampling */</p> <hr /> <div>&lt;div style = &quot;align:left; background:#00ffff; font-size: 150%&quot;&gt;<br /> If you<br /> use ideas, plots, text, code and other intellectual property developed by someone else<br /> in your wikicoursenote' contribution , you have to cite the<br /> original source. If you copy a sentence or a paragraph from work done by someone<br /> else, in addition to citing the original source you have to use quotation marks to<br /> identify the scope of the copied material. Evidence of copying or plagiarism will<br /> cause a failing mark in the course.<br /> <br /> Example of citing the original source<br /> <br /> Assumptions Underlying Principal Component Analysis can be found here&lt;ref&gt;http://support.sas.com/publishing/pubcat/chaps/55129.pdf&lt;/ref&gt;<br /> <br /> &lt;/div&gt;<br /> <br /> ==Important Notes==<br /> &lt;span style=&quot;color:#ff0000;font-size: 200%&quot;&gt; To make distinction between the material covered in class and additional material that you have add to the course, use the following convention. For anything that is not covered in the lecture write:&lt;/span&gt;<br /> <br /> &lt;div style = &quot;align:left; background:#F5F5DC; font-size: 120%&quot;&gt;<br /> In the news recently was a story that captures some of the ideas behind PCA. Over the past two years, Scott Golder and Michael Macy, researchers from Cornell University, collected 509 million Twitter messages from 2.4 million users in 84 different countries. The data they used were words collected at various times of day and they classified the data into two different categories: positive emotion words and negative emotion words. Then, they were able to study this new data to evaluate subjects' moods at different times of day, while the subjects were in different parts of the world. They found that the subjects generally exhibited positive emotions in the mornings and late evenings, and negative emotions mid-day. They were able to &quot;project their data onto a smaller dimensional space&quot; using PCS. Their paper, &quot;Diurnal and Seasonal Mood Vary with Work, Sleep, and Daylength Across Diverse Cultures,&quot; is available in the journal Science.&lt;ref&gt;http://www.pcworld.com/article/240831/twitter_analysis_reveals_global_human_moodiness.html&lt;/ref&gt;.<br /> <br /> Assumptions Underlying Principal Component Analysis can be found here&lt;ref&gt;http://support.sas.com/publishing/pubcat/chaps/55129.pdf&lt;/ref&gt;<br /> <br /> &lt;/div&gt;<br /> <br /> == Introduction, Class 1 - Tuesday, May 7 ==<br /> <br /> <br /> <br /> === Course Instructor: Ali Ghodsi ===<br /> &lt;!-- br tag for spacing--&gt;<br /> Lecture: &lt;br /&gt;<br /> 001: T/Th 8:30-9:50am MC1085 &lt;br /&gt;<br /> 002: T/Th 1:00-2:20pm DC1351 &lt;br /&gt;<br /> Tutorial: &lt;br /&gt;<br /> 2:30-3:20pm Mon M3 1006 &lt;br /&gt;<br /> Office Hours: &lt;br /&gt;<br /> Friday at 10am, M3 4208 &lt;br /&gt;<br /> <br /> === Midterm ===<br /> Monday June 17,2013 from 2:30pm-3:20pm<br /> <br /> === Final ===<br /> Saturday August 10,2013 from 7:30pm-10:00pm<br /> <br /> === TA(s): ===<br /> &lt;!-- br tag for spacing--&gt;<br /> {| class=&quot;wikitable&quot;<br /> |-<br /> ! TA<br /> ! Day<br /> ! Time<br /> ! Location<br /> |-<br /> | Lu Cheng<br /> | Monday<br /> | 3:30-5:30 pm<br /> | M3 3108, space 2<br /> |-<br /> | Han ShengSun<br /> | Tuesday<br /> | 4:00-6:00 pm<br /> | M3 3108, space 2<br /> |-<br /> | Yizhou Fang<br /> | Wednesday<br /> | 1:00-3:00 pm<br /> | M3 3108, space 1<br /> |-<br /> | Huan Cheng<br /> | Thursday<br /> | 3:00-5:00 pm<br /> | M3 3111, space 1<br /> |-<br /> | Wu Lin<br /> | Friday<br /> | 11:00-1:00 pm<br /> | M3 3108, space 1<br /> |}<br /> <br /> === Four Fundamental Problems ===<br /> &lt;!-- br tag for spacing--&gt;<br /> 1 Classification: Given input object X, we have a function which will take this input X and identify which 'class (Y)' it belongs to (Discrete Case) &lt;br /&gt;<br /> &lt;font size=&quot;3&quot;&gt;i.e taking value from x, we could predict y.&lt;/font&gt;<br /> (For example, if you have 40 images of oranges and 60 images of apples (represented by x), you can estimate a function that takes the images and states what type of fruit it is - note Y is discrete in this case.) &lt;br /&gt;<br /> 2 Regression: Same as classification but in the continuous case except y is non discrete. Results from regression are often used for prediction,forecasting and etc. (Example of stock prices, height, weight, etc.) &lt;br /&gt;<br /> (A simple practice might be investigating the hypothesis that higher levels of education cause higher levels of income.) &lt;br /&gt;<br /> 3 Clustering: Use common features of objects in same class or group to form clusters.(in this case, x is given, y is unknown; For example, clustering by provinces to measure average height of Canadian men.) &lt;br /&gt;<br /> 4 Dimensionality Reduction (also known as Feature extraction, Manifold learning): Used when we have a variable in high dimension space and we want to reduce the dimension &lt;br /&gt;<br /> <br /> === Applications ===<br /> <br /> Most useful when structure of the task is not well understood but can be characterized by a dataset with strong statistical regularity&lt;br /&gt;<br /> Examples: &lt;br /&gt;<br /> *Computer Vision, Computer Graphics, Finance (fraud detection), Machine Learning &lt;br /&gt;<br /> *Search and recommendation (eg. Google, Amazon) &lt;br /&gt;<br /> *Automatic speech recognition, speaker verification &lt;br /&gt;<br /> *Text parsing &lt;br /&gt;<br /> *Face identification &lt;br /&gt;<br /> *Tracking objects in video&lt;br /&gt;<br /> *Financial prediction(e.g. credit cards) &lt;br /&gt;<br /> *Fraud detection &lt;br /&gt;<br /> *Medical diagnosis &lt;br /&gt;<br /> <br /> === Course Information ===<br /> <br /> '''Prerequisite:''' (One of CS 116, 126/124, 134, 136, 138, 145, SYDE 221/322) and (STAT 230 with a grade of at least 60% or STAT 240) and (STAT 231 or 241)<br /> <br /> '''Antirequisite:''' CM 361/STAT 341, CS 437, 457 &lt;!--- Moved these down to declutter table of contents ---&gt;<br /> <br /> '''General Information'''<br /> *No required textbook<br /> *Recommended: &quot;Simulation&quot; by Sheldon M. Ross<br /> *Computing parts of the course will be done in Matlab, but prior knowledge of Matlab is not essential (will have a tutorial on it)<br /> *First midterm will be held on Monday, June 17 from 2:30 to 3:30<br /> *Announcements and assignments will be posted on Learn.<br /> *Other course material on: http://wikicoursenote.com/wiki/<br /> *Log on to both Learn and wikicoursenote frequently.<br /> *Email all questions and concerns to UWStat340@gmail.com. Do not use your personal email address! Do not email instructor or TAs about the class directly to their personal accounts!<br /> <br /> '''Wikicourse note (complete at least 12 contributions to get 10% of final mark):'''<br /> When applying for an account in the wikicourse note, please use the quest account as your login name while the uwaterloo email as the registered email. This is important as the quest id will be used to identify the students who make the contributions.<br /> Example:&lt;br/&gt;<br /> User: questid&lt;br/&gt;<br /> Email: questid@uwaterloo.ca&lt;br/&gt;<br /> After the student has made the account request, do wait for several hours before students can login into the account using the passwords stated in the email. During the first login, students will be ask to create a new password for their account. <br /> <br /> '''As a technical/editorial contributor''': Make contributions within 1 week and do not copy the notes on the blackboard.<br /> <br /> ''All contributions are now considered general contributions you must contribute to 50% of lectures for full marks''<br /> <br /> *A general contribution can be correctional (fixing mistakes) or technical (expanding content, adding examples, etc.) but at least half of your contributions should be technical for full marks.<br /> <br /> Do not submit copyrighted work without permission, cite original sources.<br /> Each time you make a contribution, check mark the table. Marks are calculated on an honour system, although there will be random verifications. If you are caught claiming to contribute but have not, you will not be credited.<br /> <br /> '''Wikicoursenote contribution form''' : https://docs.google.com/forms/d/1Sgq0uDztDvtcS5JoBMtWziwH96DrBz2JiURvHPNd-xs/viewform<br /> <br /> - you can submit your contributions multiple times.&lt;br /&gt;<br /> - you will be able to edit the response right after submitting&lt;br /&gt;<br /> - send email to make changes to an old response : uwstat340@gmail.com&lt;br /&gt;<br /> <br /> === Tentative Topics ===<br /> - Random variable and stochastic process generation&lt;br /&gt;<br /> - Discrete-Event Systems&lt;br /&gt;<br /> - Variance reduction&lt;br /&gt;<br /> - Markov Chain Monte Carlo<br /> <br /> ==Class 2 - Thursday, May 9==<br /> ===Generating Random Numbers===<br /> ==== Introduction ====<br /> Simulation is the imitation of a process or system over time. Computational power has introduced the possibility of using simulation study to analyze models used to describe a situation.<br /> <br /> In order to perform a simulation study, we should:<br /> &lt;br\&gt; 1 Use a computer to generate (pseudo*) random numbers (rand in MATLAB).&lt;br&gt;<br /> 2 Use these numbers to generate values of random variable from distributions: for example, set a variable in terms of uniform u ~ U(0,1).&lt;br&gt;<br /> 3 Using the concept of discrete events, we show how the random variables can be used to generate the behavior of a stochastic model over time. (Note: A stochastic model is the opposite of deterministic model, where there are several directions the process can evolve to)&lt;br&gt;<br /> 4 After continually generating the behavior of the system, we can obtain estimators and other quantities of interest.&lt;br&gt;<br /> <br /> The building block of a simulation study is the ability to generate a random number. This random number is a value from a random variable distributed uniformly on (0,1). There are many different methods of generating a random number: &lt;br&gt;<br /> &lt;br&gt;&lt;font size=&quot;3&quot;&gt;Physical Method: Roulette wheel, lottery balls, dice rolling, card shuffling etc. &lt;br&gt;<br /> &lt;br&gt;Numerically/Arithmetically: Use of a computer to successively generate pseudorandom numbers. The &lt;br /&gt;sequence of numbers can appear to be random; however they are deterministically calculated with an &lt;br /&gt;equation which defines pseudorandom. &lt;br&gt;&lt;/font&gt;<br /> <br /> (Source: Ross, Sheldon M., and Sheldon M. Ross. Simulation. San Diego: Academic, 1997. Print.)<br /> <br /> *We use the prefix pseudo because computer generates random numbers based on algorithms, which suggests that generated numbers are not truly random. Therefore pseudo-random numbers is used.<br /> <br /> In general, a deterministic model produces specific results given certain inputs by the model user, contrasting with a '''stochastic''' model which encapsulates randomness and probabilistic events.<br /> [[File:Det_vs_sto.jpg]]<br /> &lt;br&gt;A computer cannot generate truly random numbers because computers can only run algorithms, which are deterministic in nature. They can, however, generate Pseudo Random Numbers&lt;br&gt;<br /> <br /> '''Pseudo Random Numbers''' are the numbers that seem random but are actually determined by a relative set of original values. It is a chain of numbers pre-set by a formula or an algorithm, and the value jump from one to the next, making it look like a series of independent random events. The flaw of this method is that, eventually the chain returns to its initial position and pattern starts to repeat, but if we make the number set large enough we can prevent the numbers from repeating too early. Although the pseudo random numbers are deterministic, these numbers have a sequence of value and all of them have the appearances of being independent uniform random variables. Being deterministic, pseudo random numbers are valuable and beneficial due to the ease to generate and manipulate.<br /> <br /> When people repeat the test many times, the results will be the closed express values, which make the trials look deterministic. However, for each trial, the result is random. So, it looks like pseudo random numbers.<br /> <br /> ==== Mod ====<br /> Let &lt;math&gt;n \in \N&lt;/math&gt; and &lt;math&gt;m \in \N^+&lt;/math&gt;, then by Division Algorithm, <br /> &lt;math&gt;\exists q, \, r \in \N \;\text{with}\; 0\leq r &lt; m, \; \text{s.t.}\; n = mq+r&lt;/math&gt;, <br /> where &lt;math&gt;q&lt;/math&gt; is called the quotient and &lt;math&gt;r&lt;/math&gt; the remainder. Hence we can define a binary function<br /> &lt;math&gt;\mod : \N \times \N^+ \rightarrow \N &lt;/math&gt; given by &lt;math&gt;r:=n \mod m&lt;/math&gt; which returns the remainder after division by m. <br /> &lt;br /&gt;<br /> Generally, mod means taking the reminder after division by m.<br /> &lt;br /&gt;<br /> We say that n is congruent to r mod m if n = mq + r, where m is an integer. <br /> Values are between 0 and m-1 &lt;br /&gt;<br /> if y = ax + b, then &lt;math&gt;b:=y \mod a&lt;/math&gt;. &lt;br /&gt;<br /> <br /> '''Example 1:'''&lt;br /&gt;<br /> <br /> &lt;math&gt;30 = 4 \cdot 7 + 2&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;2 := 30\mod 7&lt;/math&gt;&lt;br /&gt;<br /> &lt;br /&gt;<br /> &lt;math&gt;25 = 8 \cdot 3 + 1&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;1: = 25\mod 3&lt;/math&gt;&lt;br /&gt;<br /> &lt;br /&gt;<br /> &lt;math&gt;-3=5\cdot (-1)+2&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;2:=-3\mod 5&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;br /&gt;<br /> '''Example 2:'''&lt;br /&gt;<br /> <br /> If &lt;math&gt;23 = 3 \cdot 6 + 5&lt;/math&gt; &lt;br /&gt;<br /> <br /> Then equivalently, &lt;math&gt;5 := 23\mod 6&lt;/math&gt;&lt;br /&gt;<br /> &lt;br /&gt;<br /> If &lt;math&gt;31 = 31 \cdot 1&lt;/math&gt; &lt;br /&gt;<br /> <br /> Then equivalently, &lt;math&gt;0 := 31\mod 31&lt;/math&gt;&lt;br /&gt;<br /> &lt;br /&gt;<br /> If &lt;math&gt;-37 = 40\cdot (-1)+ 3&lt;/math&gt; &lt;br /&gt;<br /> <br /> Then equivalently, &lt;math&gt;3 := -37\mod 40&lt;/math&gt;&lt;br /&gt;<br /> <br /> '''Example 3:'''&lt;br /&gt;<br /> &lt;math&gt;77 = 3 \cdot 25 + 2&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;2 := 77\mod 3&lt;/math&gt;&lt;br /&gt;<br /> &lt;br /&gt;<br /> &lt;math&gt;25 = 25 \cdot 1 + 0&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;0: = 25\mod 25&lt;/math&gt;&lt;br /&gt;<br /> &lt;br /&gt;<br /> <br /> <br /> <br /> <br /> '''Note:''' &lt;math&gt;\mod&lt;/math&gt; here is different from the modulo congruence relation in &lt;math&gt;\Z_m&lt;/math&gt;, which is an equivalence relation instead of a function.<br /> <br /> The modulo operation is useful for determining if an integer divided by another integer produces a non-zero remainder. But both integers should satisfy &lt;math&gt;n = mq + r&lt;/math&gt;, where &lt;math&gt;m&lt;/math&gt;, &lt;math&gt;r&lt;/math&gt;, &lt;math&gt;q&lt;/math&gt;, and &lt;math&gt;n&lt;/math&gt; are all integers, and &lt;math&gt;r&lt;/math&gt; is smaller than &lt;math&gt;m&lt;/math&gt;. The above rules also satisfy when any of &lt;math&gt;m&lt;/math&gt;, &lt;math&gt;r&lt;/math&gt;, &lt;math&gt;q&lt;/math&gt;, and &lt;math&gt;n&lt;/math&gt; is negative integer, see the third example.<br /> <br /> ==== Mixed Congruential Algorithm ====<br /> We define the Linear Congruential Method to be &lt;math&gt;x_{k+1}=(ax_k + b) \mod m&lt;/math&gt;, where &lt;math&gt;x_k, a, b, m \in \N, \;\text{with}\; a, m \neq 0&lt;/math&gt;. Given a '''seed''' (i.e. an initial value &lt;math&gt;x_0 \in \N&lt;/math&gt;), we can obtain values for &lt;math&gt;x_1, \, x_2, \, \cdots, x_n&lt;/math&gt; inductively. The Multiplicative Congruential Method, invented by Berkeley professor D. H. Lehmer, may also refer to the special case where &lt;math&gt;b=0&lt;/math&gt; and the Mixed Congruential Method is case where &lt;math&gt;b \neq 0&lt;/math&gt; &lt;br /&gt;. Their title as &quot;mixed&quot; arises from the fact that it has both a multiplicative and additive term. <br /> <br /> An interesting fact about '''Linear Congruential Method''' is that it is one of the oldest and best-known pseudo random number generator algorithms. It is very fast and requires minimal memory to retain state. However, this method should not be used for applications that require high randomness. They should not be used for Monte Carlo simulation and cryptographic applications. (Monte Carlo simulation will consider possibilities for every choice of consideration, and it shows the extreme possibilities. This method is not precise enough.)&lt;br /&gt;<br /> <br /> [[File:Linear_Congruential_Statment.png‎|600px]] &quot;Source: STAT 340 Spring 2010 Course Notes&quot;<br /> <br /> '''First consider the following algorithm'''&lt;br /&gt;<br /> &lt;math&gt;x_{k+1}=x_{k} \mod m&lt;/math&gt; &lt;br /&gt;<br /> <br /> such that: if &lt;math&gt;x_{0}=5(mod 150)&lt;/math&gt;, &lt;math&gt;x_{n}=3x_{n-1}&lt;/math&gt;, find &lt;math&gt;x_{1},x_{8},x_{9}&lt;/math&gt;. &lt;br /&gt;<br /> &lt;math&gt;x_{n}=(3^n)*5(mod 150)&lt;/math&gt; &lt;br /&gt;<br /> &lt;math&gt;x_{1}=45,x_{8}=105,x_{9}=15&lt;/math&gt; &lt;br /&gt; <br /> <br /> <br /> <br /> '''Example'''&lt;br /&gt;<br /> &lt;math&gt;\text{Let }x_{0}=10,\,m=3&lt;/math&gt;&lt;br //&gt;<br /> <br /> :&lt;math&gt;\begin{align}<br /> <br /> x_{1} &amp;{}= 10 &amp;{}\mod{3} = 1 \\<br /> <br /> x_{2} &amp;{}= 1 &amp;{}\mod{3} = 1 \\<br /> <br /> x_{3} &amp;{}= 1 &amp;{}\mod{3} =1 \\<br /> \end{align}&lt;/math&gt;<br /> &lt;math&gt;\ldots&lt;/math&gt;&lt;br /&gt;<br /> <br /> Excluding &lt;math&gt;x_{0}&lt;/math&gt;, this example generates a series of ones. In general, excluding &lt;math&gt;x_{0}&lt;/math&gt;, the algorithm above will always generate a series of the same number less than M. Hence, it has a period of 1. The '''period''' can be described as the length of a sequence before it repeats. We want a large period with a sequence that is random looking. We can modify this algorithm to form the Multiplicative Congruential Algorithm. &lt;br /&gt;<br /> <br /> <br /> <br /> &lt;math&gt;x_{k+1}=(a \cdot x_{k} + b) \mod m &lt;/math&gt;(a little tip: &lt;math&gt;(a \cdot b)\mod c = (a\mod c)\cdot(b\mod c))&lt;/math&gt;&lt;br/&gt;<br /> <br /> '''Example'''&lt;br /&gt;<br /> &lt;math&gt;\text{Let }a=2,\, b=1, \, m=3, \, x_{0} = 10&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;\begin{align}<br /> \text{Step 1: } 0&amp;{}=(2\cdot 10 + 1) &amp;{}\mod 3 \\<br /> \text{Step 2: } 1&amp;{}=(2\cdot 0 + 1) &amp;{}\mod 3 \\<br /> \text{Step 3: } 0&amp;{}=(2\cdot 1 + 1) &amp;{}\mod 3 \\<br /> \end{align}&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;\ldots&lt;/math&gt;&lt;br /&gt;<br /> <br /> This example generates a sequence with a repeating cycle of two integers.&lt;br /&gt;<br /> <br /> (If we choose the numbers properly, we could get a sequence of &quot;random&quot; numbers. How do we find the value of &lt;math&gt;a,b,&lt;/math&gt; and &lt;math&gt;m&lt;/math&gt;? At the very least &lt;math&gt;m&lt;/math&gt; should be a very '''large''', preferably prime number. The larger &lt;math&gt;m&lt;/math&gt; is, the higher the possibility to get a sequence of &quot;random&quot; numbers. This is easier to solve in Matlab. In Matlab, the command rand() generates random numbers which are uniformly distributed on the interval (0,1)). Matlab uses &lt;math&gt;a=7^5, b=0, m=2^{31}-1&lt;/math&gt; – recommended in a 1988 paper, &quot;Random Number Generators: Good Ones Are Hard To Find&quot; by Stephen K. Park and Keith W. Miller (Important part is that &lt;math&gt;m&lt;/math&gt; should be '''large and prime''')&lt;br /&gt; <br /> <br /> Note: &lt;math&gt;\frac {x_{n+1}}{m-1}&lt;/math&gt; is an approximation to the value of a U(0,1) random variable.&lt;br /&gt; <br /> <br /> <br /> <br /> '''MatLab Instruction for Multiplicative Congruential Algorithm:'''&lt;br /&gt;<br /> Before you start, you need to clear all existing defined variables and operations:&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;clear all<br /> &gt;&gt;close all<br /> &lt;/pre&gt;<br /> <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;a=17<br /> &gt;&gt;b=3<br /> &gt;&gt;m=31<br /> &gt;&gt;x=5<br /> &gt;&gt;mod(a*x+b,m)<br /> ans=26<br /> &gt;&gt;x=mod(a*x+b,m)<br /> &lt;/pre&gt;<br /> <br /> ''(Note: &lt;br /&gt;<br /> 1. Keep repeating this command over and over again and you will get random numbers – this is how the command rand works in a computer. &lt;br /&gt;<br /> 2. There is a function in MATLAB called '''RAND''' to generate a random number between 0 and 1. &lt;br /&gt;<br /> For example, in MATLAB, we can use '''rand(1,1000)''' to generate 1000's numbers between 0 and 1. This is essentially a vector with 1 row, 1000 columns, with each entry a random number between 0 and 1.&lt;br /&gt;<br /> 3. If we would like to generate 1000 or more numbers, we could use a '''for''' loop&lt;br /&gt;&lt;br /&gt;<br /> <br /> ''(Note on MATLAB commands: &lt;br /&gt;<br /> 1. clear all: clears all variables.&lt;br /&gt;<br /> 2. close all: closes all figures.&lt;br /&gt;<br /> 3. who: displays all defined variables.&lt;br /&gt;<br /> 4. clc: clears screen.&lt;br /&gt;<br /> 5. ; : prevents the results from printing.&lt;br /&gt;<br /> 6. disstool: displays a graphing tool.&lt;br /&gt;&lt;br /&gt;<br /> <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;a=13<br /> &gt;&gt;b=0<br /> &gt;&gt;m=31<br /> &gt;&gt;x(1)=1<br /> &gt;&gt;for ii=2:1000<br /> x(ii)=mod(a*x(ii-1)+b,m);<br /> end<br /> &gt;&gt;size(x)<br /> ans=1 1000<br /> &gt;&gt;hist(x)<br /> &lt;/pre&gt;<br /> [[File:MCA_Example.jpg|300px]]<br /> <br /> ''(Note: The semicolon after the x(ii)=mod(a*x(ii-1)+b,m) ensures that Matlab will not print the entire vector of x. It will instead calculate it internally and you will be able to work with it. Adding the semicolon to the end of this line reduces the run time significantly.) ''<br /> <br /> <br /> This algorithm involves three integer parameters &lt;math&gt;a, b,&lt;/math&gt; and &lt;math&gt;m&lt;/math&gt; and an initial value, &lt;math&gt;x_0&lt;/math&gt; called the '''seed'''. A sequence of numbers is defined by &lt;math&gt;x_{k+1} = ax_k+ b \mod m&lt;/math&gt;. &lt;br /&gt;<br /> <br /> Note: For some bad &lt;math&gt;a&lt;/math&gt; and &lt;math&gt;b&lt;/math&gt;, the histogram may not look uniformly distributed.&lt;br /&gt;<br /> <br /> Note: In MATLAB, hist(x) will generate a graph representing the distribution. Use this function after you run the code to check the real sample distribution.<br /> <br /> '''Example''': &lt;math&gt;a=13, b=0, m=31&lt;/math&gt;&lt;br /&gt;<br /> The first 30 numbers in the sequence are a permutation of integers from 1 to 30, and then the sequence repeats itself so '''it is important to choose &lt;math&gt;m&lt;/math&gt; large''' to decrease the probability of each number repeating itself too early. Values are between &lt;math&gt;0&lt;/math&gt; and &lt;math&gt;m-1&lt;/math&gt;. If the values are normalized by dividing by &lt;math&gt;m-1&lt;/math&gt;, then the results are '''approximately''' numbers uniformly distributed in the interval [0,1]. There is only a finite number of values (30 possible values in this case). In MATLAB, you can use function &quot;hist(x)&quot; to see if it looks uniformly distributed. We saw that the values between 0-30 had the same frequency in the histogram, so we can conclude that they are uniformly distributed. &lt;br /&gt;<br /> <br /> If &lt;math&gt;x_0=1&lt;/math&gt;, then &lt;br /&gt;<br /> :&lt;math&gt;x_{k+1} = 13x_{k}\mod{31}&lt;/math&gt;<br /> <br /> So,<br /> <br /> :&lt;math&gt;\begin{align} x_{0} &amp;{}= 1 \\<br /> <br /> x_{1} &amp;{}= 13 \times 1 + 0 &amp;{}\mod{31} = 13 \\<br /> <br /> x_{2} &amp;{}= 13 \times 13 + 0 &amp;{}\mod{31} = 14 \\<br /> <br /> x_{3} &amp;{}= 13 \times 14 + 0 &amp;{}\mod{31} =27 \\<br /> \end{align}&lt;/math&gt;<br /> <br /> etc.<br /> <br /> For example, with &lt;math&gt;a = 3, b = 2, m = 4, x_0 = 1&lt;/math&gt;, we have:<br /> <br /> :&lt;math&gt;x_{k+1} = (3x_{k} + 2)\mod{4}&lt;/math&gt;<br /> <br /> So,<br /> <br /> :&lt;math&gt;\begin{align}<br /> x_{0} &amp;{}= 1 \\<br /> x_{1} &amp;{}= 3 \times 1 + 2 \mod{4} = 1 \\<br /> x_{2} &amp;{}= 3 \times 1 + 2 \mod{4} = 1 \\<br /> \end{align}&lt;/math&gt;&lt;br /&gt;<br /> Another Example, a =3, b =2, m = 5, x_0=1<br /> etc.<br /> &lt;hr/&gt;<br /> &lt;p style=&quot;color:red;font-size:16px;&quot;&gt;FAQ:&lt;/P&gt;<br /> 1.Why is it 1 to 30 instead of 0 to 30 in the example above?&lt;br&gt;<br /> ''&lt;math&gt;b = 0&lt;/math&gt; so in order to have &lt;math&gt;x_k&lt;/math&gt; equal to 0, &lt;math&gt;x_{k-1}&lt;/math&gt; must be 0 (since &lt;math&gt;a=13&lt;/math&gt; is relatively prime to 31). However, the seed is 1. Hence, we will never observe 0 in the sequence.''&lt;br&gt;<br /> Alternatively, {0} and {1,2,...,30} are two orbits of the left multiplication by 13 in the group &lt;math&gt;\Z_{31}&lt;/math&gt;.&lt;br&gt;<br /> 2.Will the number 31 ever appear?Is there a probability that a number never appears? &lt;br&gt;<br /> ''The number 31 will never appear. When you perform the operation &lt;math&gt;\mod m&lt;/math&gt;, the largest possible answer that you could receive is &lt;math&gt;m-1&lt;/math&gt;. Whether or not a particular number in the range from 0 to &lt;math&gt;m - 1&lt;/math&gt; appears in the above algorithm will be dependent on the values chosen for &lt;math&gt;a, b&lt;/math&gt; and &lt;math&gt;m&lt;/math&gt;. ''<br /> &lt;hr/&gt;<br /> <br /> '''Examples:[From Textbook]'''&lt;br /&gt;<br /> &lt;math&gt;\text{If }x_0=3 \text{ and } x_n=(5x_{n-1}+7)\mod 200&lt;/math&gt;, &lt;math&gt;\text{find }x_1,\cdots,x_{10}&lt;/math&gt;.&lt;br /&gt;<br /> '''Solution:'''&lt;br /&gt;<br /> &lt;math&gt;\begin{align}<br /> x_1 &amp;{}= (5 \times 3+7) &amp;{}\mod{200} &amp;{}= 22 \\<br /> x_2 &amp;{}= 117 &amp;{}\mod{200} &amp;{}= 117 \\<br /> x_3 &amp;{}= 592 &amp;{}\mod{200} &amp;{}= 192 \\<br /> x_4 &amp;{}= 2967 &amp;{}\mod{200} &amp;{}= 167 \\<br /> x_5 &amp;{}= 14842 &amp;{}\mod{200} &amp;{}= 42 \\<br /> x_6 &amp;{}= 74217 &amp;{}\mod{200} &amp;{}= 17 \\<br /> x_7 &amp;{}= 371092 &amp;{}\mod{200} &amp;{}= 92 \\<br /> x_8 &amp;{}= 1855467 &amp;{}\mod{200} &amp;{}= 67 \\<br /> x_9 &amp;{}= 9277342 &amp;{}\mod{200} &amp;{}= 142 \\<br /> x_{10} &amp;{}= 46386717 &amp;{}\mod{200} &amp;{}= 117 \\<br /> \end{align}&lt;/math&gt;&lt;br /&gt;<br /> <br /> '''Comments:'''&lt;br /&gt;<br /> <br /> Matlab code:<br /> a=5;<br /> b=7;<br /> m=200;<br /> x(1)=3;<br /> for ii=2:1000<br /> x(ii)=mod(a*x(ii-1)+b,m);<br /> end<br /> size(x);<br /> hist(x)<br /> <br /> <br /> <br /> Typically, it is good to choose &lt;math&gt;m&lt;/math&gt; such that &lt;math&gt;m&lt;/math&gt; is large, and &lt;math&gt;m&lt;/math&gt; is prime. Careful selection of parameters '&lt;math&gt;a&lt;/math&gt;' and '&lt;math&gt;b&lt;/math&gt;' also helps generate relatively &quot;random&quot; output values, where it is harder to identify patterns. For example, when we used a composite (non prime) number such as 40 for &lt;math&gt;m&lt;/math&gt;, our results were not satisfactory in producing an output resembling a uniform distribution.&lt;br /&gt;<br /> <br /> The computed values are between 0 and &lt;math&gt;m-1&lt;/math&gt;. If the values are normalized by dividing by '''&lt;math&gt;m-1&lt;/math&gt;''', their result is numbers uniformly distributed on the interval &lt;math&gt;\left[0,1\right]&lt;/math&gt; (similar to computing from uniform distribution).&lt;br /&gt;<br /> <br /> From the example shown above, if we want to create a large group of random numbers, it is better to have large, prime &lt;math&gt;m&lt;/math&gt; so that the generated random values will not repeat after several iterations. Note: the period for this example is 8: from '&lt;math&gt;x_2&lt;/math&gt;' to '&lt;math&gt;x_9&lt;/math&gt;'.&lt;br /&gt;<br /> <br /> There has been a research on how to choose uniform sequence. Many programs give you the options to choose the seed. Sometimes the seed is chosen by CPU.&lt;br /&gt;<br /> <br /> &lt;span style=&quot;background:#F5F5DC&quot;&gt;Theorem (extra knowledge)&lt;/span&gt;&lt;br /&gt;<br /> Let c be a non-zero constant. Then for any seed x0, and LCG will have largest max. period if and only if&lt;br /&gt;<br /> (i) m and c are coprime;&lt;br /&gt;<br /> (ii) (a-1) is divisible by all prime factor of m;&lt;br /&gt;<br /> (iii) if and only if m is divisible by 4, then a-1 is also divisible by 4.&lt;br /&gt;<br /> <br /> We want our LCG to have a large cycle.<br /> We call a cycle with m element the maximal period.<br /> We can make it bigger by making m big and prime.<br /> Recall:any number you can think of can be broken into a factor of prime<br /> Define coprime:Two numbers X and Y, are coprime if they do not share any prime factors.<br /> <br /> Example:&lt;br /&gt;<br /> &lt;font size=&quot;3&quot;&gt;Xn=(15Xn-1 + 4) mod 7&lt;/font&gt;&lt;br /&gt;<br /> (i) m=7 c=4 -&gt; coprime;&lt;br /&gt;<br /> (ii) a-1=14 and a-1 is divisible by 7;&lt;br /&gt;<br /> (iii) dose not apply.&lt;br /&gt;<br /> (The extra knowledge stops here)<br /> <br /> <br /> <br /> In this part, I learned how to use R code to figure out the relationship between two integers<br /> division, and their remainder. And when we use R to calculate R with random variables for a range such as(1:1000),the graph of distribution is like uniform distribution.<br /> &lt;div style=&quot;border:1px solid #cccccc;border-radius:10px;box-shadow: 0 5px 15px 1px rgba(0, 0, 0, 0.6), 0 0 200px 1px rgba(255, 255, 255, 0.5);padding:20px;margin:20px;background:#FFFFAD;&quot;&gt;<br /> &lt;h4 style=&quot;text-align:center;&quot;&gt;Summary of Multiplicative Congruential Algorithm&lt;/h4&gt;<br /> &lt;p&gt;&lt;b&gt;Problem:&lt;/b&gt; generate Pseudo Random Numbers.&lt;/p&gt;<br /> &lt;b&gt;Plan:&lt;/b&gt; <br /> &lt;ol&gt;<br /> &lt;li&gt;find integer: &lt;i&gt;a b m&lt;/i&gt;(large prime) &lt;i&gt;x&lt;sub&gt;0&lt;/sub&gt;&lt;/i&gt;(the seed) .&lt;/li&gt;<br /> &lt;li&gt;&lt;math&gt;x_{k+1}=(ax_{k}+b)&lt;/math&gt;mod m&lt;/li&gt;<br /> &lt;/ol&gt;<br /> &lt;b&gt;Matlab Instruction:&lt;/b&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;&amp;gt;&amp;gt;clear all<br /> &amp;gt;&amp;gt;close all<br /> &amp;gt;&amp;gt;a=17<br /> &amp;gt;&amp;gt;b=3<br /> &amp;gt;&amp;gt;m=31<br /> &amp;gt;&amp;gt;x=5<br /> &amp;gt;&amp;gt;mod(a*x+b,m)<br /> ans=26<br /> &amp;gt;&amp;gt;x=mod(a*x+b,m)<br /> &lt;/pre&gt;<br /> &lt;/div&gt;<br /> Another algorithm for generating pseudo random numbers is the multiply with carry method. Its simplest form is similar to the linear congruential generator. They differs in that the parameter b changes in the MWC algorithm. It is as follows: &lt;br&gt;<br /> <br /> 1.) x&lt;sub&gt;k+1&lt;/sub&gt; = ax&lt;sub&gt;k&lt;/sub&gt; + b&lt;sub&gt;k&lt;/sub&gt; mod m &lt;br&gt;<br /> 2.) b&lt;sub&gt;k+1&lt;/sub&gt; = floor((ax&lt;sub&gt;k&lt;/sub&gt; + b&lt;sub&gt;k&lt;/sub&gt;)/m) &lt;br&gt;<br /> 3.) set k to k + 1 and go to step 1<br /> [http://www.javamex.com/tutorials/random_numbers/multiply_with_carry.shtml Source]<br /> <br /> === Inverse Transform Method ===<br /> Now that we know how to generate random numbers, we use these values to sample form distributions such as exponential. However, to easily use this method, the probability distribution consumed must have a cumulative distribution function (cdf) &lt;math&gt;F&lt;/math&gt; with a tractable (that is, easily found) inverse &lt;math&gt;F^{-1}&lt;/math&gt;.&lt;br /&gt;<br /> <br /> '''Theorem''': &lt;br /&gt;<br /> If we want to generate the value of a discrete random variable X, we must generate a random number U, uniformly distributed over (0,1).<br /> Let &lt;math&gt;F:\R \rightarrow \left[0,1\right]&lt;/math&gt; be a cdf. If &lt;math&gt;U \sim U\left[0,1\right]&lt;/math&gt;, then the random variable given by &lt;math&gt;X:=F^{-1}\left(U\right)&lt;/math&gt;<br /> follows the distribution function &lt;math&gt;F\left(\cdot\right)&lt;/math&gt;,<br /> where &lt;math&gt;F^{-1}\left(u\right):=\inf F^{-1}\big(\left[u,+\infty\right)\big) = \inf\{x\in\R | F\left(x\right) \geq u\}&lt;/math&gt; is the generalized inverse.&lt;br /&gt;<br /> '''Note''': &lt;math&gt;F&lt;/math&gt; need not be invertible everywhere on the real line, but if it is, then the generalized inverse is the same as the inverse in the usual case. We only need it to be invertible on the range of F(x), [0,1]. <br /> <br /> '''Proof of the theorem:'''&lt;br /&gt;<br /> The generalized inverse satisfies the following: &lt;br /&gt;<br /> <br /> :&lt;math&gt;P(X\leq x)&lt;/math&gt; &lt;br /&gt;<br /> &lt;math&gt;= P(F^{-1}(U)\leq x)&lt;/math&gt; (since &lt;math&gt;X= F^{-1}(U)&lt;/math&gt; by the inverse method)&lt;br /&gt;<br /> &lt;math&gt;= P((F(F^{-1}(U))\leq F(x))&lt;/math&gt; (since &lt;math&gt;F &lt;/math&gt; is monotonically increasing) &lt;br /&gt;<br /> &lt;math&gt;= P(U\leq F(x)) &lt;/math&gt; (since &lt;math&gt; P(U\leq a)= a&lt;/math&gt; for &lt;math&gt;U \sim U(0,1), a \in [0,1]&lt;/math&gt;,&lt;br /&gt;<br /> &lt;math&gt;= F(x) , \text{ where } 0 \leq F(x) \leq 1 &lt;/math&gt; &lt;br /&gt;<br /> <br /> This is the c.d.f. of X. &lt;br /&gt;<br /> &lt;br /&gt;<br /> <br /> That is &lt;math&gt;F^{-1}\left(u\right) \leq x \Leftrightarrow u \leq F\left(x\right)&lt;/math&gt;&lt;br /&gt;<br /> <br /> Finally, &lt;math&gt;P(X \leq x) = P(F^{-1}(U) \leq x) = P(U \leq F(x)) = F(x)&lt;/math&gt;, since &lt;math&gt;U&lt;/math&gt; is uniform on the unit interval.&lt;br /&gt;<br /> <br /> This completes the proof.&lt;br /&gt;<br /> <br /> Therefore, in order to generate a random variable X~F, it can generate U according to U(0,1) and then make the transformation x=&lt;math&gt; F^{-1}(U) &lt;/math&gt; &lt;br /&gt;<br /> <br /> Note that we can apply the inverse on both sides in the proof of the inverse transform only if the pdf of X is monotonic. A monotonic function is one that is either increasing for all x, or decreasing for all x. Of course, this holds true for all CDFs, since they are monotonic by definition. &lt;br /&gt;<br /> <br /> In short, what the theorem tells us is that we can use a random number &lt;math&gt; U from U(0,1) &lt;/math&gt; to randomly sample a point on the CDF of X, then apply the inverse of the CDF to map the given probability to its domain, which gives us the random variable X.&lt;br/&gt;<br /> <br /> <br /> '''Example 1 - Exponential''': &lt;math&gt; f(x) = \lambda e^{-\lambda x}&lt;/math&gt;&lt;br/&gt;<br /> Calculate the CDF:&lt;br /&gt;<br /> &lt;math&gt; F(x)= \int_0^x f(t) dt = \int_0^x \lambda e ^{-\lambda t}\ dt&lt;/math&gt;<br /> &lt;math&gt; = \frac{\lambda}{-\lambda}\, e^{-\lambda t}\, | \underset{0}{x} &lt;/math&gt;<br /> &lt;math&gt; = -e^{-\lambda x} + e^0 =1 - e^{- \lambda x} &lt;/math&gt;&lt;br /&gt;<br /> Solve the inverse:&lt;br /&gt;<br /> &lt;math&gt; y=1-e^{- \lambda x} \Rightarrow 1-y=e^{- \lambda x} \Rightarrow x=-\frac {ln(1-y)}{\lambda}&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt; y=-\frac {ln(1-x)}{\lambda} \Rightarrow F^{-1}(x)=-\frac {ln(1-x)}{\lambda}&lt;/math&gt;&lt;br /&gt;<br /> Note that 1 − U is also uniform on (0, 1) and thus −log(1 − U) has the same distribution as −logU. &lt;br /&gt;<br /> Steps: &lt;br /&gt;<br /> Step 1: Draw U ~U[0,1];&lt;br /&gt;<br /> Step 2: &lt;math&gt; x=\frac{-ln(U)}{\lambda} &lt;/math&gt; &lt;br /&gt;&lt;br /&gt;<br /> <br /> <br /> EXAMPLE 2 Normal distribution <br /> G(y)=P[Y&lt;=y)<br /> =P[-sqr (y) &lt; z &lt; sqr (y))<br /> =integrate from -sqr(z) to Sqr(z) 1/sqr(2pi) e ^(-z^2/2) dz<br /> = 2 integrate from 0 to sqr(y) 1/sqr(2pi) e ^(-z^2/2) dz<br /> its the cdf of Y=z^2 <br /> <br /> pdf g(y)= G'(y)<br /> pdf pf x^2 (1) <br /> <br /> '''MatLab Code''':&lt;br /&gt;<br /> <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;u=rand(1,1000);<br /> &gt;&gt;hist(u) # this will generate a fairly uniform diagram<br /> &lt;/pre&gt;<br /> [[File:ITM_example_hist(u).jpg|300px]]<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> #let λ=2 in this example; however, you can make another value for λ<br /> &gt;&gt;x=(-log(1-u))/2;<br /> &gt;&gt;size(x) #1000 in size <br /> &gt;&gt;figure<br /> &gt;&gt;hist(x) #exponential <br /> &lt;/pre&gt;<br /> [[File:ITM_example_hist(x).jpg|300px]]<br /> <br /> '''Example 2 - Continuous Distribution''':&lt;br /&gt;<br /> <br /> &lt;math&gt; f(x) = \dfrac {\lambda } {2}e^{-\lambda \left| x-\theta \right| } for -\infty &lt; X &lt; \infty , \lambda &gt;0 &lt;/math&gt;&lt;br/&gt;<br /> <br /> Calculate the CDF:&lt;br /&gt;<br /> <br /> &lt;math&gt; F(x)= \frac{1}{2} e^{-\lambda (\theta - x)} , for \ x \le \theta &lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt; F(x) = 1 - \frac{1}{2} e^{-\lambda (x - \theta)}, for \ x &gt; \theta &lt;/math&gt;&lt;br/&gt;<br /> <br /> Solve for the inverse:&lt;br /&gt;<br /> <br /> &lt;math&gt;F^{-1}(x)= \theta + ln(2y)/\lambda, for \ 0 \le y \le 0.5&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;F^{-1}(x)= \theta - ln(2(1-y))/\lambda, for \ 0.5 &lt; y \le 1&lt;/math&gt;&lt;br/&gt;<br /> <br /> Algorithm:&lt;br /&gt;<br /> Steps: &lt;br /&gt;<br /> Step 1: Draw U ~ U[0, 1];&lt;br /&gt;<br /> Step 2: Compute &lt;math&gt;X = F^-1(U)&lt;/math&gt; i.e. &lt;math&gt;X = \theta + \frac {1}{\lambda} ln(2U)&lt;/math&gt; for U &lt; 0.5 else &lt;math&gt;X = \theta -\frac {1}{\lambda} ln(2(1-U))&lt;/math&gt;<br /> <br /> <br /> '''Example 3 - &lt;math&gt;F(x) = x^5&lt;/math&gt;''':&lt;br/&gt;<br /> Given a CDF of X: &lt;math&gt;F(x) = x^5&lt;/math&gt;, transform U~U[0,1]. &lt;br /&gt;<br /> Sol: <br /> Let &lt;math&gt;y=x^5&lt;/math&gt;, solve for x: &lt;math&gt;x=y^\frac {1}{5}&lt;/math&gt;. Therefore, &lt;math&gt;F^{-1} (x) = x^\frac {1}{5}&lt;/math&gt;&lt;br /&gt;<br /> Hence, to obtain a value of x from F(x), we first set 'u' as an uniform distribution, then obtain the inverse function of F(x), and set<br /> &lt;math&gt;x= u^\frac{1}{5}&lt;/math&gt;&lt;br /&gt;&lt;br /&gt;<br /> <br /> Algorithm:&lt;br /&gt;<br /> Steps: &lt;br /&gt;<br /> Step 1: Draw U ~ rand[0, 1];&lt;br /&gt;<br /> Step 2: X=U^(1/5);&lt;br /&gt;<br /> <br /> '''Example 4 - BETA(1,β)''':&lt;br/&gt;<br /> Given u~U[0,1], generate x from BETA(1,β)&lt;br /&gt;<br /> Solution:<br /> &lt;math&gt;F(x)= 1-(1-x)^\beta&lt;/math&gt;, <br /> &lt;math&gt;u= 1-(1-x)^\beta&lt;/math&gt;&lt;br /&gt;<br /> Solve for x: <br /> &lt;math&gt;(1-x)^\beta = 1-u&lt;/math&gt;, <br /> &lt;math&gt;1-x = (1-u)^\frac {1}{\beta}&lt;/math&gt;, <br /> &lt;math&gt;x = 1-(1-u)^\frac {1}{\beta}&lt;/math&gt;&lt;br /&gt;<br /> let β=3, use Matlab to construct N=1000 observations from Beta(1,3)&lt;br /&gt;<br /> '''MatLab Code''':&lt;br /&gt;<br /> <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt; u = rand(1,1000);<br /> x = 1-(1-u)^(1/3);<br /> &gt;&gt; hist(x,50)<br /> &gt;&gt; mean(x)<br /> &lt;/pre&gt;<br /> <br /> '''Example 5 - Estimating &lt;math&gt;\pi&lt;/math&gt;''':&lt;br/&gt;<br /> Let's use rand() and Monte Carlo Method to estimate &lt;math&gt;\pi&lt;/math&gt; &lt;br /&gt;<br /> N= total number of points &lt;br /&gt;<br /> N&lt;sub&gt;c&lt;/sub&gt; = total number of points inside the circle&lt;br /&gt;<br /> Prob[(x,y) lies in the circle=&lt;math&gt;\frac {Area(circle)}{Area(square)}&lt;/math&gt;&lt;br /&gt;<br /> If we take square of size 2, circle will have area =&lt;math&gt;\pi (\frac {2}{2})^2 =\pi&lt;/math&gt;.&lt;br /&gt;<br /> Thus &lt;math&gt;\pi= 4(\frac {N_c}{N})&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;font size=&quot;3&quot;&gt;For example, '''UNIF(a,b)'''&lt;br /&gt;<br /> &lt;math&gt;y = F(x) = (x - a)/ (b - a) &lt;/math&gt;<br /> &lt;math&gt;x = (b - a ) * y + a&lt;/math&gt;<br /> &lt;math&gt;X = a + ( b - a) * U&lt;/math&gt;&lt;br /&gt;<br /> where U is UNIF(0,1)&lt;/font&gt;<br /> <br /> '''Limitations:'''&lt;br /&gt;<br /> 1. This method is flawed since not all functions are invertible or monotonic: generalized inverse is hard to work on.&lt;br /&gt;<br /> 2. It may be impractical since some CDF's and/or integrals are not easy to compute such as Gaussian distribution.&lt;br /&gt;<br /> <br /> We learned how to prove the transformation from cdf to inverse cdf,and use the uniform distribution to obtain a value of x from F(x).<br /> We can also use uniform distribution in inverse method to determine other distributions.<br /> The probability of getting a point for a circle over the triangle is a closed uniform distribution, each point in the circle and over the triangle is almost the same. Then, we can look at the graph to determine what kind of distribution the graph resembles.<br /> <br /> ==== Probability Distribution Function Tool in MATLAB ====<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> disttool #shows different distributions<br /> &lt;/pre&gt; <br /> <br /> This command allows users to explore different types of distribution and see how the changes affect the parameters on the plot of either a CDF or PDF.<br /> <br /> <br /> [[File:Disttool.jpg|450px]]<br /> change the value of mu and sigma can change the graph skew side.<br /> <br /> == Class 3 - Tuesday, May 14 ==<br /> === Recall the Inverse Transform Method ===<br /> Let U~Unif(0,1),then the random variable X = F&lt;sup&gt;-1&lt;/sup&gt;(u) has distribution F. &lt;br /&gt;<br /> To sample X with CDF F(x), &lt;br /&gt;<br /> <br /> &lt;math&gt;1) U~ \sim~ Unif [0,1] &lt;/math&gt;<br /> '''2) X = F&lt;sup&gt;-1&lt;/sup&gt;(u) '''&lt;br /&gt;<br /> <br /> <br /> <br /> <br /> <br /> &lt;br /&gt;<br /> <br /> '''Note''': CDF of a U(a,b) random variable is:<br /> :&lt;math&gt;<br /> F(x)= \begin{cases}<br /> 0 &amp; \text{for }x &lt; a \\[8pt]<br /> \frac{x-a}{b-a} &amp; \text{for }a \le x &lt; b \\[8pt]<br /> 1 &amp; \text{for }x \ge b<br /> \end{cases}<br /> &lt;/math&gt; <br /> <br /> Thus, for &lt;math&gt; U &lt;/math&gt; ~ &lt;math&gt;U(0,1) &lt;/math&gt;, we have &lt;math&gt;P(U\leq 1) = 1&lt;/math&gt; and &lt;math&gt;P(U\leq 1/2) = 1/2&lt;/math&gt;.&lt;br /&gt;<br /> More generally, we see that &lt;math&gt;P(U\leq a) = a&lt;/math&gt;.&lt;br /&gt;<br /> For this reason, we had &lt;math&gt;P(U\leq F(x)) = F(x)&lt;/math&gt;.&lt;br /&gt;<br /> <br /> '''Reminder: ''' &lt;br /&gt; <br /> '''This is only for uniform distribution &lt;math&gt; U~ \sim~ Unif [0,1] &lt;/math&gt; '''&lt;br /&gt;<br /> &lt;math&gt; P (U \le 1) = 1 &lt;/math&gt; &lt;br /&gt;<br /> &lt;math&gt; P (U \le 0.5) = 0.5 &lt;/math&gt; &lt;br /&gt;<br /> &lt;math&gt; P (U \le a) = a &lt;/math&gt; &lt;br /&gt;<br /> <br /> [[File:2.jpg]] &lt;math&gt;P(U\leq a)=a&lt;/math&gt;<br /> <br /> Note that on a single point there is no mass probability (i.e. &lt;math&gt;u&lt;/math&gt; &lt;= 0.5, is the same as &lt;math&gt; u &lt;/math&gt; &lt; 0.5) <br /> More formally, this is saying that &lt;math&gt; P(X = x) = F(x)- \lim_{s \to x^-}F(x)&lt;/math&gt; , which equals zero for any continuous random variable<br /> <br /> ====Limitations of the Inverse Transform Method====<br /> <br /> Though this method is very easy to use and apply, it does have a major disadvantage/limitation:<br /> <br /> * We need to find the inverse cdf &lt;math&gt; F^{-1}(\cdot) &lt;/math&gt;. In some cases the inverse function does not exist, or is difficult to find because it requires a closed form expression for F(x).<br /> <br /> For example, it is too difficult to find the inverse cdf of the Gaussian distribution, so we must find another method to sample from the Gaussian distribution.<br /> <br /> In conclusion, we need to find another way of sampling from more complicated distributions<br /> <br /> === Discrete Case ===<br /> The same technique can be used for discrete case. We want to generate a discrete random variable x, that has probability mass function: &lt;br/&gt;<br /> <br /> :&lt;math&gt;\begin{align}P(X = x_i) &amp;{}= p_i \end{align}&lt;/math&gt;<br /> :&lt;math&gt;x_0 \leq x_1 \leq x_2 \dots \leq x_n&lt;/math&gt;<br /> :&lt;math&gt;\sum p_i = 1&lt;/math&gt;<br /> <br /> Algorithm for applying Inverse Transformation Method in Discrete Case (Procedure):&lt;br&gt;<br /> 1. Define a probability mass function for &lt;math&gt;x_{i}&lt;/math&gt; where i = 1,....,k. Note: k could grow infinitely. &lt;br&gt;<br /> 2. Generate a uniform random number U, &lt;math&gt; U~ \sim~ Unif [0,1] &lt;/math&gt;&lt;br&gt;<br /> 3. If &lt;math&gt;U\leq p_{o}&lt;/math&gt;, deliver &lt;math&gt;X = x_{o}&lt;/math&gt;&lt;br&gt;<br /> 4. Else, if &lt;math&gt;U\leq p_{o} + p_{1} &lt;/math&gt;, deliver &lt;math&gt;X = x_{1}&lt;/math&gt;&lt;br&gt;<br /> 5. Repeat the process again till we reached to &lt;math&gt;U\leq p_{o} + p_{1} + ......+ p_{k}&lt;/math&gt;, deliver &lt;math&gt;X = x_{k}&lt;/math&gt;&lt;br&gt;<br /> <br /> Note that after generating a random U, the value of X can be determined by finding the interval &lt;math&gt;[F(x_{j-1}),F(x_{j})]&lt;/math&gt; in which U lies. &lt;br /&gt;<br /> <br /> <br /> <br /> '''Example 3.0:''' &lt;br /&gt;<br /> Generate a random variable from the following probability function:&lt;br /&gt;<br /> {| class=&quot;wikitable&quot;<br /> |-<br /> |-<br /> | x<br /> | -2<br /> | -1<br /> | 0<br /> | 1<br /> | 2<br /> |-<br /> | f(x)<br /> | 0.1<br /> | 0.5<br /> | 0.07<br /> | 0.03<br /> | 0.3<br /> |}<br /> <br /> Answer:&lt;br /&gt;<br /> 1. Gen U~U(0,1)&lt;br /&gt;<br /> 2. If U &lt; 0.5 then output -1&lt;br /&gt;<br /> else if U &lt; 0.8 then output 2&lt;br /&gt;<br /> else if U &lt; 0.9 then output -2&lt;br /&gt;<br /> else if U &lt; 0.97 then output 0 else output 1&lt;br /&gt;<br /> <br /> '''Example 3.1 (from class):''' (Coin Flipping Example)&lt;br /&gt;<br /> We want to simulate a coin flip. We have U~U(0,1) and X = 0 or X = 1. <br /> <br /> We can define the U function so that: <br /> <br /> If &lt;math&gt;U\leq 0.5&lt;/math&gt;, then X = 0<br /> <br /> and if &lt;math&gt;0.5 &lt; U\leq 1&lt;/math&gt;, then X =1. <br /> <br /> This allows the probability of Heads occurring to be 0.5 and is a good generator of a random coin flip.<br /> <br /> &lt;math&gt; U~ \sim~ Unif [0,1] &lt;/math&gt; <br /> :&lt;math&gt;\begin{align}<br /> P(X = 0) &amp;{}= 0.5\\<br /> P(X = 1) &amp;{}= 0.5\\<br /> \end{align}&lt;/math&gt;<br /> The answer is:<br /> :&lt;math&gt;<br /> x = \begin{cases}<br /> 0, &amp; \text{if } U\leq 0.5 \\<br /> 1, &amp; \text{if } 0.5 &lt; U \leq 1<br /> \end{cases}&lt;/math&gt;<br /> <br /> <br /> * '''Code'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;for ii=1:1000<br /> u=rand;<br /> if u&lt;0.5<br /> x(ii)=0;<br /> else<br /> x(ii)=1;<br /> end<br /> end<br /> &gt;&gt;hist(x)<br /> &lt;/pre&gt;<br /> [[File:Coin_example.jpg|300px]]<br /> <br /> Note: The role of semi-colon in Matlab: Matlab will not print out the results if the line ends in a semi-colon and vice versa.<br /> <br /> '''Example 3.2 (From class):'''<br /> <br /> Suppose we have the following discrete distribution:<br /> <br /> :&lt;math&gt;\begin{align}<br /> P(X = 0) &amp;{}= 0.3 \\<br /> P(X = 1) &amp;{}= 0.2 \\<br /> P(X = 2) &amp;{}= 0.5<br /> \end{align}&lt;/math&gt;<br /> [[File:33.jpg]]<br /> <br /> The cumulative distribution function (cdf) for this distribution is then:<br /> <br /> :&lt;math&gt;<br /> F(x) = \begin{cases}<br /> 0, &amp; \text{if } x &lt; 0 \\<br /> 0.3, &amp; \text{if } x &lt; 1 \\<br /> 0.5, &amp; \text{if } x &lt; 2 \\<br /> 1, &amp; \text{if } x \ge 2<br /> \end{cases}&lt;/math&gt;<br /> <br /> Then we can generate numbers from this distribution like this, given &lt;math&gt;U \sim~ Unif[0, 1]&lt;/math&gt;:<br /> <br /> :&lt;math&gt;<br /> x = \begin{cases}<br /> 0, &amp; \text{if } U\leq 0.3 \\<br /> 1, &amp; \text{if } 0.3 &lt; U \leq 0.5 \\<br /> 2, &amp; \text{if } 0.5 &lt;U\leq 1<br /> \end{cases}&lt;/math&gt;<br /> <br /> &quot;Procedure&quot;&lt;br /&gt;<br /> 1. Draw U~u (0,1)&lt;br /&gt;<br /> 2. if U&lt;=0.3 deliver x=0&lt;br /&gt;<br /> 3. else if 0.3&lt;U&lt;=0.5 deliver x=1&lt;br /&gt;<br /> 4. else 0.5&lt;U&lt;=1 deliver x=2<br /> <br /> Can you find a faster way to run this algorithm? Consider:<br /> <br /> :&lt;math&gt;<br /> x = \begin{cases}<br /> 2, &amp; \text{if } U\leq 0.5 \\<br /> 1, &amp; \text{if } 0.5 &lt; U \leq 0.7 \\<br /> 0, &amp; \text{if } 0.7 &lt;U\leq 1<br /> \end{cases}&lt;/math&gt;<br /> <br /> The logic for this is that U is most likely to fall into the largest range. Thus by putting the largest range (in this case x &gt;= 0.5) we can improve the run time of this algorithm. Could this algorithm be improved further using the same logic?<br /> <br /> * '''Code''' (as shown in class)&lt;br /&gt;<br /> Use Editor window to edit the code &lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;close all<br /> &gt;&gt;clear all<br /> &gt;&gt;for ii=1:1000<br /> u=rand;<br /> if u&lt;=0.3<br /> x(ii)=0;<br /> elseif u&lt;=0.5<br /> x(ii)=1;<br /> else<br /> x(ii)=2;<br /> end<br /> end<br /> &gt;&gt;size(x)<br /> &gt;&gt;hist(x)<br /> &lt;/pre&gt;<br /> [[File:Discrete_example.jpg|300px]]<br /> <br /> The algorithm above generates a vector (1,1000) containing 0's ,1's and 2's in differing proportions. Due to the criteria for accepting 0, 1 or 2 into the vector we get proportions of 0,1 &amp;2 that correspond to their respective probabilities. So plotting the histogram (frequency of 0,1&amp;2) doesn't give us the pmf but a frequency histogram that shows the proportions of each, which looks identical to the pmf.<br /> <br /> '''Example 3.3''': Generating a random variable from pdf &lt;br&gt;<br /> :&lt;math&gt;<br /> f_{x}(x) = \begin{cases}<br /> 2x, &amp; \text{if } 0\leq x \leq 1 \\<br /> 0, &amp; \text{if } otherwise<br /> \end{cases}&lt;/math&gt;<br /> <br /> :&lt;math&gt;<br /> F_{x}(x) = \begin{cases}<br /> 0, &amp; \text{if } x &lt; 0 \\<br /> \int_{0}^{x}2sds = x^{2}, &amp; \text{if } 0\leq x \leq 1 \\<br /> 1, &amp; \text{if } x &gt; 1 <br /> \end{cases}&lt;/math&gt;<br /> <br /> :&lt;math&gt;\begin{align} U = x^{2}, X = F^{-1}x(U)= U^{\frac{1}{2}}\end{align}&lt;/math&gt;<br /> <br /> '''Example 3.4''': Generating a Bernoulli random variable &lt;br&gt;<br /> :&lt;math&gt;\begin{align} P(X = 1) = p, P(X = 0) = 1 - p\end{align}&lt;/math&gt;<br /> :&lt;math&gt;<br /> F(x) = \begin{cases}<br /> 1-p, &amp; \text{if } x &lt; 1 \\<br /> 1, &amp; \text{if } x \ge 1<br /> \end{cases}&lt;/math&gt;<br /> 1. Draw &lt;math&gt; U~ \sim~ Unif [0,1] &lt;/math&gt;&lt;br&gt;<br /> 2. &lt;math&gt;<br /> X = \begin{cases}<br /> 0, &amp; \text{if } 0 &lt; U &lt; 1-p \\<br /> 1, &amp; \text{if } 1-p \le U &lt; 1<br /> \end{cases}&lt;/math&gt;<br /> <br /> <br /> '''Example 3.5''': Generating Binomial(n,p) Random Variable&lt;br&gt;<br /> &lt;math&gt; use p\left( x=i+1\right) =\dfrac {n-i} {i+1}\dfrac {p} {1-p}p\left( x=i\right) &lt;/math&gt;<br /> <br /> Step 1: Generate a random number &lt;math&gt;U&lt;/math&gt;.&lt;br&gt;<br /> Step 2: &lt;math&gt;c = \frac {p}{(1-p)}&lt;/math&gt;, &lt;math&gt;i = 0&lt;/math&gt;, &lt;math&gt;pr = (1-p)^n&lt;/math&gt;, &lt;math&gt;F = pr&lt;/math&gt;&lt;br&gt;<br /> Step 3: If U&lt;F, set X = i and stop,&lt;br&gt;<br /> Step 4: &lt;math&gt; pr = \, {\frac {c(n-i)}{(i+1)}} {pr}, F = F +pr, i = i+1&lt;/math&gt;&lt;br&gt;<br /> Step 5: Go to step 3&lt;br&gt;<br /> *Note: These steps can be found in Simulation 5th Ed. by Sheldon Ross.<br /> *Note: Another method by seeing the Binomial as a sum of n independent Bernoulli random variables, U1, ..., Un. Then set X equal to the number of Ui that are less than or equal to p. To use this method, n random numbers are needed and n comparisons need to be done. On the other hand, the inverse transformation method is simpler because only one random variable needs to be generated and it makes 1 + np comparisons.&lt;br&gt;<br /> Step 1: Generate n uniform numbers U1 ... Un.&lt;br&gt;<br /> Step 2: X = &lt;math&gt;\sum U_i &lt; = p&lt;/math&gt; where P is the probability of success.<br /> <br /> '''Example 3.6''': Generating a Poisson random variable &lt;br&gt;<br /> <br /> &quot;Let X ~ Poi(u). Write an algorithm to generate X.<br /> The PDF of a poisson is:<br /> :&lt;math&gt;\begin{align} f(x) = \frac {\, e^{-u} u^x}{x!} \end{align}&lt;/math&gt;<br /> We know that<br /> :&lt;math&gt;\begin{align} P_{x+1} = \frac {\, e^{-u} u^{x+1}}{(x+1)!} \end{align}&lt;/math&gt;<br /> The ratio is &lt;math&gt;\begin{align} \frac {P_{x+1}}{P_x} = ... = \frac {u}{{x+1}} \end{align}&lt;/math&gt;<br /> Therefore, &lt;math&gt;\begin{align} P_{x+1} = \, {\frac {u}{x+1}} P_x\end{align}&lt;/math&gt;<br /> <br /> Algorithm: &lt;br&gt;<br /> 1) Generate U ~ U(0,1) &lt;br&gt;<br /> 2) &lt;math&gt;\begin{align} X = 0 \end{align}&lt;/math&gt;<br /> &lt;math&gt;\begin{align} F = P(X = 0) = e^{-u}*u^0/{0!} = e^{-u} = p \end{align}&lt;/math&gt;<br /> 3) If U&lt;F, output x &lt;br&gt;<br /> &lt;font size=&quot;3&quot;&gt;Else,&lt;/font&gt; &lt;math&gt;\begin{align} p = (u/(x+1))^p \end{align}&lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt;\begin{align} F = F + p \end{align}&lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt;\begin{align} x = x + 1 \end{align}&lt;/math&gt; &lt;br&gt;<br /> 4) Go to 1&quot; &lt;br&gt;<br /> <br /> Acknowledgements: This is an example from Stat 340 Winter 2013<br /> <br /> <br /> '''Example 3.7''': Generating Geometric Distribution:<br /> <br /> Consider Geo(p) where p is the probability of success, and define random variable X such that X is the total number of trials required to achieve the first success. x=1,2,3..... We have pmf:<br /> &lt;math&gt;P(X=x_i) = \, p (1-p)^{x_{i}-1}&lt;/math&gt;<br /> We have CDF:<br /> &lt;math&gt;F(x)=P(X \leq x)=1-P(X&gt;x) = 1-(1-p)^x&lt;/math&gt;, P(X&gt;x) means we get at least x failures before we observe the first success.<br /> Now consider the inverse transform:<br /> :&lt;math&gt;<br /> x = \begin{cases}<br /> 1, &amp; \text{if } U\leq p \\<br /> 2, &amp; \text{if } p &lt; U \leq 1-(1-p)^2 \\<br /> 3, &amp; \text{if } 1-(1-p)^2 &lt;U\leq 1-(1-p)^3 \\<br /> ....<br /> k, &amp; \text{if } 1-(1-p)^{k-1} &lt;U\leq 1-(1-p)^k <br /> ....<br /> \end{cases}&lt;/math&gt;<br /> <br /> <br /> '''Note''': Unlike the continuous case, the discrete inverse-transform method can always be used for any discrete distribution (but it may not be the most efficient approach) &lt;br&gt;<br /> <br /> <br /> <br /> '''General Procedure'''&lt;br /&gt;<br /> 1. Draw U ~ U(0,1)&lt;br /&gt;<br /> 2. If &lt;math&gt;U \leq P_{0}&lt;/math&gt; deliver &lt;math&gt;x = x_{0}&lt;/math&gt;&lt;br /&gt;<br /> 3. Else if &lt;math&gt;U \leq P_{0} + P_{1}&lt;/math&gt; deliver &lt;math&gt;x = x_{1}&lt;/math&gt;&lt;br /&gt;<br /> 4. Else if &lt;math&gt;U \leq P_{0} + P_{1} + P_{2} &lt;/math&gt; deliver &lt;math&gt;x = x_{2}&lt;/math&gt;&lt;br /&gt;<br /> ... <br /> &lt;font size=&quot;3&quot;&gt;Else if&lt;/font&gt; &lt;math&gt;U \leq P_{0} + ... + P_{k} &lt;/math&gt; &lt;font size=&quot;3&quot;&gt;deliver&lt;/font&gt; &lt;math&gt;x = x_{k}&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;br /'''&gt;===Inverse Transform Algorithm for Generating a Binomial(n,p) Random Variable(from textbook)==='''<br /> &lt;br /&gt;step 1: Generate a random number U<br /> &lt;br /&gt;step 2: c=p/(1-p),i=0, pr=(1-p)&lt;sup&gt;n&lt;/sup&gt;, F=pr.<br /> &lt;br /&gt;step 3: If U&lt;F, set X=i and stop.<br /> &lt;br /&gt;step 4: pr =[c(n-i)/(i+1)]pr, F=F+pr, i=i+1.<br /> &lt;br /&gt;step 5: Go to step 3.<br /> <br /> <br /> '''Problems'''&lt;br /&gt;<br /> Though this method is very easy to use and apply, it does have a major disadvantage/limitation:<br /> We need to find the inverse cdf F^{-1}(\cdot) . In some cases the inverse function does not exist, or is difficult to find because it requires a closed form expression for F(x).<br /> For example, it is too difficult to find the inverse cdf of the Gaussian distribution, so we must find another method to sample from the Gaussian distribution.<br /> In conclusion, we need to find another way of sampling from more complicated distributions<br /> Flipping a coin is a discrete case of uniform distribution, and the code below shows an example of flipping a coin 1000 times; the result is close to the expected value 0.5.&lt;br&gt;<br /> Example 2, as another discrete distribution, shows that we can sample from parts like 0,1 and 2, and the probability of each part or each trial is the same.&lt;br&gt;<br /> Example 3 uses inverse method to figure out the probability range of each random varible.<br /> &lt;div style=&quot;border:1px solid #cccccc;border-radius:10px;box-shadow: 0 5px 15px 1px rgba(0, 0, 0, 0.6), 0 0 200px 1px rgba(255, 255, 255, 0.5);padding:20px;margin:20px;background:#FFFFAD;&quot;&gt;<br /> &lt;h2 style=&quot;text-align:center;&quot;&gt;Summary of Inverse Transform Method&lt;/h2&gt;<br /> &lt;p&gt;&lt;b&gt;Problem:&lt;/b&gt;generate types of distribution.&lt;/p&gt;<br /> &lt;p&gt;&lt;b&gt;Plan:&lt;/b&gt;&lt;/p&gt;<br /> &lt;b style='color:lightblue;'&gt;Continuous case:&lt;/b&gt;<br /> &lt;ol&gt;<br /> &lt;li&gt;find CDF F&lt;/li&gt;<br /> &lt;li&gt;find the inverse F&lt;sup&gt;-1&lt;/sup&gt;&lt;/li&gt;<br /> &lt;li&gt;Generate a list of uniformly distributed number {x}&lt;/li&gt;<br /> &lt;li&gt;{F&lt;sup&gt;-1&lt;/sup&gt;(x)} is what we want&lt;/li&gt;<br /> &lt;/ol&gt;<br /> &lt;b&gt;Matlab Instruction&lt;/b&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;&amp;gt;&amp;gt;u=rand(1,1000);<br /> &amp;gt;&amp;gt;hist(u)<br /> &amp;gt;&amp;gt;x=(-log(1-u))/2;<br /> &amp;gt;&amp;gt;size(x) <br /> &amp;gt;&amp;gt;figure<br /> &amp;gt;&amp;gt;hist(x)<br /> &lt;/pre&gt;<br /> &lt;br&gt;<br /> &lt;b style='color:lightblue'&gt;Discrete case:&lt;/b&gt;<br /> &lt;ol&gt;<br /> &lt;li&gt;generate a list of uniformly distributed number {u}&lt;/li&gt;<br /> &lt;li&gt;d&lt;sub&gt;i&lt;/sub&gt;=x&lt;sub&gt;i&lt;/sub&gt; if&lt;math&gt; X=x_i, &lt;/math&gt; if &lt;math&gt; F(x_{i-1})&lt;U\leq F(x_i) &lt;/math&gt;&lt;/li&gt;<br /> &lt;li&gt;{d&lt;sub&gt;i&lt;/sub&gt;=x&lt;sub&gt;i&lt;/sub&gt;} is what we want&lt;/li&gt;<br /> &lt;/ol&gt;<br /> &lt;b&gt;Matlab Instruction&lt;/b&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;&amp;gt;&amp;gt;for ii=1:1000<br /> u=rand;<br /> if u&amp;lt;0.5<br /> x(ii)=0;<br /> else<br /> x(ii)=1;<br /> end<br /> end<br /> &amp;gt;&amp;gt;hist(x)<br /> &lt;/pre&gt;<br /> &lt;/div&gt;<br /> <br /> === Generalized Inverse-Transform Method ===<br /> <br /> Valid for any CDF F(x): return X=min{x:F(x)&lt;math&gt;\leq&lt;/math&gt; U}, where U~U(0,1)<br /> <br /> 1. Continues, possibly with flat spots (i.e. not strictly increasing) <br /> <br /> 2. Discrete<br /> <br /> 3. Mixed continues discrete<br /> <br /> <br /> '''Advantages of Inverse-Transform Method'''<br /> <br /> Inverse transform method preserves monotonicity and correlation<br /> <br /> which helps in<br /> <br /> 1. Variance reduction methods ...<br /> <br /> 2. Generating truncated distributions ...<br /> <br /> 3. Order statistics ...<br /> <br /> ===Acceptance-Rejection Method===<br /> <br /> Although the inverse transformation method does allow us to change our uniform distribution, it has two limits;<br /> # Not all functions have inverse functions (ie, the range of x and y have limit and do not fix the inverse functions)<br /> # For some distributions, such as Gaussian, it is too difficult to find the inverse<br /> <br /> To generate random samples for these functions, we will use different methods, such as the '''Acceptance-Rejection Method'''. This method is more efficient than the inverse transform method. The basic idea is to find an alternative probability distribution with density function f(x);<br /> <br /> Suppose we want to draw random sample from a target density function ''f(x)'', ''x∈S&lt;sub&gt;x&lt;/sub&gt;'', where ''S&lt;sub&gt;x&lt;/sub&gt;'' is the support of ''f(x)''. If we can find some constant ''c''(≥1) (In practice, we prefer c as close to 1 as possible) and a density function ''g(x)'' having the same support ''S&lt;sub&gt;x&lt;/sub&gt;'' so that ''f(x)≤cg(x), ∀x∈S&lt;sub&gt;x&lt;/sub&gt;'', then we can apply the procedure for Acceptance-Rejection Method. Typically we choose a density function that we already know how to sample from for ''g(x)''.<br /> <br /> [[File:AR_Method.png]]<br /> <br /> <br /> The main logic behind the Acceptance-Rejection Method is that:&lt;br&gt;<br /> 1. We want to generate sample points from an unknown distribution, say f(x).&lt;br&gt;<br /> 2. We use &lt;math&gt;\,cg(x)&lt;/math&gt; to generate points so that we have more points than f(x) could ever generate for all x. (where c is a constant, and g(x) is a known distribution)&lt;br&gt;<br /> 3. For each value of x, we accept and reject some points based on a probability, which will be discussed below.&lt;br&gt;<br /> <br /> Note: If the red line was only g(x) as opposed to &lt;math&gt;\,c g(x)&lt;/math&gt; (i.e. c=1), then &lt;math&gt;g(x) \geq f(x)&lt;/math&gt; for all values of x if and only if g and f are the same functions. This is because the sum of pdf of g(x)=1 and the sum of pdf of f(x)=1, hence, &lt;math&gt;g(x) \ngeqq f(x)&lt;/math&gt; \,&amp;forall;x. &lt;br&gt;<br /> <br /> Also remember that &lt;math&gt;\,c g(x)&lt;/math&gt; always generates higher probability than what we need. Thus we need an approach of getting the proper probabilities.&lt;br&gt;&lt;br&gt;<br /> <br /> c must be chosen so that &lt;math&gt;f(x)\leqslant c g(x)&lt;/math&gt; for all value of x. c can only equal 1 when f and g have the same distribution. Otherwise:&lt;br&gt;<br /> Either use a software package to test if &lt;math&gt;f(x)\leqslant c g(x)&lt;/math&gt; for an arbitrarily chosen c &gt; 0, or:&lt;br&gt;<br /> 1. Find first and second derivatives of f(x) and g(x).&lt;br&gt;<br /> 2. Identify and classify all local and absolute maximums and minimums, using the First and Second Derivative Tests, as well as all inflection points.&lt;br&gt;<br /> 3. Verify that &lt;math&gt;f(x)\leqslant c g(x)&lt;/math&gt; at all the local maximums as well as the absolute maximums.&lt;br&gt;<br /> 4. Verify that &lt;math&gt;f(x)\leqslant c g(x)&lt;/math&gt; at the tail ends by calculating &lt;math&gt;\lim_{x \to +\infty} \frac{f(x)}{\, c g(x)}&lt;/math&gt; and &lt;math&gt;\lim_{x \to -\infty} \frac{f(x)}{\, c g(x)}&lt;/math&gt; and seeing that they are both &lt; 1. Use of L'Hopital's Rule should make this easy, since both f and g are p.d.f's, resulting in both of them approaching 0.&lt;br&gt;<br /> 5.Efficiency: the number of times N that steps 1 and 2 need to be called(also the number of iterations needed to successfully generate X) is a random variable and has a geometric distribution with success probability &lt;math&gt;p=P(U \leq f(Y)/(cg(Y)))&lt;/math&gt; , &lt;math&gt;P(N=n)=(1-p(n-1))p ,n \geq 1&lt;/math&gt;.Thus on average the number of iterations required is given by &lt;math&gt; E(N)=\frac{1} p&lt;/math&gt;<br /> <br /> c should be close to the maximum of f(x)/g(x), not just some arbitrarily picked large number. Otherwise, the Acceptance-Rejection method will have more rejections (since our probability &lt;math&gt;f(x)\leqslant c g(x)&lt;/math&gt; will be close to zero). This will render our algorithm inefficient. <br /> <br /> The expected number of iterations of the algorithm required with an X is c.<br /> &lt;br&gt;<br /> '''Note:''' &lt;br&gt;<br /> 1. Value around x&lt;sub&gt;1&lt;/sub&gt; will be sampled more often under cg(x) than under f(x).There will be more samples than we actually need, if &lt;math&gt;\frac{f(y)}{\, c g(y)}&lt;/math&gt; is small, the acceptance-rejection technique will need to be done to these points to get the accurate amount.In the region above x&lt;sub&gt;1&lt;/sub&gt;, we should accept less and reject more. &lt;br&gt;<br /> 2. Value around x&lt;sub&gt;2&lt;/sub&gt;: number of sample that are drawn and the number we need are much closer. So in the region above x&lt;sub&gt;2&lt;/sub&gt;, we accept more. As a result, g(x) and f(x) are comparable.&lt;br&gt;<br /> 3. The constant c is needed because we need to adjust the height of g(x) to ensure that it is above f(x). Besides that, it is best to keep the number of rejected varieties small for maximum efficiency. &lt;br&gt; <br /> <br /> Another way to understand why the the acceptance probability is &lt;math&gt;\frac{f(y)}{\, c g(y)}&lt;/math&gt;, is by thinking of areas. From the graph above, we see that the target function in under the proposed function c g(y). Therefore, &lt;math&gt;\frac{f(y)}{\, c g(y)}&lt;/math&gt; is the proportion or the area under c g(y) that also contains f(y). Therefore we say we accept sample points for which u is less then &lt;math&gt;\frac{f(y)}{\, c g(y)}&lt;/math&gt; because then the sample points are guaranteed to fall under the area of c g(y) that contains f(y). &lt;br&gt;<br /> &lt;br&gt;<br /> <br /> '''There are 2 cases that are possible:''' &lt;br&gt;<br /> -Sample of points is more than enough, &lt;math&gt;c g(x) \geq f(x) &lt;/math&gt; &lt;br&gt;<br /> -Similar or the same amount of points, &lt;math&gt;c g(x) \geq f(x) &lt;/math&gt; &lt;br&gt;<br /> '''There is 1 case that is not possible:''' &lt;br&gt;<br /> -Less than enough points, such that &lt;math&gt; g(x) &lt;/math&gt; is greater than &lt;math&gt; f &lt;/math&gt;, &lt;math&gt;g(x) \geq f(x)&lt;/math&gt; &lt;br&gt;<br /> &lt;br&gt;<br /> <br /> '''Procedure'''<br /> <br /> #Draw Y~g(.)<br /> #Draw U~u(0,1) (Note: U and Y are independent)<br /> #If &lt;math&gt;u\leq \frac{f(y)}{cg(y)}&lt;/math&gt; (which is &lt;math&gt;P(accepted|y)&lt;/math&gt;) then x=y, else return to Step 1&lt;br&gt;<br /> <br /> <br /> Note: Recall &lt;math&gt;P(U\leq a)=a&lt;/math&gt;. Thus by comparing u and &lt;math&gt;\frac{f(y)}{\, c g(y)}&lt;/math&gt;, we can get a probability of accepting y at these points. For instance, at some points that cg(x) is much larger than f(x), the probability of accepting x=y is quite small.&lt;br&gt;<br /> ie. At X&lt;sub&gt;1&lt;/sub&gt;, low probability to accept the point since f(x) is much smaller than cg(x).&lt;br&gt;<br /> At X&lt;sub&gt;2&lt;/sub&gt;, high probability to accept the point. &lt;math&gt;P(U\leq a)=a&lt;/math&gt; in Uniform Distribution.<br /> <br /> Note: Since U is the variable for uniform distribution between 0 and 1. It equals to 1 for all. The condition depends on the constant c. so the condition changes to &lt;math&gt;c\leq \frac{f(y)}{g(y)}&lt;/math&gt;<br /> <br /> <br /> introduce the relationship of cg(x)and f(x),and prove why they have that relationship and where we can use this rule to reject some cases.<br /> and learn how to see the graph to find the accurate point to reject or accept the ragion above the random variable x.<br /> for the example, x1 is bad point and x2 is good point to estimate the rejection and acceptance<br /> <br /> '''Some notes on the constant C'''&lt;br&gt;<br /> 1. C is chosen such that &lt;math&gt; c g(y)\geq f(y)&lt;/math&gt;, that is,&lt;math&gt; c g(y)&lt;/math&gt; will always dominate &lt;math&gt;f(y)&lt;/math&gt;. Because of this, <br /> C will always be greater than or equal to one and will only equal to one if and only if the proposal distribution and the target distribution are the same. It is normally best to choose C such that the absolute maxima of both &lt;math&gt; c g(y)&lt;/math&gt; and &lt;math&gt; f(y)&lt;/math&gt; are the same.&lt;br&gt;<br /> <br /> 2. &lt;math&gt; \frac {1}{C} &lt;/math&gt; is the area of &lt;math&gt; F(y)&lt;/math&gt; over the area of &lt;math&gt; c G(y)&lt;/math&gt; and is the acceptance rate of the points generated. For example, if &lt;math&gt; \frac {1}{C} = 0.7&lt;/math&gt; then on average, 70 percent of all points generated are accepted.&lt;br&gt;<br /> <br /> 3. C is the average number of times Y is generated from g .<br /> <br /> === Theorem ===<br /> <br /> Let &lt;math&gt;f: \R \rightarrow [0,+\infty]&lt;/math&gt; be a well-defined pdf, and &lt;math&gt;\displaystyle Y&lt;/math&gt; be a random variable with pdf &lt;math&gt;g: \R \rightarrow [0,+\infty]&lt;/math&gt; such that &lt;math&gt;\exists c \in \R^+&lt;/math&gt; with &lt;math&gt;f \leq c \cdot g&lt;/math&gt;. If &lt;math&gt;\displaystyle U \sim~ U(0,1)&lt;/math&gt; is independent of &lt;math&gt;\displaystyle Y&lt;/math&gt;, then the random variable defined as &lt;math&gt;X := Y \vert U \leq \frac{f(Y)}{c \cdot g(Y)}&lt;/math&gt; has pdf &lt;math&gt;\displaystyle f&lt;/math&gt;, and the condition &lt;math&gt;U \leq \frac{f(Y)}{c \cdot g(Y)}&lt;/math&gt; is denoted by &quot;Accepted&quot;.<br /> <br /> === Proof ===<br /> Recall the conditional probability formulas:&lt;br /&gt;<br /> &lt;math&gt;\begin{align}<br /> P(A|B)=\frac{P(A \cap B)}{P(B)}, \text{ or }P(A|B)=\frac{P(B|A)P(A)}{P(B)} \text{ for pmf}<br /> \end{align}&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;P(y|accepted)=f(y)=\frac{P(accepted|y)P(y)}{P(accepted)}&lt;/math&gt;&lt;br /&gt; <br /> &lt;br /&gt;based on the concept from '''procedure-step1''':&lt;br /&gt;<br /> &lt;math&gt;P(y)=g(y)&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;P(accepted|y)=\frac{f(y)}{cg(y)}&lt;/math&gt; &lt;br /&gt;<br /> (the larger the value is, the larger the chance it will be selected) &lt;br /&gt;&lt;br /&gt;<br /> <br /> <br /> &lt;math&gt;<br /> \begin{align}<br /> P(accepted)&amp;=\int_y\ P(accepted|y)P(y)\\<br /> &amp;=\int_y\ \frac{f(s)}{cg(s)}g(s)ds\\<br /> &amp;=\frac{1}{c} \int_y\ f(s) ds\\<br /> &amp;=\frac{1}{c}<br /> \end{align}&lt;/math&gt;&lt;br /&gt;<br /> <br /> Therefore:&lt;br /&gt;<br /> &lt;math&gt;\begin{align}<br /> P(x)&amp;=P(y|accepted)\\<br /> &amp;=\frac{\frac{f(y)}{cg(y)}g(y)}{1/c}\\<br /> &amp;=\frac{\frac{f(y)}{c}}{1/c}\\<br /> &amp;=f(y)\end{align}&lt;/math&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;<br /> <br /> '''''Here is an alternative introduction of Acceptance-Rejection Method'''''<br /> <br /> '''Comments:'''<br /> <br /> -Acceptance-Rejection Method is not good for all cases. The limitation with this method is that sometimes many points will be rejected. One obvious disadvantage is that it could be very hard to pick the &lt;math&gt;g(y)&lt;/math&gt; and the constant &lt;math&gt;c&lt;/math&gt; in some cases. We have to pick the SMALLEST C such that &lt;math&gt;cg(x) \leq f(x)&lt;/math&gt; else the the algorithm will not be efficient. This is because &lt;math&gt;f(x)/cg(x)&lt;/math&gt; will become smaller and probability &lt;math&gt;u \leq f(x)/cg(x)&lt;/math&gt; will go down and many points will be rejected making the algorithm inefficient. <br /> <br /> -'''Note:''' When &lt;math&gt;f(y)&lt;/math&gt; is very different than &lt;math&gt;g(y)&lt;/math&gt;, it is less likely that the point will be accepted as the ratio above would be very small and it will be difficult for &lt;math&gt;U&lt;/math&gt; to be less than this small value. &lt;br/&gt;An example would be when the target function (&lt;math&gt;f&lt;/math&gt;) has a spike or several spikes in its domain - this would force the known distribution (&lt;math&gt;g&lt;/math&gt;) to have density at least as large as the spikes, making the value of &lt;math&gt;c&lt;/math&gt; larger than desired. As a result, the algorithm would be highly inefficient.<br /> <br /> '''Acceptance-Rejection Method'''&lt;br/&gt;<br /> '''Example 1''' (discrete case)&lt;br/&gt;<br /> We wish to generate X~Bi(2,0.5), assuming that we cannot generate this directly.&lt;br/&gt;<br /> We use a discrete distribution DU[0,2] to approximate this.&lt;br/&gt;<br /> &lt;math&gt;f(x)=Pr(X=x)=2Cx×(0.5)^2\,&lt;/math&gt;&lt;br/&gt;<br /> <br /> {| class=wikitable align=left<br /> |&lt;math&gt;x&lt;/math&gt;||0||1||2 <br /> |-<br /> |&lt;math&gt;f(x)&lt;/math&gt;||1/4||1/2||1/4 <br /> |-<br /> |&lt;math&gt;g(x)&lt;/math&gt;||1/3||1/3||1/3 <br /> |-<br /> |&lt;math&gt;c=f(x)/g(x)&lt;/math&gt;||3/4||3/2||3/4<br /> |-<br /> |&lt;math&gt;f(x)/(cg(x))&lt;/math&gt;||1/2||1||1/2<br /> |}<br /> <br /> <br /> Since we need &lt;math&gt;c \geq f(x)/g(x)&lt;/math&gt;&lt;br/&gt;<br /> We need &lt;math&gt;c=3/2&lt;/math&gt;&lt;br/&gt;<br /> <br /> Therefore, the algorithm is:&lt;br/&gt;<br /> 1. Generate &lt;math&gt;u,v~U(0,1)&lt;/math&gt;&lt;br/&gt;<br /> 2. Set &lt;math&gt;y= \lfloor 3*u \rfloor&lt;/math&gt; (This is using uniform distribution to generate DU[0,2]&lt;br/&gt;<br /> 3. If &lt;math&gt;(y=0)&lt;/math&gt; and &lt;math&gt;(v&lt;\tfrac{1}{2}), output=0&lt;/math&gt; &lt;br/&gt;<br /> If &lt;math&gt;(y=2) &lt;/math&gt; and &lt;math&gt;(v&lt;\tfrac{1}{2}), output=2 &lt;/math&gt;&lt;br/&gt;<br /> Else if &lt;math&gt;y=1, output=1&lt;/math&gt;&lt;br/&gt;<br /> <br /> <br /> An elaboration of “c”&lt;br/&gt;<br /> c is the expected number of times the code runs to output 1 random variable. Remember that when &lt;math&gt;u &lt; \tfrac{f(x)}{cg(x)}&lt;/math&gt; is not satisfied, we need to go over the code again.&lt;br/&gt;<br /> <br /> Proof&lt;br/&gt;<br /> <br /> Let &lt;math&gt;f(x)&lt;/math&gt; be the function we wish to generate from, but we cannot use inverse transform method to generate directly.&lt;br/&gt;<br /> Let &lt;math&gt;g(x)&lt;/math&gt; be the helper function &lt;br/&gt;<br /> Let &lt;math&gt;kg(x)&gt;=f(x)&lt;/math&gt;&lt;br/&gt;<br /> Since we need to generate y from &lt;math&gt;g(x)&lt;/math&gt;,&lt;br/&gt;<br /> &lt;math&gt;Pr(select y)=g(y)&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;Pr(output y|selected y)=Pr(u&lt;f(y)/(cg(y)))= f(y)/(cg(y))&lt;/math&gt; (Since u~Unif(0,1))&lt;br/&gt;<br /> &lt;math&gt;Pr(output y)=Pr(output y1|selected y1)Pr(select y1)+ Pr(output y2|selected y2)Pr(select y2)+…+ Pr(output yn|selected yn)Pr(select yn)=1/c&lt;/math&gt; &lt;br/&gt;<br /> Consider that we are asking for expected time for the first success, it is a geometric distribution with probability of success=1/c&lt;br/&gt;<br /> Therefore, &lt;math&gt;E(X)=1/(1/c))=c&lt;/math&gt; &lt;br/&gt;<br /> <br /> Acknowledgements: Some materials have been borrowed from notes from Stat340 in Winter 2013.<br /> <br /> Use the conditional probability to proof if the probability is accepted, then the result is closed pdf of the original one.<br /> the example shows how to choose the c for the two function &lt;math&gt;g(x)&lt;/math&gt; and &lt;math&gt;f(x)&lt;/math&gt;.<br /> <br /> === Example of Acceptance-Rejection Method===<br /> <br /> Generating a random variable having p.d.f. &lt;br /&gt;<br /> &lt;math&gt;\displaystyle f(x) = 20x(1 - x)^3, 0&lt; x &lt;1 &lt;/math&gt;&lt;br /&gt;<br /> Since this random variable (which is beta with parameters (2,4)) is concentrated in the interval (0, 1), let us consider the acceptance-rejection method with&lt;br /&gt;<br /> &lt;math&gt;\displaystyle g(x) = 1,0&lt;x&lt;1&lt;/math&gt;&lt;br /&gt;<br /> To determine the constant c such that f(x)/g(x) &lt;= c, we use calculus to determine the maximum value of&lt;br /&gt;<br /> &lt;math&gt;\displaystyle f(x)/g(x) = 20x(1 - x)^3 &lt;/math&gt;&lt;br /&gt;<br /> Differentiation of this quantity yields &lt;br /&gt; <br /> &lt;math&gt;\displaystyle d/dx[f(x)/g(x)]=20*[(1-x)^3-3x(1-x)^2]&lt;/math&gt;&lt;br /&gt;<br /> Setting this equal to 0 shows that the maximal value is attained when x = 1/4, <br /> and thus, &lt;br /&gt;<br /> &lt;math&gt;\displaystyle f(x)/g(x)&lt;= 20*(1/4)*(3/4)^3=135/64=c &lt;/math&gt;&lt;br /&gt;<br /> Hence,&lt;br /&gt;<br /> &lt;math&gt;\displaystyle f(x)/cg(x)=(256/27)*(x*(1-x)^3)&lt;/math&gt;&lt;br /&gt;<br /> and thus the simulation procedure is as follows:<br /> <br /> 1) Generate two random numbers U1 and U2 .<br /> <br /> 2) If U&lt;sub&gt;2&lt;/sub&gt;&lt;(256/27)*U&lt;sub&gt;1&lt;/sub&gt;*(1-U&lt;sub&gt;1&lt;/sub&gt;)&lt;sup&gt;3&lt;/sup&gt;, set X=U&lt;sub&gt;1&lt;/sub&gt;, and stop<br /> Otherwise return to step 1). <br /> The average number of times that step 1) will be performed is c = 135/64.<br /> <br /> (The above example is from http://www.cs.bgu.ac.il/~mps042/acceptance.htm, example 2.)<br /> <br /> use the derivative to proof the accepetance-rejection method,<br /> find the local maximum of f(x)/g(x).<br /> and we can calculate the best constant c.<br /> <br /> ===Another Example of Acceptance-Rejection Method===<br /> Generate a random variable from:&lt;br /&gt; <br /> &lt;math&gt;\displaystyle f(x)=3*x^2, 0&lt;x&lt;1 &lt;/math&gt;&lt;br /&gt;<br /> Assume g(x) to be uniform over interval (0,1), where 0&lt; x &lt;1&lt;br /&gt;<br /> Therefore:&lt;br /&gt;<br /> &lt;math&gt;\displaystyle c = max(f(x)/(g(x)))= 3&lt;/math&gt;&lt;br /&gt; <br /> <br /> the best constant c is the max(f(x)/(cg(x))) and the c make the area above the f(x) and below the g(x) to be small.<br /> because g(.) is uniform so the g(x) is 1. max(g(x)) is 1&lt;br /&gt;<br /> &lt;math&gt;\displaystyle f(x)/(cg(x))= x^2&lt;/math&gt;&lt;br /&gt;<br /> Acknowledgement: this is example 1 from http://www.cs.bgu.ac.il/~mps042/acceptance.htm<br /> <br /> == Class 4 - Thursday, May 16 == <br /> <br /> '''Goals'''&lt;br&gt; <br /> *When we want to find target distribution &lt;math&gt;f(x)&lt;/math&gt;, we need to first find a proposal distribution &lt;math&gt;g(x)&lt;/math&gt; that is easy to sample from. &lt;br&gt; <br /> *Relationship between the proposal distribution and target distribution is: &lt;math&gt; c \cdot g(x) \geq f(x) &lt;/math&gt;, where c is constant. This means that the area of f(x) is under the area of &lt;math&gt; c \cdot g(x)&lt;/math&gt;. &lt;br&gt;<br /> *Chance of acceptance is less if the distance between &lt;math&gt;f(x)&lt;/math&gt; and &lt;math&gt; c \cdot g(x)&lt;/math&gt; is big, and vice-versa, we use &lt;math&gt; c &lt;/math&gt; to keep &lt;math&gt; \frac {f(x)}{c \cdot g(x)} &lt;/math&gt; below 1 (so &lt;math&gt;f(x) \leq c \cdot g(x)&lt;/math&gt;). Therefore, we must find the constant &lt;math&gt; C &lt;/math&gt; to achieve this.&lt;br /&gt;<br /> *In other words, &lt;math&gt;C&lt;/math&gt; is chosen to make sure &lt;math&gt; c \cdot g(x) \geq f(x) &lt;/math&gt;. However, it will not make sense if &lt;math&gt;C&lt;/math&gt; is simply chosen to be arbitrarily large. We need to choose &lt;math&gt;C&lt;/math&gt; such that &lt;math&gt;c \cdot g(x)&lt;/math&gt; fits &lt;math&gt;f(x)&lt;/math&gt; as tightly as possible. This means that we must find the minimum c such that the area of f(x) is under the area of c*g(x). &lt;br /&gt;<br /> *The constant c cannot be a negative number.&lt;br /&gt;<br /> <br /> <br /> '''How to find C''':&lt;br /&gt;<br /> <br /> &lt;math&gt;\begin{align}<br /> &amp;c \cdot g(x) \geq f(x)\\<br /> &amp;c\geq \frac{f(x)}{g(x)} \\<br /> &amp;c= \max \left(\frac{f(x)}{g(x)}\right) <br /> \end{align}&lt;/math&gt;&lt;br&gt;<br /> <br /> If &lt;math&gt;f&lt;/math&gt; and &lt;math&gt; g &lt;/math&gt; are continuous, we can find the extremum by taking the derivative and solve for &lt;math&gt;x_0&lt;/math&gt; such that:&lt;br/&gt;<br /> &lt;math&gt; 0=\frac{d}{dx}\frac{f(x)}{g(x)}|_{x=x_0}&lt;/math&gt; &lt;br/&gt;<br /> <br /> Thus &lt;math&gt; c = \frac{f(x_0)}{g(x_0)} &lt;/math&gt;&lt;br/&gt;<br /> <br /> Note: This procedure is called the Acceptance-Rejection Method.&lt;br&gt; <br /> <br /> '''The Acceptance-Rejection method''' involves finding a distribution that we know how to sample from, g(x), and multiplying g(x) by a constant c so that &lt;math&gt;c \cdot g(x)&lt;/math&gt; is always greater than or equal to f(x). Mathematically, we want &lt;math&gt; c \cdot g(x) \geq f(x) &lt;/math&gt;.<br /> And it means, c has to be greater or equal to &lt;math&gt;\frac{f(x)}{g(x)}&lt;/math&gt;. So the smallest possible c that satisfies the condition is the maximum value of &lt;math&gt;\frac{f(x)}{g(x)}&lt;/math&gt;&lt;br/&gt;. <br /> But in case of c being too large, the chance of acceptance of generated values will be small, thereby losing efficiency of the algorithm. Therefore, it is best to get the smallest possible c such that &lt;math&gt; c g(x) \geq f(x)&lt;/math&gt;. &lt;br&gt;<br /> <br /> '''Important points:'''&lt;br&gt; <br /> <br /> *For this method to be efficient, the constant c must be selected so that the rejection rate is low. (The efficiency for this method is &lt;math&gt;\left ( \frac{1}{c} \right )&lt;/math&gt;)&lt;br&gt;<br /> *It is easy to show that the expected number of trials for an acceptance is &lt;math&gt; \frac{Total Number of Trials} {C} &lt;/math&gt;. &lt;br&gt;<br /> *recall the '''acceptance rate is 1/c'''. (Not rejection rate) <br /> :Let &lt;math&gt;X&lt;/math&gt; be the number of trials for an acceptance, &lt;math&gt; X \sim~ Geo(\frac{1}{c})&lt;/math&gt;&lt;br&gt;<br /> :&lt;math&gt;\mathbb{E}[X] = \frac{1}{\frac{1}{c}} = c &lt;/math&gt;<br /> *The number of trials needed to generate a sample size of &lt;math&gt;N&lt;/math&gt; follows a negative binomial distribution. The expected number of trials needed is then &lt;math&gt;cN&lt;/math&gt;.&lt;br&gt;<br /> *So far, the only distribution we know how to sample from is the '''UNIFORM''' distribution. &lt;br&gt;<br /> <br /> <br /> '''Procedure''': &lt;br&gt;<br /> <br /> 1. Choose &lt;math&gt;g(x)&lt;/math&gt; (simple density function that we know how to sample, i.e. Uniform so far) &lt;br&gt;<br /> The easiest case is &lt;math&gt;U~ \sim~ Unif [0,1] &lt;/math&gt;. However, in other cases we need to generate UNIF(a,b). We may need to perform a linear transformation on the &lt;math&gt;U~ \sim~ Unif [0,1] &lt;/math&gt; variable. &lt;br&gt;<br /> 2. Find a constant c such that :&lt;math&gt; c \cdot g(x) \geq f(x) &lt;/math&gt;, otherwise return to step 1.<br /> <br /> '''Recall the general procedure of Acceptance-Rejection Method'''<br /> #Let &lt;math&gt;Y \sim~ g(y)&lt;/math&gt; <br /> #Let &lt;math&gt;U \sim~ Unif [0,1] &lt;/math&gt;<br /> #If &lt;math&gt;U \leq \frac{f(Y)}{c \cdot g(Y)}&lt;/math&gt; then X=Y; else return to step 1 (This is not the way to find C. This is the general procedure.)<br /> <br /> &lt;hr&gt;&lt;b&gt;Example: &lt;br&gt; <br /> <br /> Generate a random variable from the pdf&lt;/b&gt;&lt;br&gt;<br /> &lt;math&gt; f(x) = <br /> \begin{cases} <br /> 2x, &amp; \mbox{if }0 \leqslant x \leqslant 1 \\<br /> 0, &amp; \mbox{otherwise}<br /> \end{cases} &lt;/math&gt;<br /> <br /> We can note that this is a special case of Beta(2,1), where, <br /> &lt;math&gt;beta(a,b)=\frac{\Gamma(a+b)}{\Gamma(a)\Gamma(b)}x^{(a-1)}(1-x)^{(b-1)}&lt;/math&gt;&lt;br&gt;<br /> <br /> Where &amp;Gamma; (n) = (n - 1)! if n is positive integer<br /> <br /> &lt;math&gt;Gamma(z)=\int _{0}^{\infty }t^{z-1}e^{-t}dt&lt;/math&gt;<br /> <br /> Aside: Beta function<br /> <br /> In mathematics, the beta function, also called the Euler integral of the first kind, is a special function defined by<br /> &lt;math&gt;B(x,y)=\int_0^1 \! {t^{(x-1)}}{(1-t)^{(y-1)}}\,dt&lt;/math&gt;&lt;br&gt;<br /> <br /> <br /> &lt;math&gt;beta(2,1)= \frac{\Gamma(3)}{(\Gamma(2)\Gamma(1))}x^1 (1-x)^0 = 2x&lt;/math&gt;&lt;br&gt;<br /> <br /> &lt;hr&gt;<br /> &lt;math&gt;g=u(0,1)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;y=g&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;f(x)\leq c\cdot g(x)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;c\geq \frac{f(x)}{g(x)}&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;c = \max \frac{f(x)}{g(x)} &lt;/math&gt;&lt;br&gt;<br /> &lt;br&gt;&lt;math&gt;c = \max \frac{2x}{1}, 0 \leq x \leq 1&lt;/math&gt;&lt;br&gt;<br /> Taking x = 1 gives the highest possible c, which is c=2<br /> &lt;br /&gt;Note that c is a scalar greater than 1.<br /> &lt;br /&gt;cg(x) is proposal dist, and f(x) is target dist.<br /> <br /> [[File:Beta(2,1)_example.jpg|750x750px]]<br /> <br /> '''Note:''' g follows uniform distribution, it only covers half of the graph which runs from 0 to 1 on y-axis. Thus we need to multiply by c to ensure that &lt;math&gt;c\cdot g&lt;/math&gt; can cover entire f(x) area. In this case, c=2, so that makes g run from 0 to 2 on y-axis which covers f(x).<br /> <br /> '''Comment:'''&lt;br&gt; <br /> From the picture above, we could observe that the area under f(x)=2x is a half of the area under the pdf of UNIF(0,1). This is why in order to sample 1000 points of f(x), we need to sample approximately 2000 points in UNIF(0,1).<br /> And in general, if we want to sample n points from a distritubion with pdf f(x), we need to scan approximately &lt;math&gt;n\cdot c&lt;/math&gt; points from the proposal distribution (g(x)) in total. &lt;br&gt;<br /> &lt;b&gt;Step&lt;/b&gt;<br /> &lt;ol&gt;<br /> &lt;li&gt;Draw y~U(0,1)&lt;/li&gt;<br /> &lt;li&gt;Draw u~U(0,1)&lt;/li&gt;<br /> &lt;li&gt;if &lt;math&gt;u \leq \frac{(2\cdot y)}{(2\cdot 1)}, u \leq y,&lt;/math&gt; then &lt;math&gt; x=y&lt;/math&gt;&lt;br&gt;<br /> &lt;li&gt;Else go to Step 1&lt;/li&gt;<br /> &lt;/ol&gt;<br /> <br /> '''Note:''' In the above example, we sample 2 numbers. If second number (u) is less than or equal to first number (y), then accept x=y, if not then start all over.<br /> <br /> &lt;span style=&quot;font-weight:bold;color:green;&quot;&gt;Matlab Code&lt;/span&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;close all<br /> &gt;&gt;clear all<br /> &gt;&gt;ii=1; # ii:numbers that are accepted<br /> &gt;&gt;jj=1; # jj:numbers that are generated<br /> &gt;&gt;while ii&lt;1000<br /> y=rand;<br /> u=rand;<br /> jj=jj+1;<br /> if u&lt;=y<br /> x(ii)=y;<br /> ii=ii+1;<br /> end<br /> end<br /> &gt;&gt;hist(x) # It is a histogram<br /> &gt;&gt;jj<br /> jj = 2024 # should be around 2000<br /> &lt;/pre&gt;<br /> [[File:ARM_Example.jpg|300px]]<br /> <br /> :'''*Note:''' The reason that a for loop is not used is that we need to continue the looping until we get 1000 successful samples. We will reject some samples during the process and therefore do not know the number of y we are going to generate. <br /> <br /> :'''*Note2:''' In this example, we used c=2, which means we accept half of the points we generate on average. Generally speaking, 1/c would be the probability of acceptance, and an indicator of the efficiency of your chosen proposal distribution and algorithm. <br /> <br /> :'''*Note3:''' We use '''while''' instead of '''for''' when looping because we do not know how many iterations are required to generate 1000 successful samples. We can view this as a negative binomial distribution so while the expected number of iterations required is n * c, it will likely deviate from this amount. We expect 2000 in this case.<br /> <br /> :'''*Note4:''' If c=1, we will accept all points, which is the ideal situation. However, this is essentially impossible because if c = 1 then our distributions f(x) and g(x) must be identical, so we will have to be satisfied with as close to 1 as possible.<br /> <br /> '''Use Inverse Method for this Example'''&lt;br&gt;<br /> :&lt;math&gt;F(x)=\int_0^x \! 2s\,ds={x^2}-0={x^2}&lt;/math&gt;&lt;br&gt;<br /> :&lt;math&gt;y=x^2&lt;/math&gt;&lt;br&gt;<br /> :&lt;math&gt;x=\sqrt y&lt;/math&gt;<br /> :&lt;math&gt; F^{-1}\left (\, x \, \right) =\sqrt x&lt;/math&gt;<br /> <br /> :*'''Procedure'''<br /> :1: Draw &lt;math&gt; U~ \sim~ Unif [0,1] &lt;/math&gt;&lt;br&gt;<br /> :2: &lt;math&gt; x=F^{-1}\left (\, u\, \right) =\sqrt u&lt;/math&gt;<br /> <br /> &lt;span style=&quot;font-weight:bold;color:green;&quot;&gt;Matlab Code&lt;/span&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;u=rand(1,1000);<br /> &gt;&gt;x=u.^0.5;<br /> &gt;&gt;hist(x)<br /> &lt;/pre&gt;<br /> [[File:ARM(IFM)_Example.jpg|300px]]<br /> <br /> &lt;span style=&quot;font-weight:bold;colour:green;&quot;&gt;Matlab Tip:&lt;/span&gt;<br /> Periods, &quot;.&quot;,meaning &quot;element-wise&quot;, are used to describe the operation you want performed on each element of a vector. In the above example, to take the square root of every element in U, the notation U.^0.5 is used. However if you want to take the square root of the entire matrix U the period, &quot;.&quot; would be excluded. i.e. Let matrix B=U^0.5, then &lt;math&gt;B^T*B=U&lt;/math&gt;. For example if we have a two 1 X 3 matrices and we want to find out their product; using &quot;.&quot; in the code will give us their product. However, if we don't use &quot;.&quot;, it will just give us an error. For example, a =[1 2 3] b=[2 3 4] are vectors, a.*b=[2 6 12], but a*b does not work since the matrix dimensions must agree.<br /> <br /> '''<br /> '''Example for A-R method:'''<br /> <br /> Given &lt;math&gt; f(x)= \frac{3}{4} (1-x^2), -1 \leq x \leq 1 &lt;/math&gt;, use A-R method to generate random number<br /> <br /> <br /> [[Solution:]]<br /> <br /> Let g=U(-1,1) and g(x)=1/2<br /> <br /> let y ~ f, <br /> &lt;math&gt; cg(x)\geq f(x),<br /> c\frac{1}{2} \geq \frac{3}{4} (1-x^2) /1, <br /> c=max 2\cdot\frac{3}{4} (1-x^2) = 3/2 &lt;/math&gt;<br /> <br /> The process:<br /> <br /> :1: Draw U1 ~ U(0,1) &lt;br&gt;<br /> :2: Draw U2 ~ U(0,1) &lt;br&gt;<br /> :3: let &lt;math&gt; y = U1*2 - 1 &lt;/math&gt;<br /> :4: if &lt;math&gt;U2 \leq \frac { \frac{3}{4} * (1-y^2)} { \frac{3}{4}} = {1-y^2}&lt;/math&gt;, then x=y, '''note that''' (3/4(1-y^2)/(3/4) is getting from f(y) / (cg(y)) )<br /> :5: else: return to '''step 1''' <br /> <br /> ----<br /> <br /> <br /> =====Example of Acceptance-Rejection Method=====<br /> <br /> &lt;math&gt;\begin{align} <br /> &amp; f(x) = 3x^2, 0&lt;x&lt;1 \\<br /> \end{align}&lt;/math&gt;&lt;br\&gt;<br /> <br /> &lt;math&gt;\begin{align}<br /> &amp; g(x)=1, 0&lt;x&lt;1 \\<br /> \end{align}&lt;/math&gt;&lt;br\&gt;<br /> <br /> &lt;math&gt;c = \max \frac{f(x)}{g(x)} = \max \frac{3x^2}{1} = 3 &lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\frac{f(x)}{c \cdot g(x)} = x^2&lt;/math&gt;<br /> <br /> 1. Generate two uniform numbers in the unit interval &lt;math&gt;U_1, U_2 \sim~ U(0,1)&lt;/math&gt;&lt;br&gt;<br /> 2. If &lt;math&gt;U_2 \leqslant {U_1}^2&lt;/math&gt;, accept &lt;math&gt;\begin{align}U_1\end{align}&lt;/math&gt; as the random variable with pdf &lt;math&gt;\begin{align}f\end{align}&lt;/math&gt;, if not return to Step 1<br /> <br /> We can also use &lt;math&gt;\begin{align}g(x)=2x\end{align}&lt;/math&gt; for a more efficient algorithm<br /> <br /> &lt;math&gt;c = \max \frac{f(x)}{g(x)} = \max \frac {3x^2}{2x} = \frac {3x}{2} &lt;/math&gt;.<br /> Use the inverse method to sample from &lt;math&gt;\begin{align}g(x)\end{align}&lt;/math&gt;<br /> &lt;math&gt;\begin{align}G(x)=x^2\end{align}&lt;/math&gt;.<br /> Generate &lt;math&gt;\begin{align}U\end{align}&lt;/math&gt; from &lt;math&gt;\begin{align}U(0,1)\end{align}&lt;/math&gt; and set &lt;math&gt;\begin{align}x=sqrt(u)\end{align}&lt;/math&gt;<br /> <br /> 1. Generate two uniform numbers in the unit interval &lt;math&gt;U_1, U_2 \sim~ U(0,1)&lt;/math&gt;&lt;br&gt;<br /> 2. If &lt;math&gt;U_2 \leq \frac{3\sqrt{U_1}}{2}&lt;/math&gt;, accept &lt;math&gt;U_1&lt;/math&gt; as the random variable with pdf &lt;math&gt;f&lt;/math&gt;, if not return to Step 1<br /> <br /> *Note :the function &lt;math&gt;\begin{align}q(x) = c * g(x)\end{align}&lt;/math&gt; is called an envelop or majoring function.&lt;br&gt;<br /> To obtain a better proposing function &lt;math&gt;\begin{align}g(x)\end{align}&lt;/math&gt;, we can first assume a new &lt;math&gt;\begin{align}q(x)\end{align}&lt;/math&gt; and then solve for the normalizing constant by integrating.&lt;br&gt;<br /> In the previous example, we first assume &lt;math&gt;\begin{align}q(x) = 3x\end{align}&lt;/math&gt;. To find the normalizing constant, we need to solve &lt;math&gt;k *\sum 3x = 1&lt;/math&gt; which gives us k = 2/3. So,&lt;math&gt;\begin{align}g(x) = k*q(x) = 2x\end{align}&lt;/math&gt;.<br /> <br /> <br /> '''Possible Limitations'''<br /> <br /> -This method could be computationally inefficient depending on the rejection rate. We may have to sample many points before&lt;br&gt; <br /> we get the 1000 accepted points. In the example we did in class relating the &lt;math&gt;f(x)=2x&lt;/math&gt;, &lt;br&gt;<br /> we had to sample around 2070 points before we finally accepted 1000 sample points.&lt;br&gt;<br /> -If the form of the proposal distribution, g, is very different from target distribution, f, then c is very large and the algorithm is not computationally efficient.&lt;br&gt;<br /> <br /> '''Acceptance - Rejection Method Application on Normal Distribution''' &lt;br&gt;<br /> <br /> &lt;math&gt;X \sim∼ N(\mu,\sigma^2), \text{ or } X = \sigma Z + \mu, Z \sim~ N(0,1) &lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\vert Z \vert&lt;/math&gt; has probability density function of &lt;br&gt;<br /> <br /> f(x) = (2/&lt;math&gt;\sqrt{2\pi}&lt;/math&gt;) e&lt;sup&gt;-x&lt;sup&gt;2&lt;/sup&gt;/2&lt;/sup&gt;<br /> <br /> g(x) = e&lt;sup&gt;-x&lt;/sup&gt;<br /> <br /> Take h(x) = f(x)/g(x) and solve for h'(x) = 0 to find x so that h(x) is maximum. <br /> <br /> Hence x=1 maximizes h(x) =&gt; c = &lt;math&gt;\sqrt{2e/\pi}&lt;/math&gt;<br /> <br /> Thus f(y)/cg(y) = e&lt;sup&gt;-(y-1)&lt;sup&gt;2&lt;/sup&gt;/2&lt;/sup&gt;<br /> <br /> <br /> learn how to use code to calculate the c between f(x) and g(x).<br /> <br /> &lt;p style=&quot;font-weight:bold;text-size:20px;&quot;&gt;How to transform &lt;math&gt;U(0,1)&lt;/math&gt; to &lt;math&gt;U(a, b)&lt;/math&gt;&lt;/p&gt;<br /> <br /> 1. Draw U from &lt;math&gt;U(0,1)&lt;/math&gt;<br /> <br /> 2. Take &lt;math&gt;Y=(b-a)U+a&lt;/math&gt;<br /> <br /> 3. Now Y follows &lt;math&gt;U(a,b)&lt;/math&gt;<br /> <br /> '''Example''': Generate a random variable z from the Semicircular density &lt;math&gt;f(x)= \frac{2}{\pi R^2} \sqrt{R^2-x^2}, -R\leq x\leq R&lt;/math&gt;.<br /> <br /> -&gt; Proposal distribution: UNIF(-R, R)<br /> <br /> -&gt; We know how to generate using &lt;math&gt; U \sim UNIF (0,1) &lt;/math&gt; Let &lt;math&gt; Y= 2RU-R=R(2U-1)&lt;/math&gt;, therefore Y follows &lt;math&gt;U(-R,R)&lt;/math&gt;<br /> <br /> -&gt; In order to maximize the function we must maximize the top and minimize the bottom.<br /> <br /> Now, we need to find c:<br /> Since c=max[f(x)/g(x)], where &lt;br /&gt;<br /> &lt;math&gt;f(x)= \frac{2}{\pi R^2} \sqrt{R^2-x^2}&lt;/math&gt;, &lt;math&gt;g(x)=\frac{1}{2R}&lt;/math&gt;, &lt;math&gt;-R\leq x\leq R&lt;/math&gt;&lt;br /&gt;<br /> Thus, we have to maximize R^2-x^2.<br /> =&gt; When x=0, it will be maximized.<br /> Therefore, c=4/pi. * Note: This also means that the probability of accepting a point is &lt;math&gt;\pi/4&lt;/math&gt;.<br /> <br /> We will accept the points with limit f(x)/[cg(x)].<br /> Since &lt;math&gt;\frac{f(y)}{cg(y)}=\frac{\frac{2}{\pi R^{2}} \sqrt{R^{2}-y^{2}}}{\frac{4}{\pi} \frac{1}{2R}}=\frac{\frac{2}{\pi R^{2}} \sqrt{R^{2}-R^{2}(2U-1)^{2}}}{\frac{2}{\pi R}}&lt;/math&gt; <br /> <br /> * Note: Y= R(2U-1)<br /> We can also get Y= R(2U-1) by using the formula y = a+(b-a)*u, to transform U~(0,1) to U~(a,b). Letting a=-R and b=R, and substituting it in the formula y = a+(b-a)*u, we get Y= R(2U-1).<br /> <br /> Thus, &lt;math&gt;\frac{f(y)}{cg(y)}=\sqrt{1-(2U-1)^{2}}&lt;/math&gt; * this also means the probability we can accept points<br /> <br /> The algorithm to generate random variable x is then:<br /> <br /> 1. Draw &lt;Math&gt;\ U&lt;/math&gt; from &lt;math&gt;\ U(0,1)&lt;/math&gt;<br /> <br /> 2. Draw &lt;Math&gt;\ U_{1}&lt;/math&gt; from &lt;math&gt;\ U(0,1)&lt;/math&gt;<br /> <br /> 3. If &lt;math&gt;U_{1} \leq \sqrt{1-(2U-1)^2}, set x = U_{1}&lt;/math&gt;<br /> else return to step 1.<br /> <br /> <br /> <br /> The condition is &lt;br /&gt;<br /> &lt;Math&gt; U_{1} \leq \sqrt{(1-(2U-1)^2)}&lt;/Math&gt;&lt;br&gt;<br /> &lt;Math&gt;\ U_{1}^2 \leq 1 - (2U -1)^2&lt;/Math&gt;&lt;br&gt;<br /> &lt;Math&gt;\ U_{1}^2 - 1 \leq -(2U - 1)^2&lt;/Math&gt;&lt;br&gt;<br /> &lt;Math&gt;\ 1 - U_{1}^2 \geq (2U - 1)^2&lt;/Math&gt;<br /> <br /> <br /> <br /> <br /> '''One more example about AR method''' &lt;br/&gt;<br /> (In this example, we will see how to determine the value of c when c is a function with unknown parameters instead of a value)<br /> Let &lt;math&gt;f(x)=x×e^{-x}, x &gt; 0 &lt;/math&gt; &lt;br/&gt;<br /> Use &lt;math&gt;g(x)=a×e^{-a×x}&lt;/math&gt; to generate random variable &lt;br/&gt;<br /> &lt;br/&gt;<br /> Solution: First of all, we need to find c&lt;br/&gt;<br /> &lt;math&gt;cg(x)&gt;=f(x)&lt;/math&gt; &lt;br/&gt;<br /> &lt;math&gt;c&gt;=\frac{f(x)}{g(x)}&lt;/math&gt; &lt;br/&gt;<br /> &lt;math&gt;\frac{f(x)}{g(x)}=\frac{x}{a} * e^{-(1-a)x}&lt;/math&gt; &lt;br/&gt;<br /> take derivative with respect to x, and set it to 0 to get the maximum, &lt;br/&gt;<br /> &lt;math&gt;\frac{1}{a} * e^{-(1-a)x} - \frac{x}{a} * e^{-(1-a)x} * (1-a) = 0 &lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;x=\frac {1}{1-a}&lt;/math&gt; &lt;br/&gt;<br /> <br /> &lt;math&gt;\frac {f(x)}{g(x)} = \frac {e^{-1}}{a*(1-a)} &lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;\frac {f(0)}{g(0)} = 0&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;\frac {f(\infty)}{g(\infty)} = 0&lt;/math&gt;&lt;br/&gt;<br /> &lt;br/&gt;<br /> therefore, &lt;b&gt;&lt;math&gt;c= \frac {e^{-1}}{a*(1-a)}&lt;/math&gt;&lt;/b&gt;&lt;br/&gt;<br /> &lt;br/&gt;<br /> &lt;b&gt;In order to minimize c, we need to find the appropriate a&lt;/b&gt; &lt;br/&gt;<br /> Take derivative with respect to a and set it to be zero, &lt;br/&gt;<br /> We could get &lt;math&gt;a= \frac {1}{2}&lt;/math&gt; &lt;br/&gt;<br /> &lt;b&gt;&lt;math&gt;c=\frac{4}{e}&lt;/math&gt;&lt;/b&gt;<br /> &lt;br/&gt;<br /> Procedure: &lt;br/&gt;<br /> 1. Generate u v ~unif(0,1) &lt;br/&gt;<br /> 2. Generate y from g, since g is exponential with rate 2, let y=-0.5*ln(u) &lt;br/&gt;<br /> 3. If &lt;math&gt;v&lt;\frac{f(y)}{c\cdot g(y)}&lt;/math&gt;, output y&lt;br/&gt;<br /> Else, go to 1&lt;br/&gt;<br /> <br /> Acknowledgements: The example above is from Stat 340 Winter 2013 notes.<br /> <br /> '''Summary of how to find the value of c''' &lt;br/&gt;<br /> Let &lt;math&gt;h(x) = \frac {f(x)}{g(x)}&lt;/math&gt;, and then we have the following:&lt;br /&gt;<br /> 1. First, take derivative of h(x) with respect to x, get x&lt;sub&gt;1&lt;/sub&gt;;&lt;br /&gt;<br /> 2. Plug x&lt;sub&gt;1&lt;/sub&gt; into h(x) and get the value(or a function) of c, denote as c&lt;sub&gt;1&lt;/sub&gt;;&lt;br /&gt;<br /> 3. Check the endpoints of x and sub the endpoints into h(x);&lt;br /&gt;<br /> 4. (if c&lt;sub&gt;1&lt;/sub&gt; is a value, then we can ignore this step) Since we want the smallest value of c such that &lt;math&gt;f(x) \leq c\cdot g(x)&lt;/math&gt; for all x, we want the unknown parameter that minimizes c. &lt;br /&gt;So we take derivative of c&lt;sub&gt;1&lt;/sub&gt; with respect to the unknown parameter (ie k=unknown parameter) to get the value of k. &lt;br /&gt;Then we submit k to get the value of c&lt;sub&gt;1&lt;/sub&gt;. (Double check that &lt;math&gt;c_1 \geq 1&lt;/math&gt;&lt;br /&gt;<br /> 5. Pick the maximum value of h(x) to be the value of c.&lt;br /&gt;<br /> <br /> For the two examples above, we need to generate the probability function to uniform distribution,<br /> and figure out &lt;math&gt;c=max\frac {f(y)}{g(y)} &lt;/math&gt;.<br /> If &lt;math&gt;v&lt;\frac {f(y)}{c\cdot g(y)}&lt;/math&gt;, output y.<br /> <br /> <br /> '''Summary of when to use the Accept Rejection Method''' &lt;br/&gt;<br /> 1) When the calculation of inverse cdf cannot to be computed or is too difficult to compute. &lt;br/&gt;<br /> 2) When f(x) can be evaluated to at least one of the normalizing constant. &lt;br/&gt;<br /> 3) A constant c where &lt;math&gt;f(x)\leq c\cdot g(x)&lt;/math&gt;&lt;br/&gt;<br /> 4) A uniform draw&lt;br/&gt;<br /> <br /> ==== Interpretation of 'C' ====<br /> We can use the value of c to calculate the acceptance rate by &lt;math&gt;\tfrac{1}{c}&lt;/math&gt;.<br /> <br /> For instance, assume c=1.5, then we can tell that 66.7% of the points will be accepted (&lt;math&gt;\tfrac{1}{1.5} = 0.667&lt;/math&gt;). We can also call the efficiency of the method is 66.7%.<br /> <br /> Likewise, if the minimum value of possible values for C is &lt;math&gt;\tfrac{4}{3}&lt;/math&gt;, &lt;math&gt;1/ \tfrac{4}{3}&lt;/math&gt; of the generated random variables will be accepted. Thus the efficient of the algorithm is 75%.<br /> <br /> In order to ensure the algorithm is as efficient as possible, the 'C' value should be as close to one as possible, such that &lt;math&gt;\tfrac{1}{c}&lt;/math&gt; approaches 1 =&gt; 100% acceptance rate.<br /> <br /> <br /> &gt;&gt; close All<br /> &gt;&gt; clear All<br /> &gt;&gt; i=1<br /> &gt;&gt; j=0;<br /> &gt;&gt; while ii&lt;1000<br /> y=rand<br /> u=rand<br /> if u&lt;=y;<br /> x(ii)=y<br /> ii=ii+1<br /> end<br /> end<br /> <br /> == Class 5 - Tuesday, May 21 ==<br /> Recall the example in the last lecture. The following code will generate a random variable required by the question.<br /> <br /> * '''Code'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;close all<br /> &gt;&gt;clear all<br /> &gt;&gt;ii=1;<br /> &gt;&gt;R=1; #Note: that R is a constant in which we can change <br /> i.e. if we changed R=4 then we would have a density between -4 and 4<br /> &gt;&gt;while ii&lt;1000<br /> u1 = rand;<br /> u2 = rand;<br /> y = R*(2*u2-1);<br /> if (1-u1^2)&gt;=(2*u2-1)^2<br /> x(ii) = y;<br /> ii = ii + 1; #Note: for beginner programmers that this step increases <br /> the ii value for next time through the while loop<br /> end<br /> end<br /> &gt;&gt;hist(x,20) # 20 is the number of bars<br /> <br /> &gt;&gt;hist(x,30) #30 is the number of bars<br /> &lt;/pre&gt;<br /> <br /> calculate process:<br /> &lt;math&gt;u_{1} &lt;= \sqrt (1-(2u-1)^2) &lt;/math&gt; &lt;br&gt; <br /> &lt;math&gt;(u_{1})^2 &lt;=(1-(2u-1)^2) &lt;/math&gt; &lt;br&gt; <br /> &lt;math&gt;(u_{1})^2 -1 &lt;=(-(2u-1)^2) &lt;/math&gt; &lt;br&gt; <br /> &lt;math&gt;1-(u_{1})^2 &gt;=((2u-1)^2-1) &lt;/math&gt; &lt;br&gt; <br /> <br /> <br /> MATLAB tips: hist(x,y) plots a histogram of variable x, where y is the number of bars in the graph.<br /> <br /> [[File:ARM_cont_example.jpg|300px]]<br /> <br /> === Discrete Examples ===<br /> * '''Example 1''' &lt;br&gt;<br /> Generate random variable &lt;math&gt;X&lt;/math&gt; according to p.m.f&lt;br/&gt;<br /> &lt;math&gt;\begin{align}<br /> P(x &amp;=1) &amp;&amp;=0.15 \\<br /> P(x &amp;=2) &amp;&amp;=0.25 \\<br /> P(x &amp;=3) &amp;&amp;=0.3 \\<br /> P(x &amp;=4) &amp;&amp;=0.1 \\<br /> P(x &amp;=5) &amp;&amp;=0.2 \\<br /> \end{align}&lt;/math&gt;&lt;br/&gt;<br /> <br /> The discrete case is analogous to the continuous case. Suppose we want to generate an X that is a discrete random variable with pmf f(x)=P(X=x). Suppose also that we use the discrete uniform distribution as our target distribution, then &lt;math&gt; g(x)= P(X=x) =0.2 &lt;/math&gt; for all X.<br /> <br /> The following algorithm then yields our X:<br /> <br /> Step 1 Draw discrete uniform distribution of 1, 2, 3, 4 and 5, &lt;math&gt;Y \sim~ g&lt;/math&gt;.&lt;br/&gt;<br /> Step 2 Draw &lt;math&gt;U \sim~ U(0,1)&lt;/math&gt;.&lt;br/&gt;<br /> Step 3 If &lt;math&gt;U \leq \frac{f(Y)}{c \cdot g(Y)}&lt;/math&gt;, then &lt;b&gt; X = Y &lt;/b&gt;;&lt;br/&gt;<br /> Else return to Step 1.&lt;br/&gt;<br /> <br /> C can be found by maximizing the ratio :&lt;math&gt; \frac{f(x)}{g(x)} &lt;/math&gt;. To do this, we want to maximize &lt;math&gt; f(x) &lt;/math&gt; and minimize &lt;math&gt; g(x) &lt;/math&gt;. &lt;br&gt;<br /> :&lt;math&gt;c = max \frac{f(x)}{g(x)} = \frac {0.3}{0.2} = 1.5 &lt;/math&gt; &lt;br/&gt;<br /> Note: In this case &lt;math&gt;f(x)=P(X=x)=0.3&lt;/math&gt; (highest probability from the discrete probabilities in the question)<br /> :&lt;math&gt;\frac{p(x)}{cg(x)} = \frac{p(x)}{1.5*0.2} = \frac{p(x)}{0.3} &lt;/math&gt;&lt;br&gt;<br /> Note: The U is independent from y in Step 2 and 3 above.<br /> ~The constant c is a indicator of rejection rate or efficiency of the algorithm. It can represent the average number of trials of the algorithm. Thus, a higher c would mean that the algorithm is comparatively inefficient.<br /> <br /> the acceptance-rejection method of pmf, the uniform probability is the same for all variables, and there are 5 parameters(1,2,3,4,5), so g(x) is 0.2<br /> <br /> Remember that we always want to choose &lt;math&gt; cg &lt;/math&gt; to be equal to or greater than &lt;math&gt; f &lt;/math&gt;, but as close as possible.<br /> &lt;br /&gt;limitations: If the form of the proposal dist g is very different from target dist f, then c is very large and the algorithm is not computatively efficient.<br /> <br /> * '''Code for example 1'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;close all<br /> &gt;&gt;clear all<br /> &gt;&gt;p=[.15 .25 .3 .1 .2]; %This a vector holding the values<br /> &gt;&gt;ii=1;<br /> &gt;&gt;while ii &lt; 1000<br /> y=unidrnd(5); %generates random numbers for the discrete uniform <br /> u=rand; distribution with maximum 5.<br /> if u&lt;= p(y)/0.3<br /> x(ii)=y;<br /> ii=ii+1;<br /> end<br /> end<br /> &gt;&gt;hist(x)<br /> &lt;/pre&gt;<br /> [[File:ARM_disc_example.jpg|300px]]<br /> <br /> unidrnd(k) draws from the discrete uniform distribution of integers &lt;math&gt;1,2,3,...,k&lt;/math&gt; If this function is not built in to your MATLAB then we can do simple transformation on the rand(k) function to make it work like the unidrnd(k) function. <br /> <br /> The acceptance rate is &lt;math&gt;\frac {1}{c}&lt;/math&gt;, so the lower the c, the more efficient the algorithm. Theoretically, c equals 1 is the best case because all samples would be accepted; however it would only be true when the proposal and target distributions are exactly the same, which would never happen in practice. <br /> <br /> For example, if c = 1.5, the acceptance rate would be &lt;math&gt;\frac {1}{1.5}=\frac {2}{3}&lt;/math&gt;. Thus, in order to generate 1000 random values, on average, a total of 1500 iterations would be required. <br /> <br /> A histogram to show 1000 random values of f(x), more random value make the probability close to the express probability value.<br /> <br /> <br /> * '''Example 2'''&lt;br&gt;<br /> p(x=1)=0.1&lt;br /&gt;p(x=2)=0.3&lt;br /&gt;p(x=3)=0.6&lt;br /&gt;<br /> Let g be the uniform distribution of 1, 2, or 3&lt;br /&gt;<br /> g(x)= 1/3&lt;br /&gt;<br /> &lt;math&gt;c=max(\tfrac{p_{x}}{g(x)})=0.6/(\tfrac{1}{3})=1.8&lt;/math&gt;&lt;br /&gt;<br /> Hence &lt;math&gt;\tfrac{p(x)}{cg(x)} = p(x)/(1.8 (\tfrac{1}{3}))= \tfrac{p(x)}{0.6}&lt;/math&gt;<br /> <br /> 1,y~g&lt;br /&gt;<br /> 2,u~U(0,1)&lt;br /&gt;<br /> 3, If &lt;math&gt;U \leq \frac{f(y)}{cg(y)}&lt;/math&gt;, set x = y. Else go to 1.<br /> <br /> * '''Code for example 2'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;close all<br /> &gt;&gt;clear all<br /> &gt;&gt;p=[.1 .3 .6]; %This a vector holding the values <br /> &gt;&gt;ii=1;<br /> &gt;&gt;while ii &lt; 1000<br /> y=unidrnd(3); %generates random numbers for the discrete uniform distribution with maximum 3<br /> u=rand; <br /> if u&lt;= p(y)/0.6<br /> x(ii)=y; <br /> ii=ii+1; %else ii=ii+1<br /> end<br /> end<br /> &gt;&gt;hist(x)<br /> &lt;/pre&gt;<br /> <br /> <br /> * '''Example 3'''&lt;br&gt;<br /> <br /> Suppose &lt;math&gt;\begin{align}p_{x} = e^{-3}3^{x}/x! , x\geq 0\end{align}&lt;/math&gt; (Poisson distribution)<br /> <br /> '''First:''' Try the first few &lt;math&gt;\begin{align}p_{x}'s\end{align}&lt;/math&gt;: 0.0498, 0.149, 0.224, 0.224, 0.168, 0.101, 0.0504, 0.0216, 0.0081, 0.0027 for &lt;math&gt;\begin{align} x = 0,1,2,3,4,5,6,7,8,9 \end{align}&lt;/math&gt;&lt;br&gt;<br /> <br /> '''Proposed distribution:''' Use the geometric distribution for &lt;math&gt;\begin{align}g(x)\end{align}&lt;/math&gt;;&lt;br&gt;<br /> <br /> &lt;math&gt;\begin{align}g(x)=p(1-p)^{x}\end{align}&lt;/math&gt;, choose &lt;math&gt;\begin{align}p=0.25\end{align}&lt;/math&gt;&lt;br&gt;<br /> <br /> Look at &lt;math&gt;\begin{align}p_{x}/g(x)\end{align}&lt;/math&gt; for the first few numbers: 0.199 0.797 1.59 2.12 2.12 1.70 1.13 0.647 0.324 0.144 for &lt;math&gt;\begin{align} x = 0,1,2,3,4,5,6,7,8,9 \end{align}&lt;/math&gt;&lt;br&gt;<br /> <br /> We want &lt;math&gt;\begin{align}c=max(p_{x}/g(x))\end{align}&lt;/math&gt; which is approximately 2.12&lt;br&gt;<br /> <br /> '''The general procedures to generate &lt;math&gt;\begin{align}p(x)\end{align}&lt;/math&gt; is as follows:'''<br /> <br /> 1. Generate &lt;math&gt;\begin{align}U_{1} \sim~ U(0,1); U_{2} \sim~ U(0,1)\end{align}&lt;/math&gt;&lt;br&gt;<br /> <br /> 2. &lt;math&gt;\begin{align}j = \lfloor \frac{ln(U_{1})}{ln(.75)} \rfloor+1;\end{align}&lt;/math&gt;&lt;br&gt;<br /> <br /> 3. if &lt;math&gt;U_{2} &lt; \frac{p_{j}}{cg(j)}&lt;/math&gt;, set &lt;math&gt;\begin{align}X = x_{j}\end{align}&lt;/math&gt;, else go to step 1.<br /> <br /> Note: In this case, &lt;math&gt;\begin{align}f(x)/g(x)\end{align}&lt;/math&gt; is extremely difficult to differentiate so we were required to test points. If the function is very easy to differentiate, we can calculate the max as if it were a continuous function then check the two surrounding points for which is the highest discrete value.<br /> <br /> *'''Example 4''' (Hypergeometric &amp; Binomial)&lt;br&gt; <br /> <br /> Suppose we are given f(x) such that it is hypergeometically distributed, given 10 white balls, 5 red balls, and select 3 balls, let X be the number of red ball selected, without replacement. <br /> <br /> Choose g(x) such that it is binomial distribution, Bin(3, 1/3). Find the rejection constant, c<br /> <br /> Solution:<br /> For hypergeometric: &lt;math&gt;P(X=0) =\binom{10}{3}/\binom{15}{3} =0.2637, P(x=1)=\binom{10}{2} * \binom{5}{1} /\binom{15}{3}=0.4945, P(X=2)=\binom{10}{1} * \binom{5}{2} /\binom{15}{3}=0.2198,&lt;/math&gt;&lt;br&gt;&lt;br&gt;<br /> &lt;math&gt;P(X=3)=\binom{5}{3}/\binom{15}{3}= 0.02198&lt;/math&gt;<br /> <br /> <br /> For Binomial g(x): P(X=0) = (2/3)^3=0.2963; P(X=1)= 3*(1/3)*(2/3)^2 = 0.4444, P(X=2)=3*(1/3)^2*(2/3)=0.2222, P(X=3)=(1/3)^3=0.03704<br /> <br /> Find the value of f/g for each X<br /> <br /> X=0: 0.8898; <br /> X=1: 1.1127; <br /> X=2: 0.9891; <br /> X=3: 0.5934<br /> <br /> Choose the maximum which is [[c=1.1127]]<br /> <br /> Looking for the max f(x) is 0.4945 and the max g(x) is 0.4444, so we can calculate the max c is 1.1127.<br /> But for the graph, this c is not the best because it does not cover all the point of f(x), so we need to move the c*g(x) graph to cover all f(x), and decreasing the rejection ratio.<br /> <br /> Limitation: If the shape of the proposed distribution g is very different from the target distribution f, then the rejection rate will be high (High c value). Computationally, the algorithm is always right; however it is inefficient and requires many iterations. &lt;br&gt;<br /> Here is an example: <br /> [[File:ARM_Fail.jpg]]<br /> <br /> In the above example, we need to move c*g(x) to the peak of f to cover the whole f. Thus c will be very large and 1/c will be small.<br /> The higher the rejection rate, more points will be rejected.&lt;br&gt; <br /> More on rejection/acceptance rate: 1/c is the acceptance rate. As c decreases (note: the minimum value of c is 1), the acceptance rate increases. In our last example, 1/c=1/1.5≈66.67%. Around 67% of points generated will be accepted.&lt;br&gt;<br /> &lt;div style=&quot;margin-bottom:10px;border:10px solid red;background: yellow&quot;&gt; the example below provides a better understanding about the pros and cons of the AR method. The AR method is useless when dealing with sampling distribution with a higher peak since c will be large, hence making our algorithm inefficient&lt;br&gt;<br /> which brings the acceptance rate low which leads to very time consuming sampling &lt;/div&gt;<br /> &lt;div style=&quot;border:1px solid #cccccc;border-radius:10px;box-shadow: 0 5px 15px 1px rgba(0, 0, 0, 0.6), 0 0 200px 1px rgba(255, 255, 255, 0.5);padding:20px;margin:20px;background:#FFFFAD;&quot;&gt;<br /> &lt;h2 style=&quot;text-align:center;&quot;&gt;Acceptance-Rejection Method&lt;/h2&gt;<br /> &lt;p&gt;&lt;b&gt;Problem:&lt;/b&gt; The CDF is not invertible or it is difficult to find the inverse.&lt;/p&gt;<br /> &lt;p&gt;&lt;b&gt;Plan:&lt;/b&gt;&lt;/p&gt;<br /> &lt;ol&gt;<br /> &lt;li&gt;Draw y~g(.)&lt;/li&gt;<br /> &lt;li&gt;Draw u~Unif(0,1)&lt;/li&gt;<br /> &lt;li&gt;If &lt;math&gt;u\leq \frac{f(y)}{cg(y)}&lt;/math&gt;then set x=y. Else return to Step 1&lt;/li&gt;<br /> &lt;/ol&gt;<br /> &lt;p&gt;x will have the desired distribution.&lt;/p&gt;<br /> &lt;b&gt;Matlab Example&lt;/b&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;close all<br /> clear all<br /> ii=1;<br /> R=1;<br /> while ii&amp;lt;1000<br /> u1 = rand;<br /> u2 = rand;<br /> y = R*(2*u2-1);<br /> if (1-u1^2)&amp;gt;=(2*u2-1)^2<br /> x(ii) = y;<br /> ii = ii + 1;<br /> end<br /> end<br /> hist(x,20)<br /> &lt;/pre&gt;<br /> &lt;/div&gt;<br /> <br /> <br /> Recall that,<br /> Suppose we have an efficient method for simulating a random variable having probability mass function {q(j),j&gt;=0}. We can use this as the basis for simulating from the distribution having mass function {p(j),j&gt;=0} by first simulating a random variable Y having mass function {q(j)} and then accepting this simulated value with a probability proportional to p(Y)/q(Y).<br /> Specifically, let c be a constant such that <br /> p(j)/q(j)&lt;=c for all j such that p(j)&gt;0<br /> We now have the following technique, called the acceptance-rejection method, for simulating a random variable X having mass function p(j)=P{X=j}.<br /> <br /> === Sampling from commonly used distributions ===<br /> <br /> Please note that this is not a general technique as is that of acceptance-rejection sampling. Later, we will generalize the distributions for multidimensional purposes.<br /> <br /> * '''Gamma'''&lt;br /&gt;<br /> <br /> The CDF of the Gamma distribution &lt;math&gt;Gamma(t,\lambda)&lt;/math&gt; is(t denotes the shape, &lt;math&gt;\lambda&lt;/math&gt; denotes the scale: &lt;br&gt;<br /> &lt;math&gt; F(x) = \int_0^{x} \frac{e^{-y}y^{t-1}}{(t-1)!} \mathrm{d}y, \; \forall x \in (0,+\infty)&lt;/math&gt;, where &lt;math&gt;t \in \N^+ \text{ and } \lambda \in (0,+\infty)&lt;/math&gt;.&lt;br&gt;<br /> <br /> The gamma distribution is often used to model waiting times between a certain number of events. It can also be expressed as the sum of infinitely many independent and identically distributed exponential distributions. This distribution has two parameters: the number of exponential terms n, and the rate parameter &lt;math&gt;\lambda&lt;/math&gt;. In this distribution there is the Gamma function, &lt;math&gt;\Gamma &lt;/math&gt; which has some very useful properties. &quot;Source: STAT 340 Spring 2010 Course Notes&quot; &lt;br/&gt;<br /> <br /> Neither Inverse Transformation nor Acceptance-Rejection Method can be easily applied to Gamma distribution.<br /> However, we can use additive property of Gamma distribution to generate random variables.<br /> <br /> * '''Additive Property'''&lt;br /&gt;<br /> If &lt;math&gt;X_1, \dots, X_t&lt;/math&gt; are independent exponential distributions with hazard rate &lt;math&gt; \lambda &lt;/math&gt; (in other words, &lt;math&gt; X_i\sim~ Exp (\lambda) &lt;/math&gt;&lt;math&gt;, Exp (\lambda)= Gamma (1, \lambda)), then \Sigma_{i=1}^t X_i \sim~ Gamma (t, \lambda) &lt;/math&gt;<br /> <br /> <br /> Side notes: if &lt;math&gt; X_i\sim~ Gamma(a,\lambda)&lt;/math&gt; and &lt;math&gt; Y_i\sim~ Gamma(B,\lambda)&lt;/math&gt; are independent gamma distributions, then &lt;math&gt;\frac{X}{X+Y}&lt;/math&gt; has a distribution of &lt;math&gt; Beta(a,B). &lt;/math&gt;<br /> <br /> <br /> If we want to sample from the Gamma distribution, we can consider sampling from &lt;math&gt;t&lt;/math&gt; independent exponential distributions using the Inverse Method for each &lt;math&gt; X_i&lt;/math&gt; and add them up. Note that this only works the specific set of gamma distributions where t is a positive integer.<br /> <br /> According to this property, a random variable that follows Gamma distribution is the sum of i.i.d (independent and identically distributed) exponential random variables. Now we want to generate 1000 values of &lt;math&gt;Gamma(20,10)&lt;/math&gt; random variables, so we need to obtain the value of each one by adding 20 values of &lt;math&gt;X_i \sim~ Exp(10)&lt;/math&gt;. To achieve this, we generate a 20-by-1000 matrix whose entries follow &lt;math&gt;Exp(10)&lt;/math&gt; and add the rows together.&lt;br /&gt;<br /> &lt;math&gt; x_1 \sim~Exp(\lambda)&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;x_2 \sim~Exp(\lambda)&lt;/math&gt;&lt;br /&gt;<br /> ...&lt;br /&gt;<br /> &lt;math&gt;x_t \sim~Exp(\lambda)&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;x_1+x_2+...+x_t~&lt;/math&gt;<br /> <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;l=1<br /> &gt;&gt;u-rand(1,1000);<br /> &gt;&gt;x=-(1/l)*log(u); <br /> &gt;&gt;hist(x)<br /> &gt;&gt;rand<br /> &lt;/pre&gt;<br /> <br /> <br /> * '''Procedure '''<br /> <br /> :#Sample independently from a uniform distribution &lt;math&gt;t&lt;/math&gt; times, giving &lt;math&gt; U_1,\dots,U_t \sim~ U(0,1)&lt;/math&gt; <br /> :#Use the Inverse Transform Method, &lt;math&gt; X_i = -\frac {1}{\lambda}\log(1-U_i)&lt;/math&gt;, giving &lt;math&gt; X_1,\dots,X_t \sim~Exp(\lambda)&lt;/math&gt;<br /> :#Use the additive property,&lt;math&gt; X = \Sigma_{i=1}^t X_i \sim~ Gamma (t, \lambda) &lt;/math&gt;&lt;br&gt;<br /> <br /> <br /> * '''Note for Procedure '''<br /> :#If &lt;math&gt;U\sim~U(0,1)&lt;/math&gt;, then &lt;math&gt;U&lt;/math&gt; and &lt;math&gt;1-U&lt;/math&gt; will have the same distribution (both follows &lt;math&gt;U(0,1)&lt;/math&gt;)<br /> :#This is because the range for &lt;math&gt;1-U&lt;/math&gt; is still &lt;math&gt;(0,1)&lt;/math&gt;, and their densities are identical over this range.<br /> :#Let &lt;math&gt;Y=1-U&lt;/math&gt;, &lt;math&gt;Pr(Y&lt;=y)=Pr(1-U&lt;=y)=Pr(U&gt;=1-y)=1-Pr(U&lt;=1-y)=1-(1-y)=y&lt;/math&gt;, thus &lt;math&gt;1-U\sim~U(0,1)&lt;/math&gt;<br /> <br /> <br /> <br /> * '''Code'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;close all<br /> &gt;&gt;clear all<br /> &gt;&gt;lambda = 1;<br /> &gt;&gt;u = rand(20, 1000); Note: this command generate a 20x1000 matrix <br /> (which means we generate 1000 number for each X_i with t=20); <br /> all the elements are generated by rand<br /> &gt;&gt;x = (-1/lambda)*log(1-u); Note: log(1-u) is essentially the same as log(u) only if u~U(0,1) <br /> &gt;&gt;xx = sum(x) Note: sum(x) will sum all elements in the same column. <br /> size(xx) can help you to verify<br /> &gt;&gt;size(sum(x)) Note: see the size of x if we forget it<br /> (the answer is 20 1000)<br /> &gt;&gt;hist(x(1:)) Note: the graph of the first exponential distribution <br /> &gt;&gt;hist(xx)<br /> &lt;/pre&gt;<br /> [[File:Gamma_example.jpg|300px]]<br /> <br /> <br /> <br /> size(x) and size(u) are both 20*1000 matrix.<br /> Since if u~unif(0, 1), u and 1 - u have the same distribution, we can substitute 1-u with u to simply the equation.<br /> Alternatively, the following command will do the same thing with the previous commands.<br /> <br /> * '''Code'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;close all<br /> &gt;&gt;clear all<br /> &gt;&gt;lambda = 1;<br /> &gt;&gt;xx = sum((-1/lambda)*log(rand(20, 1000))); ''This is simple way to put the code in one line. <br /> Here we can use either log(u) or log(1-u) since U~U(0,1);<br /> &gt;&gt;hist(xx)<br /> &lt;/pre&gt;<br /> <br /> In the matrix rand(20,1000) means 20 row with 1000 numbers for each.<br /> use the code to show the generalize the distributions for multidimensional purposes in different cases, such as sum xi (each xi not equal xj), and they are independent, or matrix. Finally, we can see the conclusion is shown by the histogram.<br /> <br /> === Other Sampling Method: Box Muller ===<br /> [[File:Unnamed_QQ_Screenshot20130521203625.png‎]]<br /> * From cartesian to polar coordinates &lt;br /&gt;<br /> &lt;math&gt; R=\sqrt{x_{1}^2+x_{2}^2}= x_{2}/sin(\theta)= x_{1}/cos(\theta)&lt;/math&gt; &lt;br /&gt;<br /> &lt;math&gt; tan(\theta)=x_{2}/x_{1} \rightarrow \theta=tan^{-1}(x_{2}/x_{1})&lt;/math&gt; &lt;br /&gt;<br /> <br /> *Box-Muller Transformation:&lt;br&gt;<br /> It is a transformation that consumes two continuous uniform random variables &lt;math&gt; X \sim U(0,1), Y \sim U(0,1) &lt;/math&gt; and outputs a bivariate normal random variable with &lt;math&gt; Z_1\sim N(0,1), Z_2\sim N(0,1). &lt;/math&gt;<br /> <br /> === '''Matlab''' ===<br /> <br /> If X is a matrix,<br /> * ''X(1,:)'' returns the first row<br /> * ''X(:,1)'' returns the first column<br /> * ''X(i,j)'' returns the (i,j)th entry<br /> * ''sum(X,'''1''')'' or ''sum(X)'' is a summation of the '''rows''' of X. The output is a row vector of the sums of each column.<br /> * ''sum(X,'''2''')'' is a summation of the '''columns''' of X, returning a vector.<br /> * ''rand(r,c)'' will generate uniformly distributed random numbers in r rows and c columns.<br /> * The dot operator (.), when placed before a function, such as +,-,^, *, and many others specifies to apply that function to every element of a vector or a matrix. For example, to add a constant c to elements of a matrix A, do A.+c as opposed to simply A+c. The dot operator is not required for functions that can only take a number as their input (such as log).<br /> * Matlab processes loops very slow, while it is fast with matrices and vectors, so it is preferable to use the dot operator to and matrices of random numbers than loops if it is possible.<br /> <br /> == Class 6 - Thursday, May 23 ==<br /> <br /> === Announcement ===<br /> 1. On the day of each lecture, students from the morning section can only contribute the first half of the lecture (i.e. 8:30 - 9:10 am), so that the second half can be saved for the ones from the afternoon section. After the day of lecture, students are free to contribute anything.<br /> <br /> === Standard Normal distribution ===<br /> If X ~ N(0,1) i.e. Standard Normal Distribution - then its p.d.f. is of the form<br /> :&lt;math&gt;f(x) = \frac{1}{\sqrt{2\pi}}\, e^{- \frac{\scriptscriptstyle 1}{\scriptscriptstyle 2} x^2}&lt;/math&gt;<br /> <br /> *Warning : the General Normal distribution is:<br /> &lt;table&gt;<br /> &lt;tr&gt;<br /> &lt;td&gt;&lt;div onmouseover=&quot;document.getElementById('woyun').style.visibility='visible'&quot;<br /> onmouseout=&quot;document.getElementById('woyun').style.visibility='hidden'&quot;&gt;<br /> &lt;math&gt;<br /> f(x) = \frac{1}{\sigma\sqrt{2\pi}} e^{ -\frac{(x-\mu)^2}{2\sigma^2} }<br /> &lt;/math&gt;<br /> &lt;/div&gt;<br /> &lt;/td&gt;<br /> &lt;td&gt;<br /> &lt;div id=&quot;woyun&quot; style=&quot;<br /> <br /> visibility:hidden;<br /> width:100px;<br /> height:100px;<br /> background:#FFFFAD;<br /> position:relative;<br /> animation:movement infinite;<br /> animation-duration:2s;<br /> animation-direction:alternate;<br /> <br /> <br /> /* Safari and Chrome */<br /> -webkit-animation:movement infinite;<br /> -webkit-animation-duration:2s;<br /> -webkit-animation-direction:alternate; <br /> <br /> <br /> @keyframes movement<br /> {<br /> from {left:0px;}<br /> to {left:200px;}<br /> }<br /> <br /> @-webkit-keyframes movement /* Safari and Chrome */<br /> {<br /> from {left:0px;}<br /> to {left:200px;}<br /> }&quot;<br /> &gt;which is almost useless in this course&lt;/div&gt;<br /> &lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;/table&gt;<br /> where &lt;math&gt; \mu &lt;/math&gt; is the mean or expectation of the distribution and &lt;math&gt; \sigma &lt;/math&gt; is standard deviation &lt;br /&gt;<br /> &lt;br /&gt;<br /> *N(0,1) is standard normal. &lt;math&gt; \mu &lt;/math&gt; =0 and &lt;math&gt; \sigma &lt;/math&gt;=1 &lt;br /&gt;<br /> &lt;br /&gt;<br /> <br /> Let X and Y be independent standard normal.<br /> <br /> Let &lt;math&gt; \theta &lt;/math&gt; and R denote the Polar coordinate of the vector (X, Y) <br /> where &lt;math&gt; X = R \cdot \sin\theta &lt;/math&gt; and &lt;math&gt; Y = R \cdot \cos \theta &lt;/math&gt;<br /> <br /> [[File:rtheta.jpg]]<br /> <br /> Note: R must satisfy two properties:<br /> <br /> :1. Be a positive number (as it is a length)<br /> <br /> :2. It must be from a distribution that has more data points closer to the origin so that as we go further from the origin, less points are generated (the two options are Chi-squared and Exponential distribution) <br /> <br /> The form of the joint distribution of R and &lt;math&gt;\theta&lt;/math&gt; will show that the best choice for distribution of R&lt;sup&gt;2&lt;/sup&gt; is exponential.<br /> <br /> <br /> We cannot use the Inverse Transformation Method since F(x) does not have a closed form solution. So we will use joint probability function of two independent standard normal random variables and polar coordinates to simulate the distribution:<br /> <br /> We know that <br /> <br /> &lt;math&gt;R^{2}= X^{2}+Y^{2}&lt;/math&gt; and &lt;math&gt; \tan(\theta) = \frac{y}{x} &lt;/math&gt; where X and Y are two independent standard normal<br /> :&lt;math&gt;f(x) = \frac{1}{\sqrt{2\pi}}\, e^{- \frac{\scriptscriptstyle 1}{\scriptscriptstyle 2} x^2}&lt;/math&gt;<br /> :&lt;math&gt;f(y) = \frac{1}{\sqrt{2\pi}}\, e^{- \frac{\scriptscriptstyle 1}{\scriptscriptstyle 2} y^2}&lt;/math&gt;<br /> :&lt;math&gt;f(x,y) = \frac{1}{\sqrt{2\pi}}\, e^{- \frac{\scriptscriptstyle 1}{\scriptscriptstyle 2} x^2} * \frac{1}{\sqrt{2\pi}}\, e^{- \frac{\scriptscriptstyle 1}{\scriptscriptstyle 2} y^2}=\frac{1}{2\pi}\, e^{- \frac{\scriptscriptstyle 1}{\scriptscriptstyle 2} (x^2+y^2)} &lt;/math&gt;&lt;br /&gt; - Since for independent distributions, their joint probability function is the multiplication of two independent probability functions. It can also be shown using 1-1 transformation that the joint distribution of R and θ is given by, 1-1 transformation:&lt;br /&gt;<br /> <br /> <br /> '''Let &lt;math&gt;d=R^2&lt;/math&gt;'''&lt;br /&gt;<br /> <br /> &lt;math&gt;x= \sqrt {d}\cos \theta &lt;/math&gt;<br /> &lt;math&gt;y= \sqrt {d}\sin \theta &lt;/math&gt;<br /> then <br /> &lt;math&gt;\left| J\right| = \left| \dfrac {1} {2}d^{-\frac {1} {2}}\cos \theta d^{\frac{1}{2}}\cos \theta +\sqrt {d}\sin \theta \dfrac {1} {2}d^{-\frac{1}{2}}\sin \theta \right| = \dfrac {1} {2}&lt;/math&gt;<br /> It can be shown that the joint density of &lt;math&gt; d /R^2&lt;/math&gt; and &lt;math&gt; \theta &lt;/math&gt; is:<br /> :&lt;math&gt;\begin{matrix} f(d,\theta) = \frac{1}{2}e^{-\frac{d}{2}}*\frac{1}{2\pi},\quad d = R^2 \end{matrix},\quad for\quad 0\leq d&lt;\infty\ and\quad 0\leq \theta\leq 2\pi &lt;/math&gt;<br /> <br /> <br /> <br /> Note that &lt;math&gt; \begin{matrix}f(r,\theta)\end{matrix}&lt;/math&gt; consists of two density functions, Exponential and Uniform, so assuming that r and &lt;math&gt;\theta&lt;/math&gt; are independent<br /> &lt;math&gt; \begin{matrix} \Rightarrow d \sim~ Exp(1/2), \theta \sim~ Unif[0,2\pi] \end{matrix} &lt;/math&gt;<br /> ::* &lt;math&gt; \begin{align} R^2 = d = x^2 + y^2 \end{align} &lt;/math&gt;<br /> ::* &lt;math&gt; \tan(\theta) = \frac{y}{x} &lt;/math&gt;<br /> &lt;math&gt;\begin{align} f(d) = Exp(1/2)=\frac{1}{2}e^{-\frac{d}{2}}\ \end{align}&lt;/math&gt; <br /> &lt;br&gt;<br /> &lt;math&gt;\begin{align} f(\theta) =\frac{1}{2\pi}\ \end{align}&lt;/math&gt;<br /> &lt;br&gt;<br /> <br /> To sample from the normal distribution, we can generate a pair of independent standard normal X and Y by:&lt;br /&gt;<br /> <br /> 1) Generating their polar coordinates&lt;br /&gt;<br /> 2) Transforming back to rectangular (Cartesian) coordinates.&lt;br /&gt;<br /> <br /> <br /> '''Alternative Method of Generating Standard Normal Random Variables'''&lt;br /&gt; <br /> <br /> Step 1: Generate &lt;math&gt;u_{1}&lt;/math&gt; ~&lt;math&gt;Unif(0,1)&lt;/math&gt;&lt;br /&gt;<br /> Step 2: Generate &lt;math&gt;Y_{1}&lt;/math&gt; ~&lt;math&gt;Exp(1)&lt;/math&gt;,&lt;math&gt;Y_{2}&lt;/math&gt;~&lt;math&gt;Exp(2)&lt;/math&gt;&lt;br /&gt;<br /> Step 3: If &lt;math&gt;Y_{2} \geq(Y_{1}-1)^2/2&lt;/math&gt;,set &lt;math&gt;V=Y1&lt;/math&gt;,otherwise,go to step 1&lt;br /&gt;<br /> Step 4: If &lt;math&gt;u_{1} \leq 1/2&lt;/math&gt;,then &lt;math&gt;X=-V&lt;/math&gt;&lt;br /&gt;<br /> <br /> ===Expectation of a Standard Normal distribution===&lt;br /&gt;<br /> <br /> The expectation of a standard normal distribution is 0&lt;br /&gt;<br /> <br /> '''Proof:''' &lt;br /&gt;<br /> <br /> :&lt;math&gt;\operatorname{E}[X]= \;\int_{-\infty}^{\infty} x \frac{1}{\sqrt{2\pi}} e^{-x^2/2} \, dx.&lt;/math&gt;<br /> :&lt;math&gt;\phi(x) = \frac{1}{\sqrt{2\pi}}\, e^{- \frac{\scriptscriptstyle 1}{\scriptscriptstyle 2} x^2}.&lt;/math&gt;<br /> :&lt;math&gt;=\;\int_{-\infty}^{\infty} x \phi(x), dx.&lt;/math&gt;<br /> :Since the first derivative ''ϕ''′(''x'') is −''xϕ''(''x'')<br /> :&lt;math&gt;=\;\ - \int_{-\infty}^{\infty} \phi'(x), dx.&lt;/math&gt;<br /> :&lt;math&gt;= - \left[\phi(x)\right]_{-\infty}^{\infty}&lt;/math&gt;<br /> :&lt;math&gt;= 0&lt;/math&gt;&lt;br /&gt;<br /> <br /> '''Note,''' more intuitively, because x is an odd function (f(x)+f(-x)=0). Taking integral of x will give &lt;math&gt;x^2/2 &lt;/math&gt; which is an even function (f(x)=f(-x)). This is in relation to the symmetrical properties of the standard normal distribution. If support is from negative infinity to infinity, then the integral will return 0.&lt;br /&gt;<br /> <br /> <br /> '''Procedure (Box-Muller Transformation Method):''' &lt;br /&gt;<br /> <br /> Pseudorandom approaches to generating normal random variables used to be limited. Inefficient methods such as inverse Gaussian function, sum of uniform random variables, and acceptance-rejection were used. In 1958, a new method was proposed by George Box and Mervin Muller of Princeton University. This new technique was easy to use and also had the accuracy to the inverse transform sampling method that it grew more valuable as computers became more computationally astute. &lt;br&gt;<br /> The Box-Muller method takes a sample from a bivariate independent standard normal distribution, each component of which is thus a univariate standard normal. The algorithm is based on the following two properties of the bivariate independent standard normal distribution: &lt;br&gt;<br /> if &lt;math&gt;Z = (Z_{1}, Z_{2}&lt;/math&gt;) has this distribution, then &lt;br&gt;<br /> <br /> 1.&lt;math&gt;R^2=Z_{1}^2+Z_{2}^2&lt;/math&gt; is exponentially distributed with mean 2, i.e. &lt;br&gt;<br /> &lt;math&gt;P(R^2 \leq x) = 1-e^{-x/2}&lt;/math&gt;. &lt;br&gt;<br /> 2.Given &lt;math&gt;R^2&lt;/math&gt;, the point &lt;math&gt;(Z_{1},Z_{2}&lt;/math&gt;) is uniformly distributed on the circle of radius R centered at the origin. &lt;br&gt;<br /> We can use these properties to build the algorithm: &lt;br&gt;<br /> <br /> <br /> 1) Generate random number &lt;math&gt; \begin{align} U_1,U_2 \sim~ \mathrm{Unif}(0, 1) \end{align} &lt;/math&gt; &lt;br /&gt;<br /> 2) Generate polar coordinates using the exponential distribution of d and uniform distribution of θ,<br /> <br /> <br /> <br /> &lt;math&gt; \begin{align} R^2 = d = -2\log(U_1), &amp; \quad r = \sqrt{d} \\ &amp; \quad \theta = 2\pi U_2 \end{align} &lt;/math&gt;<br /> <br /> <br /> &lt;math&gt; \begin{matrix} \ R^2 \sim~ Exp(1/2), \theta \sim~ Unif[0,2\pi] \end{matrix} &lt;/math&gt; &lt;br /&gt;<br /> <br /> Note: If U~unif(0,1), then ln(1-U)=ln(U)<br /> <br /> 3) Transform polar coordinates (i.e. R and θ) back to Cartesian coordinates (i.e. X and Y), &lt;br&gt; &lt;math&gt; \begin{align} x = R\cos(\theta) \\ y = R\sin(\theta) \end{align} &lt;/math&gt; &lt;br /&gt;.<br /> <br /> Alternatively,&lt;br&gt; &lt;math&gt; x =\cos(2\pi U_2)\sqrt{-2\ln U_1}\, &lt;/math&gt; and&lt;br&gt; &lt;math&gt; y =\sin(2\pi U_2)\sqrt{-2\ln U_1}\, &lt;/math&gt;&lt;br /&gt;<br /> <br /> <br /> '''Note:''' In steps 2 and 3, we are using a similar technique as that used in the inverse transform method. &lt;br /&gt;<br /> The Box-Muller Transformation Method generates a pair of independent Standard Normal distributions, X and Y (Using the transformation of polar coordinates). &lt;br /&gt;<br /> <br /> If you want to generate a number of independent standard normal distributed numbers (more than two), you can run the Box-Muller method several times.&lt;br/&gt;<br /> For example: &lt;br /&gt;<br /> If you want 8 independent standard normal distributed numbers, then run the Box-Muller methods 4 times (8/2 times). &lt;br /&gt; <br /> If you want 9 independent standard normal distributed numbers, then run the Box-Muller methods 5 times (10/2 times), and then delete one. &lt;br /&gt;<br /> <br /> <br /> '''Matlab Code'''&lt;br /&gt;<br /> <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;close all<br /> &gt;&gt;clear all<br /> &gt;&gt;u1=rand(1,1000);<br /> &gt;&gt;u2=rand(1,1000);<br /> &gt;&gt;d=-2*log(u1);<br /> &gt;&gt;tet=2*pi*u2;<br /> &gt;&gt;x=d.^0.5.*cos(tet);<br /> &gt;&gt;y=d.^0.5.*sin(tet);<br /> &gt;&gt;hist(tet) <br /> &gt;&gt;hist(d)<br /> &gt;&gt;hist(x)<br /> &gt;&gt;hist(y)<br /> &lt;/pre&gt;<br /> &lt;br&gt;<br /> '''Remember''': For the above code to work the &quot;.&quot; needs to be after the d to ensure that each element of d is raised to the power of 0.5.&lt;br /&gt; Otherwise matlab will raise the entire matrix to the power of 0.5.&quot;&lt;br&gt;<br /> <br /> '''Note:'''&lt;br&gt;the first graph is hist(tet) and it is a uniform distribution.&lt;br&gt;The second one is hist(d) and it is a exponential distribution.&lt;br&gt;The third one is hist(x) and it is a normal distribution.&lt;br&gt;The last one is hist(y) and it is also a normal distribution.<br /> <br /> Attention:There is a &quot;dot&quot; between sqrt(d) and &quot;*&quot;. It is because d and tet are vectors. &lt;br&gt;<br /> <br /> <br /> [[File:Normal_theta.jpg|300px]][[File:Normal_d.jpg|300px]]<br /> [[File:normal_x.jpg|300x300px]][[File:normal_y.jpg|300x300px]]<br /> <br /> As seen in the histograms above, X and Y generated from this procedure have a standard normal distribution.<br /> <br /> * '''Code'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;close all<br /> &gt;&gt;clear all<br /> &gt;&gt;x=randn(1,1000);<br /> &gt;&gt;hist(x)<br /> &gt;&gt;hist(x+2)<br /> &gt;&gt;hist(x*2+2)&lt;br&gt;<br /> &lt;/pre&gt;<br /> &lt;br&gt;<br /> '''Note:'''&lt;br&gt; <br /> 1. randn is random sample from a standard normal distribution.&lt;br /&gt;<br /> 2. hist(x+2) will be centered at 2 instead of at 0. &lt;br /&gt;<br /> 3. hist(x*3+2) is also centered at 2. The mean doesn't change, but the variance of x*3+2 becomes nine times (3^2) the variance of x.&lt;br /&gt;<br /> [[File:Normal_x.jpg|300x300px]][[File:Normal_x+2.jpg|300x300px]][[File:Normal(2x+2).jpg|300px]]<br /> &lt;br /&gt;<br /> <br /> &lt;b&gt;Comment&lt;/b&gt;:&lt;br /&gt;<br /> Box-Muller transformations are not computationally efficient. The reason for this is the need to compute sine and cosine functions. A way to get around this time-consuming difficulty is by an indirect computation of the sine and cosine of a random angle (as opposed to a direct computation which generates U and then computes the sine and cosine of 2πU. &lt;br /&gt;<br /> <br /> <br /> <br /> '''Alternative Methods of generating normal distribution'''&lt;br /&gt;<br /> <br /> 1. Even though we cannot use inverse transform method, we can approximate this inverse using different functions.One method would be '''rational approximation'''.&lt;br /&gt;<br /> 2.'''Central limit theorem''' : If we sum 12 independent U(0,1) distribution and subtract 6 (which is E(ui)*12)we will approximately get a standard normal distribution.&lt;br /&gt;<br /> 3. '''Ziggurat algorithm''' which is known to be faster than Box-Muller transformation and a version of this algorithm is used for the randn function in matlab.&lt;br /&gt;<br /> <br /> If Z~N(0,1) and X= μ +Zσ then X~&lt;math&gt; N(\mu, \sigma^2)&lt;/math&gt;<br /> <br /> If Z&lt;sub&gt;1&lt;/sub&gt;, Z&lt;sub&gt;2&lt;/sub&gt;... Z&lt;sub&gt;d&lt;/sub&gt; are independent identically distributed N(0,1),<br /> then Z=(Z&lt;sub&gt;1&lt;/sub&gt;,Z&lt;sub&gt;2&lt;/sub&gt;...Z&lt;sub&gt;d&lt;/sub&gt;)&lt;sup&gt;T&lt;/sup&gt; ~N(0, I&lt;sub&gt;d&lt;/sub&gt;), where 0 is the zero vector and I&lt;sub&gt;d&lt;/sub&gt; is the identity matrix.<br /> <br /> For the histogram, the constant is the parameter that affect the center of the graph.<br /> <br /> === Proof of Box Muller Transformation ===<br /> <br /> '''Definition:'''&lt;br /&gt;<br /> A transformation which transforms from a '''two-dimensional continuous uniform''' distribution to a '''two-dimensional bivariate normal''' distribution (or complex normal distribution).<br /> <br /> Let U&lt;sub&gt;1&lt;/sub&gt; and U&lt;sub&gt;2&lt;/sub&gt; be independent uniform (0,1) random variables. Then<br /> &lt;math&gt;X_{1} = ((-2lnU_{1})^.5)*cos(2\pi U_{2})&lt;/math&gt;<br /> <br /> &lt;math&gt;X_{2} = (-2lnU_{1})^0.5*sin(2\pi U_{2})&lt;/math&gt;<br /> are '''independent''' N(0,1) random variables.<br /> <br /> This is a standard transformation problem. The joint distribution is given by <br /> f(x1 ,x2) = f&lt;sub&gt;u1&lt;/sub&gt;, &lt;sub&gt;u2&lt;/sub&gt;(g1^− 1(x1,x2),g2^− 1(x1,x2)) * | J |<br /> <br /> where J is the Jacobian of the transformation,<br /> <br /> J = |∂u&lt;sub&gt;1&lt;/sub&gt;/∂x&lt;sub&gt;1&lt;/sub&gt;,∂u&lt;sub&gt;1&lt;/sub&gt;/∂x&lt;sub&gt;2&lt;/sub&gt;|<br /> |∂u&lt;sub&gt;2&lt;/sub&gt;/∂x&lt;sub&gt;1&lt;/sub&gt;,∂u&lt;sub&gt;2&lt;/sub&gt;/∂x&lt;sub&gt;2&lt;/sub&gt;|<br /> where <br /> u&lt;sub&gt;1&lt;/sub&gt; = g&lt;sub&gt;1&lt;/sub&gt; ^-1(x1,x2)<br /> u&lt;sub&gt;2&lt;/sub&gt; = g&lt;sub&gt;2&lt;/sub&gt; ^-1(x1,x2)<br /> <br /> Inverting the above transformation, we have<br /> u1 = exp^{-(x&lt;sub&gt;1&lt;/sub&gt; ^2+ x&lt;sub&gt;2&lt;/sub&gt; ^2)/2}<br /> u2 = (1/2pi)*tan^-1 (x&lt;sub&gt;2&lt;/sub&gt;/x&lt;sub&gt;1&lt;/sub&gt;)<br /> <br /> Finally we get<br /> f(x1,x2) = {exp^(-(x1^2+x2^2)/2)}/2pi<br /> which factors into two standard normal pdfs.<br /> <br /> <br /> (The quote is from http://mathworld.wolfram.com/Box-MullerTransformation.html)<br /> (The proof is from http://www.math.nyu.edu/faculty/goodman/teaching/MonteCarlo2005/notes/GaussianSampling.pdf)<br /> <br /> === General Normal distributions ===<br /> General normal distribution is a special version of the standard normal distribution. The domain of the general normal distribution is affected by the standard deviation and translated by the mean value. <br /> *The pdf of the general normal distribution is <br /> :<br /> &lt;table&gt;<br /> &lt;tr&gt;<br /> &lt;td&gt;&lt;div onmouseover=&quot;document.getElementById('woyun').style.visibility='visible'&quot;<br /> onmouseout=&quot;document.getElementById('woyun').style.visibility='hidden'&quot;&gt;<br /> &lt;math&gt;<br /> f(x) = \frac{1}{\sigma\sqrt{2\pi}} e^{ -\frac{(x-\mu)^2}{2\sigma^2} }<br /> &lt;/math&gt;<br /> &lt;/div&gt;<br /> &lt;/td&gt;<br /> &lt;td&gt;<br /> &lt;div id=&quot;woyun&quot; style=&quot;visibility:hidden&quot;&gt;which is almost useless in this course&lt;/div&gt;<br /> &lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;/table&gt;<br /> where &lt;math&gt; \mu &lt;/math&gt; is the mean or expectation of the distribution and &lt;math&gt; \sigma &lt;/math&gt; is standard deviation &lt;br /&gt;<br /> <br /> The probability density must be scaled by 1/sigma so that the integral is still 1.(Acknowledge: https://en.wikipedia.org/wiki/Normal_distribution)<br /> The special case of the normal distribution is standard normal distribution, which the variance is 1 and the mean is zero. If X is a general normal deviate, then &lt;math&gt; Z=\dfrac{X - (\mu)}{\sigma} &lt;/math&gt; will have a standard normal distribution.<br /> <br /> If Z ~ N(0,1), and we want &lt;math&gt;X &lt;/math&gt;~&lt;math&gt; N(\mu, \sigma^2)&lt;/math&gt;, then &lt;math&gt;X = \mu + \sigma * Z&lt;/math&gt; Since &lt;math&gt;E(x) = \mu +\sigma*0 = \mu &lt;/math&gt; and &lt;math&gt;Var(x) = 0 +\sigma^2*1&lt;/math&gt;<br /> <br /> If &lt;math&gt;Z_1,...Z_d&lt;/math&gt; ~ N(0,1) and are independent then &lt;math&gt;Z = (Z_1,..Z_d)^{T} &lt;/math&gt;~ &lt;math&gt;N(0,I_d)&lt;/math&gt;<br /> ie.<br /> * '''Code'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;close all<br /> &gt;&gt;clear all<br /> &gt;&gt;z1=randn(1,1000); &lt;-generate variable from standard normal distribution<br /> &gt;&gt;z2=randn(1,1000);<br /> &gt;&gt;z=[z1;z2]; &lt;-produce a vector<br /> &gt;&gt;plot(z(1,:),z(2,:),'.')<br /> &lt;/pre&gt;<br /> [[File:Nonstdnormal_example.jpg|300px]]<br /> <br /> If Z~N(0,Id) and X= &lt;math&gt;\underline{\mu} + \Sigma^{\frac{1}{2}} \,Z &lt;/math&gt; then &lt;math&gt;\underline{X}&lt;/math&gt; ~&lt;math&gt;N(\underline{\mu},\Sigma)&lt;/math&gt;<br /> <br /> =====Non-Standard Normal Distributions=====<br /> <br /> '''Example 1: Single-variate Normal'''<br /> <br /> If X ~ Norm(0, 1) then (a + bX) has a normal distribution with a mean of &lt;math&gt;\displaystyle a&lt;/math&gt; and a standard deviation of &lt;math&gt;\displaystyle b&lt;/math&gt; (which is equivalent to a variance of &lt;math&gt;\displaystyle b^2&lt;/math&gt;). Using this information with the Box-Muller transform, we can generate values sampled from some random variable &lt;math&gt;\displaystyle Y\sim N(a,b^2) &lt;/math&gt; for arbitrary values of &lt;math&gt;\displaystyle a,b&lt;/math&gt;.<br /> <br /> # Generate a sample u from Norm(0, 1) using the Box-Muller transform.<br /> # Set v = a + bu.<br /> <br /> The values for v generated in this way will be equivalent to sample from a &lt;math&gt;\displaystyle N(a, b^2)&lt;/math&gt;distribution. We can modify the MatLab code used in the last section to demonstrate this. We just need to add one line before we generate the histogram:<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> v = a + b * x;<br /> &lt;/pre&gt;<br /> <br /> For instance, this is the histogram generated when b = 15, a = 125:<br /> <br /> [[File:Hist normal.jpg|center|500]]<br /> <br /> '''Example 2: Multi-variate Normal'''<br /> <br /> The Box-Muller method can be extended to higher dimensions to generate multivariate normals. The objects generated will be nx1 vectors, and their variance will be described by nxn covariance matrices.<br /> <br /> &lt;math&gt;\mathbf{z} = N(\mathbf{u}, \Sigma)&lt;/math&gt; defines the n by 1 vector &lt;math&gt;\mathbf{z}&lt;/math&gt; such that:<br /> <br /> * &lt;math&gt;\displaystyle u_i&lt;/math&gt; is the average of &lt;math&gt;\displaystyle z_i&lt;/math&gt;<br /> * &lt;math&gt;\!\Sigma_{ii}&lt;/math&gt; is the variance of &lt;math&gt;\displaystyle z_i&lt;/math&gt;<br /> * &lt;math&gt;\!\Sigma_{ij}&lt;/math&gt; is the co-variance of &lt;math&gt;\displaystyle z_i&lt;/math&gt; and &lt;math&gt;\displaystyle z_j&lt;/math&gt;<br /> <br /> If &lt;math&gt;\displaystyle z_1, z_2, ..., z_d&lt;/math&gt; are normal variables with mean 0 and variance 1, then the vector &lt;math&gt;\displaystyle (z_1, z_2,..., z_d) &lt;/math&gt; has mean 0 and variance &lt;math&gt;\!I&lt;/math&gt;, where 0 is the zero vector and &lt;math&gt;\!I&lt;/math&gt; is the identity matrix. This fact suggests that the method for generating a multivariate normal is to generate each component individually as single normal variables.<br /> <br /> The mean and the covariance matrix of a multivariate normal distribution can be adjusted in ways analogous to the single variable case. If &lt;math&gt;\mathbf{z} \sim N(0,I)&lt;/math&gt;, then &lt;math&gt;\Sigma^{1/2}\mathbf{z}+\mu \sim N(\mu,\Sigma)&lt;/math&gt;. Note here that the covariance matrix is symmetric and nonnegative, so its square root should always exist.<br /> <br /> We can compute &lt;math&gt;\mathbf{z}&lt;/math&gt; in the following way:<br /> <br /> # Generate an n by 1 vector &lt;math&gt;\mathbf{x} = \begin{bmatrix}x_{1} &amp; x_{2} &amp; ... &amp; x_{n}\end{bmatrix}&lt;/math&gt; where &lt;math&gt;x_{i}&lt;/math&gt; ~ Norm(0, 1) using the Box-Muller transform.<br /> # Calculate &lt;math&gt;\!\Sigma^{1/2}&lt;/math&gt; using singular value decomposition.<br /> # Set &lt;math&gt;\mathbf{z} = \Sigma^{1/2} \mathbf{x} + \mathbf{u}&lt;/math&gt;.<br /> <br /> The following MatLab code provides an example, where a scatter plot of 10000 random points is generated. In this case x and y have a co-variance of 0.9 - a very strong positive correlation.<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> x = zeros(10000, 1);<br /> y = zeros(10000, 1);<br /> for ii = 1:10000<br /> u1 = rand;<br /> u2 = rand;<br /> R2 = -2 * log(u1);<br /> theta = 2 * pi * u2;<br /> x(ii) = sqrt(R2) * cos(theta);<br /> y(ii) = sqrt(R2) * sin(theta);<br /> end<br /> <br /> E = [1, 0.9; 0.9, 1];<br /> [u s v] = svd(E);<br /> root_E = u * (s ^ (1 / 2)) * u';<br /> <br /> z = (root_E * [x y]');<br /> z(1,:) = z(1,:) + 0;<br /> z(2,:) = z(2,:) + -3;<br /> <br /> scatter(z(1,:), z(2,:))<br /> &lt;/pre&gt;<br /> <br /> Note: The svd command computes the matrix singular value decomposition.<br /> <br /> [u,s,v] = svd(E) produces a diagonal matrix s of the same dimension as E, with nonnegative diagonal elements in decreasing order, and unitary matrices u and v so that E = u*s*v'.<br /> <br /> This code generated the following scatter plot:<br /> <br /> [[File:scatter covar.jpg|center|500px]]<br /> <br /> In Matlab, we can also use the function &quot;sqrtm()&quot; or &quot;chol()&quot; (Cholesky Decomposition) to calculate square root of a matrix directly. Note that the resulting root matrices may be different but this does materially affect the simulation.<br /> Here is an example:<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> E = [1, 0.9; 0.9, 1];<br /> r1 = sqrtm(E);<br /> r2 = chol(E);<br /> &lt;/pre&gt;<br /> <br /> R code for a multivariate normal distribution:<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> n=10000;<br /> r2&lt;--2*log(runif(n));<br /> theta&lt;-2*pi*(runif(n));<br /> x&lt;-sqrt(r2)*cos(theta);<br /> <br /> y&lt;-sqrt(r2)*sin(theta);<br /> a&lt;-matrix(c(x,y),nrow=n,byrow=F);<br /> e&lt;-matrix(c(1,.9,09,1),nrow=2,byrow=T);<br /> svde&lt;-svd(e);<br /> root_e&lt;-svde$u %*% diag(svde$d)^1/2;<br /> z&lt;-t(root_e %*%t(a));<br /> z[,1]=z[,1]+5;<br /> z[,2]=z[,2]+ -8;<br /> par(pch=19);<br /> plot(z,col=rgb(1,0,0,alpha=0.06))<br /> &lt;/pre&gt;<br /> <br /> [[File:m_normal.png|center|500px]]<br /> <br /> === Bernoulli Distribution ===<br /> The Bernoulli distribution is a discrete probability distribution, which usually describes an event that only has two possible results, i.e. success or failure (x=0 or 1). If the event succeed, we usually take value 1 with success probability p, and take value 0 with failure probability q = 1 - p. <br /> <br /> P ( x = 0) = q = 1 - p &lt;br /&gt;<br /> P ( x = 1) = p &lt;br /&gt;<br /> P ( x = 0) + P (x = 1) = p + q = 1 &lt;br /&gt;<br /> <br /> If X~Ber(p), its pdf is of the form &lt;math&gt;f(x)= p^{x}(1-p)^{(1-x)}&lt;/math&gt;, x=0,1<br /> &lt;br&gt; P is the success probability.<br /> <br /> The Bernoulli distribution is a special case of binomial distribution, where the variate x only has two outcomes; so that the Bernoulli also can use the probability density function of the binomial distribution with the variate x taking values 0 and 1.<br /> <br /> The most famous example for the Bernoulli Distribution would be the &quot;Flip Coin&quot; question, which has only two possible outcomes(Success or Failure) with the same probabilities of 0.5 <br /> <br /> Let x1,x2 denote the lifetime of 2 independent particles, x1~exp(&lt;math&gt;\lambda&lt;/math&gt;), x2~exp(&lt;math&gt;\lambda&lt;/math&gt;)<br /> we are interested in y=min(x1,x2)<br /> <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> <br /> Procedure:<br /> <br /> To simulate the event of flipping a coin, let P be the probability of flipping head and X = 1 and 0 represent<br /> flipping head and tail respectively:<br /> <br /> 1. Draw U ~ Uniform(0,1)<br /> <br /> 2. If U &lt;= P<br /> <br /> X = 1<br /> <br /> Else<br /> <br /> X = 0<br /> <br /> 3. Repeat as necessary<br /> <br /> &lt;/pre&gt;<br /> <br /> An intuitive way to think of this is in the coin flip example we discussed in a previous lecture. In this example we set p = 1/2 and this allows for 50% of points to be heads or tails.<br /> <br /> * '''Code to Generate Bernoulli(p = 0.3)'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> i = 1;<br /> <br /> while (i &lt;=1000)<br /> u =rand();<br /> p = 0.3;<br /> if (u &lt;= p)<br /> x(i) = 1;<br /> else<br /> x(i) = 0;<br /> end<br /> i = i + 1;<br /> end<br /> <br /> hist(x)<br /> &lt;/pre&gt;<br /> <br /> However, we know that if &lt;math&gt;\begin{align} X_i \sim Bernoulli(p) \end{align}&lt;/math&gt; where each &lt;math&gt;\begin{align} X_i \end{align}&lt;/math&gt; is independent,&lt;br /&gt;<br /> &lt;math&gt;U = \sum_{i=1}^{n} X_i \sim Binomial(n,p)&lt;/math&gt;&lt;br /&gt;<br /> So we can sample from binomial distribution using this property.<br /> Note: We can consider Binomial distribution as the sum of n, ''independent'', Bernoulli distributions<br /> &lt;div style=&quot;background:#CCFF33;border-radius:5px;box-shadow: 10px 10px 5px #888888;padding:30px;&quot;&gt;<br /> * '''Code to Generate Binomial(n = 20,p = 0.7)'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> p = 0.7;<br /> n = 20;<br /> <br /> for k=1:5000<br /> i = 1;<br /> for i=1:n<br /> u=rand();<br /> if (u &lt;= p)<br /> y(i) = 1;<br /> else<br /> y(i) = 0;<br /> end<br /> end<br /> <br /> x(k) = sum(y==1);<br /> end<br /> <br /> hist(x)<br /> <br /> &lt;/pre&gt;<br /> <br /> <br /> <br /> <br /> &lt;/div&gt;<br /> Note: We can also regard the Bernoulli Distribution as either a conditional distribution or &lt;math&gt;f(x)= p^{x}(1-p)^{(1-x)}&lt;/math&gt;, x=0,1.<br /> <br /> Comments on Matlab:<br /> When doing operations on vectors, always put a dot before the operator if you want the operation to be done to every element in the vector. <br /> example: Let V be a vector with dimension 2*4 and you want each element multiply by 3. <br /> The Matlab code is 3.*V<br /> <br /> some examples for using code to generate distribution.<br /> <br /> == Class 7 - Tuesday, May 28 ==<br /> <br /> <br /> [[Note that the material in this lecture will not be on the exam; it was only to supplement what we have learned.]]<br /> ===Universality of the Uniform Distribution/Inverse Method===<br /> <br /> The inverse method is universal in the sense that we can potentially sample from any distribution where we can find the inverse of the cumulative distribution function.<br /> <br /> Procedure:<br /> <br /> 1) Generate U~Unif (0, 1)&lt;br&gt;<br /> 2) Set &lt;math&gt;x=F^{-1}(u)&lt;/math&gt;&lt;br&gt;<br /> 3) X~f(x)&lt;br&gt;<br /> <br /> '''Remark'''&lt;br&gt;<br /> 1) The preceding can be written algorithmically for discrete random variables as &lt;br&gt;<br /> Generate a random number U ~ U(0,1] &lt;br&gt;<br /> If U &lt; p&lt;sub&gt;0&lt;/sub&gt; set X = x&lt;sub&gt;0&lt;/sub&gt; and stop &lt;br&gt;<br /> If U &lt; p&lt;sub&gt;0&lt;/sub&gt; + p&lt;sub&gt;1&lt;/sub&gt; set X = x&lt;sub&gt;1&lt;/sub&gt; and stop &lt;br&gt;<br /> ... &lt;br&gt;<br /> 2) If the x&lt;sub&gt;i&lt;/sub&gt;, i&gt;=0, are ordered so that x&lt;sub&gt;0&lt;/sub&gt; &lt; x&lt;sub&gt;1&lt;/sub&gt; &lt; x&lt;sub&gt;2&lt;/sub&gt; &lt;... and if we let F denote the distribution function of X, then X will equal x&lt;sub&gt;j&lt;/sub&gt; if F(x&lt;sub&gt;j-1&lt;/sub&gt;) &lt;= U &lt; F(x&lt;sub&gt;j&lt;/sub&gt;)<br /> <br /> '''Example 1'''&lt;br&gt;<br /> <br /> Let &lt;math&gt;X&lt;/math&gt;&lt;sub&gt;1&lt;/sub&gt;,&lt;math&gt;X&lt;/math&gt;&lt;sub&gt;2&lt;/sub&gt; denote the lifetime of two independent particles:&lt;br&gt;<br /> &lt;math&gt;X&lt;/math&gt;&lt;sub&gt;1&lt;/sub&gt;~exp(&lt;math&gt;\lambda&lt;/math&gt;&lt;sub&gt;1&lt;/sub&gt;)&lt;br&gt;<br /> &lt;math&gt;X&lt;/math&gt;&lt;sub&gt;2&lt;/sub&gt;~exp(&lt;math&gt;\lambda&lt;/math&gt;&lt;sub&gt;2&lt;/sub&gt;)&lt;br&gt;<br /> <br /> We are interested in &lt;math&gt;y=min(X&lt;/math&gt;&lt;sub&gt;1&lt;/sub&gt;&lt;math&gt;,X&lt;/math&gt;&lt;sub&gt;2&lt;/sub&gt;&lt;math&gt;)&lt;/math&gt;&lt;br&gt;<br /> Design an algorithm based on the Inverse-Transform Method to generate samples according to &lt;math&gt;f&lt;/math&gt;&lt;sub&gt;y&lt;/sub&gt;&lt;math&gt;(y)&lt;/math&gt;&lt;br&gt;<br /> <br /> '''Solution:'''&lt;br&gt;<br /> <br /> x&lt;sub&gt;1&lt;/sub&gt;~exp(&lt;math&gt;\lambda_1&lt;/math&gt;)&lt;br&gt;<br /> x&lt;sub&gt;2&lt;/sub&gt;~exp(&lt;math&gt;\lambda_2&lt;/math&gt;)&lt;br&gt;<br /> &lt;math&gt;f_{x(x)}=\lambda e^{-\lambda x},x\geq0 &lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt;F_X(x)=1-e^{-\lambda x}, x\geq 0&lt;/math&gt;&lt;br&gt;<br /> <br /> &lt;math&gt;1-F_Y(y) = P(Y&gt;y)&lt;/math&gt; = P(min(X&lt;sub&gt;1&lt;/sub&gt;,X&lt;sub&gt;2&lt;/sub&gt;) &gt; y) = &lt;math&gt;\, P((X_1)&gt;y) P((X_2)&gt;y) = e^{\, -(\lambda_1 + \lambda_2) y}&lt;/math&gt;&lt;br&gt;<br /> <br /> &lt;math&gt;F_Y(y)=1-e^{\, -(\lambda_1 + \lambda_2) y}, y\geq 0&lt;/math&gt;&lt;br&gt;<br /> <br /> &lt;math&gt;U=1-e^{\, -(\lambda_1 + \lambda_2) y}&lt;/math&gt; =&gt; &lt;math&gt;y=\, {-\frac {1}{{\lambda_1 +\lambda_2}}} ln(1-u)&lt;/math&gt;&lt;br&gt;<br /> <br /> '''Procedure:'''<br /> <br /> Step1: Generate U~ U(0, 1)&lt;br&gt;<br /> <br /> Step2: set &lt;math&gt;y=\, {-\frac {1}{{\lambda_1 +\lambda_2}}} ln(1-u)&lt;/math&gt;&lt;br&gt;<br /> <br /> or set &lt;math&gt;y=\, {-\frac {1} {{\lambda_1 +\lambda_2}}} ln(u)&lt;/math&gt;&lt;br&gt;<br /> Since it is a uniform distribution, therefore after generate a lot of times 1-u and u are the same.<br /> <br /> <br /> * '''Matlab Code'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt; lambda1 = 1;<br /> &gt;&gt; lambda2 = 2;<br /> &gt;&gt; u = rand;<br /> &gt;&gt; y = -log(u)/(lambda1 + lambda2) <br /> &lt;/pre&gt;<br /> <br /> If we generalize this example from two independent particles to n independent particles we will have:&lt;br&gt;<br /> <br /> &lt;math&gt;X&lt;/math&gt;&lt;sub&gt;1&lt;/sub&gt;~exp(&lt;math&gt;\lambda&lt;/math&gt;&lt;sub&gt;1&lt;/sub&gt;)&lt;br&gt;&lt;math&gt;X&lt;/math&gt;&lt;sub&gt;2&lt;/sub&gt;~exp(&lt;math&gt;\lambda&lt;/math&gt;&lt;sub&gt;2&lt;/sub&gt;)&lt;br&gt; ...&lt;br&gt; &lt;math&gt;X&lt;/math&gt;&lt;sub&gt;n&lt;/sub&gt;~exp(&lt;math&gt;\lambda&lt;/math&gt;&lt;sub&gt;n&lt;/sub&gt;)&lt;br&gt;.<br /> <br /> And the algorithm using the inverse-transform method as follows:<br /> <br /> step1: Generate U~U(0,1)<br /> <br /> Step2: &lt;math&gt;y=\, {-\frac {1}{{ \sum\lambda_i}}} ln(1-u)&lt;/math&gt;&lt;br&gt;<br /> <br /> <br /> '''Example 2'''&lt;br&gt;<br /> Consider U~Unif[0,1)&lt;br&gt;<br /> &lt;math&gt;X=\, a (1-\sqrt{1-u})&lt;/math&gt;, <br /> &lt;br&gt;where a&gt;0 and a is a real number<br /> What is the distribution of X?&lt;br&gt;<br /> <br /> '''Solution:'''&lt;br&gt;<br /> <br /> We can find a form for the cumulative distribution function of X by isolating U as U~Unif[0,1) will take values from the range of F(X)uniformly. It then remains to differentiate the resulting form by X to obtain the probability density function.<br /> <br /> &lt;math&gt;X=\, a (1-\sqrt{1-u})&lt;/math&gt;&lt;br&gt;<br /> =&gt;&lt;math&gt;1-\frac {x}{a}=\sqrt{1-u}&lt;/math&gt;&lt;br&gt;<br /> =&gt;&lt;math&gt;u=1-(1-\frac {x}{a})^2&lt;/math&gt;&lt;br&gt;<br /> =&gt;&lt;math&gt;u=\, {\frac {x}{a}} (2-\frac {x}{a})&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;f(x)=\frac {dF(x)}{dx}=\frac {2}{a}-\frac {2x}{a^2}=\, \frac {2}{a} (1-\frac {x}{a})&lt;/math&gt;&lt;br&gt;<br /> [[File:Example_2_diagram.jpg]]<br /> <br /> '''Example 3'''&lt;br&gt;<br /> <br /> Suppose F&lt;sub&gt;X&lt;/sub&gt;(x) = x&lt;sup&gt;n&lt;/sup&gt;, 0 ≤ x ≤ 1, n ∈ N &gt; 0. Generate values from X.&lt;br&gt;<br /> <br /> '''Solution:'''&lt;br&gt;<br /> &lt;br&gt;<br /> 1. Generate &lt;math&gt;U ~\sim~ Unif[0, 1)&lt;/math&gt;&lt;br&gt;<br /> 2. Set &lt;math&gt;X = U^{1/n}&lt;/math&gt;&lt;br&gt;<br /> &lt;br&gt;<br /> For example, when &lt;math&gt;n = 20&lt;/math&gt;,&lt;br&gt;<br /> &lt;math&gt;U = 0.6&lt;/math&gt; =&gt; &lt;math&gt;X = U^{1/20} = 0.974&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;U = 0.5 =&gt;&lt;/math&gt; &lt;math&gt;X = U^{1/20} = 0.966&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;U = 0.2&lt;/math&gt; =&gt; &lt;math&gt;X = U^{1/20} = 0.923&lt;/math&gt;&lt;br&gt;<br /> &lt;br&gt;<br /> Observe from above that the values of X for n = 20 are close to 1, this is because we can view &lt;math&gt;X^n&lt;/math&gt; as the maximum of n independent random variables &lt;math&gt;X,&lt;/math&gt; &lt;math&gt;X~\sim~Unif(0,1)&lt;/math&gt; and is much likely to be close to 1 as n increases. This is because when n is large the exponent tends towards 0. This observation is the motivation for method 2 below.&lt;br&gt;<br /> <br /> Recall that<br /> If Y = max (X&lt;sub&gt;1&lt;/sub&gt;, X&lt;sub&gt;2&lt;/sub&gt;, ... , X&lt;sub&gt;n&lt;/sub&gt;), where X&lt;sub&gt;1&lt;/sub&gt;, X&lt;sub&gt;2&lt;/sub&gt;, ... , X&lt;sub&gt;n&lt;/sub&gt; are independent, &lt;br&gt;<br /> F&lt;sub&gt;Y&lt;/sub&gt;(y) = P(Y ≤ y) = P(max (X&lt;sub&gt;1&lt;/sub&gt;, X&lt;sub&gt;2&lt;/sub&gt;, ... , X&lt;sub&gt;n&lt;/sub&gt;) ≤ y) = P(X&lt;sub&gt;1&lt;/sub&gt; ≤ y, X&lt;sub&gt;2&lt;/sub&gt; ≤ y, ... , X&lt;sub&gt;n&lt;/sub&gt; ≤ y) = F&lt;sub&gt;x&lt;sub&gt;1&lt;/sub&gt;&lt;/sub&gt;(y) F&lt;sub&gt;x&lt;sub&gt;2&lt;/sub&gt;&lt;/sub&gt;(y) ... F&lt;sub&gt;x&lt;sub&gt;n&lt;/sub&gt;&lt;/sub&gt;(y)&lt;br&gt;<br /> Similarly if &lt;math&gt; Y = min(X_1,\ldots,X_n)&lt;/math&gt; then the cdf of &lt;math&gt;Y&lt;/math&gt; is &lt;math&gt;F_Y = 1- &lt;/math&gt;&lt;math&gt;\prod&lt;/math&gt;&lt;math&gt;(1- F_{X_i})&lt;/math&gt;&lt;br&gt; <br /> &lt;br&gt;<br /> '''Method 1:''' Following the above result we can see that in this example, F&lt;sub&gt;X&lt;/sub&gt; = x&lt;sup&gt;n&lt;/sup&gt; is the cumulative distribution function of the max of n uniform random variables between 0 and 1 (since for U~Unif(0, 1), F&lt;sub&gt;U&lt;/sub&gt;(x) = &lt;br&gt;<br /> '''Method 2:''' generate X by having a sample of n independent U~Unif(0, 1) and take the max of the n samples to be x. However, the solution given above using inverse-transform method only requires generating one uniform random number instead of n of them, so it is a more efficient method.<br /> &lt;br&gt;<br /> <br /> Generate the Y = max (X1, X2, ... , Xn), Y = min (X1, X2, ... , Xn), pdf and cdf, but (xi and xj are independent) i,j=1,2,3,4,5.....<br /> <br /> '''Example 4 (New)'''&lt;br&gt;<br /> Now, we are having an similar example as example 1 just doing the maximum way.<br /> <br /> Let X&lt;sub&gt;1&lt;/sub&gt;,X&lt;sub&gt;2&lt;/sub&gt; denote the lifetime of two independent particles:&lt;br&gt;<br /> &lt;math&gt;\, X_1, X_2 \sim exp(\lambda)&lt;/math&gt;&lt;br&gt;<br /> <br /> We are interested in Z=max(X&lt;sub&gt;1&lt;/sub&gt;,X&lt;sub&gt;2&lt;/sub&gt;)&lt;br&gt;<br /> Design an algorithm based on the Inverse-Transform Method to generate samples according to f&lt;sub&gt;Z&lt;/sub&gt;(z)&lt;br&gt;<br /> <br /> &lt;math&gt;\, F_Z(z)=P[Z&lt;=z] = F_{X_1}(z) \cdot F_{X_2}(z) = (1-e^{-\lambda z})^2&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt; \text{thus } F^{-1}(z) = -\frac{1}{\lambda}\log(1-\sqrt z)&lt;/math&gt;&lt;br&gt;<br /> <br /> To sample Z: &lt;br&gt;<br /> &lt;math&gt;\, \text{Step 1: Generate } U \sim U[0,1)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\, \text{Step 2: Let } Z = -\frac{1}{\lambda}\log(1-\sqrt U)&lt;/math&gt;, therefore we can generate random variable of Z.&lt;br&gt;&lt;br&gt;<br /> <br /> '''Discrete Case:'''<br /> &lt;font size=&quot;3&quot;&gt;<br /> u~unif(0,1)&lt;br&gt;<br /> x &lt;- 0, S &lt;- P&lt;sub&gt;0&lt;/sub&gt;&lt;br&gt;<br /> while u &lt; S&lt;br&gt;<br /> x &lt;- x + 1&lt;br&gt;<br /> S &lt;- S + P&lt;sub&gt;0&lt;/sub&gt;&lt;br&gt;<br /> Return x&lt;br&gt;&lt;/font&gt;<br /> <br /> ===Decomposition Method===<br /> The CDF, F, is a composition if &lt;math&gt;F_{X}(x)&lt;/math&gt; can be written as:<br /> <br /> &lt;math&gt;F_{X}(x) = \sum_{i=1}^{n}p_{i}F_{X_{i}}(x)&lt;/math&gt; where<br /> <br /> 1) p&lt;sub&gt;i&lt;/sub&gt; &gt; 0<br /> <br /> 2) &lt;math&gt;\sum_{i=1}^{n}&lt;/math&gt;p&lt;sub&gt;i&lt;/sub&gt; = 1.<br /> <br /> 3) &lt;math&gt;F_{X_{i}}(x)&lt;/math&gt; is a CDF<br /> <br /> The general algorithm to generate random variables from a composition CDF is:<br /> <br /> 1) Generate U,V ~ &lt;math&gt; Unif(0,1)&lt;/math&gt;<br /> <br /> 2) If U &lt; p&lt;sub&gt;1&lt;/sub&gt;, V = &lt;math&gt;F_{X_{1}}(x)&lt;/math&gt;&lt;sup&gt;-1&lt;/sup&gt;<br /> <br /> 3) Else if U &lt; p&lt;sub&gt;1&lt;/sub&gt; + p&lt;sub&gt;2&lt;/sub&gt;, V = &lt;math&gt;F_{X_{2}}(x)&lt;/math&gt;&lt;sup&gt;-1&lt;/sup&gt;<br /> <br /> 4) Repeat from Step 1 (if N randomly generated variables needed, repeat N times)<br /> <br /> &lt;b&gt;Explanation&lt;/b&gt;&lt;br&gt;<br /> Each random variable that is a part of X contributes &lt;math&gt;p_{i} F_{X_{i}}(x)&lt;/math&gt; to &lt;math&gt;F_{X}(x)&lt;/math&gt; every time.<br /> From a sampling point of view, that is equivalent to contributing &lt;math&gt;F_{X_{i}}(x)&lt;/math&gt; &lt;math&gt;p_{i}&lt;/math&gt; of the time. The logic of this is similar to that of the Accept-Reject Method, but instead of rejecting a value depending on the value u takes, we instead decide which distribution to sample it from.<br /> <br /> <br /> &lt;b&gt; Simplified Version &lt;/b&gt;&lt;br&gt;<br /> 1) Generate &lt;math&gt;u \sim Unif(0,1)&lt;/math&gt; &lt;br&gt;<br /> 2) Set &lt;math&gt; X=0, s=P_0&lt;/math&gt;&lt;br&gt;<br /> 3) While &lt;math&gt; u &gt; s, &lt;/math&gt;&lt;br&gt;<br /> set &lt;math&gt; X = X+1&lt;/math&gt; and &lt;math&gt; s=s+P_x &lt;/math&gt; &lt;br&gt;<br /> 4) Return &lt;math&gt; X &lt;/math&gt;<br /> <br /> === Examples of Decomposition Method ===<br /> &lt;b&gt;Example 1&lt;/b&gt; &lt;br&gt;<br /> &lt;math&gt;f(x) = \frac{5}{12}(1+(x-1)^4) 0\leq x\leq 2&lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt;f(x) = \frac{5}{12}+\frac{5}{12}(x-1)^4 = \frac{5}{6} (\frac{1}{2})+\frac {1}{6}(\frac{5}{2})(x-1))^4&lt;/math&gt; &lt;br&gt;<br /> Let&lt;math&gt;f_{x_1}= \frac{1}{2}&lt;/math&gt; and &lt;math&gt;f_{x_2} = \frac {5}{2}(x-1)^4&lt;/math&gt; &lt;br&gt;<br /> <br /> Algorithm: <br /> Generate U~Unif(0,1) &lt;br&gt;<br /> If &lt;math&gt;0&lt;u&lt;\frac {5}{6}&lt;/math&gt;, then we sample from f&lt;sub&gt;x1&lt;/sub&gt; &lt;br&gt;<br /> Else if &lt;math&gt;\frac{5}{6}&lt;u&lt;1&lt;/math&gt;, we sample from f&lt;sub&gt;x2&lt;/sub&gt; &lt;br&gt;<br /> We can find the inverse CDF of f&lt;sub&gt;x2&lt;/sub&gt; and utilize the Inverse Transform Method in order to sample from f&lt;sub&gt;x2&lt;/sub&gt; &lt;br&gt;<br /> Sampling from f&lt;sub&gt;x1&lt;/sub&gt; is more straightforward since it is uniform over the interval (0,2) &lt;br&gt;<br /> <br /> divided f(x) to two pdf of x1 and x2, with uniform distribution, of two range of uniform.<br /> <br /> &lt;b&gt;Example 2&lt;/b&gt; &lt;br&gt;<br /> &lt;math&gt;f(x)=\frac{1}{4}e^{-x}+2x+\frac{1}{12}, \quad 0\leq x \leq 3 &lt;/math&gt; &lt;br&gt;<br /> We can rewrite f(x) as &lt;math&gt;f(x)=(\frac{1}{4}) e^{-x}+(\frac{2}{4}) 4x+(\frac{1}{4}) \frac{1}{3}&lt;/math&gt; &lt;br&gt;<br /> Let f&lt;sub&gt;x1&lt;/sub&gt; = &lt;math&gt;e^{-x}&lt;/math&gt;, f&lt;sub&gt;x2&lt;/sub&gt; = 4x, and f&lt;sub&gt;x3&lt;/sub&gt; = &lt;math&gt;\frac{1}{3}&lt;/math&gt; &lt;br&gt;<br /> Generate U~Unif(0,1)&lt;br&gt;<br /> If &lt;math&gt;0&lt;u&lt;\frac{1}{4}&lt;/math&gt;, we sample from f&lt;sub&gt;x1&lt;/sub&gt; &lt;br&gt;&lt;br&gt;<br /> If &lt;math&gt;\frac{1}{4}\leq u &lt; \frac{3}{4}&lt;/math&gt;, we sample from f&lt;sub&gt;x2&lt;/sub&gt; &lt;br&gt;&lt;br&gt;<br /> Else if &lt;math&gt;\frac{3}{4} \leq u &lt; 1&lt;/math&gt;, we sample from f&lt;sub&gt;x3&lt;/sub&gt; &lt;br&gt;<br /> We can find the inverse CDFs of f&lt;sub&gt;x1&lt;/sub&gt; and f&lt;sub&gt;x2&lt;/sub&gt; and utilize the Inverse Transform Method in order to sample from f&lt;sub&gt;x1&lt;/sub&gt; and f&lt;sub&gt;x2&lt;/sub&gt; &lt;br&gt;&lt;br&gt;<br /> We find F&lt;sub&gt;x1&lt;/sub&gt; = &lt;math&gt; 1-e^{-x}&lt;/math&gt; and F&lt;sub&gt;x2&lt;/sub&gt; = &lt;math&gt;2x^{2}&lt;/math&gt; &lt;br&gt;<br /> We find the inverses are &lt;math&gt; X = -ln(1-u)&lt;/math&gt; for F&lt;sub&gt;x1&lt;/sub&gt; and &lt;math&gt; X = \sqrt{\frac{U}{2}}&lt;/math&gt; for F&lt;sub&gt;x2&lt;/sub&gt; &lt;br&gt;<br /> Sampling from f&lt;sub&gt;x3&lt;/sub&gt; is more straightforward since it is uniform over the interval (0,3) &lt;br&gt;<br /> <br /> In general, to write an &lt;b&gt;efficient &lt;/b&gt; algorithm for: &lt;br&gt;<br /> &lt;math&gt;F_{X}(x) = p_{1}F_{X_{1}}(x) + p_{2}F_{X_{2}}(x) + ... + p_{n}F_{X_{n}}(x)&lt;/math&gt; &lt;br&gt;<br /> We would first calculate &lt;math&gt; {q_i} = \sum_{j=1}^i p_j, \forall i = 1,\dots, n&lt;/math&gt;<br /> Then Generate &lt;math&gt; U \sim~ Unif(0,1) &lt;/math&gt; &lt;br&gt;<br /> If &lt;math&gt; U &lt; q_1 &lt;/math&gt; sample from &lt;math&gt; f_1 &lt;/math&gt; &lt;br&gt;<br /> else if &lt;math&gt; u&lt;q_i &lt;/math&gt; sample from &lt;math&gt; f_i &lt;/math&gt; for &lt;math&gt; 1 &lt; i &lt; n &lt;/math&gt;&lt;br&gt;<br /> else sample from &lt;math&gt; f_n &lt;/math&gt; &lt;br&gt;<br /> <br /> when we divided the pdf of different range of f(x1) f(x2) and f(x3), and generate all of them and inverse, U~U(0,1)<br /> &lt;div style=&quot;background:#66CCFF;padding:20px;border-radius:5px;&quot;&gt;<br /> <br /> === Example of Decomposition Method ===<br /> <br /> &lt;math&gt;F_x(x) = \frac {1}{3} x+\frac {1}{3} x^2+\frac {1}{3} x^3, 0\leq x\leq 1&lt;/math&gt;<br /> <br /> Let &lt;math&gt;U =F_x(x) = \frac {1}{3} x+\frac {1}{3} x^2+\frac {1}{3} x^3&lt;/math&gt;, solve for x.<br /> <br /> &lt;math&gt;P_1=\frac{1}{3}, F_{x1} (x)= x, P_2=\frac{1}{3},F_{x2} (x)= x^2, <br /> P_3=\frac{1}{3},F_{x3} (x)= x^3&lt;/math&gt;<br /> <br /> '''Algorithm:'''<br /> <br /> Generate &lt;math&gt;\,U \sim Unif [0,1)&lt;/math&gt;<br /> <br /> Generate &lt;math&gt;\,V \sim Unif [0,1)&lt;/math&gt;<br /> <br /> if &lt;math&gt;0\leq u \leq \frac{1}{3}, x = v&lt;/math&gt;<br /> <br /> else if &lt;math&gt;u \leq \frac{2}{3}, x = v^{\frac{1}{2}}&lt;/math&gt;<br /> <br /> else &lt;math&gt;x=v^{\frac{1}{3}}&lt;/math&gt; &lt;br&gt;<br /> <br /> <br /> '''Matlab Code:''' <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> u=rand # U is <br /> v=rand<br /> if u&lt;1/3<br /> x=v<br /> elseif u&lt;2/3<br /> x=sqrt(v)<br /> else<br /> x=v^(1/3)<br /> end<br /> &lt;/pre&gt;<br /> <br /> <br /> &lt;span style=&quot;margin:0 auto;&quot;&gt;=== Example of Decomposition Method(new) ===&lt;/span&gt;<br /> <br /> F&lt;sub&gt;x&lt;/sub&gt;(x) = 1/2*x+1/2*x&lt;sup&gt;2&lt;/sup&gt;, 0&lt;= x&lt;=1<br /> <br /> let U =F&lt;sub&gt;x&lt;/sub&gt;(x) = 1/2*x+1/2*x&lt;sup&gt;2&lt;/sup&gt;, solve for x.<br /> <br /> P&lt;sub&gt;1&lt;/sub&gt;=1/2, F&lt;sub&gt;x1&lt;/sub&gt;(x)= x, P&lt;sub&gt;2&lt;/sub&gt;=1/2,F&lt;sub&gt;x2&lt;/sub&gt;(x)= x&lt;sup&gt;2&lt;/sup&gt;, <br /> <br /> '''Algorithm:'''<br /> <br /> Generate U ~ Unif [0,1)<br /> <br /> Generate V~ Unif [0,1)<br /> <br /> if 0&lt;u&lt;1/2, x = v<br /> <br /> else x = v&lt;sup&gt;1/2&lt;/sup&gt;<br /> <br /> <br /> '''Matlab Code:''' <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> u=rand<br /> v=rand<br /> if u&lt;1/2<br /> x=v<br /> else<br /> x=sqrt(v)<br /> end<br /> &lt;/pre&gt;<br /> &lt;/div&gt;<br /> <br /> '''Extra Knowledge about Decomposition Method'''<br /> <br /> There are different types and applications of Decomposition Method<br /> <br /> 1. Primal decomposition<br /> <br /> 2. Dual decomposition<br /> <br /> 3. Decomposition with constraints<br /> <br /> 4. More general decomposition structures<br /> <br /> 5. Rate control<br /> <br /> 6. Single commodity network ﬂow<br /> <br /> For More Details, please refer to http://www.stanford.edu/class/ee364b/notes/decomposition_notes.pdf<br /> <br /> ===Fundamental Theorem of Simulation===<br /> Consider two shapes, A and B, where B is a sub-shape (subset) of A. <br /> We want to sample uniformly from inside the shape B.<br /> Then we can sample uniformly inside of A, and throw away all samples outside of B, and this will leave us with a uniform sample from within B. <br /> (Basis of the Accept-Reject algorithm)<br /> <br /> The advantage of this method is that we can sample a unknown distribution from a easy distribution. The disadvantage of this method is that it may need to reject many points, which is inefficient.&lt;br /&gt;<br /> Inverse each part of partial CDF, the partial CDF is divided by the original CDF, partial range is uniform distribution.&lt;br /&gt;<br /> More specific definition of the theorem can be found here.&lt;ref&gt;http://www.bus.emory.edu/breno/teaching/MCMC_GibbsHandouts.pdf&lt;/ref&gt;<br /> <br /> Matlab code:<br /> <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> close all <br /> clear all<br /> ii=1;<br /> while ii&lt;1000<br /> u=rand<br /> y=R*(2*U-1)<br /> if (1-U^2)&gt;=(2*u-1)^2<br /> x(ii)=y;<br /> ii=ii+1<br /> end<br /> &lt;/pre&gt;<br /> <br /> ===Question 2===<br /> <br /> Use Acceptance and Rejection Method to sample from &lt;math&gt;f_X(x)=b*x^n*(1-x)^n&lt;/math&gt; , &lt;math&gt;n&gt;0&lt;/math&gt;, &lt;math&gt;0&lt;x&lt;1&lt;/math&gt;<br /> <br /> Solution:<br /> This is a beta distribution, Beta ~&lt;math&gt;\int _{0}^{1}b*x^{n}*(1-x)^{n}dx = 1&lt;/math&gt;<br /> <br /> U&lt;sub&gt;1~Unif[0,1)<br /> <br /> <br /> U&lt;sub&gt;2~Unif[0,1)<br /> <br /> fx=&lt;math&gt; bx^{1/2}(1-x)^{1/2} &lt;= bx^{-1/2}\sqrt2 ,0&lt;=x&lt;=1/2 &lt;/math&gt;<br /> <br /> <br /> <br /> The beta distribution maximized at 0.5 with value &lt;math&gt;(1/4)^n&lt;/math&gt;.<br /> So, &lt;math&gt;c=b*(1/4)^n&lt;/math&gt;&lt;br /&gt;<br /> Algorithm: &lt;br /&gt;<br /> 1.Draw &lt;math&gt;U_1&lt;/math&gt; from &lt;math&gt;U(0, 1)&lt;/math&gt;. &lt;math&gt; U_2&lt;/math&gt; from &lt;math&gt;U(0, 1)&lt;/math&gt; &lt;br /&gt;<br /> 2.If &lt;math&gt;U_2&lt;=b*(U_1)^n*(1-(U_1))^n/b*(1/4)^n=(4*(U_1)*(1-(U_1)))^n&lt;/math&gt;&lt;br /&gt;<br /> then X=U_1<br /> Else return to step 1.<br /> <br /> Discrete Case:<br /> Most discrete random variables do not have a closed form inverse CDF. Also, its CDF &lt;math&gt;F:X \rightarrow [0,1]&lt;/math&gt; is not necessarily onto. This means that not every point in the interval &lt;math&gt; [0,1] &lt;/math&gt; has a preimage in the support set of X through the CDF function.&lt;br /&gt;<br /> <br /> Let &lt;math&gt;X&lt;/math&gt; be a discrete random variable where &lt;math&gt;a \leq X \leq b&lt;/math&gt; and &lt;math&gt;a,b \in \mathbb{Z}&lt;/math&gt; . &lt;br&gt;<br /> To sample from &lt;math&gt;X&lt;/math&gt;, we use the partition method below: &lt;br&gt;<br /> <br /> &lt;math&gt;\, \text{Step 1: Generate u from } U \sim Unif[0,1]&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\, \text{Step 2: Set } x=a, s=P(X=a)&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;\, \text{Step 3: While } u&gt;s, x=x+1, s=s+P(X=x)&lt;/math&gt; &lt;br /&gt;<br /> &lt;math&gt;\, \text{Step 4: Return } x&lt;/math&gt;&lt;br /&gt;<br /> <br /> ==Class 8 - Thursday, May 30, 2013==<br /> <br /> In this lecture, we will discuss algorithms to generate 3 well-known distributions: Binomial, Geometric and Poisson. For each of these distributions, we will first state its general understanding, probability mass function, expectation and variance. Then, we will derive one or more algorithms to sample from each of these distributions, and implement the algorithms on Matlab. &lt;br \&gt;<br /> <br /> ===The Bernoulli distribution===<br /> <br /> The Bernoulli distribution is a special case of the binomial distribution, where n = 1. X ~ Bin(1, p) has the same meaning as X ~ Ber(p), where p is the probability of success and 1-p is the probability of failure (we usually define a variate q, q= 1-p). The mean of Bernoulli is p and the variance is p(1-p). Bin(n, p), is the distribution of the sum of n independent Bernoulli trials, Bernoulli(p), each with the same probability p, where 0&lt;p&lt;1. &lt;br&gt;<br /> For example, let X be the event that a coin toss results in a &quot;head&quot; with probability ''p'', then ''X~Bernoulli(p)''. &lt;br&gt;<br /> P(X=1)= p<br /> P(X=0)= q = 1-p<br /> Therefore, P(X=0) + P(X=1) = p + q = 1<br /> <br /> '''Algorithm: '''<br /> <br /> 1) Generate &lt;math&gt;u\sim~Unif(0,1)&lt;/math&gt; &lt;br&gt;<br /> 2) If &lt;math&gt;u \leq p&lt;/math&gt;, then &lt;math&gt;x = 1 &lt;/math&gt;&lt;br&gt;<br /> else &lt;math&gt;x = 0&lt;/math&gt; &lt;br&gt;<br /> The answer is: &lt;br&gt;<br /> when &lt;math&gt; U \leq p, x=1&lt;/math&gt; &lt;br&gt;<br /> when &lt;math&gt;U \geq p, x=0&lt;/math&gt;&lt;br&gt;<br /> 3) Repeat as necessary<br /> <br /> * '''Matlab Code'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt; p = 0.8 % an arbitrary probability for example<br /> &gt;&gt; for i = 1: 100<br /> &gt;&gt; u = rand;<br /> &gt;&gt; if u &lt; p<br /> &gt;&gt; x(ii) = 1;<br /> &gt;&gt; else<br /> &gt;&gt; x(ii) = 0;<br /> &gt;&gt; end<br /> &gt;&gt; end<br /> &gt;&gt; hist(x)<br /> &lt;/pre&gt;<br /> <br /> ===The Binomial Distribution===<br /> <br /> In general, if the random variable X follows the binomial distribution with parameters n and p, we write X ~ Bin(n, p). <br /> (Acknowledge: https://en.wikipedia.org/wiki/Binomial_distribution)<br /> If X ~ B(n, p), then its pmf is of form:<br /> <br /> f(x)=(nCx) p&lt;sup&gt;x&lt;/sup&gt;(1-p)&lt;sup&gt;(n-x)&lt;/sup&gt;, x=0,1,...n&lt;br /&gt;<br /> Or f(x) = &lt;math&gt;(n!/x!(n-x)!)&lt;/math&gt; p&lt;sup&gt;x&lt;/sup&gt;(1-p)&lt;sup&gt;(n-x)&lt;/sup&gt;, x=0,1,...n &lt;br /&gt;<br /> <br /> Mean (x) = E(x) = &lt;math&gt; np &lt;/math&gt;<br /> Variance = &lt;math&gt; np(1-p) &lt;/math&gt;&lt;br/&gt;<br /> <br /> Generate n uniform random number &lt;math&gt;U_1,...,U_n&lt;/math&gt; and let X be the number of &lt;math&gt;U_i&lt;/math&gt; that are less than or equal to p.<br /> The logic behind this algorithm is that the Binomial Distribution is simply a Bernoulli Trial, with a probability of success of p, repeated n times. Thus, we can sample from the distribution by sampling from n Bernoulli. The sum of these n bernoulli trials will represent one binomial sampling. Thus, in the below example, we are sampling 1000 realizations from 20 Bernoulli random variables. By summing up the rows of the 20 by 1000 matrix that is produced, we are summing up the 20 bernoulli outcomes to produce one binomial sampling. We have 1000 rows, which means we have realizations from 1000 binomial random variables when this sum is done (the output of the sum is a 1 by 1000 sized vector).&lt;br /&gt;<br /> To continue with the previous example, let X be the number of heads in a series of ''n'' independent coin tosses - where for each toss, the probability of coming up with a head is ''p'' - then ''X~Bin(n, p)''. &lt;br /&gt;<br /> MATLAB tips: to get a pdf f(x), we can use code binornd(N,P). N means number of trials and p is the probability of success. a=[2 3 4],if set a&lt;3, will produce a=[1 0 0]. If you set &quot;a == 3&quot;, it will produce [0 1 0]. If a=[2 6 9 10], if set a&lt;4, will produce a=[1 0 0 0], because only the first element (2) is less than 4, meanwhile the rest are greater. So we can use this to get the number which is less than p.&lt;br /&gt;<br /> <br /> Algorithm for Bernoulli is given as above<br /> <br /> '''Code'''&lt;br&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;a=[3 5 8];<br /> &gt;&gt;a&lt;5<br /> ans= 1 0 0<br /> <br /> &gt;&gt;rand(20,1000)<br /> &gt;&gt;rand(20,1000)&lt;0.4<br /> &gt;&gt;A = sum(rand(20,1000)&lt;0.4) #sum of raws ~ Bin(20 , 0.3)<br /> &gt;&gt;hist(A)<br /> &gt;&gt;mean(A)<br /> Note: 1 in the above code means sum the matrix by column<br /> <br /> &gt;&gt;sum(sum(rand(20,1000)&lt;0.4)&gt;8)/1000<br /> This is an estimate of Pr[A&gt;8].<br /> <br /> &lt;/pre&gt;<br /> <br /> [[File:Binomial_example.jpg|300px]]<br /> <br /> remark: a=[2 3 4],if set a&lt;3, will produce a=[1 0 0]. If you set &quot;a == 3&quot;, it will produce [0 1 0].<br /> using code to find some value what i want to get from the matrix. Its useful to define some matrixs.<br /> <br /> Relation between Bernoulli Distribution and Binomial Distribution:<br /> For instance, we want to find numbers ≤0.3. Uniform collects which is ≤0.3, and Binomial calculates how many numbers are there ≤0.3.<br /> <br /> ===The Geometric Distribution===<br /> Geometric distribution is a discrete distribution. There are two types geometric distributions, the first one is the probability distribution of the number of X Bernoulli fail trials, with probability 1-p, needed until the first success situation happened, X come from the set { 1, 2, 3, ...}; the other one is the probability distribution of the number Y = X − 1 of failures, with probability 1-p, before the first success, Y comes from the set { 0, 1, 2, 3, ... }.<br /> <br /> For example,&lt;br /&gt;<br /> If the success event showed at the first time, which x=1, then f(x)=p.&lt;br /&gt;<br /> If the success event showed at the second time and failed at the first time, which x = 2, then f(x)= p(1-p).&lt;br /&gt;<br /> If the success event showed at the third time and failed at the first and second time, which x = 3, then f(x)= p(1-p)&lt;sup&gt;2 &lt;/sup&gt;. etc.&lt;br /&gt;<br /> If the success event showed at the k time and all failed before time k, which implies x = k, then f(k)= p(1-p)&lt;sup&gt;(k-1)&lt;/sup&gt;&lt;br /&gt;<br /> which is,&lt;br /&gt;<br /> x Pr&lt;br /&gt;<br /> 1 P&lt;br /&gt;<br /> 2 P(1-P)&lt;br /&gt;<br /> 3 P(1-P)&lt;sup&gt;2&lt;/sup&gt;&lt;br /&gt;<br /> . .&lt;br /&gt;<br /> . .&lt;br /&gt;<br /> . .&lt;br /&gt;<br /> n P(1-P)&lt;sup&gt;(n-1)&lt;/sup&gt;&lt;br /&gt;<br /> Also, the sequence of the outputs of the probability is a geometric sequence.<br /> <br /> For example, suppose a die is thrown repeatedly until the first time a &quot;6&quot; appears. This is a question of geometric distribution of the number of times on the set { 1, 2, 3, ... } with p = 1/6.<br /> <br /> Generally speaking, if X~G(p) then its pdf is of the form f(x)=(1-p)&lt;sup&gt;(x-1)&lt;/sup&gt;*p, x=1,2,...&lt;br /&gt;<br /> The random variable X is the number of trials required until the first success in a series of independent''' Bernoulli trials'''.&lt;br /&gt;<br /> <br /> <br /> <br /> Other properties<br /> <br /> <br /> Probability mass function : P(X=k) = p(1-p)&lt;sup&gt;(k-1)&lt;/sup&gt;<br /> <br /> Tail probability : P(X&gt;n) = &lt;math&gt;(1-p)^n&lt;/math&gt;<br /> <br /> The CDF : P(X&lt;n) = 1 - &lt;math&gt;(1-p)^n&lt;/math&gt;<br /> <br /> <br /> &lt;span style=&quot;background:#F5F5DC&quot;&gt;<br /> <br /> Mean of x = 1/p<br /> Var(x) = (1-p)/p^2<br /> <br /> There are two ways to look at a geometric distribution.<br /> <br /> &lt;b&gt;1st Method&lt;/b&gt;<br /> <br /> We look at the number of trials before the first success. This includes the last trial in which you succeeded. This will be used in our course. <br /> <br /> pdf is of form f(x)=&gt;(1-p)&lt;sup&gt;(x-1)&lt;/sup&gt;*(p), x = 1, 2, 3, ...<br /> <br /> &lt;b&gt;2nd Method&lt;/b&gt;<br /> <br /> This involves modeling the failure before the first success. This does not include the last trial in which we succeeded. <br /> <br /> pdf is of form f(x)=&gt; ((1-p)^x)*p , x = 0, 1, 2, ....<br /> <br /> &lt;/span&gt;<br /> <br /> <br /> If Y~Exp(&lt;math&gt;\lambda&lt;/math&gt;) then &lt;math&gt;X=\left \lfloor Y \right \rfloor+1&lt;/math&gt; is geometric.&lt;br /&gt;<br /> Choose e^(-&lt;math&gt;\lambda&lt;/math&gt;)=1-p. Then X ~ geo (p) &lt;br /&gt;<br /> <br /> P (X &gt; x) = (1-p)&lt;sup&gt;x&lt;/sup&gt;(because first x trials are not successful) &lt;br/&gt;<br /> <br /> NB: An advantage of using this method is that nothing is rejected. We accept all the points, and the method is more efficient. Also, this method is closer to the inverse transform method as nothing is being rejected. &lt;br /&gt;<br /> <br /> '''Proof''' &lt;br/&gt;<br /> <br /> &lt;math&gt;P(X&gt;x) = P( \left \lfloor Y \right \rfloor + 1 &gt; X) = P(\left \lfloor Y \right \rfloor &gt; x- 1) = P(Y&gt;= x) = e^{-\lambda × x} &lt;/math&gt; &lt;br&gt;<br /> <br /> SInce p = 1- e&lt;sup&gt;-&lt;math&gt;\lambda&lt;/math&gt;&lt;/sup&gt; or &lt;math&gt;\lambda&lt;/math&gt;= &lt;math&gt;-log(1-p)&lt;/math&gt;(compare the pdf of exponential distribution and Geometric distribution,we can look at e&lt;sup&gt;-&lt;math&gt;\lambda&lt;/math&gt;&lt;/sup&gt; the probability of the fail trial), then &lt;br&gt;<br /> <br /> P(X&gt;x) = e&lt;sup&gt;(-&lt;math&gt;\lambda&lt;/math&gt; * x)&lt;/sup&gt; = e&lt;sup&gt;log(1-p)*x&lt;/sup&gt; = (1-p)&lt;sup&gt;x&lt;/sup&gt; &lt;br/&gt;<br /> <br /> Note that floor(Y)&gt;X -&gt; Y &gt;= X+1 (X is an integer) &lt;br/&gt;<br /> <br /> proof how to use EXP distribution to find P(X&gt;x)=(1-p)^x<br /> <br /> &lt;br&gt;<br /> Suppose X has the exponential distribution with rate parameter &lt;math&gt; \lambda &gt; 0 &lt;/math&gt; &lt;br&gt;<br /> the &lt;math&gt;\left \lfloor X \right \rfloor &lt;/math&gt; and &lt;math&gt;\left \lceil X \right \rceil &lt;/math&gt; have geometric distribution on &lt;math&gt; \mathcal{N} &lt;/math&gt; and &lt;math&gt; \mathcal{N}_{+} &lt;/math&gt; respectively each with success probability &lt;math&gt; 1-e^ {- \lambda} &lt;/math&gt; &lt;br&gt;<br /> <br /> Proof: &lt;br&gt;<br /> &lt;math&gt;\text{For } n \in \mathcal{N} &lt;/math&gt;&lt;br//&gt;<br /> <br /> &lt;math&gt;\begin{align}<br /> P(\left \lfloor X \right \rfloor = n)&amp;{}= P( n \leq X &lt; n+1) \\<br /> &amp;{}= F( n+1) - F(n) \\<br /> \text{By algebra and simplification:} \\<br /> P(\left \lfloor X \right \rfloor = n)&amp;{}= (e^ {-\lambda})^n \cdot (1 - e^ {-\lambda}) \\<br /> &amp;{}= Geo (1 - e^ {-\lambda}) \\<br /> <br /> \text{Proof of ceiling part follows immediately.} \\<br /> \end{align}&lt;/math&gt; &lt;br//&gt;<br /> <br /> <br /> <br /> <br /> <br /> '''Algorithm:''' &lt;br /&gt;<br /> 1) Let &lt;math&gt;\lambda = -\log (1-p) &lt;/math&gt;&lt;br /&gt;<br /> 2) Generate a &lt;math&gt;Y \sim Exp(\lambda )&lt;/math&gt; &lt;br /&gt;<br /> 3) We can then let &lt;math&gt;X = \left \lfloor Y \right \rfloor + 1, where X\sim Geo(p)&lt;/math&gt; &lt;br /&gt;<br /> note: &lt;math&gt;\left \lfloor Y \right \rfloor &gt;2 -&gt; Y&gt;=3&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt; \left \lfloor Y \right \rfloor &gt;5 -&gt; Y&gt;=6&lt;/math&gt;&lt;br /&gt;<br /> &lt;br /&gt;<br /> <br /> &lt;math&gt;\left \lfloor Y \right \rfloor&gt;x &lt;/math&gt; -&gt; Y&gt;= X+1 &lt;br /&gt;<br /> <br /> &lt;math&gt;P(Y&gt;=X)&lt;/math&gt;&lt;br /&gt;<br /> Y ~ Exp (&lt;math&gt;\lambda&lt;/math&gt;)&lt;br /&gt;<br /> pdf of Y : &lt;math&gt;\lambda e^{-\lambda}&lt;/math&gt;&lt;br /&gt;<br /> cdf of Y : &lt;math&gt;1- e^{-\lambda}&lt;/math&gt;&lt;br /&gt;<br /> cdf &lt;math&gt;P(Y&lt;x)=1-e^{-\lambda x}&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;P(Y&gt;=x)=1-(1- e^{-\lambda x})=e^{-\lambda x}&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt; e^{-\lambda}=1-p -&gt; -log(1-p)=\lambda&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;P(Y&gt;=x)=e^{-\lambda x}=e^{log(1-p)x}=(1-p)^x&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;E[x]=1/P &lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;Var= (1-P)/(P^2)&lt;/math&gt;&lt;br /&gt;<br /> P(X&gt;x)&lt;br /&gt;<br /> =P(floor(y)+1&gt;x)&lt;br /&gt;<br /> =P(floor(y)&gt;x-1)&lt;br /&gt;<br /> =P(y&gt;=x)<br /> <br /> use &lt;math&gt;e^{-\lambda}=1-p&lt;/math&gt; to figure out the mean and variance.<br /> '''Code'''&lt;br&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;p=0.4;<br /> &gt;&gt;l=-log(1-p);<br /> &gt;&gt;u=rand(1,1000);<br /> &gt;&gt;y=(-1/l)*log(u);<br /> &gt;&gt;x=floor(y)+1;<br /> &gt;&gt;hist(x)<br /> <br /> ===Note:===<br /> mean(x)~E[X]=&gt; 1/p<br /> Var(x)~V[X]=&gt; (1-p)/p^2<br /> <br /> A specific Example:<br /> Consider x=5<br /> &gt;&gt; sum(x==5)/1000 -&gt; chance that will succeed at fifth trial;<br /> &gt;&gt; ans = <br /> 0.0780<br /> &gt;&gt; sum(x&gt;10)/1000 -&gt; chance that will succeed after 10 trials<br /> &gt;&gt; ans = <br /> 0.0320<br /> <br /> &lt;/pre&gt;<br /> <br /> Note that the above mean is the average amount of times you should try until you get a successful case.&lt;br/&gt;<br /> <br /> [[File:Geometric_example.jpg|300px]]<br /> <br /> &lt;span style=&quot;background:#F5F5DC&quot;&gt;<br /> EXAMPLE for geometric distribution: Consider the case of rolling a die: &lt;/span&gt;<br /> <br /> X=the number of rolls that it takes for the number 5 to appear. <br /> <br /> We have X ~Geo(1/6), &lt;math&gt;f(x)=(1/6)*(5/6)^{x-1}&lt;/math&gt;, x=1,2,3.... <br /> <br /> Now, let &lt;math&gt;\left \lfloor Y \right \rfloor=e^{\lambda}&lt;/math&gt; =&gt; x=floor(Y) +1 <br /> <br /> Let &lt;math&gt;e^{-\lambda}=5/6&lt;/math&gt; <br /> <br /> &lt;math&gt;P(X&gt;x) = P(Y&gt;=x)&lt;/math&gt; (from the class notes) <br /> <br /> We have &lt;math&gt;e^{-\lambda *x} = (5/6)^x&lt;/math&gt; <br /> <br /> Algorithm: let &lt;math&gt;\lambda = -\log(5/6)&lt;/math&gt; <br /> <br /> 1) Let Y be &lt;math&gt;e^{\lambda}&lt;/math&gt;, exponentially distributed <br /> <br /> 2) Set &lt;math&gt;X= \left \lfloor Y \right \rfloor +1 &lt;/math&gt;, to generate X <br /> <br /> &lt;math&gt; E[x]=6, Var[X]=5/6 /(1/6^2) = 30 &lt;/math&gt;<br /> <br /> <br /> &lt;span style=&quot;background:#F5F5DC&quot;&gt;GENERATING NEGATIVE BINOMIAL RV USING GEOMETRIC RV'S&lt;/span&gt;<br /> <br /> Property of negative binomial Random Variable: &lt;br/&gt;<br /> <br /> The negative binomial random variable is a sum of r independent geometric random variables.&lt;br/&gt;<br /> <br /> Using this property we can formulate the following algorithm:&lt;br/&gt;<br /> <br /> Step 1: Generate r geometric rv's each with probability p using the procedure presented above.&lt;br/&gt;<br /> Step 2: Take the sum of these r geometric rv's. This RV follows NB(r,p)&lt;br/&gt;<br /> <br /> remark the step 1 and step 2. Looking for the floor Y, and e^(-mu)=1-p=5/6, and then generate x.<br /> <br /> ===Poisson Distribution===<br /> If &lt;math&gt;\displaystyle X \sim \text{Poi}(\lambda)&lt;/math&gt;, its pdf is of the form &lt;math&gt;\displaystyle \, f(x) = \frac{e^{-\lambda}\lambda^x}{x!}&lt;/math&gt; , where &lt;math&gt;\displaystyle \lambda &lt;/math&gt; is the rate parameter.&lt;br /&gt;<br /> <br /> definition:In probability theory and statistics, the Poisson distribution (pronounced [pwasɔ̃]) is a discrete probability distribution that expresses the probability of a given number of events occurring in a fixed interval of time and/or space if these events occur with a known average rate and independently of the time since the last event. The Poisson distribution can also be used for the number of events in other specified intervals such as distance, area or volume.<br /> For instance, suppose someone typically gets 4 pieces of mail per day on average. There will be, however, a certain spread: sometimes a little more, sometimes a little less, once in a while nothing at all. Given only the average rate, for a certain period of observation (pieces of mail per day, phonecalls per hour, etc.), and assuming that the process, or mix of processes, that produces the event flow is essentially random, the Poisson distribution specifies how likely it is that the count will be 3, or 5, or 10, or any other number, during one period of observation. That is, it predicts the degree of spread around a known average rate of occurrence.<br /> The Derivation of the Poisson distribution section shows the relation with a formal definition.(from Wikipedia)<br /> <br /> Understanding of Poisson distribution:<br /> <br /> If customers '''independently''' come to bank over time, all following exponential distributions with rate &lt;math&gt;\lambda&lt;/math&gt; per unit of time, then <br /> X(t) = # of customer in [0,t] ~ Poi&lt;math&gt;(\lambda t)&lt;/math&gt;<br /> <br /> Its mean and variance are&lt;br /&gt;<br /> &lt;math&gt;\displaystyle E[X]=\lambda&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;\displaystyle Var[X]=\lambda&lt;/math&gt;&lt;br /&gt;<br /> An useful property: If &lt;math&gt;X_i \sim \mathrm{Pois}(\lambda_i)\, i=1,\dots,n&lt;/math&gt; are independent and &lt;math&gt;\lambda=\sum_{i=1}^n \lambda_i&lt;/math&gt;, then &lt;math&gt;Y = \left( \sum_{i=1}^n X_i \right) \sim \mathrm{Pois}(\lambda)&lt;/math&gt;<br /> <br /> A Poisson random variable X can be interpreted as the maximal number of i.i.d. (Independent and Identically Distributed) exponential variables(with parameter &lt;math&gt;\lambda&lt;/math&gt;) whose sum does not exceed 1.&lt;br /&gt;<br /> The traditional understanding of the Poisson distribution as the total number of events in a specific interval can be understood here since the above definition simply describes the Poisson as the sum of waiting times for n events in an interval of length 1.<br /> &lt;br /&gt;<br /> &lt;br /&gt;<br /> &lt;math&gt;\displaystyle\text{Let } Y_j \sim \text{Exp}(\lambda), U_j \sim \text{Unif}(0,1)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;Y_j = -\frac{1}{\lambda}\log(U_j) \text{ from Inverse Transform Method}&lt;/math&gt;&lt;br&gt;&lt;br&gt;<br /> <br /> &lt;math&gt;\begin{align} <br /> X &amp;= \max \{ n: \sum_{j=1}^{n} Y_j \leq 1 \} \\<br /> &amp;= \max \{ n: \sum_{j=1}^{n} - \frac{1}{\lambda}\log(U_j) \leq 1 \} \\<br /> &amp;= \max \{ n: \sum_{j=1}^{n} \log(U_j) &gt;= -\lambda \} \\<br /> &amp;= \max \{ n: \log(\prod_{j=1}^{n} U_j) &gt;= -\lambda \} \\<br /> &amp;= \max \{ n: \prod_{j=1}^{n} U_j &gt;= e^{-\lambda} \} \\<br /> &amp;= \min \{ n: \prod_{j=1}^{n} U_j &gt;= e^{-\lambda} \} - 1 \\<br /> \end{align}&lt;/math&gt;&lt;br&gt;&lt;br /&gt;<br /> <br /> Note: From above, we can use Logarithm Rules &lt;math&gt;\log(a)+\log(b)=\log(ab)&lt;/math&gt; to generate the result.&lt;br&gt;&lt;br /&gt;<br /> '''Algorithm:''' &lt;br /&gt;<br /> 1) Set n=1, a=1 &lt;br /&gt;<br /> 2) Generate &lt;math&gt;U_n \sim U(0,1), a=aU_n &lt;/math&gt; &lt;br /&gt;<br /> 3) If &lt;math&gt;a &gt;= e^{-\lambda}&lt;/math&gt; , then n=n+1, and go to Step 2. Else, x=n-1 &lt;br /&gt;<br /> <br /> using inverse-method to proof mean and variance of poisson distribution.<br /> <br /> ===MATLAB Code for generating Poisson Distribution===<br /> &lt;pre style='font-size:16px'&gt;<br /> &gt;&gt;l=2; N=1000 <br /> &gt;&gt;for ii=1:N<br /> n=1;<br /> a=1;<br /> u=rand;<br /> a=a*u;<br /> while a&gt;exp(-l)<br /> n=n+1;<br /> u=rand;<br /> a=a*u;<br /> end<br /> x(ii)=n-1;<br /> end<br /> &gt;&gt;hist(x)<br /> &gt;&gt;Sum(x==1)/N # Probability of x=1<br /> &gt;&gt;Sum(x&gt;3)/N # Probability of x &gt; 3<br /> &lt;/pre&gt;<br /> <br /> [[File:Poisson_example.jpg|300px]]<br /> <br /> === Another way to generate random variable from poisson distribution ===<br /> &lt;br/&gt;<br /> Note: &lt;math&gt;P(X=x)=\frac {e^{-\lambda}\lambda^x}{x!}, \forall x \in \N&lt;/math&gt;&lt;br/&gt;<br /> Let &lt;math&gt;\displaystyle p(x) = P(X=x)&lt;/math&gt; denote the pmf of &lt;math&gt;\displaystyle X&lt;/math&gt;.&lt;br/&gt;<br /> Then ratio is &lt;math&gt;\frac{p(x+1)}{p(x)}=\frac{\lambda}{x+1}, \forall x \in \N&lt;/math&gt; &lt;br/&gt;<br /> Therefore, &lt;math&gt;p(x+1)=\frac{\lambda}{x+1}p(x)&lt;/math&gt; &lt;br/&gt;<br /> Algorithm: &lt;br/&gt;<br /> 1. Set &lt;math&gt;\displaystyle x=0&lt;/math&gt;&lt;br/&gt;<br /> 2. Set &lt;math&gt;\displaystyle F=p=e^{-\lambda}&lt;/math&gt; &lt;br/&gt;<br /> 3. Generate &lt;math&gt;\displaystyle U \sim~ \text{Unif}(0,1)&lt;/math&gt; &lt;br/&gt;<br /> 4. If &lt;math&gt;\displaystyle U&lt;F&lt;/math&gt;, output &lt;math&gt;\displaystyle x&lt;/math&gt;&lt;br/&gt;<br /> Else&lt;br/&gt;<br /> &lt;math&gt;\displaystyle p=\frac{\lambda}{x+1} p&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;\displaystyle F=F+p&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;\displaystyle x = x+1&lt;/math&gt;&lt;br/&gt;<br /> Go to 4.&lt;br/&gt;<br /> <br /> This is indeed the inverse-transform method, with a clever way to calculate the CDF on the fly.<br /> <br /> u=rand(0.1000)<br /> hist(x)<br /> <br /> == Class 9 - Tuesday, June 4, 2013 ==<br /> <br /> === Beta Distribution ===<br /> The beta distribution is a continuous probability distribution. &lt;br&gt;<br /> PDF:&lt;math&gt;\displaystyle \text{ } f(x) = \frac{\Gamma(\alpha+\beta)}{\Gamma(\alpha)\Gamma(\beta)}x^{\alpha-1}(1-x)^{\beta-1} &lt;/math&gt;&lt;br&gt; where &lt;math&gt;0 \leq x \leq 1&lt;/math&gt; and &lt;math&gt;\alpha&lt;/math&gt;&gt;0, &lt;math&gt;\beta&lt;/math&gt;&gt;0&lt;br/&gt;<br /> &lt;div style = &quot;align:left; background:#F5F5DC; font-size: 120%&quot;&gt;<br /> Definition:<br /> In probability theory and statistics, the beta distribution is a family of continuous probability distributions defined on the interval [0, 1] parametrized by two positive shape parameters, denoted by α and β, that appear as exponents of the random variable and control the shape of the distribution.&lt;br/.&gt;<br /> More can be find in the link: &lt;ref&gt;http://en.wikipedia.org/wiki/Beta_distribution&lt;/ref&gt;<br /> &lt;/div&gt;<br /> <br /> There are two positive shape parameters in this distribution defined as alpha and beta: &lt;br&gt;<br /> -Both parameters are greater than 0, and X is within the interval [0,1]. &lt;br&gt;<br /> -Alpha is used as exponents of the random variable. &lt;br&gt;<br /> -Beta is used to control the shape of the this distribution. We use the beta distribution to build the model of the behavior of random variables, which are limited to intervals of finite length. &lt;br&gt;<br /> -For example, we can use the beta distribution to analyze the time allocation of sunshine data and variability of soil properties. &lt;br&gt;<br /> <br /> If X~Beta(&lt;math&gt;\alpha, \beta&lt;/math&gt;) then its p.d.f. is of the form<br /> <br /> :&lt;math&gt;\displaystyle \text{ } f(x) = \frac{\Gamma(\alpha+\beta)}{\Gamma(\alpha)\Gamma(\beta)}x^{\alpha-1}(1-x)^{\beta-1} &lt;/math&gt; where &lt;math&gt;0 \leq x \leq 1&lt;/math&gt; and &lt;math&gt;\alpha&lt;/math&gt;&gt;0, &lt;math&gt;\beta&lt;/math&gt;&gt;0&lt;br&gt;<br /> and<br /> &lt;math&gt;f(x;\alpha,\beta)= 0 &lt;/math&gt; otherwise<br /> Note: &lt;math&gt;\Gamma(\alpha)=(\alpha-1)! &lt;/math&gt; if &lt;math&gt;\alpha&lt;/math&gt; is a positive integer.<br /> <br /> Note: Gamma Function Properties<br /> <br /> If &lt;math&gt;\alpha=\frac{1}{2} , <br /> <br /> \Gamma(\frac {1}{2})=\sqrt\pi &lt;/math&gt; <br /> <br /> The mean of the beta distribution is &lt;math&gt;\frac{\alpha}{\alpha + \beta}&lt;/math&gt;. The variance is &lt;math&gt;\frac{\alpha\beta}{(\alpha+\beta)^2 (\alpha + \beta + 1)}&lt;/math&gt;<br /> The variance of the beta distribution decreases monotonically if &lt;math&gt; \alpha = \beta &lt;/math&gt; and as &lt;math&gt; \alpha = \beta &lt;/math&gt; increases, the variance decreases. <br /> <br /> The formula for the cumulative distribution function of the beta distribution is also called the incomplete beta function ratio (commonly denoted by Ix) and is defined as F(x) = I(x)(p,q) <br /> <br /> To generate random variables of a Beta distribution, there are multiple cases depending on the value of &lt;math&gt;\alpha &lt;/math&gt; and &lt;math&gt; \beta &lt;/math&gt;:<br /> <br /> '''Case 1:''' If &lt;math&gt;\alpha=1&lt;/math&gt; and &lt;math&gt;\beta=1&lt;/math&gt;<br /> <br /> :&lt;math&gt;\displaystyle \text{Beta}(1,1) = \frac{\Gamma(1+1)}{\Gamma(1)\Gamma(1)}x^{1-1}(1-x)^{1-1}&lt;/math&gt;&lt;br&gt;<br /> :&lt;math&gt; = \frac{1!}{0!0!}x^{0}(1-x)^{0}&lt;/math&gt;&lt;br&gt;<br /> :&lt;math&gt; = 1 &lt;/math&gt;&lt;br&gt;<br /> <br /> Note: 0! = 1. &lt;br&gt;<br /> Hence, the distribution is:&lt;br&gt;<br /> :&lt;math&gt;\displaystyle \text{Beta}(1,1) = U (0, 1) &lt;/math&gt;&lt;br&gt;<br /> If the Question asks for sampling Beta Distribution, we can sample from Uniform Distribution which we already know how to sample from&lt;br&gt;<br /> Algorithm:&lt;br&gt;<br /> Generate U~Unif(0,1)&lt;br&gt;<br /> <br /> '''Case 2:''' Either &lt;math&gt;\alpha=1&lt;/math&gt; or &lt;math&gt;\beta=1&lt;/math&gt;<br /> <br /> <br /> e.g. &lt;math&gt;\alpha=1&lt;/math&gt;<br /> We don't make any assumption about &lt;math&gt;\beta&lt;/math&gt; except that it is a positive integer. &lt;br\&gt;<br /> <br /> :&lt;math&gt;\displaystyle \text{f}(x) = \frac{\Gamma(1+\beta)}{\Gamma(1)\Gamma(\beta)}x^{1-1}(1-x)^{\beta-1}=\beta(1-x)^{\beta-1}&lt;/math&gt;&lt;br&gt;<br /> <br /> :&lt;math&gt;\beta=1 &lt;/math&gt;<br /> <br /> :&lt;math&gt;\displaystyle \text{f}(x) = \frac{\Gamma(\alpha+1)}{\Gamma(\alpha)\Gamma(1)}x^{\alpha-1}(1-x)^{1-1}=\alpha x^{\alpha-1}&lt;/math&gt;&lt;br&gt;<br /> <br /> By integrating &lt;math&gt;f(x)&lt;/math&gt;, we find the CDF of X is &lt;math&gt;F(x) = x^{\alpha}&lt;/math&gt;.<br /> As &lt;math&gt;F(x)^{-1} = x^\frac {1}{\alpha}&lt;/math&gt;, using the inverse transform method, &lt;math&gt; X = U^\frac {1}{\alpha} &lt;/math&gt; with U ~ U[0,1].<br /> <br /> '''Algorithm'''<br /> <br /> :1. Generate U~Unif(0,1)&lt;br\&gt;<br /> :2. Assign &lt;math&gt;x = u^\frac {1}{\alpha}&lt;/math&gt;<br /> <br /> After we have simplified this example, we can use other distribution methods to solve the problem.<br /> <br /> '''MATLAB Code to generate random n variables using the above algorithm'''<br /> &lt;pre style='font-size:16px'&gt;<br /> <br /> x = rand(1,n).^(1/alpha) <br /> &lt;/pre&gt;<br /> <br /> '''Case 3:'''&lt;br\&gt; To sample from beta in general, we use the property that &lt;br\&gt;<br /> <br /> :if &lt;math&gt;Y_1&lt;/math&gt; follows gamma &lt;math&gt;(\alpha,1)&lt;/math&gt;&lt;br\&gt;<br /> : &lt;math&gt;Y_2&lt;/math&gt; follows gamma &lt;math&gt;(\beta,1)&lt;/math&gt;&lt;br\&gt;<br /> <br /> '''Note:<br /> 1. &lt;math&gt;\alpha&lt;/math&gt; and &lt;math&gt;\beta&lt;/math&gt; are shape parameters here and 1 is the scale parameter.&lt;br\&gt;'''<br /> :then &lt;math&gt;Y=\frac {Y_1}{Y_1+Y_2}&lt;/math&gt; follows Beta &lt;math&gt;(\alpha,\beta)&lt;/math&gt;&lt;br\&gt;<br /> 2.Exponential: &lt;math&gt;-\frac{1}{\lambda} \log(u)&lt;/math&gt; &lt;br\&gt;<br /> 3.Gamma: &lt;math&gt;-\frac{1}{\lambda} \log(u_1 * \cdots * u_t)&lt;/math&gt;&lt;br\&gt;<br /> <br /> '''Algorithm'''&lt;br\&gt;<br /> *1. Sample from Y1 ~ Gamma (&lt;math&gt;\alpha&lt;/math&gt;,1) &lt;math&gt;\alpha&lt;/math&gt; is the shape, and 1 is the scale. &lt;br\&gt;<br /> *2. Sample from Y2 ~ Gamma (&lt;math&gt;\beta&lt;/math&gt;,1) &lt;br\&gt;<br /> *3. Set <br /> :&lt;math&gt; Y = \frac{Y_1}{Y_1+Y_2}&lt;/math&gt;&lt;br&gt;<br /> <br /> Please see the following example for Matlab code. &lt;br&gt; <br /> <br /> <br /> '''Case 4:'''&lt;br\&gt; Use The Acceptance-Rejection Method &lt;br\&gt;<br /> The beta density is&lt;br /&gt;<br /> &lt;math&gt;\displaystyle \text{Beta}(\alpha,\beta) = \frac{\Gamma(\alpha+\beta)}{\Gamma(\alpha)\Gamma(\beta)}x^{\alpha-1}(1-x)^{\beta-1} &lt;/math&gt; where &lt;math&gt;0 \leq x \leq 1&lt;/math&gt;&lt;br&gt;<br /> Assume &lt;math&gt;\alpha,\beta \geq 1&lt;/math&gt;. Then &lt;math&gt;\displaystyle f(x)&lt;/math&gt; has the maximum at &lt;math&gt;\frac{\alpha-1}{\alpha+\beta-2}&lt;/math&gt;.&lt;br /&gt;<br /> (Please note that we could find the maximum by taking the derivative of f(x), let f'(x)=0 and then use maximum likelihood estimate to find what the maximum value is)&lt;br&gt;<br /> Define&lt;br /&gt;<br /> &lt;math&gt; c=f(\frac{\alpha-1}{\alpha+\beta-2}&lt;/math&gt;) and choose &lt;math&gt;\displaystyle g(x)=1&lt;/math&gt;.&lt;br /&gt;<br /> The A-R method becomes&lt;br /&gt;<br /> 1.Generate independent &lt;math&gt;\displaystyle U_1&lt;/math&gt; and &lt;math&gt;\displaystyle U_2&lt;/math&gt; from &lt;math&gt;\displaystyle UNIF[0,1]&lt;/math&gt; until &lt;math&gt;\displaystyle cU_2 \leq f(U_1)&lt;/math&gt;;&lt;br /&gt;<br /> 2.Return &lt;math&gt;\displaystyle U_1&lt;/math&gt;.&lt;br /&gt;<br /> '''MATLAB Code for generating Beta Distribution'''<br /> &lt;pre style='font-size:16px'&gt;<br /> &gt;&gt;Y1 = sum(-log(rand(10,1000))) #Gamma(10,1), sum 10 exponentials for each of the 1000 samples<br /> <br /> &gt;&gt;Y2 = sum(-log(rand(5,1000))) #Gamma(5,1), sum 5 exponentials for each of the 1000 samples<br /> <br /> %NOTE: here, lamda is 1, since the scale parameter for Y1 &amp; Y2 are both 1<br /> <br /> &gt;&gt;Y=Y1./(Y1+Y2) #Don't forget to divide elements using &quot;.&quot; Where Y follows Beta(10,5)<br /> <br /> &gt;&gt;figure <br /> <br /> &gt;&gt;hist(Y1) #Gamma curve<br /> <br /> &gt;&gt;figure<br /> <br /> &gt;&gt;hist(Y2) #Gamma curve<br /> <br /> &gt;&gt;figure<br /> <br /> &gt;&gt;hist(Y) #Do this to check that the shape fits beta. ~Beta(10,5).<br /> <br /> &gt;&gt;disttool #Check the beta plot.<br /> <br /> &lt;/pre&gt;<br /> This is the histogram of Y, precisely simulated version of Beta (10,5)<br /> <br /> <br /> [[File:Beta(10,5)_Simulated.jpg|300px]]<br /> <br /> This is the pdf of various beta distributions<br /> <br /> [[File:325px-Beta_distribution_pdf.png|300px]]<br /> <br /> [[File:untitled.jpg|300px]]&lt;br /&gt;<br /> MATLAB tips: rand(10,1000) produces one 10*1000 matrix and sum(rand(10,1000)) produces a 10*1000 matrix<br /> and each element in the matrix follows CDF of uniform distribution.<br /> <br /> Example for the code to explain the beta distribution.<br /> <br /> <br /> '''Another MATLAB Code for generating Beta Distribution using AR method'''<br /> &lt;pre style='font-size:16px'&gt;<br /> &gt;&gt;alpha = 3<br /> &gt;&gt;beta = 2<br /> &gt;&gt; a = sum (-log(rand(alpha,1000)))<br /> &gt;&gt; b = sum (-log(rand(beta,1000)))<br /> &gt;&gt; aandb=sum(-log(rand(alpha+beta,1000)))<br /> &gt;&gt; t = (alpha - 1)/(alpha + beta -2)<br /> &gt;&gt; c = (andb/(a*b))*t^(alpha-1)*(1-t)^(beta-1)<br /> &gt;&gt; u1 = rand<br /> &gt;&gt; u2 = rand<br /> &gt;&gt; x = (andb/(a*b))*u1^(alpha-1)*(1-u1)^(beta-1)<br /> &gt;&gt; while c*u2&gt;x<br /> &gt;&gt; u1 = rand<br /> &gt;&gt; u2 = rand<br /> &gt;&gt; x = (andb/(a*b))*u1^(alpha-1)*(1-u1)^(beta-1)<br /> &gt;&gt; end<br /> &gt;&gt; u1<br /> &lt;/pre&gt;<br /> <br /> === Random Vector Generation ===<br /> We want to sample from &lt;math&gt;X = (X_1, X_2, &lt;/math&gt;…,&lt;math&gt; X_d)&lt;/math&gt;, a d-dimensional vector from a known pdf &lt;math&gt;f(x)&lt;/math&gt; and cdf &lt;math&gt;F(x)&lt;/math&gt;.<br /> We need to take into account the following two cases: <br /> <br /> ====Case 1====<br /> if the &lt;math&gt;x_1, x_2 \cdots, x_d&lt;/math&gt;'s are independent, then&lt;br/&gt;<br /> &lt;math&gt;f(x) = f(x_1,\cdots, x_d) = f(x_1)\cdots f(x_d)&lt;/math&gt;&lt;br/&gt;<br /> we can sample from each component &lt;math&gt;x_1, x_2,\cdots, x_d&lt;/math&gt; individually, and then form a vector.&lt;br/&gt;<br /> <br /> based on the property of independence, we can derive the pdf or pmf of &lt;math&gt;x=x_1,x_2,x_3,x_4,x_5,\cdots&lt;/math&gt;<br /> <br /> ====Case 2====<br /> If &lt;math&gt;X_1, X_2, \cdots , X_d&lt;/math&gt; are not independent&lt;br/&gt;<br /> &lt;math&gt;f(x) = f(x_1, \cdots , x_d) = f(x_1) f(x_2|x_1) \cdots f(x_d|x_{d-1},\cdots ,x_1)&lt;/math&gt;&lt;br/&gt;<br /> we need to know the conditional distributions of &lt;math&gt;f(x_2|x_1), f(x_3|x_2, x_1),\cdots, f(x_d|x_{d-1}, \cdots, x_1)&lt;/math&gt;&lt;br/&gt;<br /> This is generally a hard problem. Conditional probabilities are not easy to compute, then sampling from these would be based on your statistics knowledge.<br /> In each case, we have to consider the previous cases.<br /> &lt;math&gt;f(x_1)&lt;/math&gt; is one-dimensional, some as &lt;math&gt;f(x_2|x_1)&lt;/math&gt; and all others.<br /> In general, one could consider the covariance matrix &lt;math&gt; C &lt;/math&gt; of random variables &lt;math&gt; X_1&lt;/math&gt;,…,&lt;math&gt;X_d &lt;/math&gt;. &lt;br&gt;<br /> Suppose we now have the Cholesky factor &lt;math&gt; G&lt;/math&gt; of &lt;math&gt; C &lt;/math&gt; (i.e. &lt;math&gt; C = GG^T &lt;/math&gt;). In matlab, we use Chol(C) &lt;br&gt; <br /> For any d-tuple &lt;math&gt; X := (X_1 ,\ldots , X_d) &lt;/math&gt; (i.e random variable generated by &lt;math&gt; X_1,\ldots , X_d &lt;/math&gt; respectively)<br /> &lt;math&gt; GX &lt;/math&gt; would yield the desired distribution. &lt;br/&gt;<br /> <br /> '''Note''' (Product Rule)&lt;br/&gt;<br /> 1.) All cases can use this (independent or dependent): &lt;math&gt;f(x) = f(x_1, x_2)= f(x_1) f(x_2|x_1)&lt;/math&gt; &lt;br/&gt;<br /> 2.) If we determine that &lt;math&gt;x_1&lt;/math&gt; and &lt;math&gt; x_2&lt;/math&gt; are ''independent'', then we can use &lt;math&gt;f(x) = f(x_1, x_2)= f(x_1)f(x_2)&lt;/math&gt; &lt;br/&gt;<br /> *ie. If late for class=&lt;math&gt;x_1&lt;/math&gt; and sick=&lt;math&gt;x_2&lt;/math&gt;, then these are dependent variables so can only use equation 1 (&lt;math&gt;f(x) = f(x_1, x_2)= f(x_1) f(x_2|x_1)&lt;/math&gt;)&lt;br/&gt;<br /> *ie. If late for class=&lt;math&gt;x_1&lt;/math&gt; and milk is white=&lt;math&gt;x_2&lt;/math&gt;, then these are independent variables so can use both equations 1 and 2. &lt;br/&gt;<br /> <br /> the case show the formula of the X = (X1,X2,…,Xd), a d-dimensional vector, when they are not independent of each x. we use conditional function to define the probability function of x with d-dimensional.<br /> <br /> ====Example====<br /> Generate uniform random vectors<br /> <br /> 1) x = (x&lt;sub&gt;1&lt;/sub&gt;, …, x&lt;sub&gt;d&lt;/sub&gt;) from the d-dimensional rectangle &lt;br/&gt;<br /> 2) D = { (x&lt;sub&gt;1&lt;/sub&gt;, …, x&lt;sub&gt;d&lt;/sub&gt;) : a&lt;sub&gt;i&lt;/sub&gt; &lt;= x&lt;sub&gt;i&lt;/sub&gt; &lt;= b&lt;sub&gt;i&lt;/sub&gt; , i = 1, …, d} &lt;br/&gt;<br /> <br /> Algorithm: &lt;br/&gt;<br /> 1) For i = 1 to d &lt;br/&gt;<br /> 2) U&lt;sub&gt;i&lt;/sub&gt; ~ U(0,1) &lt;br/&gt;<br /> 3) x&lt;sub&gt;i&lt;/sub&gt; = a&lt;sub&gt;i&lt;/sub&gt; + U(b&lt;sub&gt;i&lt;/sub&gt;-a&lt;sub&gt;i&lt;/sub&gt;) &lt;br/&gt;<br /> 4) End &lt;br/&gt;<br /> <br /> *Note: x&lt;sub&gt;i&lt;/sub&gt; = a&lt;sub&gt;i&lt;/sub&gt; + U(b&lt;sub&gt;i&lt;/sub&gt;-a&lt;sub&gt;i&lt;/sub&gt;) denotes X&lt;sub&gt;i&lt;/sub&gt; ~U(a&lt;sub&gt;i&lt;/sub&gt;,b&lt;sub&gt;i&lt;/sub&gt;) &lt;br/&gt;<br /> <br /> An example of the 2-D case is given below:<br /> <br /> &lt;pre style='font-size:14px'&gt;<br /> &gt;&gt;a=[1 2]; <br /> &gt;&gt;b=[4 6]; <br /> &gt;&gt;for i=1:2<br /> u(i) = rand(); <br /> x(i) = a(i) + (b(i) - a(i))*u(i);<br /> end<br /> <br /> &gt;&gt;hold on =&gt; this is to retain current graph when adding new graphs<br /> &gt;&gt;rectangle('Position',[1 2 3 4]) =&gt; draw the boundary of the rectangle<br /> &gt;&gt;axis([0 10 0 10]) =&gt; change the size of axes<br /> &gt;&gt;plot(x(1),x(2),'.')<br /> <br /> &lt;/pre&gt;<br /> [[File:2d_ex.jpg|300px]]<br /> <br /> ==== Matlab Code: ====<br /> <br /> &lt;pre style='font-size:14px'&gt;<br /> function x = urectangle (d,n,a,b)<br /> for ii = 1:d;<br /> u(ii,:) = rand(1,n);<br /> x(ii,:) = a+ u(ii,:)*(b-a);<br /> %keyboard #makes the function stop at this step so you can evaluate the variables<br /> end<br /> <br /> &gt;&gt;x=urectangle(2, 100, 2, 5);<br /> &gt;&gt;scatter(x(1,:),x(2,:))<br /> <br /> &gt;&gt;x=urectangle(2, 10000, 2, 5); #generate 10000 numbers (instead of 100)<br /> &gt;&gt;x=urectangle(3, 10000, 2, 5); #changed to 3-dimensional<br /> &gt;&gt;scatter3(x(1,:), x(2,:), x(3,:))<br /> &gt;&gt;axis square<br /> &lt;/pre&gt;<br /> [[File:Urectangle_2d.jpg|300px]][[File:Urectangle_3d.jpg|300px]]<br /> <br /> === Vector Acceptance-Rejection Method ===<br /> <br /> The acceptance-rejection method can be extended to n-dimensional cases, with the same concept:<br /> <br /> If a random vector is to be generated uniformly from G, an irregular shape in the nth dimension, and W is a regular shape arbitrarily close to G in the nth dimension, then acceptance-rejection method can be applied as follows:<br /> <br /> 1. Sample from the regular shape W<br /> <br /> 2. Accept sample points if they are inside G<br /> <br /> &lt;br&gt;'''Example:''' &lt;br&gt; <br /> Generate a random vector Z that is uniformly distributed over region G<br /> <br /> G: d-dimensional unit ball, &lt;math&gt;G = \big\{{x: \sum_{i}{x_i}^2 \leq 1}\big\}&lt;/math&gt;<br /> <br /> w: d-dimensional hypercube, &lt;math&gt;W = \big\{{-1 \leq x_i \leq 1}\big\}_{i=1}^d&lt;/math&gt;<br /> <br /> '''Procedure:'''&lt;br /&gt;<br /> Step 1: &lt;math&gt;U_1 \sim~ U(0,1),\cdots, U_d \sim~ U(0,1)&lt;/math&gt;&lt;br /&gt;<br /> Step 2: &lt;math&gt;X_1 = 1 - 2U_1, \cdots, X_d = 1 - 2U_d, R = \sum_i X_i^2&lt;/math&gt;&lt;br /&gt;<br /> Step 3: If &lt;math&gt;R \leq 1, Z=(X_1, ..... , X_d)&lt;/math&gt;&lt;br /&gt;<br /> Else go to step 1<br /> <br /> it is an example of the vector A/R, regular shape is W likes the proposal distribution g(x), G is the target distribution g(x) &lt;br\&gt;<br /> <br /> Suppose we sampled from the target area W uniformly, let Aw, Ag indicate the area of W and G, g(x)=1/Aw and f(x)=1/Ag<br /> <br /> <br /> The following is a picture relating to the example<br /> <br /> [[File:Untitled.jpg]]<br /> <br /> Matlab code:<br /> &lt;pre style='font-size:16px'&gt;<br /> u = rand(d,n);<br /> z = 1- 2 *u;<br /> R = sum(z.^2);<br /> jj=1;<br /> <br /> for ii=1:n<br /> <br /> if R(ii)&lt;=1<br /> <br /> x(:,jj)=z(:,ii);<br /> jj=jj+1;<br /> <br /> end<br /> <br /> end<br /> <br /> output = x;<br /> <br /> end<br /> &lt;/pre&gt;<br /> <br /> ==Class 10 - Thursday June 6th 2013 == <br /> MATLAB code for using Acceptance/Rejection Method to sample from a d-dimensional unit ball.<br /> G: d-dimensional unit ball G<br /> W: d-dimensional Hypercube<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> 1) U1~UNIF(0,1)<br /> U2~UNIF(0,1)<br /> ...<br /> Ud~UNIF(0,1)<br /> 2) X1 = 1-2U1<br /> X2 = 1-2U2<br /> ...<br /> Xd = 1-2Ud<br /> R = sum(Xi^2)<br /> 3) If R&lt;=1<br /> X = (X1,X2,...,Xd),<br /> else go to step 1<br /> &lt;/pre&gt;<br /> <br /> ==== Code: ====<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> function output = Unitball(d,n) <br /> <br /> u = rand(d,n);<br /> z = 1- 2 *u;<br /> R = sum(z.^2);<br /> jj=1;<br /> <br /> for ii=1:n<br /> <br /> if R(ii)&lt;=1<br /> <br /> x(:,jj)=z(:,ii);<br /> jj=jj+1;<br /> <br /> end<br /> <br /> end<br /> <br /> output = x;<br /> <br /> end<br /> <br /> &gt;&gt; data = Unitball(d, n)<br /> &gt;&gt; scatter(data(1,:), data(2,:)) %plot 2d graph<br /> <br /> R(ii) computes the sum of the square of each element of a vector, so if it is less than 1,<br /> then the vector is in the unit ball.<br /> <br /> x(:,jj) means all the numbers in the jj column.<br /> <br /> z(:,ii) means all the numbers in the ii column starting from 1st column until the nth<br /> column, which is the last one.<br /> <br /> higher dimension, less efficient and we need more data points<br /> <br /> Save it with the name of the pattern.<br /> <br /> &lt;/pre&gt;<br /> <br /> <br /> [[File:Unitball.png|400px]]<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> Execution: <br /> <br /> &gt;&gt;[x]=Unitball(2,10000);<br /> &gt;&gt;scatter(x(1,:),x(2,:)); %plot 2D circle<br /> &gt;&gt;axis square; %make the x-y axis has same size <br /> &gt;&gt;size(x)<br /> <br /> ans =<br /> <br /> 2 7839<br /> <br /> &gt;&gt;scatter(x(1,:),x(2,:))<br /> &lt;/pre&gt;<br /> <br /> scatter(x(1,:),x(2,:)) the (x(1,:) means all the numbers in the first row are parameter.<br /> <br /> '''Calculate the efficiency:'''<br /> &lt;pre style='font-size:16px'&gt;<br /> <br /> &gt;&gt;c=7839/10000 %Efficiency = points accepted / total points <br /> <br /> c =<br /> <br /> 0.7839<br /> <br /> &lt;/pre&gt;<br /> <br /> We can use the above program to calculate how many points in the circle condition are in the square.<br /> <br /> '''Estimate &lt;math&gt;\displaystyle \pi&lt;/math&gt;'''<br /> :*We know the radius is 1<br /> :*Then the area of the square is &lt;math&gt;(1-(-1))^2=4&lt;/math&gt;&lt;br\&gt;<br /> :*Then the area of the circle is &lt;math&gt;\pi&lt;/math&gt;&lt;br\&gt;<br /> :*&lt;math&gt;\pi&lt;/math&gt; is approximated to be &lt;math&gt;4\times c=4 \times 0.7839=3.1356&lt;/math&gt; in the above example &lt;br\&gt;<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> &gt;&gt; 4*size(x,2)/10000<br /> <br /> ans =<br /> <br /> 3.1356<br /> <br /> &gt;&gt; [x]=Unitball(3,10000);<br /> &gt;&gt; scatter3(x(1,:),x(2,:),x(3,:)) %plot 3d ball<br /> &gt;&gt; axis square<br /> &gt;&gt; size(x,2)/10000 %returns the size of the dimension of X specified by scalar 2<br /> <br /> ans =<br /> <br /> 0.5231<br /> <br /> &gt;&gt; [x]=Unitball(5,10000);<br /> &gt;&gt; size(x,2)/10000<br /> <br /> ans =<br /> <br /> 0.1648<br /> <br /> &lt;/pre&gt;<br /> 3d unit ball<br /> <br /> [[File:3-dimensional unitball.jpg|400px]]<br /> <br /> Note that c increases exponentially as d increases, which will result in a lower acceptance rate and more points being rejected. So this method is not efficient for large values of d.<br /> <br /> In practice, when we need to vectorlize a high quality image or genes then d would have to be very large. So AR method is not an efficient way to solve the problem.<br /> <br /> === Efficiency ===<br /> <br /> In the above example, the efficiency of the vector A/R is equal to the ratio<br /> <br /> &lt;math&gt;\frac{1}{C}=\frac{\text{volume of hyperball}}{\text{volume of hybercube}}= \max \frac{g(x)}{f(x)} &lt;/math&gt;<br /> <br /> In general, the efficiency can be thought of as the total number of points accepted divided by the total number of points generated.<br /> <br /> As the dimension '''increase''', the efficiency of the algorithm will '''decrease''' exponentially.<br /> <br /> For example, for approximating value of &lt;math&gt;\pi&lt;/math&gt;, when &lt;math&gt;d \text{(dimension)} =2&lt;/math&gt;, the efficiency is around 0.7869; when &lt;math&gt;d=3&lt;/math&gt;, the efficiency is around 0.5244; when &lt;math&gt;d=10&lt;/math&gt;, the efficiency is around 0.0026: it is getting close to 0.<br /> <br /> A 'C' value of 1 implies an acceptance rate of 100% (most efficient scenario) but as we sample from higher dimensions, 'C' usually gets larger. Thus, when we want to generate high dimension vectors, Acceptance-Rejection Method is not efficient to be used.<br /> <br /> &lt;span style=&quot;color:red;padding:0 auto;&quot;&gt;&lt;br&gt;The end of midterm coverage&lt;/span&gt;<br /> &lt;div style=&quot;border:1px solid #cccccc;border-radius:10px;box-shadow: 0 5px 15px 1px rgba(0, 0, 0, 0.6), 0 0 200px 1px rgba(255, 255, 255, 0.5);padding:20px;margin:20px;background:#FFFFAD;&quot;&gt;<br /> &lt;h2 style=&quot;text-align:center;&quot;&gt;Summary of vector acceptance-rejection sampling&lt;/h2&gt;<br /> &lt;p&gt;&lt;b&gt;Problem:&lt;/b&gt; &lt;math&gt; f(x_1, x_2, ...x_n)&lt;/math&gt; is difficult to sample from&lt;/p&gt;<br /> &lt;p&gt;&lt;b&gt;Plan:&lt;/b&gt;&lt;/p&gt;<br /> Let W represent the sample space covered by &lt;math&gt; f(x_1, x_2, ...x_n)&lt;/math&gt;<br /> &lt;ol&gt;<br /> &lt;li&gt;1.Draw &lt;math&gt;\vec{y}=y_1,y_2...y_n\sim~g()&lt;/math&gt; where g has sample space G which is greater than W. g is a distribution that is easy to sample from (i.e. uniform)&lt;/li&gt;<br /> &lt;li&gt;2.if &lt;math&gt;\vec{y} \subseteq W &lt;/math&gt; then &lt;math&gt;\vec{x}=\vec{y} &lt;/math&gt;&lt;br /&gt; else go 1) &lt;/li&gt;<br /> &lt;/ol&gt;<br /> &lt;p&gt;x will have the desired distribution.&lt;/p&gt;<br /> <br /> &lt;/div&gt;<br /> <br /> ==== Stochastic Process ====<br /> The basic idea of Stochastic Process (also called random process) is a collection of some random variables, <br /> &lt;math&gt;\big\{X_t:t\in T\big\}&lt;/math&gt;, where the set X is called the state space that each variable is in it and T is called the index set. <br /> <br /> '''Definition:''' In probability theory, a stochastic process /stoʊˈkæstɪk/, or sometimes random process (widely used) is a collection of random variables; this is often used to represent the evolution of some random value, or system, over time. This is the probabilistic counterpart to a deterministic process (or deterministic system). Instead of describing a process which can only evolve in one way (as in the case, for example, of solutions of an ordinary differential equation), in a stochastic or random process there is some indeterminacy: even if the initial condition (or starting point) is known, there are several (often infinitely many) directions in which the process may evolve. (from Wikipedia)<br /> <br /> A stochastic process is non-deterministic. This means that even if we know the initial condition(state), and we know some possibilities of the states to follow, the exact value of the final state remains to be uncertain. <br /> <br /> We can illustrate this with an example of speech: if &quot;I&quot; is the first word in a sentence, the set of words that could follow would be limited (eg. like, want, am), and the same happens for the third word and so on. The words then have some probabilities among them such that each of them is a random variable, and the sentence would be a collection of random variables. &lt;br&gt;<br /> Also, Different Stochastic Process has different properties.<br /> <br /> In the course, we study two Stochastic Process models.<br /> <br /> The two stochastic Process models we will study are:<br /> <br /> 1. Poisson Process-This is continuous time counting process that satisfies a couple of properties that are listed in the next section. The Poisson process is understood to be a good model for events such as incoming phone calls, number of traffic accidents, and goals during a game of hockey or soccer. It is also an example of a birth-death process.&lt;br&gt;<br /> 2. Markov Process- This is a stochastic process that satisfies the Markov property which can be understood as the memory-less property. The property states that the jump to a future state only depends on the current state of the process, and not of the process's history. This model is used to model random walks exhibited by particles, the health state of a life insurance policyholder, decision making by a memory-less mouse in a maze, etc. &lt;br&gt;<br /> <br /> <br /> =====Example=====<br /> The state space is the set of English words, and &lt;math&gt;x_t&lt;/math&gt; are words that are said. Another example involves the stock market: the set of all non-negative numbers is the state space, and &lt;math&gt;x_t&lt;/math&gt; are stock prices.<br /> <br /> stochastic process always has state space and the index set to limit the range.<br /> <br /> The state space is the set of cars, while &lt;math&gt;x_t&lt;/math&gt; are sport cars.<br /> <br /> Births in a hospital occur randomly at an average rate<br /> <br /> The number of cases of a disease in different towns<br /> <br /> ==== Poisson Process ====<br /> [[File:Possionprocessidiagram.png‎]]<br /> <br /> The Poisson process is a discrete counting process which counts the number of&lt;br\&gt;<br /> of events and the time that these occur in a given time interval.&lt;br\&gt;<br /> <br /> e.g traffic accidents , arrival of emails. Emails arrive at random time &lt;math&gt;T_1, T_2 ... T_n&lt;/math&gt; for example (2, 7, 3) is the number of emails received on day 1, day 2, day 3. This is a stochastic process and Poisson process with condition.<br /> <br /> The probability of observing x events in a given interval is given by<br /> &lt;math&gt; P(X = x) = e^{-\lambda}* \lambda^x/ x! &lt;/math&gt;<br /> where x = 0; 1; 2; 3; 4; ....<br /> <br /> -Let &lt;math&gt;N_t&lt;/math&gt; denote the number of arrivals within the time interval &lt;math&gt;(0,t]&lt;/math&gt;&lt;br\&gt;<br /> -Let &lt;math&gt;N(a,b]&lt;/math&gt; denote the number of arrivals in the time interval (a,b]&lt;br\&gt;<br /> -By definition, &lt;math&gt;N(a,b]=N_b-N_a&lt;/math&gt;&lt;br\&gt;<br /> -The two random variables &lt;math&gt;N(a,b]&lt;/math&gt; and &lt;math&gt;N(c,d]&lt;/math&gt; are independent if &lt;math&gt;(a,b]&lt;/math&gt; and &lt;math&gt;(c,d]&lt;/math&gt; do not intersect.&lt;br\&gt;<br /> <br /> <br /> '''Definition:''' An arrival counting process &lt;math&gt;N=(N_t)&lt;/math&gt; is called (Homogeneous) Poisson Process (PP) with rate &lt;math&gt;\lambda &gt; 0&lt;/math&gt; if <br /> <br /> A. The numbers of points in non-overlapping intervals are independent. &lt;br \&gt;<br /> B. The number of points in interval &lt;math&gt;I(a,b]&lt;/math&gt; has a poisson distribution with mean &lt;math&gt;\lambda (b-a)&lt;/math&gt; ,where (b-a) represents the length of I.<br /> <br /> In particular, observe that if &lt;math&gt;N=(N_t)&lt;/math&gt; is a Poisson process of rate &lt;math&gt;\lambda&gt;0&lt;/math&gt;, then the moments are<br /> E[N&lt;sub&gt;t&lt;/sub&gt;] = &lt;math&gt;\lambda t&lt;/math&gt; and Var[N&lt;sub&gt;t&lt;/sub&gt;] = &lt;math&gt;\lambda t&lt;/math&gt;<br /> <br /> the rate parameter may change over time; such a process is called a non-homogeneous Poisson process<br /> <br /> ==== Examples ====<br /> &lt;br /&gt;<br /> '''How to generate a multivariate normal with the built-in function &quot;randn&quot;: (example)'''&lt;br /&gt;<br /> (please check the general idea at the end of lecture 6 course note.)<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> z=randn(length(mu),n); %Here length(mu)=2 since s is a 2*2 matrix;<br /> s=[1 0.7;0.7 1]<br /> A=chol(s)% This only works for a positive definite A, otherwise we need to consider some other form of decomposition<br /> x=mu'*ones(1,n) + A*z; %ones(1,n) is a function expand the size of mu from 1*1<br /> %matrix to 1*n matrix;<br /> &lt;/pre&gt;<br /> For example, if we use mu = [2 5], we would get &lt;br/&gt;<br /> &lt;math&gt; = \left[ \begin{array}{ccc}<br /> 3.8214 &amp; 0.3447 \\<br /> 6.3097 &amp; 5.6157 \end{array} \right]&lt;/math&gt;<br /> <br /> <br /> If we want to use box-muller to generate a multivariate normal, we could use the code in lecture 6:<br /> &lt;pre style='font-size:16px'&gt;<br /> d = length(mu);<br /> R = chol(Sigma);<br /> <br /> U1=rand(n,d)<br /> U2=rand(d,d)<br /> D=-2*log(U1)<br /> tet=2*pi*U2<br /> Z=(D.^0.5)*cos(tet)' %since in lecture 6, we set up x and y are independent normal<br /> %distribution, so in here we can choose either cos and sin<br /> <br /> X = Z*R + ones(n,1)*mu';<br /> &lt;/pre&gt;<br /> <br /> ==== '''Central Limit Theorem''' ====<br /> <br /> Theorem: &quot;Given a distribution with mean μ and variance σ², the sampling distribution of the mean approaches a normal distribution with a mean (μ) and a variance σ²/N as N, the sample size, increases&quot;. Furthermore, the original distribution can be of any arbitrary shape, the sampling distribution of the mean will approach a normal distribution with a large enough N.&lt;ref&gt;<br /> http://davidmlane.com/hyperstat/A14043.html<br /> &lt;/ref&gt; <br /> <br /> Applying the central limit theorem to simulations, we may revise the definition to be the following: Given certain conditions, the mean of a sufficiently large number of independent random variables, each with a well defined mean and variance, will be approximately normal distributed.(i.e. if we simulate sufficiently many independent observations based on well defined mean and variance, the mean of these observations will follow an approximately normal distribution.) <br /> <br /> We illustrate with an example using 1000 observations each of 20 independent exponential random variables.<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> &gt;&gt; X = exprnd (20,20,1000); % 1000 instances of 20 exponential random numbers with mean 20<br /> &gt;&gt; hist(X(1,:))<br /> &gt;&gt; hist(X(1:2,:))<br /> ...<br /> &gt;&gt;hist(X(1:20,:)) -&gt; approaches to normal<br /> &lt;/pre&gt;<br /> <br /> (The definition of CLT is from http://en.wikipedia.org/wiki/Central_limit_theorem)<br /> <br /> &lt;math&gt; \lim_{n \to \infty} P*[{\frac{X_1 + ... + X_n -n*\mu}{\sigma*\surd n}} &lt; x] = \Phi (x)&lt;/math&gt;<br /> <br /> ==Class 11 - Tuesday，June 11, 2013==<br /> === Announcement ===<br /> Midterm covers up to the middle of last lecture, which means stochastic process will not be on midterm. There won't be any Matlab syntax questions. And Students can contribute to any previous classes. We might however be asked to write down algorithms.<br /> <br /> ===Poisson Process===<br /> A discrete stochastic variable ''X'' is said to have a Poisson distribution with parameter ''λ'' &gt; 0 if<br /> :&lt;math&gt;\!f(n)= \frac{\lambda^n e^{-\lambda}}{n!} \qquad n= 0,1,2,3,4,5,\ldots,&lt;/math&gt;.<br /> <br /> &lt;math&gt;\{X_t:t\in T\}&lt;/math&gt; where &lt;math&gt;\ X_t &lt;/math&gt; is state space and T is index set.<br /> <br /> <br /> '''Properties of Homogeneous Poisson Process'''&lt;br&gt;<br /> (a) '''Independence:''' The numbers of arrivals in non-overlapping intervals are independent &lt;br&gt;<br /> (b) '''Homogeneity or Uniformity:''' The number of arrivals in each interval I(a,b] is Poisson distribution with rate &lt;math&gt;\lambda (b-a)&lt;/math&gt;&lt;br/&gt;<br /> (c) '''Individuality:''' for a sufficiently short time period of length h, the probability of 2 or more events occurring in the interval is close to 0, or formally &lt;math&gt;\mathcal{O}(h)&lt;/math&gt;&lt;br&gt;<br /> <br /> NOTE: it is very important to note that the time between the occurrence of consecutive events (in a Poisson Process) is exponentially distributed with the same parameter as that in the Poisson distribution. This characteristic is used when trying to simulate a Poisson Process.<br /> <br /> For a small interval (t,t+h], where h is small&lt;br&gt;<br /> 1. The number of arrivals up to time t(N&lt;sub&gt;t&lt;/sub&gt;) is independent of the number of arrival in the interval&lt;br&gt;<br /> 2. &lt;math&gt; P (N(t,t+h)=1|N_{t} ) = P (N(t,t+h)=1) =\frac{e^{-\lambda h} (\lambda h)^1}{1!} =e^{-\lambda h} {\lambda h} \approx \lambda h &lt;/math&gt; since &lt;math&gt;e^{-\lambda h} \approx 1&lt;/math&gt; when h is small.&lt;br&gt;<br /> <br /> &lt;math&gt;\lambda h&lt;/math&gt; can be thought of as the probability of observing an arrival in the interval t to t+h.&lt;br&gt;<br /> <br /> Similarly, the probability of not observing an arrival in this interval is 1-&lt;math&gt;\lambda &lt;/math&gt; h.&lt;br&gt;<br /> <br /> <br /> '''Generate a Poisson Process'''&lt;br /&gt;<br /> <br /> &lt;math&gt;U_{n} \sim~ U(0,1)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;T_n-T_{n-1}=-\frac {1}{\lambda} log(U_n)&lt;/math&gt;&lt;br&gt;<br /> <br /> 1. set &lt;math&gt;T_{0}=0&lt;/math&gt; and n=1&lt;br/&gt;<br /> <br /> 2. &lt;math&gt;U_{n} \sim~ U(0,1)&lt;/math&gt;&lt;br /&gt;<br /> <br /> 3. &lt;math&gt;T_{n} = T_{n-1}-\frac {1}{\lambda} log (U_{n}) &lt;/math&gt; (declare an arrival)&lt;br /&gt;<br /> <br /> 4. if &lt;math&gt;T_{n} \gneq T&lt;/math&gt; stop&lt;br /&gt;<br /> &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&lt;br /&gt;<br /> &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;n=n+1 go to step 2&lt;br /&gt;<br /> <br /> Since &lt;math&gt;P(N(t,t+h)=1) = e^{-{\lambda} h}\lambda h&lt;/math&gt;, we can regard &lt;math&gt;\lambda &lt;/math&gt;h as a exponential distribution, and according to what we learnt, &lt;math&gt;T_n-T_{n-1} = h = -\frac {1}{\lambda} log(U_n)&lt;/math&gt;.&lt;br&gt;<br /> *Note : Recall that exponential random variable is the waiting time until one event of interested occurs.<br /> <br /> '''Review of Poisson - Example'''<br /> <br /> Let X be the r.v of the number of accidents in an hour, following the Poisson distribution with mean 1.8.<br /> <br /> &lt;math&gt;P(X=0)=e^{-1.8} &lt;/math&gt;<br /> <br /> &lt;math&gt;P(X=4)=\frac {e^{-1.8}(1.8)^4}{4!} &lt;/math&gt;<br /> <br /> &lt;math&gt;P(X\geq1) = 1 - P(x=0) = 1- e^{-1.8}&lt;/math&gt;<br /> <br /> &lt;span style=&quot;background:#F5F5DC&quot;&gt;<br /> &lt;math&gt;P(N_3&gt; 3 | N_2)=P(N_1 &gt; 2)&lt;/math&gt;<br /> &lt;/span&gt;<br /> <br /> When we use the inverse-transform method, we can assume the poisson process to be an exponential distribution, and get the h function from the inverse method. Sometimes we assume that h is very small.<br /> <br /> '''Multi-dimensional Poisson Process'''&lt;br&gt;<br /> <br /> The poisson distribution arises as the distribution of counts of occurrences of events in (multidimensional) intervals in multidimensional poisson process in a directly equivalent way to the result for unidimensional processes. This,is ''D'' is any region the multidimensional space for which |D|, the area or volume of the region, is finite, and if {{nowrap|''N''(''D'')}} is count of the number of events in ''D'', then<br /> <br /> &lt;math&gt; P(N(D)=k)=\frac{(\lambda|D|)^k e^{-\lambda|D|}}{k!} .&lt;/math&gt;<br /> <br /> === Generating a Homogeneous Poisson Process === <br /> Suppose we want to generate the first n events of a Poisson process with rate &lt;math&gt;{\lambda}&lt;/math&gt;. We showed that the times between successive events are independent exponential random variables, each with rate &lt;math&gt;{\lambda}&lt;/math&gt;. Therefore, we can first generate n random numbers U&lt;sub&gt;1&lt;/sub&gt;, U&lt;sub&gt;2&lt;/sub&gt;,...,U&lt;sub&gt;n&lt;/sub&gt; and use the inverse transform method to find the corresponding exponential variable X&lt;sub&gt;1&lt;/sub&gt;, X&lt;sub&gt;2&lt;/sub&gt;,...,X&lt;sub&gt;n&lt;/sub&gt;. Then X&lt;sub&gt;i&lt;/sub&gt; can be interpreted as the time between the (&lt;i&gt;i&lt;/i&gt;-1)st and the &lt;i&gt;i&lt;/i&gt;th event of the process. &lt;br&gt;<br /> The actual time of the &lt;i&gt;j&lt;/i&gt;th event is the sum of the first j interarrival times. Therefore, the generated values of the first n event times are &lt;math&gt;\sum_{i=1}^{j} X_i&lt;/math&gt;, j = 1...n. <br /> &lt;br&gt;<br /> <br /> Homogeneous poisson process refers to the rate of occurrences remaining constant for all periods of time. &lt;br&gt;<br /> &lt;br&gt;U&lt;sub&gt;n&lt;/sub&gt;~U(0,1)&lt;br&gt;<br /> &lt;math&gt;T_n-T_{n-1}=-\frac {1}{\lambda} log(U_n)&lt;/math&gt; &lt;br&gt;<br /> <br /> The waiting time between each occurrence follows the exponential distribution with parameter &lt;math&gt;\lambda &lt;/math&gt;.<br /> &lt;math&gt;T_n&lt;/math&gt; represents the time elapsed at the n&lt;sup&gt;th&lt;/sup&gt; occurence.<br /> <br /> 1) Set T&lt;sub&gt;0&lt;/sub&gt; = 0 ,and n = 1 &lt;br&gt;<br /> 2) U&lt;sub&gt;n&lt;/sub&gt; follow U(0,1) &lt;br&gt;<br /> 3) T&lt;sub&gt;n&lt;/sub&gt; - T&lt;sub&gt;n-1&lt;/sub&gt; =&lt;math&gt; -\frac {1}{\lambda} &lt;/math&gt; log (U&lt;sub&gt;n&lt;/sub&gt;) (Declare an arrival)&lt;br&gt;<br /> 4) if T&lt;sub&gt;n&lt;/sub&gt; &gt;T stop;<br /> else n = n + 1, go to step 2 &lt;br&gt;<br /> <br /> h is the a range and we assume the probability of every point in this rang is the same by uniform ditribution.(cause h is small)<br /> and we test the rang is Tn smaller than T. And &lt;math&gt; -\frac {1}{\lambda} &lt;/math&gt; log (U&lt;sub&gt;n&lt;/sub&gt;)represents that chance that one arrival arrives.<br /> <br /> &lt;b&gt;Higher Dimensions:&lt;/b&gt;&lt;br&gt;<br /> To sample from higher dimensional Poisson process:&lt;br&gt;<br /> 1. Generate a random number N that is Poisson distributed with parameter &lt;math&gt;{\lambda}&lt;/math&gt;*A&lt;sub&gt;d&lt;/sub&gt;, where A&lt;sub&gt;d&lt;/sub&gt; is the area under the bounded region. (ie A&lt;sub&gt;2&lt;/sub&gt; is area of the region, A&lt;sub&gt;3&lt;/sub&gt; is the volume of the 3-d space.&lt;br&gt;<br /> 2. Given N=n, generate n random (uniform) points in the region.&lt;br&gt;<br /> <br /> '''[[At the end, it generates n (cumulative) arrival times, up to time TT.]]'''<br /> <br /> ==== MatLab Code ====<br /> &lt;pre style='font-size:16px'&gt;<br /> <br /> T(1)=0; % Matlab does not allow 0 as an index, so we start with T(1).<br /> ii=1;<br /> l=2;<br /> TT=5;<br /> <br /> while T(ii)&lt;=TT<br /> u=rand;<br /> ii=ii+1;<br /> T(ii)=T(ii-1) - (1/l)*log(u); <br /> end<br /> <br /> plot(T, '.')<br /> <br /> &lt;/pre&gt;<br /> &lt;br&gt;<br /> <br /> The following plot is using TT = 50.&lt;br&gt;<br /> The number of points generated every time on average should be &lt;math&gt;\lambda&lt;/math&gt; * TT. &lt;br&gt;<br /> The maximum value of the points should be TT. &lt;br&gt;<br /> [[File:Poisson.jpg]]&lt;br&gt;<br /> when TT be big, the plot of the graph will be linear, when we set the TT be 5 or small number, the plot graph looks like discrete distribution.<br /> <br /> ===Markov chain===<br /> &quot;A Markov Chain is a stochastic process where: &lt;br/&gt;<br /> <br /> 1) Each stage has a fixed number of states, &lt;br/&gt;<br /> 2) the (conditional) probabilities at each stage only depend on the previous state.&quot; &lt;br/&gt;<br /> <br /> Source: &quot;http://math.scu.edu/~cirving/m6_chapter8_notes.pdf&quot; &lt;br/&gt;<br /> <br /> A Markov Chain is said to be irreducible if for each pair of states i and j there is a positive probability, starting in state i, that the process will ever enter state j.(source:&quot;https://en.wikipedia.org/wiki/Markov_chain&quot;)<br /> <br /> Markov Chain is the simplification of assumption, for instance, the result of GPA in university depend on the GPA's in high school, middle school, elementary school, etc., but during a job interview after university graduation, the interviewer would most likely to ask about the GPA in university of the interviewee but not the GPA from early years because they assume what happened before are summarized and adequately represented by the information of the GPA earned during university. Therefore, it's not necessary to look back to elementary school. A Markov Chain works the same way, we assume that everything that has occurred earlier in the process is only important for finding out where we are now, the future only depends on the present of where we are now, not the past of how we got here. So the n&lt;sub&gt;th&lt;/sub&gt;random variable would only depend on the n-1&lt;sub&gt;th&lt;/sub&gt;term but not all the previous ones. A Markov process exhibits the memoryless property.&lt;br/&gt;<br /> <br /> Examples of Markov Chain applications in various fields:&lt;br /&gt;<br /> *Physics: The movement of a particle (memory-less random walk)&lt;br /&gt;<br /> *Finance: The volatility of prices of financial securities or commodities&lt;br /&gt;<br /> *Actuarial Science: The pricing and valuation of multiple-state and multiple-lives insurance and annuity contracts&lt;br /&gt;<br /> *Technology: The Google link analysis algorithm &quot;PageRank&quot;&lt;br /&gt;<br /> <br /> <br /> '''Definition''' An irreducible Markov Chain is said to be aperiodic if for some n &lt;math&gt;\ge 0 &lt;/math&gt; and some state j.&lt;br /&gt;<br /> &lt;math&gt; P*(X_n=j | X_0 =j) &gt; 0 &lt;/math&gt; and &lt;math&gt; P*(X_{n+1} | X_0=j) &gt; 0 &lt;/math&gt; &lt;br /&gt;<br /> <br /> It can be shown that if the Markov Chain is irreducible and aperiodic then, &lt;br /&gt;<br /> &lt;math&gt; \pi_j = \lim_{n -&gt; \infty} P*(X_n = j) for j=1...N &lt;/math&gt; &lt;br /&gt;<br /> Source: From Simulation textbook &lt;br /&gt;<br /> <br /> Product Rule (Stochastic Process):&lt;br /&gt;<br /> &lt;math&gt;f(x_1,x_2,...,x_n)=f(x_1)f(x_2\mid x_1)f(x_3\mid x_2,x_1)...f(x_n\mid x_{n-1},x_{n-2},....)&lt;/math&gt;<br /> <br /> In Markov Chain&lt;br /&gt;<br /> &lt;math&gt; f(x_1,x_2,...,x_n)=f(x_1)f(x_2\mid x_1)f(x_3\mid x_2)...f(x_n\mid x_{n-1}) &lt;/math&gt;<br /> <br /> Concept: The current status of a subject must be relative to the past.However, it will only depend on the previous result only. In other words, if an event occurring tomorrow follows a Markov process it will depend on today and yesterday (the past) is negligible. The past (not including the current state of course) is negligible since its information is believed to have been captured and reflected in the current state. <br /> <br /> A Markov Chain is a Stochastic Process for which the distribution of &lt;math&gt;x_t&lt;/math&gt; depends only on &lt;math&gt;x_{t-1}&lt;/math&gt;.<br /> <br /> Given &lt;math&gt; x_t&lt;/math&gt;, &lt;math&gt;x_{t-1}&lt;/math&gt; and &lt;math&gt;x_{t+1} &lt;/math&gt; are independent. The process of getting &lt;math&gt; x_n &lt;/math&gt; is drawn as follows. The distribution of &lt;math&gt;x_n&lt;/math&gt; only depends on the value of &lt;math&gt;x_{n-1}&lt;/math&gt;.<br /> <br /> &lt;math&gt; x_1 \rightarrow x_2\rightarrow...\rightarrow x_n&lt;/math&gt; <br /> <br /> Formal Definition:<br /> The process &lt;math&gt; \{x_n: n \in T\} &lt;/math&gt; is a markov chain if:&lt;br /&gt;<br /> &lt;math&gt; Pr(x_n|x_{n-1},...,x_1) = Pr(x_n|x_{n-1}) \ \ \forall n\in T &lt;/math&gt; and &lt;math&gt; \forall x\in X&lt;/math&gt;<br /> <br /> &lt;span style=&quot;background:#F5F5DC&quot;&gt;CONTINUOUS TIME MARKOV PROCESS&lt;/span&gt;<br /> <br /> A continuous time markov process would be one where the time spent in each state is not discrete, but can take <br /> on positive real values. In other words, the index set is the positive real numbers. If the process is homogeneous, then the time spent will have an exponential distribution. In this case we will have a transition rate matrix that captures the rate at which we move between two states. An example will be the homogeneous Poisson process.&lt;br /&gt;<br /> <br /> <br /> ====Transition Matrix====<br /> <br /> Transition Probability: &lt;math&gt; P_{ij} = P(X_{t+1} =j | X_t =i) &lt;/math&gt; is the one-step transition probability from state i to state j.<br /> <br /> Transition Probability: &lt;math&gt; P_{ij}(k) = P(X_{t+1}(k) =j | X_t(k) =i) &lt;/math&gt; is the k-step transition probability from state i to state j.<br /> <br /> The matrix P whose elements are transition Probabilities &lt;math&gt; P_{ij} &lt;/math&gt; is a one-step transition matrix. <br /> <br /> Example:&lt;br /&gt;<br /> <br /> [[File:Transition_Map.png]]<br /> <br /> &lt;math&gt;\begin{align}<br /> P_{ab} &amp;=P(X_{t+1} &amp;=b &amp;\mid X_{t} &amp;=a) &amp;= 0.3 \\<br /> P_{aa} &amp;=P(X_{t+1} &amp;=a &amp;\mid X_{t} &amp;=a) &amp;= 0.7 \\<br /> P_{ba} &amp;=P(X_{t+1} &amp;=a &amp;\mid X_{t} &amp;=b) &amp;= 0.2 \\<br /> P_{bb} &amp;=P(X_{t+1} &amp;=b &amp;\mid X_{t} &amp;=b) &amp;= 0.8 \\<br /> \end{align}&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt; P= \left [ \begin{matrix}<br /> 0.7 &amp; 0.3 \\<br /> 0.2 &amp; 0.8<br /> \end{matrix} \right] &lt;/math&gt;<br /> <br /> Note: Column 1 corresponds to the state at time t and Column 2 corresponds to the state at time t+1.<br /> <br /> The above matrix can be drawn into a state transition diagram<br /> <br /> Properties of Transition Matrix: &lt;br /&gt;<br /> <br /> 1. &lt;math&gt; 1 \geq P_{ij} \geq 0 &lt;/math&gt;&lt;br /&gt;<br /> <br /> 2. &lt;math&gt;\sum_{j}^{}{P_{ij}=1}&lt;/math&gt; which means the rows of P should sum to 1.&lt;br /&gt;<br /> <br /> Remark: &lt;math&gt;\sum_{i}^{}{P_{ij}\neq1}&lt;/math&gt; in general. If equality holds, the matrix is called a doubly stochastic matrix.<br /> <br /> In general, one would consider a (finite) set of elements &lt;math&gt; \Omega &lt;/math&gt; such that: &lt;br&gt;<br /> <br /> &lt;math&gt; \forall x \in \Omega &lt;/math&gt;, the probability of the next state is given according to the distribution &lt;math&gt; P(x,\cdot) &lt;/math&gt; &lt;br&gt;<br /> <br /> This means our model can be simulated as a sequence of random variables &lt;math&gt; (X_0, X_1, X_2, \ldots ) &lt;/math&gt; with state space &lt;math&gt; \Omega &lt;/math&gt; and transition matrix &lt;math&gt; P = [P_{ij}] &lt;/math&gt; where &lt;math&gt; \forall t \in \N, 0 \leq s \leq t+1, x_s \in \Omega, &lt;/math&gt; &lt;br/&gt;<br /> <br /> we have to following property (Markov property): &lt;br/&gt;<br /> &lt;math&gt; P(X_{t+1}= x_{t+1} \vert \cap^{t}_{s=0} X_s = x_s) = P(X_{t+1} =x_{t+1} \vert X_t =x_t) = P(x_t,x_{t+1}) &lt;/math&gt; &lt;br&gt;<br /> <br /> And &lt;math&gt; \forall x \in \Omega, \sum_{y\in\Omega} P(x,y) =1; \; \forall x,y\in\Omega, P_{xy} = P(x,y) \geq 0 &lt;/math&gt;&lt;br&gt;<br /> <br /> Moreover if &lt;math&gt; \forall x,y \in \Omega, \exists k, P^k (x,y) &gt; 0 &lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt; |\Omega| &lt; \infty &lt;/math&gt; (i.e Any two states can be translated somehow) &lt;br&gt;<br /> <br /> Then one might consider the periodicity of the chain and derive a notion of cyclic behavior. &lt;br&gt;<br /> <br /> === Examples of Transition Matrix ===<br /> <br /> [[File:Mark13.png]]&lt;br&gt;<br /> The picture is from http://www.google.ca/imgres?imgurl=http://academic.uprm.edu/wrolke/esma6789/graphs/mark13.png&amp;imgrefurl=http://academic.uprm.edu/wrolke/esma6789/mark1.htm&amp;h=274&amp;w=406&amp;sz=5&amp;tbnid=6A8GGaxoPux9kM:&amp;tbnh=83&amp;tbnw=123&amp;prev=/search%3Fq%3Dtransition%2Bmatrix%26tbm%3Disch%26tbo%3Du&amp;zoom=1&amp;q=transition+matrix&amp;usg=__hZR-1Cp6PbZ5PfnSjs2zU6LnCiI=&amp;docid=PaQvi1F97P2urM&amp;sa=X&amp;ei=foTxUY3DB-rMyQGvq4D4Cg&amp;sqi=2&amp;ved=0CDYQ9QEwAQ&amp;dur=5515)<br /> <br /> 1.There are four states: 0,1,2, and 3.<br /> <br /> Each row adds up to 1, and all the entries are between 0 and 1(since the transition probability is always between 0 and 1).&lt;br /&gt;<br /> This matrix means that &lt;br /&gt;<br /> - at state 0, can only go to state 1, since probability is 1.&lt;br /&gt;<br /> - at state 1, can go to state 0 with a probability of 1/3 and state 2 with a probability of 2/3&lt;br /&gt;<br /> - at state 2, can go to state 1 with a probability of 2/3 and state 3 with a probability of 1/3&lt;br /&gt;<br /> - at state 3, can only go to state 2, since probability is 1.&lt;br /&gt;<br /> <br /> 2. Consider a Markov chain with state space {0, 1} and transition matrix <br /> &lt;math&gt; P= \left [ \begin{matrix}<br /> 1/3 &amp; 2/3 \\<br /> 3/4 &amp; 1/4<br /> \end{matrix} \right] &lt;/math&gt;. <br /> Assuming that the chain starts in state 0 at time n = 0, what is the probability that it is in state 1 at time n = 2?<br /> <br /> <br /> &lt;math&gt;\begin{align}<br /> P(X_{2} &amp;=1 &amp;\mid X_{0} &amp;=0) &amp; =P(X_{1} &amp;=0,X_{2} &amp;=1 &amp;\mid X_{0} &amp;=0)+P(X_{1} &amp;=1,X_{2} &amp;=1 &amp;\mid X_{0} &amp;=0)\end{align} &lt;/math&gt;<br /> &lt;math&gt; \begin{align} P(X_{1} &amp;=0 &amp;\mid X_{0} &amp;=0) * P(X_{2} &amp;=1 &amp;\mid X_{1} &amp;=0)+P(X_{1} &amp;=1 &amp;\mid X_{0} &amp;=0) * P(X_{2} &amp;=1 &amp;\mid X_{1}&amp;=1) &amp;=1/3*2/3+ 2/3*1/4 &amp;=7/18 \\<br /> \end{align}&lt;/math&gt;&lt;br /&gt;<br /> <br /> == Class 12 - Thursday，June 13, 2013 ==<br /> &lt;b&gt;Time&lt;/b&gt;<br /> Jun 17, 2013 2:30 PM - 3:30 PM<br /> ===Midterm Review===<br /> <br /> === Multiplicative Congruential Algorithm ===<br /> &lt;div style=&quot;border:1px solid red&quot;&gt;<br /> A Linear Congruential Generator (LCG) yields a sequence of randomized numbers calculated with a linear equation. The method represents one of the oldest and best-known pseudorandom number generator algorithms. The theory behind them is easy to understand, and they are easily implemented and fast, especially on computer hardware which can provide modulo arithmetic by storage-bit truncation.&lt;br&gt;<br /> from wikipedia<br /> &lt;/div&gt;<br /> <br /> &lt;math&gt;\begin{align}x_k+1= (ax_k+c) \mod m\end{align}&lt;/math&gt;&lt;br /&gt;<br /> <br /> Where a, c, m and x&lt;sub&gt;1&lt;/sub&gt; (the seed) are values we must chose before running the algorithm. While there is no set value for each, it is best for m to be large and prime. For example, Matlab uses a = 75,b = 0,m = 231 − 1.<br /> <br /> '''Examples:'''&lt;br&gt; <br /> 1. &lt;math&gt;\begin{align}X_{0} = 10 ,a = 2 , c = 1 , m = 13 \end{align}&lt;/math&gt;&lt;br&gt; <br /> <br /> &lt;math&gt;\begin{align}X_{1} = 2 * 10 + 1\mod 13 = 8\end{align}&lt;/math&gt;&lt;br&gt;<br /> <br /> &lt;math&gt;\begin{align}X_{2} = 2 * 8 + 1\mod 13 = 4\end{align}&lt;/math&gt; ... and so on&lt;br&gt;<br /> <br /> <br /> 2. &lt;math&gt;\begin{align}X_{0} = 44 ,a = 13 , c = 17 , m = 211\end{align}&lt;/math&gt;&lt;br&gt; <br /> <br /> &lt;math&gt;\begin{align}X_{1} = 13 * 44 + 17\mod 211 = 167\end{align}&lt;/math&gt;&lt;br&gt; <br /> <br /> &lt;math&gt;\begin{align}X_{2} = 13 * 167 + 17\mod 211 = 78\end{align}&lt;/math&gt;&lt;br&gt; <br /> <br /> &lt;math&gt;\begin{align}X_{3} = 13 * 78 + 17\mod 211 = 187\end{align}&lt;/math&gt; ... and so on&lt;br&gt;<br /> <br /> === Inverse Transformation Method ===<br /> '''For continuous cases:''' &lt;br/ &gt;<br /> 1. U~U(0,1)&lt;br/ &gt;<br /> 2. X=F&lt;sup&gt;-1&lt;/sup&gt;(u)&lt;br/ &gt;<br /> <br /> '''Note:''' &lt;br/&gt;<br /> In Uniform Distribution &lt;math&gt;P(X&lt;=a)=a &lt;/math&gt;&lt;br/ &gt;<br /> proof:&lt;math&gt;P(X&lt;=x) = P(F^{-1}(u)&lt;=x)=P(u&lt;=F(x)) = F(x)&lt;/math&gt;&lt;br/ &gt;<br /> <br /> '''For discrete cases:''' &lt;br/ &gt;<br /> 1. U~U(0,1)&lt;br/ &gt;<br /> 2. x=x&lt;sub&gt;i&lt;/sub&gt; if F(x&lt;sub&gt;i-1&lt;/sub&gt;)&lt;math&gt;&lt;&lt;/math&gt;u&lt;math&gt;\leq&lt;/math&gt;F(x&lt;sub&gt;i&lt;/sub&gt;)&lt;br/ &gt;<br /> <br /> ===Acceptance-Rejection Method===<br /> cg(x)&gt;=f(x)<br /> &lt;math&gt;c=max\frac{f(x)}{g(x)}&lt;/math&gt;<br /> &lt;br&gt;&lt;math&gt;\frac{1}{c}&lt;/math&gt; is the efficiency of the method/probability of acceptance<br /> <br /> 1. Y~g(x)&lt;br /&gt;<br /> 2. U~U(0,1)&lt;br /&gt;<br /> 3. If &lt;math&gt;U&lt;=\frac{f(y)}{c*g(y)}&lt;/math&gt; then X=Y<br /> else go to step 1<br /> <br /> Proof that this method generates the desired distribution:<br /> &lt;math&gt;P(Y|accepted)=\frac{P(accepted|Y)P(Y)}{P(accepted)}=\frac{\frac{f(y)}{cg(y)} g(y)}{\int_{y}^{ } \frac{f(y)}{cg(y)} g(y) dy}=\frac{\frac{f(y)}{c}}{\frac{1}{c}\cdot 1}=f(y)&lt;/math&gt;<br /> <br /> ===Multivariate===<br /> <br /> f(x&lt;sub&gt;1&lt;/sub&gt;,....,x&lt;sub&gt;n&lt;/sub&gt;)=f(x&lt;sub&gt;1&lt;/sub&gt;)f(x&lt;sub&gt;2&lt;/sub&gt;|x&lt;sub&gt;1&lt;/sub&gt;)...f(x&lt;sub&gt;n&lt;/sub&gt;|x&lt;sub&gt;n-1&lt;/sub&gt;,...x&lt;sub&gt;1&lt;/sub&gt;)<br /> in general we need knowledge of conditional distribution if x&lt;sub&gt;1&lt;/sub&gt;....x&lt;sub&gt;n&lt;/sub&gt; are independent<br /> i.e. f(x&lt;sub&gt;1&lt;/sub&gt;...x&lt;sub&gt;n&lt;/sub&gt;)=f(x&lt;sub&gt;1&lt;/sub&gt;)f(x&lt;sub&gt;2&lt;/sub&gt;)...f(x&lt;sub&gt;n&lt;/sub&gt;)<br /> <br /> === Vector A/R Method===<br /> <br /> This method is not efficient for high dimensional vectors&lt;br/&gt;<br /> <br /> - Sample uniformly from a space W that contains the sample space G of interest&lt;br/&gt;<br /> - Accept if the point is inside G &lt;br/&gt;<br /> <br /> Steps:<br /> 1. Sample uniformly from W &lt;br/&gt;<br /> g(x)=&lt;math&gt;\frac{1}{A_W}&lt;/math&gt;, where A&lt;sub&gt;W&lt;/sub&gt; is the area of W.&lt;br/&gt;<br /> f(x)=&lt;math&gt;\frac{1}{A_G}&lt;/math&gt;, where A&lt;sub&gt;G&lt;/sub&gt; is the area of G.&lt;br/&gt;<br /> 2. If the point is inside G, accept the point. Else, reject and repeat step 1.<br /> <br /> <br /> -&lt;math&gt;\frac{A_G}{A_W}&lt;/math&gt; = &lt;math&gt;\frac{1}{c}&lt;/math&gt; is the efficiency of the algorithm. That is the amount of points accepted over the amount of points rejected. This acceptance rate drops greatly for each dimension added.<br /> <br /> Note that &lt;math&gt;\frac{A_G}{A_W}&lt;/math&gt; decreases exponentially as the dimensional goes up, thus its not efficient for for high dimensional.<br /> <br /> === Common distribution ===<br /> <br /> ===Exponential===<br /> <br /> Models the waiting time until the first success.&lt;br&gt;<br /> &lt;math&gt;X\sim~Exp(\lambda)&lt;/math&gt; &lt;br /&gt;<br /> <br /> &lt;math&gt;f(x) = \lambda e^{-\lambda x} \, , x&gt;0 &lt;/math&gt;&lt;br/&gt;<br /> <br /> &lt;math&gt;1.\, U\sim~U(0,1)&lt;/math&gt;<br /> &lt;br /&gt;<br /> &lt;math&gt;2.\, x = \frac{-1}{\lambda} log(U)&lt;/math&gt;<br /> <br /> ===Normal===<br /> Box-Muller method<br /> <br /> 1.&lt;math&gt;U_{1},U_{2}\sim~ U(0,1)&lt;/math&gt; &lt;br/&gt;<br /> 2.&lt;math&gt;R^{2}=-2log(U_{1}), R^{2}\sim~ Exp(1/2)&lt;/math&gt;<br /> <br /> &lt;math&gt;\theta = 2\pi U_{2},\theta\sim~ U(0,2\pi) &lt;/math&gt;&lt;br/&gt;<br /> 3.&lt;math&gt;X=Rcos(\theta), Y=Rsin(\theta), X,Y\sim~ N(0,1)&lt;/math&gt;&lt;br/ &gt;<br /> &lt;br/ &gt;<br /> <br /> To obtain any normal distribution X once a set of standard normal samples Z is generated, apply the following transformations: &lt;br/ &gt;<br /> &lt;math&gt;Z\sim N(0,1)\rightarrow X \sim N(\mu,\sigma ^{2})&lt;/math&gt;&lt;br/ &gt;<br /> &lt;math&gt;X=\mu+\sigma~Z&lt;/math&gt;&lt;br/ &gt;<br /> <br /> In the multivariate case,&lt;br/ &gt;<br /> &lt;math&gt;\underline{Z}\sim N(\underline{0},I)\rightarrow \underline{X} \sim N(\underline{\mu},\Sigma)&lt;/math&gt; &lt;br/ &gt;<br /> &lt;math&gt;\underline{X} = \underline{\mu} +\Sigma ^{1/2} \underline{Z}&lt;/math&gt;&lt;br/&gt;<br /> Note: &lt;math&gt;\Sigma^{1/2}&lt;/math&gt; can be obtained from Cholesky decomposition (chol(A) in MATLAB), which is guaranteed to exist, as &lt;math&gt;\Sigma&lt;/math&gt; is positive semi-definite.<br /> <br /> === Gamma ===<br /> Gamma(t,λ) &lt;br&gt;<br /> t: The number of exponentials and the shape parameter&lt;br&gt;<br /> λ: The mean of the exponentials and the scale parameter&lt;br&gt; <br /> <br /> Also, Gamma(t,λ) can be expressed into a summation of t exp(λ).&lt;br&gt;<br /> &lt;math&gt;x=\frac {-1}{\lambda}\log(u_1)-\frac {1}{\lambda}\log(u_2)-.......-\frac {1}{\lambda}\log(u_t)&lt;/math&gt;<br /> <br /> &lt;math&gt;=\frac {-1}{\lambda}[\log(u_1)+\log(u_2)+.....+\log(u_t)]&lt;/math&gt;<br /> <br /> &lt;math&gt;=\frac {-1}{\lambda}\log(\prod_{j=1}^{t} U_j)&lt;/math&gt;<br /> <br /> This is a special property of gamma distribution.<br /> <br /> === Bernoulli ===<br /> <br /> A Bernoulli random variable can only take two possible values: 0 and 1. 1 represents &quot;success&quot; and 0 represents &quot;failure.&quot; If p is the probability of success, we have pdf<br /> <br /> &lt;math&gt; f(x)= p^x (1-p)^{1-x},\, x=0,1 &lt;/math&gt;&lt;br&gt;<br /> <br /> To generate a Bernoulli random variable we use the following procedure:<br /> <br /> &lt;math&gt; 1. U\sim~U(0,1)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt; 2. if\, u &lt;= p, then\, x=1\,&lt;/math&gt;&lt;br /&gt; <br /> &lt;math&gt; else\, x=0&lt;/math&gt;&lt;br/&gt;<br /> where 1 stands for success and 0 stands for failure.&lt;br&gt;<br /> <br /> ===Binomial===<br /> <br /> The sum of n independent Bernoulli trials<br /> &lt;br\&gt;<br /> &lt;math&gt; X\sim~ Bin(n,p)&lt;/math&gt;&lt;br/&gt;<br /> 1.&lt;math&gt; U1, U2, ... Un \sim~U(0,1)&lt;/math&gt;&lt;br/&gt;<br /> 2. &lt;math&gt; X= \sum^{n}_{1} I(U_i \leq p) &lt;/math&gt; ,where &lt;math&gt;I(U_i \leq p)&lt;/math&gt; is an indicator for a successful trial.&lt;br/&gt;<br /> Return to 1&lt;br/&gt;<br /> <br /> I is an indicator variable if for &lt;math&gt;U \leq P,\, then\, I(U\leq P)=1;\, else I(U&gt;P)=0.&lt;/math&gt;<br /> <br /> Repeat this N times if you need N samples.<br /> <br /> The theory behind the algorithm is the fact that the sum of n independent and identically distributed Bernoulli trials, Ber(p), follows a binomial Bin(n,p) distribution. <br /> <br /> Example:<br /> <br /> Suppose rolling a die, success= lands on 5, fail ow<br /> <br /> p=1/6, 1-p=5/6, rolling for 10 times, n=10<br /> <br /> simulate this binomial distribution.<br /> <br /> 1) Generate &lt;math&gt;U_1....U_{10} \sim~ U(0,1) &lt;/math&gt; &lt;br&gt;<br /> 2) &lt;math&gt; X= \sum^{10}_{1} I(U_i \leq \frac{1}{6}) &lt;/math&gt; &lt;br&gt;<br /> 3)Return to 1)<br /> <br /> === Beta Distribution ===<br /> &lt;math&gt;\displaystyle \text{Beta}(\alpha,\beta) = \frac{\Gamma(\alpha+\beta)}{\Gamma(\alpha)\Gamma(\beta)}x^{\alpha-1}(1-x)^{\beta-1} &lt;/math&gt; where &lt;math&gt;0 \leq x \leq 1&lt;/math&gt; and &lt;math&gt;\alpha&lt;/math&gt;&gt;0, &lt;math&gt;\beta&lt;/math&gt;&gt;0&lt;br&gt;<br /> and<br /> &lt;math&gt;f(x;\alpha,\beta)= 0 &lt;/math&gt; otherwise<br /> <br /> :&lt;math&gt;\displaystyle \text{Beta}(1,1) = U (0, 1) &lt;/math&gt;&lt;br&gt;<br /> <br /> <br /> :&lt;math&gt;\displaystyle \text{Beta}(\alpha,1)={f}(x) = \frac{\Gamma(\alpha+1)}{\Gamma(\alpha)\Gamma(1)}x^{\alpha-1}(1-x)^{1-1}=\alpha x^{\alpha-1}&lt;/math&gt;&lt;br&gt;<br /> <br /> <br /> '''Gamma Distribution'''<br /> <br /> '''Algorithm'''&lt;br\&gt;<br /> *1. Sample from Y1 ~ Gamma (&lt;math&gt;\alpha&lt;/math&gt;,1) where &lt;math&gt;\alpha&lt;/math&gt; is the shape, and 1 is the scale.&lt;br\&gt;<br /> *2. Sample from Y2 ~ Gamma (&lt;math&gt;\beta&lt;/math&gt;,1) where &lt;math&gt;\alpha&lt;/math&gt; is the shape, and 1 is the scale.&lt;br\&gt;<br /> *3. Set &lt;math&gt; Y = \frac{Y_1}{Y_1+Y_2}, &lt;/math&gt; Then Y ~ &lt;math&gt;\beta ( \alpha , \beta )&lt;/math&gt;, where we suppose &lt;math&gt;\alpha , \beta&lt;/math&gt; are integers.<br /> <br /> === Geometric ===<br /> <br /> This distribution models the number of failures before the first success.<br /> <br /> X~Geo(p)<br /> <br /> &lt;math&gt;f(x)=p*(1-p)^{x-1}, x=1, 2, ........&lt;/math&gt;&lt;br&gt;<br /> If Y~Exp&lt;math&gt;(\lambda)&lt;/math&gt; where &lt;math&gt;\lambda=-log(1-p)&lt;/math&gt;&lt;br&gt;<br /> then &lt;math&gt;X=floor[Y]+1&lt;/math&gt; is Geo(p)&lt;br&gt;<br /> <br /> '''Proof:'''<br /> &lt;math&gt;F(x)=P(X&lt;=x)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt; =1-P(X&gt;x)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt; =1-P(floor [Y]+1&gt;x)&lt;/math&gt; as &lt;math&gt; floor [Y]+1&gt;x&lt;/math&gt; implies &lt;math&gt; Y\leq x &lt;/math&gt; for all real-valued Y&lt;br&gt; <br /> &lt;math&gt; =1-P(Y&gt;=x)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt; =1-(1-P(Y&lt;x))&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt; =1-e^{-\lambda*x}&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt; =1-(1-p)^x&lt;/math&gt;, which is the CDF of Geo(p)&lt;br&gt;<br /> <br /> The above method can also be viewed with the inverse method since it is not rejecting any points. The following uses inverse method to generate Geo(p).&lt;br&gt;<br /> &lt;math&gt;F(x)=P(X \leq x)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;F(x)=1- P(X&gt;x)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;P(X \leq x)=1-(1-p)^x&lt;/math&gt; since &lt;math&gt;P(X&gt;x)=(1-p)^x&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;y=1-(1-p)^x&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;y-1=-(1-p)^x&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;1-y=(1-p)^x&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;1-y=(e^{-\lambda})^x=e^{-\lambda*x}&lt;/math&gt; since &lt;math&gt;1-p=e^{-\lambda}&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;log(1-y)=-\lambda*x&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;x=-1/(\lambda)*log(1-y)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;F^-1(x)=-1/(\lambda)*log(1-x)&lt;/math&gt;&lt;br&gt;<br /> &lt;br&gt;<br /> '''Algorithm:'''&lt;br /&gt;<br /> 1. Generate U ~ U(0,1)&lt;br&gt;<br /> 2. Generate &lt;math&gt;Y=-1/(\lambda)*log(1-u)&lt;/math&gt; where &lt;math&gt;Y ~ \sim Exp(\lambda) &lt;/math&gt;&lt;br&gt;<br /> 3. Set &lt;math&gt;X=[Y]+1&lt;/math&gt;&lt;br&gt;<br /> <br /> Note: &lt;br /&gt;<br /> If X~Unif (0,1), Y= floor(3X) = [3X]-&gt; Y ~ DU[0,2] (DU means discrete uniform)&lt;br /&gt;<br /> <br /> If X~Unif (0,1), Y= floor(5U)-2 = [5U]-2 -&gt; Y~ DU[-2,2] <br /> &lt;br&gt;<br /> <br /> There is also another intuitive method:&lt;br&gt;<br /> 1. Draw U ~ U(0,1)&lt;br&gt;<br /> 2. i = 1, Pi = 1 - (1 - P)^i. &lt;br&gt;<br /> 3. If u &lt;= Pi = 1 - (1 - P)^i, set X = i.<br /> Else, i = i + 1. &lt;br&gt;<br /> <br /> ===Poisson===<br /> <br /> This distribution models the number of times and event occurs in a given time period<br /> <br /> X~Poi&lt;math&gt;(\lambda)&lt;/math&gt; &lt;br&gt;<br /> X is the maximum number of iid Exp(&lt;math&gt;\lambda&lt;/math&gt;) whose sum is less than or equal to 1.&lt;br&gt;<br /> &lt;math&gt;X = \max\{n: \sum\limits_{i=1}^n Y_i \leq 1, Y_i \sim Exp(\lambda)\}&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt; = \max\{n: \sum\limits_{i=1}^n \frac{-1}{\lambda} log(U_i)&lt;=1 , U_i \sim U[0,1]\}&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt; = \max\{n: \prod\limits_{i=1}^n U_i &gt;= e^{-\lambda}, U_i \sim U[0,1]\}&lt;/math&gt;&lt;br&gt;<br /> <br /> '''Algorithm'''&lt;br\&gt;<br /> *1. Set n=1, a=1&lt;br\&gt;<br /> *2. &lt;math&gt;U_n&lt;/math&gt; ~ &lt;math&gt;U[0,1]&lt;/math&gt; and set &lt;math&gt;a=aU_n&lt;/math&gt;&lt;br\&gt;<br /> *3. If &lt;math&gt;a \geq e^{-\lambda}&lt;/math&gt; then: n=n+1 and go to Step 2. Else set X=n-1.<br /> <br /> An alternate way to write an algorithm for Poisson is as followings:<br /> <br /> 1) x = 0, F = P(X=0) = e^-λ = p<br /> <br /> <br /> 2) Generate U ~ U(0,1)<br /> <br /> <br /> 3) If U &lt; F, then output x<br /> <br /> <br /> 4) Else p = (λ/(x+1)) * p<br /> <br /> F = F + p<br /> <br /> x = x+1<br /> <br /> 5) else go to step 2<br /> <br /> Acknowledgments: from Spring 2012 stat 340 coursenotes<br /> <br /> == Class 13 - Tuesday June 18th 2013 ==<br /> '''Markov Chain'''<br /> &lt;br&gt;N-Step Transition Matrix: a matrix &lt;math&gt; P_n &lt;/math&gt; whose elements are the probability of moving from state i to state j in n steps. &lt;br/&gt;<br /> &lt;math&gt;P_n (i,j)=Pr⁡(X_{m+n}=j|X_m=i)&lt;/math&gt; &lt;br/&gt;<br /> <br /> Explanation: (with an example) Suppose there 10 states { 1, 2, ..., 10}, and suppose you are on state 2, then P&lt;sub&gt;8&lt;/sub&gt;(2, 5) represent the probability of moving from state 2 to state 5 in 8 steps.<br /> <br /> One-step transition probability:&lt;br/&gt;<br /> The probability of X&lt;sub&gt;n+1&lt;/sub&gt; being in state j given that X&lt;sub&gt;n&lt;/sub&gt; is in state i is called the <br /> one-step transition probability and is denoted by P&lt;sub&gt;i,j&lt;/sub&gt;&lt;sup&gt;n,n+1&lt;/sup&gt;. That is &lt;br/&gt;<br /> P&lt;sub&gt;i,j&lt;/sub&gt;&lt;sup&gt;n,n+1&lt;/sup&gt; = Pr(X&lt;sub&gt;n+1&lt;/sub&gt; =j/X&lt;sub&gt;n&lt;/sub&gt; =i)<br /> <br /> Example from previous class: &lt;br/&gt;<br /> <br /> &lt;math&gt; P= \left [ \begin{matrix}<br /> 0.7 &amp; 0.3 \\<br /> 0.2 &amp; 0.8<br /> \end{matrix} \right] &lt;/math&gt;<br /> <br /> The two step transition probability matrix is:<br /> <br /> &lt;math&gt; P P= \left [ \begin{matrix}<br /> 0.7 &amp; 0.3 \\<br /> 0.2 &amp; 0.8<br /> \end{matrix} \right] \left [ \begin{matrix}<br /> 0.7 &amp; 0.3 \\<br /> 0.2 &amp; 0.8<br /> \end{matrix} \right] &lt;/math&gt;=&lt;math&gt;\left [ \begin{matrix}<br /> 0.7(0.7)+0.3(0.2) &amp; 0.7(0.3)+0.3(0.8) \\<br /> 0.2(0.7)+0.8(0.2) &amp; 0.2(0.3)+0.8(0.8)<br /> \end{matrix} \right] &lt;/math&gt;=&lt;math&gt;\left [ \begin{matrix}<br /> 0.55 &amp; 0.45 \\<br /> 0.3 &amp; 0.7<br /> \end{matrix} \right] &lt;/math&gt;&lt;br\&gt;<br /> <br /> Interpretation:&lt;br\&gt;<br /> - If at time 0 we are in state 1, then the probability of us being in state 1 at time 2 is 0.55 and 0.45 for state 2.&lt;br\&gt;<br /> - If at time 0 we are in state 2, then the probability of us being in state 1 at time 2 is 0.3 and 0.7 for state 2.&lt;br\&gt;<br /> <br /> &lt;math&gt;P_2 = P_1 P_1 &lt;/math&gt;&lt;br\&gt;<br /> <br /> &lt;math&gt;P_3 = P_1 P_2 &lt;/math&gt;&lt;br\&gt;<br /> <br /> &lt;math&gt;P_n = P_1 P_(n-1) &lt;/math&gt;&lt;br\&gt;<br /> <br /> &lt;math&gt;P_n = P_1^n &lt;/math&gt;&lt;br\&gt;<br /> <br /> <br /> <br /> The two-step transition probability of moving from state a to state a: <br /> &lt;br/&gt;<br /> &lt;math&gt;P_2 (a,a)=Pr⁡ (X_{m+2}=a| X_m=a)=Pr⁡(X_{m+1}=a| X_m=a)Pr⁡(X_{m+2}=a|X_{m+1}=a)+ Pr⁡(X_{m+1}=b|X_m=a)Pr⁡(X_{m+2}=a|X_{m+1}=b)&lt;/math&gt; &lt;br/&gt;<br /> <br /> &lt;math&gt; =0.7(0.7)+0.3(0.2)=0.55 &lt;/math&gt;&lt;br/&gt;<br /> <br /> Another Example: &lt;br/&gt;<br /> <br /> &lt;math&gt; P= \left [ \begin{matrix}<br /> 1 &amp; 0 \\<br /> 0.7 &amp; 0.3<br /> \end{matrix} \right] &lt;/math&gt;<br /> <br /> The two step transition probability matrix is:<br /> <br /> &lt;math&gt; P P= \left [ \begin{matrix}<br /> 1 &amp; 0 \\<br /> 0.7 &amp; 0.3<br /> \end{matrix} \right] \left [ \begin{matrix}<br /> 1 &amp; 0 \\<br /> 0.7 &amp; 0.3<br /> \end{matrix} \right] &lt;/math&gt;=&lt;math&gt;\left [ \begin{matrix}<br /> 1(1)+ 0(0.7) &amp; 1(0) + 0(0.3) \\<br /> 1(0.7)+0.7(0.3) &amp; 0(0.7)+0.3(0.3)<br /> \end{matrix} \right] &lt;/math&gt;=&lt;math&gt;\left [ \begin{matrix}<br /> 1 &amp; 0 \\<br /> 0.91 &amp; 0.09<br /> \end{matrix} \right] &lt;/math&gt;&lt;br\&gt;<br /> <br /> This is the two-step transition matrix.<br /> <br /> === n-step transition matrix ===<br /> The elements of matrix P&lt;sub&gt;n&lt;/sub&gt; （i.e. the ij&lt;sub&gt;th&lt;/sub&gt; entry P&lt;sub&gt;ij&lt;/sub&gt;) is the probability of moving to state j from state i in n steps<br /> <br /> In general &lt;math&gt;P_n = P^n&lt;/math&gt; with &lt;math&gt;P_n(i,j) \geq 0&lt;/math&gt; and &lt;math&gt;\sum_{j} P_n(i,j) = 1&lt;/math&gt;&lt;br /&gt;<br /> Note: &lt;math&gt;P_2 = P_1\times P_1; P_n = P^n&lt;/math&gt;&lt;br /&gt;<br /> The equation above is a special case of the Chapman-Kolmogorov equations.&lt;br /&gt;<br /> It is true because of the Markov property or the memoryless property of Markov chains, where the probabilities of going forward to the next state &lt;br /&gt;<br /> only depends on your current state, not your previous states. By intuition, we can multiply the 1-step transition &lt;br /&gt;<br /> matrix n-times to get a n-step transition matrix.&lt;br /&gt;<br /> <br /> Example: We can see how &lt;math&gt;P_n = P^n&lt;/math&gt; from the following:<br /> &lt;br/&gt;<br /> &lt;math&gt;\vec{\mu_1}=\vec{\mu_0}\cdot P&lt;/math&gt; &lt;br/&gt;<br /> &lt;math&gt;\vec{\mu_2}=\vec{\mu_1}\cdot P&lt;/math&gt; &lt;br/&gt;<br /> &lt;math&gt;\vec{\mu_3}=\vec{\mu_2}\cdot P&lt;/math&gt; &lt;br/&gt;<br /> Therefore, <br /> &lt;br/&gt;<br /> &lt;math&gt;\vec{\mu_3}=\vec{\mu_0}\cdot P^3<br /> &lt;/math&gt; &lt;br/&gt;<br /> <br /> &lt;math&gt;P_n(i,j)&lt;/math&gt; is called n-steps transition probability. &lt;br&gt;<br /> &lt;math&gt;\vec{\mu_0} &lt;/math&gt; is called the '''initial distribution'''. &lt;br&gt;<br /> &lt;math&gt;\vec{\mu_n} = \vec{\mu_0}* P^n &lt;/math&gt; &lt;br /&gt;<br /> <br /> Example with Markov Chain:<br /> Consider a two-state Markov chain {&lt;math&gt;X_t; t = 0, 1, 2,...&lt;/math&gt;} with states {1,2} and transition probability matrix<br /> <br /> &lt;math&gt; P= \left [ \begin{matrix}<br /> 1/2 &amp; 1/2 \\<br /> 1/3 &amp; 2/3<br /> \end{matrix} \right] &lt;/math&gt;<br /> <br /> Given &lt;math&gt; X_0 = 1 &lt;/math&gt;. Compute the following:<br /> <br /> a)&lt;math&gt; P(X_1=1 | X_0=1) = P(1,1) = 1/2 &lt;/math&gt;<br /> <br /> b)&lt;math&gt; P(X_2=1, X_1=1 |X_0=1) = P(X_2=1|X_1=1)*P(X_1=1|X_0=1)= 1/2 * 1/2 = 1/4 &lt;/math&gt;<br /> <br /> c)&lt;math&gt; P(X_2=1|X_0=1)= P_2(1,1) = 5/12 &lt;/math&gt;<br /> <br /> d)&lt;math&gt; P^2=P*P= \left [ \begin{matrix}<br /> 5/12 &amp; 7/12 \\<br /> 7/18 &amp; 11/18<br /> \end{matrix} \right] &lt;/math&gt;<br /> <br /> === Marginal Distribution of Markov Chain ===<br /> We represent the probability of all states at time t with a vector &lt;math&gt;\underline{\mu_t}&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;\underline{\mu_t}~=(\mu_t(1), \mu_t(2),...\mu_t(n))&lt;/math&gt; where &lt;math&gt;\underline{\mu_t(1)}&lt;/math&gt; is the probability of being on state 1 at time t.&lt;br/&gt;<br /> and in general, &lt;math&gt;\underline{\mu_t(i)}&lt;/math&gt; shows the probability of being on state i at time t.&lt;br/&gt;<br /> For example, if there are two states a and b, then &lt;math&gt;\underline{\mu_5}&lt;/math&gt;=(0.1, 0.9) means that the chance of being in state a at time 5 is 0.1 and the chance of being on state b at time 5 is 0.9. &lt;br/&gt;<br /> If we generate a chain for many times, the frequency of states at each time shows marginal distribution of the chain at that time. &lt;br/&gt;<br /> The vector &lt;math&gt;\underline{\mu_0}&lt;/math&gt; is called the initial distribution. &lt;br/&gt;<br /> <br /> &lt;math&gt; P^2~=P\cdot P &lt;/math&gt; (as verified above) <br /> <br /> In general,<br /> &lt;math&gt; P^n~= \Pi_{i=1}^{n} P&lt;/math&gt; (P multiplied n times)&lt;br/&gt;<br /> &lt;math&gt;\mu_n~=\mu_0 P^n&lt;/math&gt;&lt;br/&gt;<br /> where &lt;math&gt;\mu_0&lt;/math&gt; is the initial distribution,<br /> and &lt;math&gt;\mu_{m+n}~=\mu_m P^n&lt;/math&gt;&lt;br/&gt;<br /> N can be negative, if P is invertible.<br /> <br /> &lt;br/&gt;<br /> 0 a a b a&lt;br/&gt;<br /> 1 a b a a&lt;br/&gt;<br /> 2 b a a b&lt;br/&gt;<br /> 3 b a b b&lt;br/&gt;<br /> 4 a a a b&lt;br/&gt;<br /> 5 a b b a&lt;br/&gt;<br /> &lt;math&gt;\mu_4~=(3/4, 1/4)&lt;/math&gt;&lt;br/&gt;<br /> <br /> if we simulate a chain many times, frequency of states at time t show the marginal distribution at time t &lt;br /&gt;<br /> <br /> <br /> <br /> <br /> Marginal Distribution<br /> <br /> &lt;math&gt;\mu_1~ = \mu_0P&lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt;\mu_2~ = \mu_1P = \mu_0PP = \mu_0P^2&lt;/math&gt; &lt;br&gt;<br /> <br /> In general, &lt;math&gt;\mu_n~ = \mu_0P^n&lt;/math&gt;&lt;br /&gt;<br /> Property: If &lt;math&gt;\mu_n~\neq\mu_t~&lt;/math&gt;(for any t less than n), then we say P does not converge. &lt;br /&gt;<br /> <br /> <br /> <br /> <br /> ==== Stationary Distribution ====<br /> <br /> <br /> <br /> <br /> &lt;math&gt;\pi&lt;/math&gt; is stationary distribution of the chain if &lt;math&gt;\pi&lt;/math&gt;P = &lt;math&gt;\pi&lt;/math&gt;<br /> <br /> where &lt;math&gt;\pi&lt;/math&gt; is a probability vector &lt;math&gt;\pi&lt;/math&gt;=(&lt;math&gt;\pi&lt;/math&gt;&lt;sub&gt;i&lt;/sub&gt; | &lt;math&gt;i \in X&lt;/math&gt;) such that all the entries are nonnegative and sum to 1. It is the eigenvector in this case.<br /> <br /> In other words, if X''&lt;sub&gt;0&lt;/sub&gt;'' is draw from &lt;math&gt;\pi&lt;/math&gt;. Then marginally, X''&lt;sub&gt;n&lt;/sub&gt;'' is also drawn from the same distribution &lt;math&gt;\pi&lt;/math&gt; for every n≥0.<br /> <br /> The above conditions are used to find the stationary distribution<br /> In matlab, we could use &lt;math&gt;P^n&lt;/math&gt; to find the stationary distribution.(n is usually larger than 100)&lt;br/&gt;<br /> <br /> <br /> '''Comments:'''&lt;br/&gt;<br /> As n gets bigger and bigger, &lt;math&gt;\mu_n&lt;/math&gt; will possibly stop changing, so the quantity &lt;math&gt;\pi&lt;/math&gt; &lt;sub&gt;i&lt;/sub&gt; can also be interpreted as the limiting probability that the chain is in the state &lt;math&gt;j&lt;/math&gt;<br /> <br /> Comments: &lt;br/&gt;<br /> 1. &lt;math&gt;\pi&lt;/math&gt; may not exist and even if it exists, it may not always be unique. &lt;br/&gt;<br /> 2. If &lt;math&gt;\pi&lt;/math&gt; exists and is unique, then &lt;math&gt;\pi&lt;/math&gt;&lt;sub&gt;i&lt;/sub&gt; is called the long-run proportion of the process in state i and the stationary distribution is also the limiting distribution of the process.&lt;br/&gt;<br /> <br /> How long do you have to wait until you reach a steady sate?<br /> Ans: There is not clear way to find that out <br /> <br /> How do you increase the time it takes to reach the steady state?<br /> Ans: Make the probabilities of transition much smaller, to reach from state 0 to state 1 and vice-versa p=0.005. And make the probabilities of staying in the same state extremely high. To stay in state 0 or state 1 p=0.995, then the matrix is in a &quot;sticky state&quot;<br /> <br /> <br /> EXAMPLE : Random Walk on the cycle S={0,1,2}<br /> <br /> &lt;math&gt;P^2 = \left[ \begin{array}{ccc}<br /> 2pq &amp; q^2 &amp; p^2 \\<br /> p^2 &amp; 2pq &amp; q^2 \\<br /> q^2 &amp; p^2 &amp; 2pq \end{array} \right]&lt;/math&gt;<br /> <br /> Suppose&lt;br/&gt;<br /> &lt;math&gt;P(x_0=0)=\frac{1}{4}&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;P(x_0=1)=\frac{1}{2}&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;P(x_0=2)=\frac{1}{4}&lt;/math&gt;&lt;br/&gt;<br /> Thus&lt;br/&gt;<br /> &lt;math&gt;\pi_0 = \left[ \begin{array}{c} \frac{1}{4} \\ \frac{1}{2} \\ \frac{1}{4} \end{array} \right]&lt;/math&gt;&lt;br/&gt;<br /> so&lt;br/&gt;<br /> &lt;math&gt;\,\pi^2 = \pi_0 * P^2 &lt;/math&gt;<br /> &lt;math&gt;= \left[ \begin{array}{c} \frac{1}{4} \\ \frac{1}{2} \\ \frac{1}{4} \end{array} \right] * \left[ \begin{array}{ccc}<br /> 2pq &amp; q^2 &amp; p^2 \\<br /> p^2 &amp; 2pq &amp; q^2 \\<br /> q^2 &amp; p^2 &amp; 2pq \end{array} \right]&lt;/math&gt;<br /> &lt;math&gt;= \left[ \begin{array}{c} \frac{1}{2}pq + \frac{1}{2}p^2+\frac{1}{4}q^2 \\ \frac{1}{4}q^2+pq+\frac{1}{4}p^2 \\ \frac{1}{4}p^2+\frac{1}{2}q^2+\frac{1}{2}pq\end{array} \right]&lt;/math&gt;<br /> <br /> ==== MatLab Code ====<br /> &lt;pre style='font-size:14px'&gt;<br /> <br /> In Matlab, you can find the stationary distribution by:<br /> <br /> &gt;&gt; p=[.7 .3;.2 .8] % Input the matrix P<br /> <br /> p =<br /> <br /> 0.7000 0.3000<br /> 0.2000 0.8000<br /> <br /> &gt;&gt; p^2 % one state to another state by 2 steps transition<br /> <br /> ans =<br /> <br /> 0.5500 0.4500<br /> 0.3000 0.7000<br /> <br /> &gt;&gt; mu=[.9 .1] <br /> <br /> mu =<br /> <br /> 0.9000 0.1000<br /> <br /> &gt;&gt; mu*p % enter mu=mu*P, repeat multiple times until the value of the vector mu remains unchanged<br /> <br /> ans =<br /> <br /> 0.6500 0.3500<br /> <br /> &gt;&gt; mu=mu*p<br /> <br /> mu =<br /> <br /> 0.4002 0.5998<br /> <br /> &gt;&gt; mu=mu*p %The vector mu will be your stationary distribution<br /> <br /> mu =<br /> <br /> 0.4000 0.6000<br /> <br /> <br /> &gt;&gt; p^100 % it is limiting distribution of chain which finally gives a stable matrix<br /> <br /> ans =<br /> <br /> 0.4000 0.6000<br /> 0.4000 0.6000<br /> <br /> <br /> &lt;/pre&gt;<br /> The definition of stationary distribution is that &lt;math&gt;\pi&lt;/math&gt; is the stationary distribution of the chain if &lt;math&gt;\pi=\pi~P&lt;/math&gt;, where &lt;math&gt;\pi&lt;/math&gt; is a probability vector. For every n&lt;math&gt;&gt;=&lt;/math&gt;0.<br /> <br /> However, just because X&lt;sub&gt;''n''&lt;/sub&gt; ~ &lt;math&gt;\pi&lt;/math&gt; for every n&lt;math&gt;&gt;=&lt;/math&gt;0 does ''not'' mean every state is independently identically distributed.<br /> <br /> '''Limiting distribution''' of the chain refers the transition matrix that reaches the stationary state. If the lim(n-&gt; infinite)P^n -&gt; c, where c is a constant, then, we say this Markov chain is coverage; otherwise, it's not coverage.<br /> <br /> Example: Find the stationary distribution of P= &lt;math&gt;\left[ {\begin{array}{ccc}<br /> 1/3 &amp; 1/3 &amp; 1/3 \\<br /> 1/4 &amp; 3/4 &amp; 0 \\<br /> 1/2 &amp; 0 &amp; 1/2 \end{array} } \right]&lt;/math&gt;<br /> <br /> Solution:<br /> &lt;math&gt;\pi=\left[ {\begin{array}{ccc}<br /> \pi_0 &amp; \pi_1 &amp; \pi_2 \end{array} } \right]&lt;/math&gt;<br /> <br /> Using the stationary distribution property &lt;math&gt;\pi=\pi~P&lt;/math&gt; we get, &lt;br&gt;<br /> &lt;math&gt;\pi_0=\frac{1}{3}\pi_0+\frac{1}{4}\pi_1+\frac{1}{2}\pi_2 &lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\pi_1=\frac{1}{3}\pi_0+\frac{3}{4}\pi_1+0\pi_2 &lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\pi_2=\frac{1}{3}\pi_0+0\pi_1+\frac{1}{2}\pi_2 &lt;/math&gt;&lt;br&gt;<br /> <br /> And since &lt;math&gt;\pi&lt;/math&gt; is a probability vector, &lt;br&gt;<br /> &lt;math&gt; \pi_{0}~ + \pi_{1} + \pi_{2} = 1 &lt;/math&gt;<br /> <br /> Solving the 4 equations for the 3 unknowns gets, &lt;br&gt;<br /> &lt;math&gt;\pi_{0}~=1/3&lt;/math&gt;, &lt;math&gt;\pi_{1}~=4/9&lt;/math&gt;, and &lt;math&gt;\pi_{2}~=2/9&lt;/math&gt; &lt;br&gt;<br /> Therefore &lt;math&gt;\pi=\left[ {\begin{array}{ccc}<br /> 1/3 &amp; 4/9 &amp; 2/9 \end{array} } \right]&lt;/math&gt;<br /> <br /> Example 2: Find the stationary distribution of P= &lt;math&gt;\left[ {\begin{array}{ccc}<br /> 1/3 &amp; 1/3 &amp; 1/3 \\<br /> 1/4 &amp; 1/2 &amp; 1/4 \\<br /> 1/6 &amp; 1/3 &amp; 1/2 \end{array} } \right]&lt;/math&gt;<br /> <br /> Solution:<br /> &lt;math&gt;\pi=\left[ {\begin{array}{ccc}<br /> \pi_0 &amp; \pi_1 &amp; \pi_2 \end{array} } \right]&lt;/math&gt;<br /> <br /> Using the stationary distribution property &lt;math&gt;\pi=\pi~P&lt;/math&gt; we get, &lt;br&gt;<br /> &lt;math&gt;\pi_0=\frac{1}{3}\pi_0+\frac{1}{4}\pi_1+\frac{1}{6}\pi_2 &lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\pi_1=\frac{1}{3}\pi_0+\frac{1}{2}\pi_1+\frac{1}{3}\pi_2 &lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\pi_2=\frac{1}{3}\pi_0+\frac{1}{4}\pi_1+\frac{1}{2}\pi_2 &lt;/math&gt;&lt;br&gt;<br /> <br /> And since &lt;math&gt;\pi&lt;/math&gt; is a probability vector, &lt;br&gt;<br /> &lt;math&gt; \pi_{0}~ + \pi_{1} + \pi_{2} = 1 &lt;/math&gt;<br /> <br /> Solving the 4 equations for the 3 unknowns gets, &lt;br&gt;<br /> &lt;math&gt;\pi_{0}=\frac {6}{25}&lt;/math&gt;, &lt;math&gt;\pi_{1}~=\frac {2}{5}&lt;/math&gt;, and &lt;math&gt;\pi_{2}~=\frac {9}{25}&lt;/math&gt; &lt;br&gt;<br /> Therefore &lt;math&gt;\pi=\left[ {\begin{array}{ccc}<br /> \frac {6}{25} &amp; \frac {2}{5} &amp; \frac {9}{25} \end{array} } \right]&lt;/math&gt;<br /> <br /> The above two examples are designed to solve for the stationary distribution of the matrix P however they also give us the limiting distribution of the matrices as we have mentioned earlier that the stationary distribution is equivalent to the limiting distribution.<br /> <br /> '''Alternate Method of Computing the Stationary Distribution''' &lt;br&gt;<br /> <br /> Recall that if &lt;math&gt;\lambda v = A v&lt;/math&gt;, then &lt;math&gt;\lambda&lt;/math&gt; is the eigenvalue of &lt;math&gt;A&lt;/math&gt; corresponding to the eigenvector &lt;math&gt;v&lt;/math&gt;&lt;br&gt;<br /> <br /> By definition of stationary distribution, &lt;math&gt;\pi = \pi P&lt;/math&gt;&lt;br&gt;<br /> Taking the transpose, &lt;math&gt;\pi^T = (\pi P)^T &lt;/math&gt;&lt;br&gt;<br /> then &lt;math&gt;I \pi^T = P^T \pi^T \Rightarrow (P^T-I) \pi^T = 0 &lt;/math&gt;&lt;br&gt;<br /> So &lt;math&gt;\pi^T &lt;/math&gt; is an eigenvector of &lt;math&gt;P^T&lt;/math&gt; with corresponding eigenvalue 1. &lt;br&gt;<br /> <br /> the transpose method to calculate the pi matrix probability.<br /> <br /> It is thus possible to compute the stationary distribution by taking the eigenvector of the transpose of the transition matrix corresponding to 1, and normalize it such that all elements are non-negative and sum to one so that the elements satisfy the definition of a stationary distribution. The transformed vector is still an eigenvector since a linear transformation of an eigenvector is still within the eigenspace. Taking the transpose of this transformed eigenvector gives the stationary distribution. &lt;br&gt; <br /> <br /> <br /> <br /> <br /> &lt;span style=&quot;background:#F5F5DC&quot;&gt;<br /> Generating Random Initial distribution&lt;br&gt;<br /> &lt;math&gt;\mu~=rand(1,n)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\mu~=\frac{\mu}{\Sigma(\mu)}&lt;/math&gt;&lt;/span&gt;<br /> <br /> &lt;span style=&quot;background:#F5F5DC&quot;&gt;<br /> Doubly Stochastic Matrices&lt;br&gt;&lt;/span&gt;<br /> We say that the transition matrix &lt;math&gt;\, P=(p_{ij})&lt;/math&gt; is doubly stochastic if both rows and columns sum to 1, i.e.,&lt;br&gt;<br /> &lt;math&gt;\, \sum_{i} p_{ji} = \sum_{j} p_{ij} = 1 &lt;/math&gt;&lt;br&gt;<br /> It is easy to show that the stationary distribution of an nxn doubly stochastic matrix P is:&lt;br&gt;<br /> &lt;math&gt; (\frac{1}{n}, \ldots , \frac{1}{n}) &lt;/math&gt;<br /> <br /> === Properties of Markov Chain ===<br /> <br /> A Markov chain is a random process usually characterized as '''memoryless''': the next state depends only on the current state and not on the sequence of events that preceded it. This specific kind of &quot;memorylessness&quot; is called the Markov property. Markov chains have many applications as statistical models of real-world processes.<br /> <br /> 1. Reducibility &lt;br&gt;<br /> State &lt;math&gt;j&lt;/math&gt; is said to be accessible from State &lt;math&gt;i&lt;/math&gt; (written &lt;math&gt;i \rightarrow j&lt;/math&gt;) if a system started in State &lt;math&gt;i&lt;/math&gt; has a non-zero probability of transitioning into State &lt;math&gt;j&lt;/math&gt; at some point. Formally, State &lt;math&gt;j&lt;/math&gt; is accessible from State &lt;math&gt;i&lt;/math&gt; if there exists an integer &lt;math&gt;n_{ij} \geq 0&lt;/math&gt; such that &lt;br&gt;<br /> &lt;math&gt;P(X_{n_{ij}} =j \vert X_0 =i) &gt; 0&lt;/math&gt;&lt;br&gt;<br /> <br /> This integer is allowed to be different for each pair of states, hence the subscripts in &lt;math&gt;n_{ij}&lt;/math&gt;. By allowing n to be zero, every state is defined to be accessible from itself.&lt;br /&gt;<br /> <br /> 2. Periodicity &lt;br&gt;<br /> State &lt;math&gt;i&lt;/math&gt; has period &lt;math&gt;k&lt;/math&gt; if any return to State &lt;math&gt;i&lt;/math&gt; must occur in multiples of &lt;math&gt;k&lt;/math&gt; time steps. Formally, the period of a state is defined as &lt;br&gt;<br /> &lt;math&gt;k= \gcd\{n:P(X_n =j \vert X_0 =i)&gt;0\} &lt;/math&gt;&lt;br /&gt;<br /> <br /> 3. Recurrence &lt;br&gt;<br /> State &lt;math&gt;i&lt;/math&gt; is said to be transient if, given that we start in State &lt;math&gt;i&lt;/math&gt;, there is a non-zero probability that we will never return to &lt;math&gt;i&lt;/math&gt;. Formally, let the random variable &lt;math&gt;T_i&lt;/math&gt; be the first return time to State &lt;math&gt;i&lt;/math&gt; (the &quot;hitting time&quot;): &lt;br&gt;<br /> &lt;math&gt;T_i = \min\{n \geq 1:X_n=i \vert X_0=i\}&lt;/math&gt;&lt;br /&gt;<br /> <br /> (The properties are from<br /> http://www2.math.uu.se/~takis/L/McRw/mcrw.pdf)<br /> <br /> CHAPMAN-KOLMOGOROV EQUATION<br /> For all &lt;math&gt;n&lt;/math&gt; and &lt;math&gt;m&lt;/math&gt;, and any state &lt;math&gt;i&lt;/math&gt; and &lt;math&gt;j&lt;/math&gt;,<br /> &lt;math&gt;P^{n+m}(X_n+m = j \vert X_0 =i)= \sum_{k} P^n(X_1 = k \vert X_0 = i)*P^m(X_1 = j \vert X_0 =k)&lt;/math&gt;<br /> <br /> == Class 14 - Thursday June 20th 2013 ==<br /> <br /> Example: Find the stationary distribution of &lt;math&gt; P= \left[ {\begin{array}{ccc}<br /> \frac{1}{3} &amp; \frac{1}{3} &amp; \frac{1}{3} \\[6pt]<br /> \frac{1}{4} &amp; \frac{3}{4} &amp; 0 \\[6pt]<br /> \frac{1}{2} &amp; 0 &amp; \frac{1}{2} \end{array} } \right]&lt;/math&gt;<br /> <br /> &lt;math&gt;\displaystyle \pi=\pi p&lt;/math&gt;<br /> <br /> Solve the system of linear equations to find a stationary distribution<br /> <br /> &lt;math&gt;\displaystyle \pi=(\frac{1}{3},\frac{4}{9}, \frac{2}{9})&lt;/math&gt;<br /> <br /> Note that &lt;math&gt;\displaystyle \pi=\pi p&lt;/math&gt; looks similar to eigenvectors/values &lt;math&gt;\displaystyle \lambda vec{u}=A vec{u}&lt;/math&gt;<br /> <br /> &lt;math&gt;\pi&lt;/math&gt; can be considered as an eigenvector of P with eigenvalue = 1. But note that the vector &lt;math&gt;vec{u}&lt;/math&gt; is a column vector and o we need to transform our &lt;math&gt;\pi&lt;/math&gt; into a column vector.<br /> <br /> &lt;math&gt;=&gt; \pi&lt;/math&gt;&lt;sup&gt;T&lt;/sup&gt;= P&lt;sup&gt;T&lt;/sup&gt;&lt;math&gt;\pi&lt;/math&gt;&lt;sup&gt;T&lt;/sup&gt;&lt;br/&gt;<br /> Then &lt;math&gt;\pi&lt;/math&gt;&lt;sup&gt;T&lt;/sup&gt; is an eigenvector of P&lt;sup&gt;T&lt;/sup&gt; with eigenvalue = 1. &lt;br /&gt;<br /> MatLab tips:[V D]=eig(A), where D is a diagonal matrix of eigenvalues and V is a matrix of eigenvectors of matrix A&lt;br /&gt;<br /> ==== MatLab Code ====<br /> &lt;pre style='font-size:14px'&gt;<br /> <br /> P = [1/3 1/3 1/3; 1/4 3/4 0; 1/2 0 1/2]<br /> <br /> pii = [1/3 4/9 2/9]<br /> <br /> [vec val] = eig(P') %% P' is the transpose of matrix P<br /> <br /> vec(:,1) = [-0.5571 -0.7428 -0.3714] %% this is in column form<br /> <br /> a = -vec(:,1)<br /> <br /> &gt;&gt; a = <br /> [0.5571 0.7428 0.3714] <br /> <br /> %% a is in column form<br /> <br /> %% Since we want this vector a to sum to 1, we have to scale it<br /> <br /> b = a/sum(a)<br /> <br /> &gt;&gt; b =<br /> [0.3333 0.4444 0.2222] <br /> <br /> %% b is also in column form<br /> <br /> %% Observe that b' = pii<br /> <br /> &lt;/pre&gt;<br /> &lt;/br&gt;<br /> ==== Limiting distribution ====<br /> A Markov chain has limiting distribution &lt;math&gt;\pi&lt;/math&gt; if<br /> <br /> &lt;math&gt;\lim_{n\to \infty} P^n= \left[ {\begin{array}{ccc}<br /> \pi_1 \\<br /> \vdots \\<br /> \pi_n \\<br /> \end{array} } \right]&lt;/math&gt;<br /> <br /> That is &lt;math&gt;\pi_j=\lim[P^n]_{ij}&lt;/math&gt; exists and is independent of i.&lt;br/&gt; <br /> <br /> A Markov Chain is convergent if and only if its limiting distribution exists. &lt;br/&gt;<br /> <br /> If the limiting distribution &lt;math&gt;\pi&lt;/math&gt; exists, it must be equal to the stationary distribution.&lt;br/&gt;<br /> <br /> This convergence means that,in the long run(n to infinity),the probability of finding the &lt;br/&gt;<br /> Markov chain in state j is approximately &lt;math&gt;\pi_j&lt;/math&gt; no matter in which state &lt;br/&gt;<br /> the chain began at time 0. &lt;br/&gt;<br /> <br /> '''Example:'''<br /> For a transition matrix &lt;math&gt; P= \left [ \begin{matrix}<br /> 0 &amp; 1 &amp; 0 \\[6pt]<br /> 0 &amp; 0 &amp; 1 \\[6pt]<br /> 1 &amp; 0 &amp; 0 \\[6pt]<br /> \end{matrix} \right] &lt;/math&gt;<br /> , find stationary distribution.&lt;br/&gt;<br /> We have:&lt;br/&gt;<br /> &lt;math&gt;0\times \pi_0+0\times \pi_1+1\times \pi_2=\pi_0&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;1\times \pi_0+0\times \pi_1+0\times \pi_2=\pi_1&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;0\times \pi_0+1\times \pi_1+0\times \pi_2=\pi_2&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;\,\pi_0+\pi_1+\pi_2=1&lt;/math&gt;&lt;br/&gt;<br /> this gives &lt;math&gt;\pi = \left [ \begin{matrix}<br /> \frac{1}{3} &amp; \frac{1}{3} &amp; \frac{1}{3} \\[6pt]<br /> \end{matrix} \right] &lt;/math&gt; &lt;br/&gt;<br /> However, there does not exist a limiting distribution. &lt;math&gt; \pi &lt;/math&gt; is stationary but is not limiting.&lt;br/&gt;<br /> &lt;br/&gt;<br /> In general, there are chains with stationery distributions that don't converge, this means that they have stationary distribution but are not limiting.&lt;br/&gt;<br /> <br /> === MatLab Code ===<br /> &lt;pre style='font-size:14px'&gt;<br /> MATLAB<br /> &gt;&gt; P=[0, 1, 0;0, 0, 1; 1, 0, 0]<br /> <br /> P =<br /> <br /> 0 1 0<br /> 0 0 1<br /> 1 0 0<br /> <br /> &gt;&gt; pii=[1/3, 1/3, 1/3]<br /> <br /> pii =<br /> <br /> 0.3333 0.3333 0.3333<br /> <br /> &gt;&gt; pii*P<br /> <br /> ans =<br /> <br /> 0.3333 0.3333 0.3333<br /> <br /> &gt;&gt; P^1000<br /> <br /> ans =<br /> <br /> 0 1 0<br /> 0 0 1<br /> 1 0 0<br /> <br /> &gt;&gt; P^10000<br /> <br /> ans =<br /> <br /> 0 1 0<br /> 0 0 1<br /> 1 0 0<br /> <br /> &gt;&gt; P^10002<br /> <br /> ans =<br /> <br /> 1 0 0<br /> 0 1 0<br /> 0 0 1<br /> <br /> &gt;&gt; P^10003<br /> <br /> ans =<br /> <br /> 0 1 0<br /> 0 0 1<br /> 1 0 0<br /> <br /> &gt;&gt; %P^10000 = P^10003<br /> &gt;&gt; % This chain does not have limiting distribution, it has a stationary distribution. <br /> <br /> This chain does not converge, it has a cycle.<br /> &lt;/pre&gt;<br /> <br /> The first condition of limiting distribution is satisfied; however, the second condition where &lt;math&gt;\pi&lt;/math&gt;&lt;sub&gt;j&lt;/sub&gt; has to be independent of i (i.e. all rows of the matrix are the same) is not met.&lt;br&gt;<br /> <br /> This example shows the distinction between having a stationary distribution and convergence(having a limiting distribution).Note: &lt;math&gt;\pi=(1/3,1/3,1/3)&lt;/math&gt; is the stationary distribution as &lt;math&gt;\pi=\pi*p&lt;/math&gt;. However, upon repeatedly multiplying P by itself (repeating the step &lt;math&gt;P^n&lt;/math&gt; as n goes to infinite) one will note that the results become a cycle (of period 3) of the same sequence of matrices. The chain has a stationary distribution, but does not converge to it. Thus, there is no limiting distribution.&lt;br&gt;<br /> <br /> '''Example:'''<br /> <br /> &lt;math&gt; P= \left [ \begin{matrix}<br /> \frac{4}{5} &amp; \frac{1}{5} &amp; 0 &amp; 0 \\[6pt]<br /> \frac{1}{5} &amp; \frac{4}{5} &amp; 0 &amp; 0 \\[6pt]<br /> 0 &amp; 0 &amp; \frac{4}{5} &amp; \frac{1}{5} \\[6pt]<br /> 0 &amp; 0 &amp; \frac{1}{10} &amp; \frac{9}{10} \\[6pt]<br /> \end{matrix} \right] &lt;/math&gt;<br /> <br /> This chain converges but is not a limiting distribution as the rows are not the same and it doesn't converge to the stationary distribution.&lt;br /&gt;<br /> &lt;br /&gt;<br /> Double Stichastic Matrix: a double stichastic matrix is a matrix whose all colums sum to 1 and all rows sum to 1.&lt;br /&gt;<br /> If a given transition matrix is a double stichastic matrix with n colums and n rows, then the stationary distribution matrix has all&lt;br/&gt;<br /> elements equals to 1/n.&lt;br/&gt;<br /> &lt;br/&gt;<br /> Example:&lt;br/&gt;<br /> For a stansition matrix &lt;math&gt; P= \left [ \begin{matrix}<br /> 0 &amp; \frac{1}{2} &amp; \frac{1}{2} \\[6pt]<br /> \frac{1}{2} &amp; 0 &amp; \frac{1}{2} \\[6pt]<br /> \frac{1}{2} &amp; \frac{1}{2} &amp; 0 \\[6pt]<br /> \end{matrix} \right] &lt;/math&gt;,&lt;br/&gt;<br /> We have:&lt;br/&gt;<br /> &lt;math&gt;0\times \pi_0+\frac{1}{2}\times \pi_1+\frac{1}{2}\times \pi_2=\pi_0&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;\frac{1}{2}\times \pi_0+0\times \pi_1+\frac{1}{2}\times \pi_2=\pi_1&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;\frac{1}{2}\times \pi_0+\frac{1}{2}\times \pi_1+0\times \pi_2=\pi_2&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;\pi_0+\pi_1+\pi_2=1&lt;/math&gt;&lt;br/&gt;<br /> The stationary distribution is &lt;math&gt;\pi = \left [ \begin{matrix}<br /> \frac{1}{3} &amp; \frac{1}{3} &amp; \frac{1}{3} \\[6pt]<br /> \end{matrix} \right] &lt;/math&gt; &lt;br/&gt;<br /> <br /> <br /> &lt;span style=&quot;font-size:20px;color:red&quot;&gt;The following contents are problematic. Please correct it if possible.&lt;/span&gt;&lt;br /&gt;<br /> Suppose we're given that the limiting distribution &lt;math&gt; \pi &lt;/math&gt; exists for stochastic matrix P, that is, &lt;math&gt; \pi = \pi \times P &lt;/math&gt; &lt;br&gt;<br /> <br /> WLOG assume P is diagonalizable, (if not we can always consider the Jordan form and the computation below is exactly the same. &lt;br&gt;<br /> <br /> Let &lt;math&gt; P = U \Sigma U^{-1} &lt;/math&gt; be the eigenvalue decomposition of &lt;math&gt; P &lt;/math&gt;, where &lt;math&gt;\Sigma = diag(\lambda_1,\ldots,\lambda_n) ; |\lambda_i| &gt; |\lambda_j|, \forall i &lt; j &lt;/math&gt;&lt;br&gt;<br /> <br /> Suppose &lt;math&gt; \pi^T = \sum a_i u_i &lt;/math&gt; where &lt;math&gt; a_i \in \mathcal{R} &lt;/math&gt; and &lt;math&gt; u_i &lt;/math&gt; are eigenvectors of &lt;math&gt; P &lt;/math&gt; for &lt;math&gt; i = 1\ldots n &lt;/math&gt; &lt;br&gt;<br /> <br /> By definition: &lt;math&gt; \pi^k = \pi P = \pi P^k \implies \pi = \pi(U \Sigma U^{-1}) (U \Sigma U^{-1} ) \ldots (U \Sigma U^{-1}) &lt;/math&gt; &lt;br&gt;<br /> <br /> Therefore &lt;math&gt; \pi^k = \sum a_i \lambda_i^k u_i &lt;/math&gt; since &lt;math&gt; &lt;u_i , u_j&gt; = 0, \forall i\neq j &lt;/math&gt;. &lt;br&gt;<br /> <br /> Therefore &lt;math&gt; \lim_{k \rightarrow \infty} \pi^k = \lim_{k \rightarrow \infty} \lambda_i^k a_1 u_1 = u_1 &lt;/math&gt;<br /> <br /> === MatLab Code ===<br /> &lt;pre style='font-size:14px'&gt;<br /> &gt;&gt; P=[1/3, 1/3, 1/3; 1/4, 3/4, 0; 1/2, 0, 1/2] % We input a matrix P. This is the same matrix as last class. <br /> <br /> P =<br /> <br /> 0.3333 0.3333 0.3333<br /> 0.2500 0.7500 0<br /> 0.5000 0 0.5000<br /> <br /> &gt;&gt; P^2<br /> <br /> ans =<br /> <br /> 0.3611 0.3611 0.2778<br /> 0.2708 0.6458 0.0833<br /> 0.4167 0.1667 0.4167<br /> <br /> &gt;&gt; P^3<br /> <br /> ans =<br /> <br /> 0.3495 0.3912 0.2593<br /> 0.2934 0.5747 0.1319<br /> 0.3889 0.2639 0.3472<br /> <br /> &gt;&gt; P^10<br /> <br /> The example of code and an example of stand distribution, then the all the pi probability in the matrix are the same.<br /> <br /> ans =<br /> <br /> 0.3341 0.4419 0.2240<br /> 0.3314 0.4507 0.2179<br /> 0.3360 0.4358 0.2282<br /> <br /> &gt;&gt; P^100 % The stationary distribution is [0.3333 0.4444 0.2222] since values keep unchanged.<br /> <br /> ans =<br /> <br /> 0.3333 0.4444 0.2222<br /> 0.3333 0.4444 0.2222<br /> 0.3333 0.4444 0.2222<br /> <br /> <br /> &gt;&gt; [vec val]=eigs(P') % We can find the eigenvalues and eigenvectors from the transpose of matrix P.<br /> <br /> vec =<br /> <br /> -0.5571 0.2447 0.8121<br /> -0.7428 -0.7969 -0.3324<br /> -0.3714 0.5523 -0.4797<br /> <br /> <br /> val =<br /> <br /> 1.0000 0 0<br /> 0 0.6477 0<br /> 0 0 -0.0643<br /> <br /> &gt;&gt; a=-vec(:,1) % The eigenvectors can be mutiplied by (-1) since λV=AV can be written as λ(-V)=A(-V)<br /> <br /> a =<br /> <br /> 0.5571<br /> 0.7428<br /> 0.3714<br /> <br /> &gt;&gt; sum(a)<br /> <br /> ans =<br /> <br /> 1.6713<br /> <br /> &gt;&gt; a/sum(a)<br /> <br /> ans =<br /> <br /> 0.3333<br /> 0.4444<br /> 0.2222<br /> &lt;/pre&gt;<br /> <br /> This is &lt;math&gt;\pi_j = lim[p^n]_(ij)&lt;/math&gt; exist and is independent of i<br /> <br /> Another example:<br /> <br /> <br /> Find the stationary distribution of P= &lt;math&gt;\left[ {\begin{array}{ccc}<br /> 0.5 &amp; 0 &amp; 0 \\<br /> 1 &amp; 0 &amp; 0.5 \\<br /> 0 &amp; 1 &amp; 0.5 \end{array} } \right]&lt;/math&gt;<br /> <br /> &lt;math&gt;\pi=\pi~P&lt;/math&gt;&lt;br&gt;<br /> <br /> &lt;math&gt;\pi=&lt;/math&gt; [&lt;math&gt;\pi&lt;/math&gt;&lt;sub&gt;0&lt;/sub&gt;, &lt;math&gt;\pi&lt;/math&gt;&lt;sub&gt;1&lt;/sub&gt;, &lt;math&gt;\pi&lt;/math&gt;&lt;sub&gt;2&lt;/sub&gt;]&lt;br&gt;<br /> <br /> The system of equations is:<br /> <br /> &lt;math&gt;0.5\pi_0+1\pi_1+0\pi_2= \pi_0=&gt; 2\pi_1 = \pi_0&lt;/math&gt;&lt;br&gt; <br /> &lt;math&gt;0\pi_0+0\pi_1+1\pi_2= \pi_1 =&gt; \pi_1=\pi_2&lt;/math&gt;&lt;br&gt; <br /> &lt;math&gt;0\pi_0+0.5\pi_1+0.5\pi_2 = \pi_2 =&gt; \pi_1 = \pi_2&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\pi_0+\pi_1+\pi_2 = 1&lt;/math&gt;&lt;br&gt;<br /> <br /> &lt;math&gt;2\pi_1+\pi_1+\pi_1 = 4\pi_1 = 1&lt;/math&gt;, which gives &lt;math&gt;\pi_1=\frac {1}{4}&lt;/math&gt; &lt;br&gt;<br /> Also, &lt;math&gt;\pi_1 = \pi_2 = \frac {1}{4}&lt;/math&gt; &lt;br&gt;<br /> So, &lt;math&gt;\pi = [\frac{1}{2}, \frac{1}{4}, \frac{1}{4}]&lt;/math&gt; &lt;br&gt;<br /> <br /> === Ergodic Chain ===<br /> <br /> A Markov chain is called an ergodic chain if it is possible to go from every state to every state (not necessarily in one move). For instance, note that we can claim a Markov chain is ergodic if it is possible to somehow start at any state i and end at any state j in the matrix. We could have a chain with states 0, 1, 2, 3, 4 where it is not possible to go from state 0 to state 4 in just one step. However, it may be possible to go from 0 to 1, then from 1 to 2, then from 2 to 3, and finally 3 to 4 so we can claim that it is possible to go from 0 to 4 and this would satisfy a requirement of an ergodic chain. The example below will further explain this concept.<br /> <br /> '''Note:'''if there's a finite number N then every other state can be reached in N steps.<br /> '''Note:'''Also note that a Ergodic chain is irreducible (all states communicate) and aperiodic (d = 1). An Ergodic chain is promised to have a stationary and limiting distribution.&lt;br/&gt;<br /> '''Ergodicity:''' A state i is said to be ergodic if it is aperiodic and positive recurrent. In other words, a state i is ergodic if it is recurrent, has a period of 1 and it has finite mean recurrence time. If all states in an irreducible Markov chain are ergodic, then the chain is said to be ergodic.&lt;br/&gt; <br /> '''Some more:'''It can be shown that a finite state irreducible Markov chain is ergodic if it has an aperiodic state. A model has the ergodic property if there's a finite number N such that any state can be reached from any other state in exactly N steps. In case of a fully connected transition matrix where all transitions have a non-zero probability, this condition is fulfilled with N=1.&lt;br/&gt;<br /> <br /> <br /> ====Example====<br /> &lt;math&gt; P= \left[ \begin{matrix}<br /> \frac{1}{3} \; &amp; \frac{1}{3} \; &amp; \frac{1}{3} \\ \\<br /> \frac{1}{4} \; &amp; \frac{3}{4} \; &amp; 0 \\ \\<br /> \frac{1}{2} \; &amp; 0 \; &amp; \frac{1}{2}<br /> \end{matrix} \right] &lt;/math&gt;&lt;br /&gt;<br /> <br /> <br /> &lt;math&gt; \pi=\left[ \begin{matrix}<br /> \frac{1}{3} &amp; \frac{4}{9} &amp; \frac{2}{9}<br /> \end{matrix} \right] &lt;/math&gt;&lt;br /&gt;<br /> <br /> <br /> There are three states in this example.<br /> <br /> [[File:ab.png]]<br /> <br /> In this case, state a can go to state a, b, or c; state b can go to state a, b, or c; and state c can go to state a, b, or c so it is possible to go from every state to every state. (Although state b cannot directly go into c in one move, it must go to a, and then to c.).<br /> <br /> A k-by-k matrix indicates that the chain has k states.<br /> <br /> - Ergodic Markov chains are irreducible.<br /> <br /> - A Markov chain is called a '''regular''' chain if some power of the transition matrix has only positive elements.&lt;br /&gt;<br /> *Any transition matrix that has no zeros determines a regular Markov chain<br /> *However, it is possible for a regular Markov chain to have a transition matrix that has zeros.<br /> &lt;br /&gt;<br /> For example, recall the matrix of the Land of Oz<br /> <br /> &lt;math&gt;P = \left[ \begin{matrix}<br /> &amp; R &amp; N &amp; S \\<br /> R &amp; 1/2 &amp; 1/4 &amp; 1/4 \\<br /> N &amp; 1/2 &amp; 0 &amp; 1/2 \\<br /> S &amp; 1/4 &amp; 1/4 &amp; 1/2 \\<br /> \end{matrix} \right]&lt;/math&gt;&lt;br /&gt;<br /> <br /> === Theorem ===<br /> An ergodic Markov chain has a unique stationary distribution &lt;math&gt;\pi&lt;/math&gt;. The limiting distribution exists and is equal to &lt;math&gt;\pi&lt;/math&gt;&lt;br/&gt;<br /> Note: Ergodic Markov Chain is irreducible, aperiodic and positive recurrent.<br /> <br /> Example: Consider the markov chain of &lt;math&gt;\left[\begin{matrix}0 &amp; 1 \\ 1 &amp; 0\end{matrix}\right]&lt;/math&gt;, the stationary distribution is obtained by solving &lt;math&gt;\pi P = \pi&lt;/math&gt;, getting &lt;math&gt;\pi=[0.5, 0.5]&lt;/math&gt;, but from the assignment we know that it does not converge, ie. there is no limiting distribution, because the Markov chain is not aperiodic and cycle repeats &lt;math&gt;P^2=\left[\begin{matrix}1 &amp; 0 \\ 0 &amp; 1\end{matrix}\right]&lt;/math&gt; and &lt;math&gt;P^3=\left[\begin{matrix}0 &amp; 1 \\ 1 &amp; 0\end{matrix}\right]&lt;/math&gt;<br /> <br /> '''Another Example'''<br /> <br /> &lt;math&gt;P=\left[ {\begin{array}{ccc}<br /> \frac{1}{4} &amp; \frac{3}{4} \\[6pt]<br /> \frac{1}{5} &amp; \frac{4}{5} \end{array} } \right]&lt;/math&gt; &lt;br&gt;<br /> <br /> <br /> <br /> [[File:Untitled*.jpg]]<br /> <br /> This matrix means that there are two points in the space, let's call them a and b&lt;br/&gt;<br /> Starting from a, the probability of staying in a is 1/4 &lt;br/&gt;<br /> Starting from a, the probability of going from a to b is 3/4 &lt;br/&gt;<br /> Starting from b, the probability of going from b to a is 1/5 &lt;br/&gt;<br /> Starting from b, the probability of staying in b is 4/5 &lt;br/&gt;<br /> <br /> Solve the equation &lt;math&gt; \pi = \pi P &lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt; \pi_0 = .25 \pi_0 + .2 \pi_1 &lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt; \pi_1 = .75 \pi_0 + .8 \pi_1 &lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt; \pi_0 + \pi_1 = 1 &lt;/math&gt; &lt;br&gt;<br /> Solving this system of equations we get: &lt;br&gt;<br /> &lt;math&gt; \pi_0 = \frac{4}{15} \pi_1 &lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt; \pi_1 = \frac{15}{19} &lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt; \pi_0 = \frac{4}{19} &lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt; \pi = [\frac{4}{19}, \frac{15}{19}] &lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt; \pi &lt;/math&gt; is the long run distribution<br /> <br /> We can use the stationary distribution to compute the expected waiting time to return to state 'a' &lt;br/&gt;<br /> given that we start at state 'a' and so on.. Formula for this will be : &lt;math&gt; E[T_{i,i}]=\frac{1}{\pi_i}&lt;/math&gt;&lt;br/&gt;<br /> In the example above this will mean that that expected waiting time for the markov process to return to&lt;br/&gt;<br /> state 'a' given that we start at state 'a' is 19/4.&lt;br/&gt;<br /> <br /> definition of limiting distribution.<br /> <br /> remark：satisfied balance of &lt;math&gt;\pi_i P_{ij} = P_{ji} \pi_j&lt;/math&gt;, so there is other way to calculate the step probability.<br /> <br /> === MatLab Code ===<br /> In the following, P is the transition matrix. eye(n) refers to the n by n Identity matrix. L is the Laplacian matrix, L = (I - P). The Laplacian matrix will have at least 1 zero Eigenvalue. For every 0 in the diagonal, there is a component. If there is exactly 1 zero Eigenvalue, then the matrix is connected and has only 1 component. The number of zeros in the Laplacian matrix is the number of parts in your graph/process. If there is more than one zero on the diagonal of this matrix, means there is a disconnect in the graph.<br /> <br /> <br /> &lt;pre style='font-size:14px'&gt;<br /> &gt;&gt; P=[1/3, 1/3, 1/3; 1/4, 3/4, 0; 1/2, 0, 1/2]<br /> <br /> P =<br /> <br /> 0.3333 0.3333 0.3333<br /> 0.2500 0.7500 0<br /> 0.5000 0 0.5000<br /> <br /> &gt;&gt; eye(3) %%returns 3x3 identity matrix<br /> <br /> ans =<br /> <br /> 1 0 0<br /> 0 1 0<br /> 0 0 1<br /> <br /> &gt;&gt; L=(eye(3)-P) <br /> <br /> L =<br /> <br /> 0.6667 -0.3333 -0.3333<br /> -0.2500 0.2500 0<br /> -0.5000 0 0.5000<br /> <br /> &gt;&gt; [vec val]=eigs(L)<br /> <br /> vec =<br /> <br /> -0.7295 0.2329 0.5774<br /> 0.2239 -0.5690 0.5774<br /> 0.6463 0.7887 0.5774<br /> <br /> <br /> val =<br /> <br /> 1.0643 0 0<br /> 0 0.3523 0<br /> 0 0 -0.0000<br /> <br /> %% Only one value of zero on the diagonal means the chain is connected<br /> <br /> &gt;&gt; P=[0.8, 0.2, 0, 0;0.2, 0.8, 0, 0; 0, 0, 0.8, 0.2; 0, 0, 0.1, 0.9]<br /> <br /> P =<br /> <br /> 0.8000 0.2000 0 0<br /> 0.2000 0.8000 0 0<br /> 0 0 0.8000 0.2000<br /> 0 0 0.1000 0.9000<br /> <br /> &gt;&gt; eye(4)<br /> <br /> ans =<br /> <br /> 1 0 0 0<br /> 0 1 0 0<br /> 0 0 1 0<br /> 0 0 0 1<br /> <br /> &gt;&gt; L=(eye(4)-P)<br /> <br /> L =<br /> <br /> 0.2000 -0.2000 0 0<br /> -0.2000 0.2000 0 0<br /> 0 0 0.2000 -0.2000<br /> 0 0 -0.1000 0.1000<br /> <br /> &gt;&gt; [vec val]=eigs(L)<br /> <br /> vec =<br /> <br /> 0.7071 0 0.7071 0<br /> -0.7071 0 0.7071 0<br /> 0 0.8944 0 0.7071<br /> 0 -0.4472 0 0.7071<br /> <br /> <br /> val =<br /> <br /> 0.4000 0 0 0<br /> 0 0.3000 0 0<br /> 0 0 -0.0000 0<br /> 0 0 0 -0.0000<br /> <br /> %% Two values of zero on the diagonal means there are two 'islands' of chains<br /> <br /> &lt;/pre&gt;<br /> <br /> &lt;math&gt;\Pi&lt;/math&gt; satisfies detailed balance if &lt;math&gt;\Pi_i P_{ij}=P_{ji} \Pi_j&lt;/math&gt;. Detailed balance guarantees that &lt;math&gt;\Pi&lt;/math&gt; is stationary distribution.&lt;br /&gt;<br /> <br /> '''Adjacency matrix''' - a matrix &lt;math&gt;A&lt;/math&gt; that dictates which states are connected and way of portraying which vertices in the matrix are adjacent. Two vertices are adjacent if there exists a path between them of length 1.If we compute &lt;math&gt;A^2&lt;/math&gt;, we can know which states are connected with paths of length 2.&lt;br /&gt;<br /> <br /> A '''Markov chain''' is called an irreducible chain if it is possible to go from every state to every state (not necessary in one more).&lt;br /&gt;<br /> Theorem: An '''ergodic''' Markov chain has a unique stationary distribution &lt;math&gt;\pi&lt;/math&gt;. The limiting distribution exists and is equal to &lt;math&gt;\pi&lt;/math&gt;. &lt;br /&gt;<br /> <br /> <br /> Markov process satisfies detailed balance if and only if it is a '''reversible''' Markov process<br /> where P is the matrix of Markov transition.&lt;br /&gt;<br /> <br /> Satisfying the detailed balance condition guarantees that &lt;math&gt;\pi&lt;/math&gt; is stationary distributed.<br /> <br /> &lt;math&gt; \pi &lt;/math&gt; satisfies detailed balance if &lt;math&gt; \pi_i P_{ij} = P_{ji} \pi_j &lt;/math&gt; &lt;br&gt;<br /> which is the same as the Markov process equation.<br /> <br /> Example in the class: <br /> &lt;math&gt;P= \left[ {\begin{array}{ccc}<br /> \frac{1}{3} &amp; \frac{1}{3} &amp; \frac{1}{3} \\[6pt]<br /> \frac{1}{4} &amp; \frac{3}{4} &amp; 0 \\[6pt]<br /> \frac{1}{2} &amp; 0 &amp; \frac{1}{2} \end{array} } \right]&lt;/math&gt;<br /> <br /> and &lt;math&gt;\pi=(\frac{1}{3},\frac{4}{9}, \frac{2}{9})&lt;/math&gt;<br /> <br /> &lt;math&gt;\pi_1 P_{1,2} = 1/3 \times 1/3 = 1/9,\, P_{2,1} \pi_2 = 1/4 \times 4/9 = 1/9 \Rightarrow \pi_1 P_{1,2} = P_{2,1} \pi_2 &lt;/math&gt;&lt;br&gt;<br /> <br /> &lt;math&gt;\pi_2 P_{2,3} = 4/9 \times 0 = 0,\, P_{3,2} \pi_3 = 0 \times 2/9 = 0 \Rightarrow \pi_2 P_{2,3} = P_{3,2} \pi_3&lt;/math&gt;&lt;br&gt;<br /> Remark：Detailed balance of &lt;math&gt; \pi_i \times Pij = Pji \times \pi_j&lt;/math&gt; ， so there is other way to calculate the step probability&lt;br /&gt;<br /> &lt;math&gt;\pi&lt;/math&gt; is stationary but is not limiting.<br /> Detailed balance implies that &lt;math&gt;\pi&lt;/math&gt; = &lt;math&gt;\pi&lt;/math&gt; * P as shown in the proof and guarantees that &lt;math&gt;\pi&lt;/math&gt; is stationary distribution.<br /> <br /> == Class 15 - Tuesday June 25th 2013 ==<br /> === Announcement ===<br /> Note to all students, the first half of today's lecture will cover the midterm's solution; however please do not post the solution on the Wikicoursenote.&lt;br /&gt;<br /> <br /> ====Detailed balance====<br /> &lt;div style=&quot;border:2px solid black&quot;&gt;<br /> &lt;b&gt;Definition (from wikipedia)&lt;/b&gt;<br /> The principle of detailed balance is formulated for kinetic systems which are decomposed into elementary processes (collisions, or steps, or elementary reactions): At equilibrium, each elementary process should be equilibrated by its reverse process.<br /> &lt;/div&gt;<br /> Let &lt;math&gt;P&lt;/math&gt; be the transition probability matrix of a Markov chain. If there exists a distribution vector &lt;math&gt;\pi&lt;/math&gt; such that &lt;math&gt;\pi_i \cdot P_{ij}=P_{ji} \cdot \pi_j, \; \forall i,j&lt;/math&gt;, then the Markov chain is said to have '''detailed balance'''. A detailed balanced Markov chain must have &lt;math&gt;\pi&lt;/math&gt; given above as a stationary distribution, that is &lt;math&gt;\pi=\pi P&lt;/math&gt;, where &lt;math&gt;\pi&lt;/math&gt; is a 1 by n matrix and P is a n by n matrix.&lt;br&gt;<br /> <br /> <br /> need to remember：<br /> '''Proof:''' &lt;br&gt;<br /> &lt;math&gt;\; [\pi P]_j = \sum_i \pi_i P_{ij} =\sum_i P_{ji}\pi_j =\pi_j\sum_i P_{ji} =\pi_j ,\forall j&lt;/math&gt;<br /> <br /> :Note: Since &lt;math&gt;\pi_j&lt;/math&gt; is a sum of column j and we can do this proof for every element in matrix P; in general, we can prove &lt;math&gt;\pi=\pi P&lt;/math&gt; &lt;br&gt;<br /> <br /> Hence &lt;math&gt;\pi&lt;/math&gt; is always a stationary distribution of &lt;math&gt;P(X_{n+1}=j|X_n=i)&lt;/math&gt;, for every n. <br /> <br /> In other terms, &lt;math&gt; P_{ij} = P(X_n = j| X_{n-1} = i) &lt;/math&gt;, where &lt;math&gt;\pi_j&lt;/math&gt; is the equilibrium probability of being in state j and &lt;math&gt;\pi_i&lt;/math&gt; is the equilibrium probability of being in state i. &lt;math&gt;P(X_{n-1} = i) = \pi_i&lt;/math&gt; is equivalent to &lt;math&gt;P(X_{n-1} = i, Xn = j)&lt;/math&gt; being symmetric in i and j.<br /> <br /> Keep in mind that the detailed balance is a sufficient but not required condition for a distribution to be stationary. <br /> i.e. A distribution satisfying the detailed balance is stationary, but a stationary distribution does not necessarily satisfy the detailed balance.<br /> <br /> In the stationary distribution &lt;math&gt;\pi=\pi P&lt;/math&gt;, in the proof the sum of the p is equal 1 so the &lt;math&gt;\pi P=\pi&lt;/math&gt;.<br /> <br /> === PageRank (http://en.wikipedia.org/wiki/PageRank) ===<br /> <br /> *PageRank is a probability distribution used to represent the likelihood that a person randomly clicking on links will arrive at any particular page. PageRank can be calculated for collections of documents of any size.<br /> *PageRank is a link-analysis algorithm developed by and named after Larry Page from Google; used for measuring a website's importance, relevance and popularity.<br /> *PageRank is a graph containing web pages and their links to each other.<br /> *Many social media sites use this (such as Facebook and Twitter)<br /> *It can also be used to find criminals (ie. theives, hackers, terrorists, etc.) by finding out the links. <br /> This is what made Google the search engine of choice over Yahoo, Bing, etc.- What made Google's search engine a huge success is not its search function, but rather the algorithm it used to rank the pages. (Ex. If we come up with 100 million search results, how do you list them by relevance and importance so the users can easily find what they are looking for. Most users will not go past the first 3 or so search pages to find what they are looking for. It is this ability to rank pages that allow Google to remain more popular than Yahoo, Bink, AskJeeves, etc.). It should be noted that after using the PageRank algorithm, Google uses other processes to filter results.&lt;br/&gt;<br /> <br /> &lt;br /&gt;'''The order of importance'''&lt;br /&gt;<br /> 1. A web page is more important if many other pages point to it&lt;br /&gt;<br /> 2. The more important a web page is, the more weight should be assigned to its outgoing links&lt;br/ &gt;<br /> 3. If a webpage has many outgoing links, then its links have less value (ex: if a page links to everyone, like 411, it is not as important as pages that have incoming links)&lt;br /&gt;<br /> <br /> &lt;br /&gt;<br /> [[File:diagram.jpg]]<br /> &lt;math&gt;L= <br /> \left[ {\begin{matrix}<br /> 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\<br /> 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\<br /> 1 &amp; 1 &amp; 0 &amp; 1 &amp; 0 \\<br /> 0 &amp; 1 &amp; 0 &amp; 0 &amp; 1 \\<br /> 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \end{matrix} } \right]&lt;/math&gt;<br /> <br /> The first row indicates who gives a link to 1. As shown in the diagram, nothing gives a link to 1, and thus it is all zero.<br /> The second row indicates who gives a link to 2. As shown in the diagram, only 1 gives a link to 2, and thus column 1 is a 1 for row 2, and the rest are all zero.<br /> <br /> ie: According to the above example &lt;br/ &gt;<br /> Page 3 is the most important since it has the most links pointing to it, therefore more weigh should be placed on its outgoing links.&lt;br/ &gt;<br /> Page 4 comes after page 3 since it has the second most links pointing to it&lt;br/ &gt;<br /> Page 2 comes after page 4 since it has the third most links pointing to it&lt;br/&gt;<br /> Page 1 and page 5 are the least important since no links point to them&lt;br/ &gt;<br /> As page 1 and page 2 have the most outgoing links, then their links have less value compared to the other pages. &lt;br/ &gt;<br /> <br /> :&lt;math&gt;<br /> Lij = \begin{cases}<br /> 1, &amp; \text{if j has a link to i} \\<br /> 0, &amp; \text{otherwise}<br /> \end{cases}&lt;/math&gt;<br /> <br /> &lt;br /&gt;<br /> &lt;math&gt;C_j=&lt;/math&gt; The number of outgoing links of page &lt;math&gt;j&lt;/math&gt;:<br /> &lt;math&gt;C_j=\sum_i L_{ij}&lt;/math&gt;<br /> (i.e. sum of entries in column j)&lt;br /&gt;<br /> &lt;br /&gt;<br /> &lt;math&gt;P_j&lt;/math&gt; is the rank of page &lt;math&gt;j&lt;/math&gt;.&lt;br /&gt;<br /> Suppose we have &lt;math&gt;N&lt;/math&gt; pages, &lt;math&gt;P&lt;/math&gt; is a vector containing ranks of all pages.&lt;br /&gt;<br /> - &lt;math&gt;P&lt;/math&gt; is a &lt;math&gt;N \times 1&lt;/math&gt; vector.<br /> <br /> - &lt;math&gt;P_i&lt;/math&gt; counts the number of incoming links of page &lt;math&gt;i&lt;/math&gt;<br /> &lt;math&gt;P_i=\sum_j L_{ij}&lt;/math&gt; &lt;br /&gt;(i.e. sum of entries in row i)<br /> <br /> For each row of &lt;math&gt;L&lt;/math&gt;, if there is a 1 in the third column, it means page three point to that page.<br /> <br /> However, we should not define the rank of the page this way because links shouldn't be treated the same. The weight of the link is based on different factors. One of the factors is the importance of the page that link is coming from. For example, in this case, there are two links going to Page 4: one from Page 2 and one from Page 5. So far, both links have been treated equally with the same weight 1. But we must rerate the two links based on the importance of the pages they are coming from.<br /> <br /> A PageRank results from a mathematical algorithm based on the webgraph, created by all World Wide Web pages as nodes and hyperlinks as edges, taking into consideration authority hubs such as cnn.com or usa.gov. The rank value indicates an importance of a particular page. A hyperlink to a page counts as a vote of support. (This would be represented in our diagram as an arrow pointing towards the page. Hence in our example, Page 3 is the most important, since it has the most 'votes of support). The PageRank of a page is defined recursively and depends on the number and PageRank metric of all pages that link to it (&quot;incoming links&quot;). A page that is linked to by many pages with high PageRank receives a high rank itself. If there are no links to a web page, then there is no support for that page (In our example, this would be Page 1 and Page 5).<br /> (source:http://en.wikipedia.org/wiki/PageRank#Description)<br /> <br /> For those interested in PageRank, here is the original paper by Google co-founders Brin and Page: http://infolab.stanford.edu/pub/papers/google.pdf<br /> <br /> === Example of Page Rank Application in Real Life ===<br /> <br /> '''Page Rank checker'''<br /> - This is a free service to check Google™ page rank instantly via online PR checker or by adding a PageRank checking button to the web pages.<br /> &lt;font size=&quot;3&quot;&gt;(http://www.prchecker.info/check_page_rank.php)&lt;/font&gt;<br /> <br /> <br /> GoogleMatrix G = d * [ (Hyperlink Matrix H) + (Dangling Nodes Matrix A) ] + ((1-d)/N) * (NxN Matrix U of all 1's)<br /> <br /> <br /> [[File:Google matrix.png]]<br /> <br /> <br /> (source: https://googledrive.com/host/0B2GQktu-wcTiaWw5OFVqT1k3bDA/)<br /> <br /> == Class 16 - Thursday June 27th 2013 ==<br /> <br /> === Page Rank ===<br /> *&lt;math&gt;<br /> L_{ij} = \begin{cases}<br /> 1, &amp; \text{if j has a link to i } \\<br /> 0, &amp; \text{otherwise} \end{cases} &lt;/math&gt; &lt;br/&gt;<br /> <br /> *&lt;math&gt;C_j&lt;/math&gt;: number of outgoing links for page j, where &lt;math&gt;c_j=\sum_i L_{ij}&lt;/math&gt; <br /> <br /> P is N by 1 vector contains rank of all N pages; for page i, the rank is &lt;math&gt;P_i&lt;/math&gt;<br /> <br /> &lt;math&gt;P_i= (1-d) + d\cdot \sum_j \frac {L_{ji}P_j}{c_j}&lt;/math&gt;<br /> pi is the rank of a new created page(that no one knows about) is 0 since &lt;math&gt;L_ij&lt;/math&gt; is 0 &lt;br/&gt;<br /> where 0 &lt; d &lt; 1 is constant (in original page rank algorithm d = 0.8), and &lt;math&gt;L_{ij}&lt;/math&gt; is 1 if j has link to i, 0 otherwise.<br /> <br /> Note that the rank of a page is proportional to the number of its incoming links and inversely proportional to the number of its outgoing links.<br /> <br /> Interpretation of the formula:&lt;br/&gt;<br /> 1) sum of L&lt;sub&gt;ij&lt;/sub&gt; is the total number of incoming links&lt;br/&gt;<br /> 2) the above sum is weighted by page rank of the pages that contain the link to i (P&lt;sub&gt;j&lt;/sub&gt;) i.e. if a high-rank page points to page i, then this link carries more weight than links from lower-rank pages.&lt;br/&gt;<br /> 3) the sum is then weighted by the inverse of the number of outgoing links from the pages that contain links to i (c&lt;sub&gt;j&lt;/sub&gt;). i.e. if a page has more outgoing links than other pages then its links carry less weight.&lt;br/&gt;<br /> 4) finally, we take a linear combination of the page rank obtained from above and a constant 1. This ensures that every page has a rank greater than zero.&lt;br/&gt;<br /> 5) d is the damping factor. It represents the probability a user, at any page, will continue clicking to another page.&lt;br/&gt;<br /> If there is no damping (i.e. d=1), then there are no assumed outgoing links for nodes with no links. However, if there is damping (e.g. d=0.8), then these nodes are assumed to have links to all pages in the web.<br /> <br /> Note that this is a system of N equations with N unknowns.&lt;br/&gt;<br /> <br /> &lt;math&gt;c_j&lt;/math&gt; is the number of outgoing links, less outgoing links means more important.&lt;br/&gt;<br /> <br /> <br /> Let D be a diagonal N by N matrix such that &lt;math&gt; D_{ii}&lt;/math&gt; = &lt;math&gt;c_i&lt;/math&gt;<br /> <br /> Note: Ranks are arbitrary, all we want to know is the order. That is, we want to know how important the page rank relative to the other pages and are not interested in the value of the page rank. <br /> <br /> &lt;math&gt;D= <br /> \left[ {\begin{matrix}<br /> c_1 &amp; 0 &amp; ... &amp; 0 \\<br /> 0 &amp; c_2 &amp; ... &amp; 0 \\<br /> 0 &amp; 0 &amp; ... &amp; 0 \\<br /> 0 &amp; 0 &amp; ... &amp; c_N \end{matrix} } \right]&lt;/math&gt;<br /> <br /> Then &lt;math&gt;P=~(1-d)e+dLD^{-1}P&lt;/math&gt;, P is an iegenvector of matrix A corresponding to an eigenvalue equal to 1.&lt;br/&gt; where e =[1 1 ....]&lt;sup&gt;T&lt;/sup&gt; , i.e. a N by 1 vector.&lt;br/&gt;<br /> We assume that rank of all N pages sums to N. The sum of rank of all N pages can be any number, as long as the ranks have certain propotion. &lt;br/&gt;<br /> i.e. e&lt;sup&gt;T&lt;/sup&gt; P = N, then &lt;math&gt;~\frac{e^{T}P}{N} = 1&lt;/math&gt;<br /> <br /> <br /> D&lt;sup&gt;-1&lt;/sup&gt; will be: <br /> <br /> D&lt;sup&gt;-1&lt;/sup&gt;&lt;math&gt;= <br /> \left[ {\begin{matrix}<br /> \frac {1}{c_1} &amp; 0 &amp; ... &amp; 0 \\<br /> 0 &amp; \frac {1}{c_2} &amp; ... &amp; 0 \\<br /> 0 &amp; 0 &amp; ... &amp; 0 \\<br /> 0 &amp; 0 &amp; ... &amp; \frac {1}{c_N} \end{matrix} } \right]&lt;/math&gt;<br /> <br /> &lt;math&gt;P=~(1-d)e+dLD^{-1}P&lt;/math&gt; where &lt;math&gt;e=\begin{bmatrix}<br /> 1\\ <br /> 1\\ <br /> ...\\ <br /> 1<br /> \end{bmatrix}&lt;/math&gt;<br /> <br /> &lt;math&gt;P=(1-d)~\frac{ee^{T}P}{N}+dLD^{-1}P&lt;/math&gt;<br /> <br /> &lt;math&gt;P=[(1-d)~\frac{ee^T}{N}+dLD^{-1}]P&lt;/math&gt;<br /> <br /> &lt;math&gt;=&gt; P=A*P&lt;/math&gt;<br /> <br /> '''Explanation of an eigenvector'''<br /> <br /> An eigenvector is a non-zero vector '''v''' such that when multiplied by a square matrix, A, the result is a scalar times the vector '''v''' itself. &lt;br&gt;<br /> That is, A*v = c*v. Where c is the eigenvalue of A corresponding to the eigenvector v. In our case of Page Rank, the eigenvalue c=1. &lt;br&gt;<br /> <br /> We obtain that &lt;math&gt;P=AP&lt;/math&gt; where &lt;math&gt;A=(1-d)~\frac{ee^T}{N}+dLD^{-1}&lt;/math&gt;&lt;br/&gt;<br /> Thus, &lt;math&gt;P&lt;/math&gt; is an eigenvector of &lt;math&gt;P&lt;/math&gt; correspond to an eigen value equals 1.&lt;br/&gt;<br /> <br /> <br /> Since,<br /> L is a N*N matrix,<br /> D&lt;sup&gt;-1&lt;/sup&gt; is a N*N matrix, <br /> P is a N*1 matrix &lt;br/&gt;<br /> Then as a result, &lt;math&gt;LD^{-1}P&lt;/math&gt; is a N*1 matrix. &lt;br/&gt;<br /> <br /> N is a N*N matrix, d is a constant between 0 and 1.<br /> <br /> '''P=AP'''&lt;br /&gt;<br /> P is an eigenvector of A with corresponding eigenvalue equal to 1.&lt;br&gt;<br /> '''P&lt;sup&gt;T&lt;/sup&gt;=P&lt;sup&gt;T&lt;/sup&gt;A&lt;sup&gt;T&lt;/sup&gt;&lt;br&gt;'''<br /> Notice that all entries in A are non-negative and each row sums to 1. Hence A satisfies the definition of a transition probability matrix.&lt;br&gt;<br /> P&lt;sup&gt;T&lt;/sup&gt; is the stationary distribution of a Markov Chain with transition probability matrix A&lt;sup&gt;T&lt;/sup&gt;.<br /> <br /> We can consider A to be the matrix describing all possible movements following links on the internet, and P&lt;sup&gt;t&lt;/sup&gt; as the probability of being on any given webpage if we were on the internet long enough.<br /> <br /> Definition of rank page and proof it steps by steps, it shows with 3 n*n matrix and and one n*1 matrix and a constant d between 0 to 1.<br /> p is the stationary distribution so p=Ap.<br /> <br /> === Damping Factor &quot;d&quot; ===<br /> <br /> The PageRank assumes that any imaginary user who is randomly clicking on links will eventually stop clicking. The probability, at any step, that the person will keep on clicking is a damping factor, &lt;math&gt;d&lt;/math&gt;. After many studies, the approximation of &lt;math&gt;d&lt;/math&gt; is 0.85. Other values for &lt;math&gt;d&lt;/math&gt; have been used in class and may appear on assignments and exams.&lt;br/&gt;<br /> <br /> In addition, &lt;math&gt;d&lt;/math&gt; is a vector of ranks that are arbitrary. For example the rank can be [1 3 2], or [10 30 20], or [0.1 0.3 0.2]. All three of these examples are relative/equivalent since they are ranks, we could even have [1 10 3]. Therefore, &lt;math&gt;d&lt;/math&gt; must have a relative rank.&lt;br/&gt;<br /> <br /> So &lt;math&gt;P_1 + P_2 + \cdots + P_n=N&lt;/math&gt; &lt;br/&gt;<br /> Which is equivalent to:<br /> &lt;math&gt;e^{T}P= [1 \cdots 1] [P_1 \cdots P_n]^T &lt;/math&gt; &lt;br/&gt;<br /> Where &lt;math&gt;[1 \cdots 1]&lt;/math&gt; is a 1 scalar vector and &lt;math&gt;[P_1 \cdots P_n]^T&lt;/math&gt; is a rank vector. &lt;br/&gt;<br /> So &lt;math&gt;e^{T}P=N -&gt; (e^{T}P)/N = 1 &lt;/math&gt;<br /> <br /> ===Examples===<br /> &lt;span style=&quot;background:#F5F5DC&quot;&gt;<br /> <br /> ==== Example 1 ====<br /> <br /> <br /> [[File:eg1.jpg]]<br /> &lt;br /&gt;<br /> &lt;/span&gt;<br /> &lt;math&gt;L= <br /> \left[ {\begin{matrix}<br /> 0 &amp; 0 &amp; 1 \\<br /> 1 &amp; 0 &amp; 0 \\<br /> 0 &amp; 1 &amp; 0 \end{matrix} } \right]\;c= <br /> \left[ {\begin{matrix}<br /> 1 &amp; 1 &amp; 1 \end{matrix} } \right]\;D= <br /> \left[ {\begin{matrix}<br /> 1 &amp; 0 &amp; 0 \\<br /> 0 &amp; 1 &amp; 0 \\<br /> 0 &amp; 0 &amp; 1 \end{matrix} } \right]&lt;/math&gt;<br /> <br /> &lt;pre style='font-size:14px'&gt;<br /> <br /> MATLAB Code<br /> <br /> d=0.8<br /> N=3<br /> A=(1-d)*ones(N)/N+d*L*pinv(D) #pinv: Moore-Penrose inverse (pseudoinverse) of symbolic matrix<br /> We use the pinv(D) function [pseudo-inverse] instead of the inv(D) function because in <br /> the case of a non-invertible matrix, it would not crash the program. <br /> [vec val]=eigs(A) (eigen-decomposition)<br /> a=-vec(:,1) (find the eigenvector equals to 1)<br /> a=a/sum(a) (normalize a)<br /> or to show that A transpose is a stationary transition matrix<br /> (transpose(A))^200 will be the same as a=a/sum(a)<br /> &lt;/pre&gt;<br /> <br /> '''NOTE:''' Changing the value of d, does not change the ranking order of the pages. <br /> <br /> By looking at each entry after normalizing a, we can tell the ranking order of each page.&lt;br&gt;<br /> &lt;span style=&quot;background:#F5F5DC&quot;&gt;<br /> <br /> c = [1 1 1] since there are 3 pages, each page is one way recurrent to each other and there is only one outgoing for each page. Hence, D is a 3x3 standard diagonal matrix.<br /> <br /> ==== Example 2 ====<br /> <br /> [[File:Screen_shot_2013-07-02_at_3.43.04_AM.png]]<br /> <br /> <br /> &lt;math&gt;L= <br /> \left[ {\begin{matrix}<br /> 0 &amp; 0 &amp; 1 \\<br /> 1 &amp; 0 &amp; 1 \\<br /> 0 &amp; 1 &amp; 0 \end{matrix} } \right]\;<br /> c= <br /> \left[ {\begin{matrix}<br /> 1 &amp; 1 &amp; 2 \end{matrix} } \right]\;<br /> D= <br /> \left[ {\begin{matrix}<br /> 1 &amp; 0 &amp; 0 \\<br /> 0 &amp; 1 &amp; 0 \\<br /> 0 &amp; 0 &amp; 2 \end{matrix} } \right]&lt;/math&gt;<br /> <br /> &lt;pre style='font-size:14px'&gt;<br /> <br /> Matlab code<br /> <br /> &gt;&gt; L=[0 0 1;1 0 1;0 1 0];<br /> &gt;&gt; C=sum(L);<br /> &gt;&gt; D=diag(C);<br /> &gt;&gt; d=0.8;<br /> &gt;&gt; N=3;<br /> &gt;&gt; A=(1-d)*ones(N)/N+d*L*pinv(D);<br /> &gt;&gt; [vec val]=eigs(A)<br /> <br /> vec =<br /> <br /> -0.3707 -0.3536 + 0.3536i -0.3536 - 0.3536i<br /> -0.6672 -0.3536 - 0.3536i -0.3536 + 0.3536i<br /> -0.6461 0.7071 0.7071 <br /> <br /> <br /> val =<br /> <br /> 1.0000 0 0 <br /> 0 -0.4000 - 0.4000i 0 <br /> 0 0 -0.4000 + 0.4000i<br /> <br /> &gt;&gt; a=-vec(:,1)<br /> <br /> a =<br /> <br /> 0.3707<br /> 0.6672<br /> 0.6461<br /> <br /> &gt;&gt; a=a/sum(a)<br /> <br /> a =<br /> <br /> 0.2201<br /> 0.3962<br /> 0.3836<br /> &lt;/pre&gt;<br /> '''NOTE:''' Page 2 is the most important page because it has 2 incomings. Similarly, page 3 is more important than page 1 because page 3 has the incoming result from page 2.<br /> <br /> This example is similar to the first example, but here, page 3 can go back to page 2, so the matrix of the outgoing matrix, the third column of the D matrix is 3 in the third row. And we use the code to calculate the p=Ap. Therefore 2, 3, 1 is the order of importance.<br /> <br /> ==== Example 3 ====<br /> <br /> [[File:eg 3.jpg]]&lt;br&gt;<br /> <br /> &lt;math&gt;L= <br /> \left[ {\begin{matrix}<br /> 0 &amp; 1 &amp; 0 \\<br /> 1 &amp; 0 &amp; 1 \\<br /> 0 &amp; 1 &amp; 0 \end{matrix} } \right]\;<br /> c= <br /> \left[ {\begin{matrix}<br /> 1 &amp; 2 &amp; 1 \end{matrix} } \right]\;<br /> D= <br /> \left[ {\begin{matrix}<br /> 1 &amp; 0 &amp; 0 \\<br /> 0 &amp; 2 &amp; 0 \\<br /> 0 &amp; 0 &amp; 1 \end{matrix} } \right]&lt;/math&gt;<br /> <br /> &lt;math&gt;d=0.8&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;N=3&lt;/math&gt;&lt;br&gt;<br /> <br /> <br /> &lt;/span&gt;<br /> this example is the second page have 2 outgoings.<br /> <br /> <br /> <br /> Another Example:<br /> <br /> Consider: 1 -&gt; ,&lt;-2 -&gt;3<br /> <br /> &lt;math&gt;L= <br /> \left[ {\begin{matrix}<br /> 0 &amp; 1 &amp; 0 \\<br /> 1 &amp; 0 &amp; 0 \\<br /> 0 &amp; 1 &amp; 0 \end{matrix} } \right]\;<br /> c= <br /> \left[ {\begin{matrix}<br /> 1 &amp; 1 &amp; 1 \end{matrix} } \right]\;<br /> D= <br /> \left[ {\begin{matrix}<br /> 1 &amp; 0 &amp; 0 \\<br /> 0 &amp; 1 &amp; 0 \\<br /> 0 &amp; 0 &amp; 1 \end{matrix} } \right]&lt;/math&gt;<br /> <br /> ==== Example 4 ====<br /> <br /> &lt;math&gt;1 \leftrightarrow 2 \rightarrow 3 \leftrightarrow 4 &lt;/math&gt;<br /> &lt;br /&gt;<br /> &lt;br /&gt;<br /> &lt;math&gt;L= <br /> \left[ {\begin{matrix}<br /> 0 &amp; 1 &amp; 0 &amp; 0 \\<br /> 1 &amp; 0 &amp; 0 &amp; 0 \\<br /> 0 &amp; 1 &amp; 0 &amp; 1 \\<br /> 0 &amp; 0 &amp; 1 &amp; 0 \end{matrix} } \right]\;&lt;/math&gt;&lt;br /&gt;<br /> <br /> '''Matlab Code:'''&lt;br&gt;<br /> &lt;pre style='font-size:16px'&gt;<br /> &gt;&gt; L=L= [0 1 0 0;1 0 0 0;0 1 0 1;0 0 1 0];<br /> &gt;&gt; C=sum(L);<br /> &gt;&gt; D=diag(C);<br /> &gt;&gt; d=0.8;<br /> &gt;&gt; N=4;<br /> &gt;&gt; A=(1-d)*ones(N)/N+d*L*pinv(D);<br /> &gt;&gt; [vec val]=eigs(A);<br /> &gt;&gt; a=vec(:,1);<br /> &gt;&gt; a=a/sum(a)<br /> a =<br /> 0.1029 &lt;- Page 1<br /> 0.1324 &lt;- Page 2<br /> 0.3971 &lt;- Page 3<br /> 0.3676 &lt;- Page 4<br /> <br /> % Therefore the PageRank for this matrix is: 3,4,2,1<br /> &lt;/pre&gt;<br /> &lt;br&gt;<br /> <br /> ==== Example 5 ====<br /> <br /> &lt;math&gt;L= <br /> \left[ {\begin{matrix}<br /> 0 &amp; 1 &amp; 0 &amp; 1 \\<br /> 1 &amp; 0 &amp; 1 &amp; 1 \\<br /> 1 &amp; 0 &amp; 0 &amp; 1 \\<br /> 1 &amp; 0 &amp; 0 &amp; 0 \end{matrix} } \right]&lt;/math&gt;<br /> <br /> &lt;math&gt;c= <br /> \left[ {\begin{matrix}<br /> 3 &amp; 1 &amp; 1 &amp; 3 \end{matrix} } \right]&lt;/math&gt;<br /> <br /> &lt;math&gt;D= <br /> \left[ {\begin{matrix}<br /> 3 &amp; 0 &amp; 0 &amp; 0 \\<br /> 0 &amp; 1 &amp; 0 &amp; 0 \\<br /> 0 &amp; 0 &amp; 1 &amp; 0 \\<br /> 0 &amp; 0 &amp; 0 &amp; 3 \end{matrix} } \right]&lt;/math&gt;<br /> <br /> &lt;pre style='font-size:14px'&gt;<br /> <br /> Matlab code<br /> <br /> &gt;&gt; L= [0 1 0 1; 1 0 1 1; 1 0 0 1;1 0 0 0];<br /> &gt;&gt; d = 0.8;<br /> &gt;&gt; N = 4;<br /> &gt;&gt; C = sum(L);<br /> &gt;&gt; D = diag(C);<br /> &gt;&gt; A=(1-d)*ones(N)/N+d*L*pinv(D);<br /> &gt;&gt; [vec val]=eigs(A);<br /> &gt;&gt; a=vec(:,1);<br /> &gt;&gt; a=a/sum(a)<br /> <br /> a =<br /> <br /> 0.3492<br /> 0.3263<br /> 0.1813<br /> 0.1431<br /> &lt;/pre&gt;<br /> <br /> ==== Example 6 ====<br /> &lt;math&gt;L= <br /> \left[ {\begin{matrix}<br /> 0 &amp; 1 &amp; 0 &amp; 0 &amp; 1\\<br /> 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0\\<br /> 0 &amp; 1 &amp; 0 &amp; 0 &amp; 0\\<br /> 0 &amp; 1 &amp; 1 &amp; 0 &amp; 1\\<br /> 0 &amp; 0 &amp; 0 &amp; 1 &amp; 0 \end{matrix} } \right]&lt;/math&gt;<br /> &lt;br /&gt;<br /> <br /> '''Matlab Code:'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt; d=0.8;<br /> &gt;&gt; L=[0 1 0 0 1;1 0 0 0 0;0 1 0 0 0;0 1 1 0 1;0 0 0 1 0];<br /> &gt;&gt; c=sum(L);<br /> &gt;&gt; D=diag(c);<br /> &gt;&gt; N=5;<br /> &gt;&gt; A=(1-d)*ones(N)/N+d*L*pinv(D);<br /> &gt;&gt; [vec val]=eigs(A);<br /> &gt;&gt; a=-vec(:,1);<br /> &gt;&gt; a=a/sum(a) <br /> a =<br /> 0.1933 &lt;- Page 1<br /> 0.1946 &lt;- Page 2<br /> 0.0919 &lt;- Page 3<br /> 0.2668 &lt;- Page 4<br /> 0.2534 &lt;- Page 5<br /> <br /> % Therefore the PageRank for this matrix is: 4,5,2,1,3<br /> &lt;/pre&gt;<br /> &lt;br&gt;<br /> <br /> == Class 17 - Tuesday July 2nd 2013 ==<br /> === Markov Chain Monte Carlo (MCMC) ===<br /> <br /> ===Introduction===<br /> It is, in general, very difficult to simulate the value of a random vector X whose component random variables are dependent. We will present a powerful approach for generating a vector whose distribution is approximately that of X. This approach, called the Markov Chain Monte Carlo Methods, has the added significance of only requiring that the mass(or density) function of X be specified up to a multiplicative constant, and this, we will see, is of great importance in applications.<br /> (referenced by Sheldon M.Ross,Simulation)<br /> The basic idea used here is to generate a Markov Chain whose stationary dsitribution is the same as the target distribution.<br /> <br /> ====Definition:====<br /> Markov Chain<br /> A Markov Chain is a special form of stochastic process in which &lt;math&gt;\displaystyle X_t&lt;/math&gt; depends only on &lt;math&gt; \displaystyle X_{t-1}&lt;/math&gt;.<br /> <br /> For example,<br /> :&lt;math&gt;\displaystyle f(X_1,...X_n)= f(X_1)f(X_2|X_1)f(X_3|X_2)...f(X_n|X_{n-1})&lt;/math&gt;<br /> A random Walk is the best example of a Markov process<br /> <br /> &lt;br&gt;'''Transition Probability:'''&lt;br&gt;<br /> The probability of going from one state to another state.<br /> :&lt;math&gt;p_{ij} = \Pr(X_{n}=j\mid X_{n-1}= i). \,&lt;/math&gt;<br /> <br /> &lt;br&gt;'''Transition Matrix:'''&lt;br&gt;<br /> For n states, transition matrix P is an &lt;math&gt;N \times N&lt;/math&gt; matrix with entries &lt;math&gt;\displaystyle P_{ij}&lt;/math&gt; as below:<br /> Markov Chain Monte Carlo (MCMC) methods are a class of algorithms for sampling from probability distributions based on constructing a Markov chain that has the desired distribution as its equilibrium distribution. The state of the chain after a large number of steps is then used as a sample of the desired distribution. The quality of the sample improves as a function of the number of steps. (http://en.wikipedia.org/wiki/Markov_chain_Monte_Carlo)&lt;/span&gt;<br /> <br /> &lt;a style=&quot;color:red&quot; href=&quot;http://www.eecs.berkeley.edu/Pubs/TechRpts/2010/EECS-2010-165.pdf&quot;&gt;some notes form UCb&lt;/a&gt;<br /> <br /> '''One of the main purposes of MCMC''' : to simulate samples from a joint distribution where the joint random variables are dependent. In general, this is not easily sampled from. Other methods learned in class allow us to simulate i.i.d random variables, but not dependent variables . In this case, we could sample non-independent random variables using a Markov Chain. Its Markov properties help to simplify the simulation process.<br /> <br /> <br /> &lt;b&gt;Basic idea:&lt;/b&gt; Given a probability distribution &lt;math&gt;\pi&lt;/math&gt; on a set &lt;math&gt;\Omega&lt;/math&gt;, we want to generate random elements of &lt;math&gt;\Omega&lt;/math&gt; with distribution &lt;math&gt;\pi&lt;/math&gt;. MCMC does that by constructing a Markov Chain with stationary distribution &lt;math&gt;\pi&lt;/math&gt; and simulating the chain. After a large number of iterations, the Markov Chain will reach its stationary distribution. By sampling from the Markov chain for large amount of iterations, we are effectively sampling from the desired distribution as the Markov Chain would converge to its stationary distribution &lt;br/&gt; <br /> <br /> Idea: generate a Markov chain whose stationary distribution is the same as target distribution. &lt;br/&gt;<br /> <br /> <br /> '''Notes'''<br /> <br /> # Regardless of the chosen starting point, the Markov Chain will converge to its stationary distribution (if it exists). However, the time taken for the chain to converge depends on its chosen starting point. Typically, the burn-in period is longer if the chain is initialized with a value of low probability density.<br /> # Markov Chain Monte Carlo can be used for sampling from a distribution, estimating the distribution, and computing the mean and optimization (e.g. simulated annealing, more on that later).<br /> # Markov Chain Monte Carlo is used to sample using “local” information. It is used as a generic “problem solving technique” to solve decision/optimization/value problems, but is not necessarily very efficient.<br /> # MCMC methods do not suffer as badly from the &quot;curse of dimensionality&quot; that badly affects efficiency in the acceptance-rejection method. This is because a point is always generated at each time-step according to the Markov Chain regardless of how many dimensions are introduced.<br /> # The goal when simulating with a Markov Chain is to create a chain with the same stationary distribution as the target distribution.<br /> # The MCMC method is usually used in continuous cases but a discrete example is given below.<br /> <br /> <br /> '''Some properties of the stationary distribution &lt;math&gt;\pi&lt;/math&gt;'''<br /> <br /> &lt;math&gt;\pi&lt;/math&gt; indicates the proportion of time the process spends in each of the states 1,2,...,n. Therefore &lt;math&gt;\pi&lt;/math&gt; satisfies the following two inequalities: &lt;br&gt;<br /> <br /> # &lt;math&gt;\pi_j = \sum_{i=1}^{n}\pi_i P_{ij}&lt;/math&gt; &lt;br /&gt; This is because &lt;math&gt;\pi_i&lt;/math&gt; is the proportion of time the process spends in state i, and &lt;math&gt;P_{ij}&lt;/math&gt; is the probability the process transition out of state i into state j. Therefore, &lt;math&gt;\pi_i p_{ij}&lt;/math&gt; is the proportion of time it takes for the process to enter state j. Therefore, &lt;math&gt;\pi_j&lt;/math&gt; is the sum of this probability over overall states i.<br /> #&lt;math&gt; \sum_{i=1}^{n}\pi_i= 1 &lt;/math&gt; as &lt;math&gt;\pi&lt;/math&gt; shows the proportion of time the chain is in each state. If we view it as the probability of the chain being in state i at time t for t sufficiently large, then it should sum to one as the chain must be in one of the states.<br /> <br /> ====Motivation example====<br /> - Suppose we want to generate a random variable X according to distribution &lt;math&gt;\pi=(\pi_1, \pi_2, ... , \pi_m)&lt;/math&gt; &lt;br/&gt;<br /> X can take m possible different values from &lt;math&gt;{1,2,3,\cdots, m}&lt;/math&gt;&lt;br /&gt;<br /> - We want to generate &lt;math&gt;\{X_t: t=0, 1, \cdots\}&lt;/math&gt; according to &lt;math&gt;\pi&lt;/math&gt;&lt;br /&gt;<br /> <br /> Suppose our example is of a bias die. &lt;br/&gt;<br /> Now we have m=6, &lt;math&gt;\pi=[0.1,0.1,0.1,0.2,0.3,0.2]&lt;/math&gt;, &lt;math&gt;X \in [1,2,3,4,5,6]&lt;/math&gt;&lt;br/&gt;<br /> <br /> Suppose &lt;math&gt;X_t=i&lt;/math&gt;. Consider an arbitrary probability transition matrix Q with entry &lt;math&gt;q_{ij}&lt;/math&gt; being the probability of moving to state j from state i. (&lt;math&gt;q_{ij}&lt;/math&gt; can not be zero.) &lt;br/&gt; <br /> <br /> &lt;math&gt; \mathbf{Q} = <br /> \begin{bmatrix}<br /> q_{11} &amp; q_{12} &amp; \cdots &amp; q_{1m} \\<br /> q_{21} &amp; q_{22} &amp; \cdots &amp; q_{2m} \\<br /> \vdots &amp; \vdots &amp; \ddots &amp; \vdots \\<br /> q_{m1} &amp; q_{m2} &amp; \cdots &amp; q_{mm}<br /> \end{bmatrix}<br /> &lt;/math&gt; &lt;br/&gt;<br /> <br /> <br /> We generate Y = j according to the i-th row of Q. Note that the i-th row of Q is a probability vector that shows the probability of moving to any state j from the current state i, i.e.&lt;math&gt;P(Y=j)=q_{ij}&lt;/math&gt;&lt;br /&gt;<br /> <br /> In the following algorithm: &lt;br&gt;<br /> &lt;math&gt;q_{ij}&lt;/math&gt; is the &lt;math&gt;ij^{th}&lt;/math&gt; entry of matrix Q. It is the probability of Y=j given that &lt;math&gt;x_t = i&lt;/math&gt;. &lt;br/&gt;<br /> &lt;math&gt;r_{ij}&lt;/math&gt; is the probability of accepting Y as &lt;math&gt;x_{t+1}&lt;/math&gt;. &lt;br/&gt;<br /> <br /> <br /> '''How to get the acceptance probability?'''<br /> <br /> If &lt;math&gt;\pi &lt;/math&gt; is the stationary distribution, then it must satisfy the detailed balance condition:&lt;br/&gt;<br /> If &lt;math&gt;\pi_i P_{ij}&lt;/math&gt; = &lt;math&gt;\pi_j P_{ji}&lt;/math&gt;&lt;br/&gt;then &lt;math&gt;\pi &lt;/math&gt; is the stationary distribution of the chain<br /> <br /> Since &lt;math&gt;P_{ij}&lt;/math&gt; = &lt;math&gt;q_{ij} r_{ij}&lt;/math&gt;, we have &lt;math&gt;\pi_i q_{ij} r_{ij}&lt;/math&gt; = &lt;math&gt;\pi_j q_{ji} r_{ji}&lt;/math&gt;.&lt;br/&gt;<br /> We want to find a general solution: &lt;math&gt;r_{ij} = a(i,j) \pi_j q_{ji}&lt;/math&gt;, where a(i,j) = a(j,i).&lt;br/&gt; <br /> <br /> '''Recall'''<br /> &lt;math&gt;r_{ij}&lt;/math&gt; is the probability of acceptance, thus it must be that &lt;br/&gt;<br /> <br /> 1.&lt;math&gt;r_{ij} = a(i,j)&lt;/math&gt; &lt;math&gt;\pi_j q_{ji} &lt;/math&gt;≤1, then we get: &lt;math&gt;a(i,j) &lt;/math&gt;≤ &lt;math&gt;1/(\pi_j q_{ji})&lt;/math&gt;<br /> <br /> 2. &lt;math&gt;r_{ji} = a(j,i) &lt;/math&gt; &lt;math&gt;\pi_i q_{ij} &lt;/math&gt; ≤ 1, then we get: &lt;math&gt;a(j,i)&lt;/math&gt; ≤ &lt;math&gt;1/(\pi_i q_{ij})&lt;/math&gt;<br /> <br /> So we choose a(i,j) as large as possible, but it needs to satisfy the two conditions above.&lt;br/&gt;<br /> <br /> &lt;math&gt;a(i,j) = \min \{\frac{1}{\pi_j q_{ji}},\frac{1}{\pi_i q_{ij}}\} &lt;/math&gt;&lt;br/&gt;<br /> <br /> Thus, &lt;math&gt; r_{ij} = \min \{\frac{\pi_j q_{ji}}{\pi_i q_{ij}}, 1\} &lt;/math&gt;&lt;br/&gt;<br /> <br /> '''Note''': <br /> 1 is the upper bound to make r&lt;sub&gt;ij&lt;/sub&gt; a probability<br /> <br /> <br /> '''Algorithm:''' &lt;br/&gt;<br /> *&lt;math&gt; (*) P(Y=j) = q_{ij} &lt;/math&gt;. &lt;math&gt;\frac{\pi_j q_{ji}}{\pi_i q_{ij}}&lt;/math&gt; is a positive ratio.<br /> <br /> *&lt;math&gt; r_{ij} = \min \{\frac{\pi_j q_{ji}}{\pi_i q_{ij}}, 1\} &lt;/math&gt; &lt;br/&gt;<br /> *&lt;math&gt;<br /> x_{t+1} = \begin{cases}<br /> Y, &amp; \text{with probability } r_{ij} \\<br /> x_t, &amp; \text{otherwise} \end{cases} &lt;/math&gt; &lt;br/&gt;<br /> * go back to the first step (*) &lt;br/&gt;<br /> <br /> We can compare this with the Acceptance-Rejection model we learned before. &lt;br/&gt;<br /> * &lt;math&gt;U&lt;/math&gt; ~ &lt;math&gt;Uniform(0,1)&lt;/math&gt; &lt;br/&gt;<br /> * If &lt;math&gt;U &lt; r_{ij}&lt;/math&gt;, then accept. &lt;br/&gt;<br /> EXCEPT that a point is always generated at each time-step. &lt;br&gt;<br /> <br /> The algorithm generates a stochastic sequence that only depends on the last state, which is a Markov Chain.&lt;br&gt;<br /> <br /> ====Metropolis Algorithm====<br /> <br /> '''Proposition: ''' Metropolis works:<br /> <br /> The &lt;math&gt;P_{ij}&lt;/math&gt;'s from Metropolis Algorithm satisfy detailed balance property w.r.t &lt;math&gt;\pi&lt;/math&gt; . i.e. &lt;math&gt;\pi_i P_{ij} = \pi_j P_{ji}&lt;/math&gt;. The new Markov Chain has a stationary distribution &lt;math&gt;\pi&lt;/math&gt;. &lt;br/&gt;<br /> '''Remarks:''' &lt;br/&gt;<br /> 1) We only need to know ratios of values of &lt;math&gt;\pi_i&lt;/math&gt;'s.&lt;br/&gt;<br /> 2) The MC might converge to &lt;math&gt;\pi&lt;/math&gt; at varying speeds depending on the proposal distribution and the value the chain is initialized with&lt;br/&gt;<br /> <br /> <br /> This algorithm generates &lt;math&gt;\{x_t: t=0,...,m\}&lt;/math&gt;. &lt;br/&gt;<br /> In the long run, the marginal distribution of &lt;math&gt; x_t &lt;/math&gt; is the stationary distribution &lt;math&gt;\underline{\Pi} &lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\{x_t: t = 0, 1,...,m\}&lt;/math&gt; is a Markov chain with probability transition matrix (PTM), P.&lt;br&gt;<br /> <br /> This is a Markov Chain since &lt;math&gt; x_{t+1} &lt;/math&gt; only depends on &lt;math&gt; x_t &lt;/math&gt;, where &lt;br&gt;<br /> &lt;math&gt; P_{ij}= \begin{cases}<br /> q_{ij} r_{ij}, &amp; \text{if }i \neq j (q_{ij} \text{is the probability of generating j from i and } r_{ij} \text{ is the probiliity of accepting)}\\[6pt]<br /> 1 - \displaystyle\sum_{k \neq i} q_{ik} r_{ik}, &amp; \text{if }i = j \end{cases} &lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;q_{ij}&lt;/math&gt; is the probability of generating state j; &lt;br/&gt;<br /> &lt;math&gt; r_{ij}&lt;/math&gt; is the probability of accepting state j as the next state. &lt;br/&gt;<br /> <br /> Therefore, the final probability of moving from state i to j when i does not equal to j is &lt;math&gt;q_{ij}*r_{ij}&lt;/math&gt;. &lt;br/&gt;<br /> For the probability of moving from state i to state i, we deduct all the probabilities of moving from state i to any j that are not equal to i, therefore, we get the second probability.<br /> <br /> ===Proof of the proposition:===<br /> <br /> A good way to think of the detailed balance equation is that they balance the probability from state i to state j with that from state j to state i. <br /> We need to show that the stationary distribition of the Markov Chain is &lt;math&gt;\underline{\Pi}&lt;/math&gt;, i.e. &lt;math&gt;\displaystyle \underline{\Pi} = \underline{\Pi}P&lt;/math&gt;&lt;br /&gt;<br /> &lt;div style=&quot;text-size:20px&quot;&gt;<br /> Recall&lt;br/&gt;<br /> If a Markov chain satisfies the detailed balance property, i.e. &lt;math&gt;\displaystyle \pi_i P_{ij} = \pi_j P_{ji} \, \forall i,j&lt;/math&gt;, then &lt;math&gt;\underline{\Pi}&lt;/math&gt; is the stationary distribution of the chain.&lt;br /&gt;&lt;br /&gt;<br /> &lt;/div&gt;<br /> <br /> '''Proof:'''<br /> <br /> WLOG, we can assume that &lt;math&gt;\frac{\pi_j q_{ji}}{\pi_i q_{ij}}&lt;1&lt;/math&gt;&lt;br/&gt;<br /> <br /> LHS:&lt;br /&gt;<br /> &lt;math&gt;\pi_i P_{ij} = \pi_i q_{ij} r_{ij} = \pi_i q_{ij} \cdot \min(\frac{\pi_j q_{ji}}{\pi_i q_{ij}},1) = \cancel{\pi_i q_{ij}} \cdot \frac{\pi_j q_{ji}}{\cancel{\pi_i q_{ij}}} = \pi_j q_{ji}&lt;/math&gt;&lt;br /&gt;<br /> <br /> RHS:&lt;br /&gt;<br /> Note that by our assumption, since &lt;math&gt;\frac{\pi_j q_{ji}}{\pi_i q_{ij}}&lt;1&lt;/math&gt;, its reciprocal &lt;math&gt;\frac{\pi_i q_{ij}}{\pi_j q_{ji}} \geq 1&lt;/math&gt;&lt;br /&gt;<br /> So &lt;math&gt;\displaystyle \pi_j P_{ji} = \pi_ j q_{ji} r_{ji} = \pi_ j q_{ji} \cdot \min(\frac{\pi_i q_{ij}}{\pi_j q_{ji}},1) = \pi_j q_{ji} \cdot 1 = \pi_ j q_{ji}&lt;/math&gt;&lt;br /&gt;<br /> <br /> Hence LHS=RHS <br /> <br /> If we assume that &lt;math&gt;\frac{\pi_j q_{ji}}{\pi_i q_{ij}}=1&lt;/math&gt;&lt;br/&gt; (essentially &lt;math&gt;\frac{\pi_j q_{ji}}{\pi_i q_{ij}}&gt;=1&lt;/math&gt;)&lt;br/&gt;<br /> <br /> LHS:&lt;br /&gt;<br /> &lt;math&gt;\pi_i P_{ij} = \pi_i q_{ij} r_{ij} = \pi_i q_{ij} \cdot \min(\frac{\pi_j q_{ji}}{\pi_i q_{ij}},1) =\pi_i q_{ij} \cdot 1 = \pi_i q_{ij}&lt;/math&gt;&lt;br /&gt;<br /> <br /> RHS:&lt;br /&gt;<br /> '''Note''' &lt;br/&gt;<br /> by our assumption, since &lt;math&gt;\frac{\pi_j q_{ji}}{\pi_i q_{ij}}\geq 1&lt;/math&gt;, its reciprocal &lt;math&gt;\frac{\pi_i q_{ij}}{\pi_j q_{ji}} \leq 1 &lt;/math&gt; &lt;br /&gt;<br /> <br /> So &lt;math&gt;\displaystyle \pi_j P_{ji} = \pi_ j q_{ji} r_{ji} = \pi_ j q_{ji} \cdot \min(\frac{\pi_i q_{ij}}{\pi_j q_{ji}},1) = \cancel{\pi_j q_{ji}} \cdot \frac{\pi_i q_{ij}}{\cancel{\pi_j q_{ji}}} = \pi_i q_{ij}&lt;/math&gt;&lt;br /&gt;<br /> <br /> Hence LHS=RHS which indicates &lt;math&gt;pi_i*P_{ij} = pi_j*P_{ji}&lt;/math&gt;&lt;math&gt;\square&lt;/math&gt;&lt;br /&gt;&lt;br /&gt;<br /> <br /> '''Note'''&lt;br /&gt; <br /> 1) If we instead assume &lt;math&gt;\displaystyle \frac{\pi_i q_{ij}}{\pi_j q_{ji}} \geq 1&lt;/math&gt;, the proof is similar with LHS= RHS = &lt;math&gt; \pi_i q_{ij} &lt;/math&gt; &lt;br /&gt;<br /> <br /> 2) If &lt;math&gt;\displaystyle i = j&lt;/math&gt;, then detailed balance is satisfied trivially.&lt;br /&gt;<br /> <br /> since &lt;math&gt;{\pi_i q_{ij}}&lt;/math&gt;, and &lt;math&gt;{\pi_j q_{ji}}&lt;/math&gt; are smaller than one. so the above steps show the proof of &lt;math&gt;\frac{\pi_i q_{ij}}{\pi_j q_{ji}}&lt;1&lt;/math&gt;.<br /> <br /> == Class 18 - Thursday July 4th 2013 ==<br /> === Last class ===<br /> <br /> Recall: The Acceptance Probability,<br /> &lt;math&gt;r_{ij}=min(\frac {{\pi_j}q_{ji}}{{\pi_i}q_{ij}},1)&lt;/math&gt; &lt;br /&gt;<br /> <br /> 1) &lt;math&gt;r_{ij}=\frac {{\pi_j}q_{ji}}{{\pi_i}q_{ij}}&lt;/math&gt;, and &lt;math&gt;r_{ji}=1 &lt;/math&gt;, (&lt;math&gt;\frac {{\pi_j}q_{ji}}{{\pi_i}q_{ij}} &lt; 1&lt;/math&gt;) &lt;br /&gt;<br /> <br /> <br /> 2) &lt;math&gt;r_{ji}=\frac {{\pi_i}q_{ij}}{{\pi_j}q_{ji}}&lt;/math&gt;, and &lt;math&gt; r{ij}=1 &lt;/math&gt;, (&lt;math&gt;\frac {{\pi_j}q_{ji}}{{\pi_i}q_{ij}} \geq 1&lt;/math&gt; ) &lt;br /&gt;<br /> <br /> ===Example: Discrete Case===<br /> <br /> <br /> Consider a biased die,<br /> &lt;math&gt;\pi&lt;/math&gt;= [0.1, 0.1, 0.2, 0.4, 0.1, 0.1]<br /> <br /> We could use any &lt;math&gt;6 x 6 &lt;/math&gt; matrix &lt;math&gt; \mathbf{Q} &lt;/math&gt; as the proposal distribution &lt;br&gt;<br /> For the sake of simplicity ,using a discrete uniform distribution is the simplest. This is because all probabilities are equivalent, hence during the calculation of r, qxy and qyx will cancel each other out.<br /> <br /> &lt;math&gt; \mathbf{Q} = <br /> \begin{bmatrix}<br /> 1/6 &amp; 1/6 &amp; \cdots &amp; 1/6 \\<br /> 1/6 &amp; 1/6 &amp; \cdots &amp; 1/6 \\<br /> \vdots &amp; \vdots &amp; \ddots &amp; \vdots \\<br /> 1/6 &amp; 1/6 &amp; \cdots &amp; 1/6<br /> \end{bmatrix}<br /> &lt;/math&gt; &lt;br/&gt;<br /> <br /> <br /> <br /> '''Algorithm''' &lt;br&gt;<br /> 1. &lt;math&gt;x_t=5&lt;/math&gt; (sample from the 5th row, although we can initialize the chain from anywhere within the support)&lt;br /&gt;<br /> 2. Y~Unif[1,2,...,6]&lt;br /&gt;<br /> 3. &lt;math&gt; r_{ij} = \min \{\frac{\pi_j q_{ji}}{\pi_i q_{ij}}, 1\} = \min \{\frac{\pi_j 1/6}{\pi_i 1/6}, 1\} = \min \{\frac{\pi_j}{\pi_i}, 1\}&lt;/math&gt;&lt;br&gt;<br /> Note: current state &lt;math&gt;i&lt;/math&gt; is &lt;math&gt;X_t&lt;/math&gt;, the candidate state &lt;math&gt;j&lt;/math&gt; is &lt;math&gt;Y&lt;/math&gt;. &lt;br&gt;<br /> Note: since &lt;math&gt;q_{ij}= q_{ji}&lt;/math&gt; for all i and j, that is, the proposal distribution is symmetric, we have &lt;math&gt; r_{ij} = \min \{\frac{\pi_j}{\pi_i }, 1\} &lt;/math&gt;&lt;br/&gt;<br /> 4. U~Unif(0,1)&lt;br/&gt;<br /> if &lt;math&gt;u \leq r_{ij}&lt;/math&gt;, X&lt;sub&gt;t+1&lt;/sub&gt;=Y&lt;br /&gt;<br /> else X&lt;sub&gt;t+1&lt;/sub&gt;=X&lt;sub&gt;t&lt;/sub&gt;&lt;br /&gt;<br /> go back to 2&lt;br&gt;<br /> <br /> Notice how a point is always generated for X&lt;sub&gt;t+1&lt;/sub&gt;, regardless of whether the candidate state Y is accepted &lt;br&gt;<br /> <br /> '''Matlab'''<br /> &lt;pre style=&quot;font-size:14px&quot;&gt;<br /> pii=[.1,.1,.2,.4,.1,.1]; <br /> x(1)=5; <br /> for ii=2:1000 <br /> Y=unidrnd(6); %%% Unidrnd(x) is a built-in function which generates a number between (0) and (x) <br /> r = min (pii(Y)/pii(x(ii-1)), 1);<br /> u=rand; <br /> if u&lt;r <br /> x(ii)=Y;<br /> else<br /> x(ii)=x(ii-1);<br /> end<br /> end<br /> hist(x,6) %generate histogram displaying all 1000 points<br /> xx = x(501,end); %After 500, the chain will mix well and converge. <br /> hist(xx,6) % The result should be better.<br /> &lt;/pre&gt;<br /> [[File:MH_example1.jpg|300px]]<br /> <br /> <br /> '''NOTE:''' Generally, we generate a large number of points (say, 1500) and throw away some of the points that were first generated(say, 500). Those first points are called the [[burn-in period]]. A chain will converge to the limiting distribution eventually, but not immediately. The burn-in period is that beginning period before the chain has converged to the desired distribution. By discarding those 500 points, our data set will be more representative of the desired limiting distribution; once the burn-in period is over, we say that the chain &quot;mixes well&quot;.<br /> <br /> ===Alternate Example: Discrete Case===<br /> <br /> <br /> Consider the weather. If it is sunny one day, there is a 5/7 chance it will be sunny the next. If it is rainy, there is a 5/8 chance it will be rainy the next.<br /> &lt;math&gt;\pi= [\pi_1 \ \pi_2] &lt;/math&gt;<br /> <br /> Use a discrete uniform distribution as the proposal distribution, because it is the simplest.<br /> <br /> &lt;math&gt; \mathbf{Q} = <br /> \begin{bmatrix}<br /> 5/7 &amp; 2/7 \\<br /> 3/8 &amp; 5/8\\<br /> <br /> \end{bmatrix}<br /> &lt;/math&gt; &lt;br/&gt;<br /> <br /> <br /> <br /> '''Algorithm''' &lt;br&gt;<br /> 1. Set initial chain state: &lt;math&gt;X_t=1&lt;/math&gt; (i.e. sample from the 1st row, although we could also choose the 2nd row)&lt;br /&gt;<br /> 2. Sample from proposal distribution: Y~q(y|x) = Unif[1,2]&lt;br /&gt;<br /> 3. &lt;math&gt; r_{ij} = \min \{\frac{\pi_j q_{ji}}{\pi_i q_{ij}}, 1\} = \min \{\frac{\pi_j 1/6}{\pi_i 1/6}, 1\} = \min \{\frac{\pi_j}{\pi_i}, 1\}&lt;/math&gt;&lt;br&gt;<br /> '''Note:''' Current state &lt;math&gt;i&lt;/math&gt; is &lt;math&gt;X_t&lt;/math&gt;, the candidate state &lt;math&gt;j&lt;/math&gt; is &lt;math&gt;Y&lt;/math&gt;. Since &lt;math&gt;q_{ij}= q_{ji}&lt;/math&gt; for all i and j, that is, the proposal distribution is symmetric, we have &lt;math&gt; r_{ij} = \min \{\frac{\pi_j}{\pi_i }, 1\} &lt;/math&gt;<br /> <br /> 4. U~Unif(0,1)&lt;br&gt;<br /> If &lt;math&gt;U \leq r_{ij}&lt;/math&gt;, then&lt;br&gt;<br /> &lt;math&gt;X_t=Y&lt;/math&gt;&lt;br&gt;<br /> else&lt;br /&gt;<br /> &lt;math&gt;X_{t+1}=X_t&lt;/math&gt;&lt;br&gt;<br /> end if&lt;br /&gt;<br /> 5. Go back to step 2&lt;br&gt;<br /> <br /> <br /> '''Generalization of the above framework to the continuous case'''&lt;br&gt;<br /> <br /> In place of &lt;math&gt;\pi&lt;/math&gt; use &lt;math&gt;f(x)&lt;/math&gt;<br /> In place of r&lt;sub&gt;ij&lt;/sub&gt; use &lt;math&gt;q(y|x)&lt;/math&gt; &lt;br&gt;<br /> In place of r&lt;sub&gt;ij&lt;/sub&gt; use &lt;math&gt;r(x,y)&lt;/math&gt; &lt;br&gt;<br /> Here, q(y|x) is a friendly distribution that is easy to sample, usually a symmetric distribution will be preferable, such that &lt;math&gt;q(y|x) = q(x|y)&lt;/math&gt; to simplify the computation for &lt;math&gt;r(x,y)&lt;/math&gt;.<br /> <br /> <br /> '''Remarks'''&lt;br&gt;<br /> 1. The chain may not get to a stationary distribution if the # of steps generated are small. That is it will take a very large amount of steps to step through the whole support&lt;br&gt;<br /> 2. The algorithm can be performed with a &lt;math&gt;\pi&lt;/math&gt; that is not even a probability mass function, it merely needs to be proportional to the probability mass function we wish to sample from. This is useful as we do not need to calculate the normalization factor. &lt;br&gt;<br /> <br /> For example, if we are given &lt;math&gt;\pi^'=\pi\alpha=[5,10,11,2,100,1]&lt;/math&gt;, we can normalize this vector by dividing the sum of all entries &lt;math&gt;s&lt;/math&gt;.&lt;br&gt;<br /> However we notice that when calculating &lt;math&gt;r_{ij}&lt;/math&gt;, &lt;br&gt;<br /> &lt;math&gt;\frac{\pi^'_j/s}{\pi^'_i/s}\times\frac{q_{ji}}{q_{ij}}=\frac{\pi^'_j}{\pi^'_i}\times\frac{q_{ji}}{q_{ij}}&lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt;s&lt;/math&gt; cancels out in this case. Therefore it is not necessary to calculate the sum and normalize the vector.&lt;br&gt;<br /> <br /> This also applies to the continuous case,where we merely need &lt;math&gt; f(x) &lt;/math&gt; to be proportional to the pdf of the distribution we wish to sample from. &lt;br&gt;<br /> <br /> ===Metropolis–Hasting Algorithm===<br /> <br /> '''Definition''': &lt;br&gt;<br /> Metropolis–Hastings algorithm is a Markov chain Monte Carlo (MCMC) method for obtaining a sequence of random samples from a probability distribution for which direct sampling is difficult. The Metropolis–Hastings algorithm can draw samples from any probability distribution P(x), provided you can compute the value of a function f(x) which is proportional to the density of P. &lt;br&gt;<br /> <br /> <br /> <br /> '''Purpose''': &lt;br&gt;<br /> &quot;The purpose of the Metropolis-Hastings Algorithm is to &lt;b&gt;generate a collection of states according to a desired distribution&lt;/b&gt; &lt;math&gt;P(x)&lt;/math&gt;. &lt;math&gt;P(x)&lt;/math&gt; is chosen to be the stationary distribution of a Markov process, &lt;math&gt;\pi(x)&lt;/math&gt;.&quot; &lt;br&gt;<br /> Source:(http://en.wikipedia.org/wiki/Metropolis%E2%80%93Hastings_algorithm)&lt;br&gt;<br /> <br /> <br /> Metropolis-Hastings is an algorithm for constructing a Markov chain with a given limiting probability distribution. In particular, we consider what happens if we apply the Metropolis-Hastings algorithm repeatedly to a “proposal” distribution which has already been updated.&lt;br&gt;<br /> <br /> <br /> The algorithm was named after Nicholas Metropolis and W. K. Hastings who extended it to the more general case in 1970.&lt;br&gt;<br /> <br /> &lt;math&gt;q(y|x)&lt;/math&gt; is used instead of &lt;math&gt;qi,j&lt;/math&gt;. In continuous case, we use these notation which means given state x, what's the probability of y.&lt;br&gt; <br /> <br /> Note that the Metropolis-Hasting algorithm possess some advantageous properties. One of which is that this algorithm &quot;can be used when \pi(x) is known up to the constant of proportionality&quot;. The second is that in this algorithm, &quot;we do not require the conditional distribution, which, in contrast, is required for the Gibbs sampler. &quot;<br /> Source:https://www.msu.edu/~blackj/Scan_2003_02_12/Chapter_11_Markov_Chain_Monte_Carlo_Methods.pdf<br /> <br /> <br /> <br /> '''Differences between the discrete and continuous case of the Markov Chain''':&lt;br/&gt;<br /> <br /> 1. &lt;math&gt;q(y|x)&lt;/math&gt; is used in continuous, instead of &lt;math&gt;q_{ij}&lt;/math&gt; in discrete &lt;br/&gt;<br /> 2. &lt;math&gt;r(x,y)&lt;/math&gt; is used in continuous, instead of &lt;math&gt;r{ij}&lt;/math&gt; in discrete &lt;br/&gt;<br /> 3. &lt;math&gt;f&lt;/math&gt; is used instead of &lt;math&gt;\pi&lt;/math&gt; &lt;br/&gt;<br /> <br /> <br /> '''Build the Acceptance Ratio'''&lt;br/&gt;<br /> Before we consider the algorithm there are a couple general steps to follow to build the acceptance ratio:&lt;br/&gt;<br /> <br /> a) Find the distribution you wish to use to generate samples from&lt;br/&gt;<br /> b) Find a candidate distribution that fits the desired distribution, q(y|x). (the proposed moves are independent of the current state)&lt;br/&gt;<br /> c) Build the acceptance ratio &lt;math&gt;\displaystyle \frac{f(y)q(x|y)}{f(x)q(y|x)}&lt;/math&gt;<br /> <br /> <br /> <br /> Assume that f(y) is the target distribution; Choose q(y|x) such that it is a friendly distribution and easy to sample from.&lt;br /&gt;<br /> '''Algorithm:'''&lt;br /&gt;<br /> <br /> # Set &lt;math&gt;\displaystyle i = 0&lt;/math&gt; and initialize the chain, i.e. &lt;math&gt;\displaystyle x_0 = s&lt;/math&gt; where &lt;math&gt;\displaystyle s&lt;/math&gt; is some state of the Markov Chain. <br /> # Sample &lt;math&gt;\displaystyle Y \sim q(y|x)&lt;/math&gt;<br /> # Set &lt;math&gt;\displaystyle r(x,y) = min(\frac{f(y)q(x|y)}{f(x)q(y|x)},1)&lt;/math&gt;<br /> # Sample &lt;math&gt;\displaystyle u \sim \text{UNIF}(0,1)&lt;/math&gt;<br /> # If &lt;math&gt;\displaystyle u \leq r(x,y), x_{i+1} = Y&lt;/math&gt;&lt;br /&gt; Else &lt;math&gt;\displaystyle x_{i+1} = x_i&lt;/math&gt;<br /> # Increment i by 1 and go to Step 2, i.e. &lt;math&gt;\displaystyle i=i+1&lt;/math&gt;<br /> <br /> &lt;br&gt; '''Note''': q(x|y) is moving from y to x and q(y|x) is moving from x to y.<br /> &lt;br&gt;We choose q(y|x) so that it is simple to sample from.<br /> &lt;br&gt;Usually, we choose a normal distribution. <br /> <br /> NOTE2: The proposal q(y|x) y depends on x (is conditional on x)the current state, this makes sense ,because it's a necessary condition for MC. So the proposal should depend on x (also their supports should match) e.g q(y|x) ~ N( x, b&lt;sup&gt;2&lt;/sup&gt;) here the proposal depends on x. <br /> If the next state is INDEPENDENT of the current state, then our proposal will not depend on x e.g. (A4 Q2, sampling from Beta(2,2) where the proposal was UNIF(0,1)which is independent of the current state. )<br /> <br /> However, it is important to remember that even if generating the proposed/candidate state does not depend on the current state, the chain is still a markov chain.<br /> <br /> &lt;br /&gt;<br /> Comparing with previous sampling methods we have learned, samples generated from M-H algorithm are not independent of each other, since we accept future sample based on the current sample. Furthermore, unlike acceptance and rejection method, we are not going to reject any points in Metropolis-Hastings. In the equivalent of the &quot;reject&quot; case, we just leave the state unchanged. In other words, if we need a sample of 1000 points, we only need to generate the sample 1000 times.&lt;br/&gt;<br /> <br /> &lt;p style=&quot;font-size:20px;color:red;&quot;&gt;<br /> Remarks<br /> &lt;/p&gt;<br /> ===='''Remark 1'''====<br /> &lt;span style=&quot;text-shadow: 0px 2px 3px 3399CC;margin-right:1em;font-family: 'Nobile', Helvetica, Arial, sans-serif;font-size:16px;line-height:25px;color:3399CC&quot;&gt;<br /> A common choice for &lt;math&gt;q(y|x)&lt;/math&gt; is a normal distribution centered at x with standard deviation b. Y~&lt;math&gt;N(x,b^2)&lt;/math&gt;<br /> <br /> In this case, &lt;math&gt; q(y|x)&lt;/math&gt; is symmetric.<br /> <br /> i.e. <br /> &lt;math&gt;q(y|x)=q(x|y)&lt;/math&gt;&lt;br&gt;<br /> (we want to sample q centered at the current state.)&lt;br&gt;<br /> &lt;math&gt;q(y|x)=\frac{1}{\sqrt{2\pi}b}\,e^{- \frac{\scriptscriptstyle 1}{\scriptscriptstyle 2b^2} (y-x)^2}&lt;/math&gt;, (centered at x)&lt;br&gt;<br /> &lt;math&gt;q(x|y)=\frac{1}{\sqrt{2\pi}b}\,e^{- \frac{\scriptscriptstyle 1}{\scriptscriptstyle 2b^2} (x-y)^2}&lt;/math&gt;,(centered at y)&lt;br&gt;<br /> &lt;math&gt;\Rightarrow (y-x)^2=(x-y)^2&lt;/math&gt;&lt;br&gt;<br /> so &lt;math&gt;~q(y \mid x)=q(x \mid y)&lt;/math&gt; &lt;br&gt;<br /> In this case &lt;math&gt;\frac{q(x \mid y)}{q(y \mid x)}=1&lt;/math&gt; and therefore &lt;math&gt; r(x,y)=\min \{\frac{f(y)}{f(x)}, 1\} &lt;/math&gt; &lt;br/&gt;&lt;br /&gt;<br /> This is true for any symmetric q. In general if q(y|x) is symmetric, then this algorithm is called Metropolis.&lt;br/&gt;<br /> When choosing function q, it makes sense to choose a distribution with the same support as the distribution you want to simulate. eg. If target is Beta, then can choose q ~ Uniform(0,1)&lt;br&gt;<br /> The chosen q is not necessarily symmetric. Depending on different target distribution, q can be uniform.&lt;/span&gt;<br /> <br /> ===='''Remark 2'''====<br /> &lt;span style=&quot;text-shadow: 0px 2px 3px 3399CC;margin-right:1em;font-family: 'Nobile', Helvetica, Arial, sans-serif;font-size:16px;line-height:25px;color:3399CC&quot;&gt;<br /> The value y is accepted if u&lt;=&lt;math&gt;min\{\frac{f(y)}{f(x)},1\}&lt;/math&gt;, so it is accepted with the probability &lt;math&gt;min\{\frac{f(y)}{f(x)},1\}&lt;/math&gt;.&lt;br/&gt;<br /> Thus, if &lt;math&gt;f(y)&gt;=f(x)&lt;/math&gt;, then y is always accepted.&lt;br/&gt;<br /> The higher that value of the pdf is in the vicinity of a point &lt;math&gt;y_1&lt;/math&gt; , the more likely it is that a random variable will take on values around &lt;math&gt;y_1&lt;/math&gt;.&lt;br/&gt;<br /> Therefore,we would want a high probability of acceptance for points generated near &lt;math&gt;y_1&lt;/math&gt;.&lt;br&gt;<br /> [[File:Diag1.png‎]]&lt;br&gt;<br /> <br /> '''Note''':&lt;br/&gt; <br /> If the proposal comes from a region with low density, we may or may not accept; however, we accept for sure if the proposal comes from a region with high density.&lt;br&gt;<br /> <br /> ===='''Remark 3'''==== <br /> <br /> One strength of the Metropolis-Hastings algorithm is that normalizing constants, which are often quite difficult to determine, can be cancelled out in the ratio &lt;math&gt; r &lt;/math&gt;. For example, consider the case where we want to sample from the beta distribution, which has the pdf:&lt;br&gt;<br /> （also notice that Metropolis Hastings is just a special case of Metropolis algorithm)<br /> <br /> &lt;math&gt;<br /> \begin{align}<br /> f(x;\alpha,\beta)&amp; = \frac{1}{\mathrm{B}(\alpha,\beta)}\, x^{\alpha-1}(1-x)^{\beta-1}\end{align}<br /> &lt;/math&gt;<br /> <br /> The beta function, ''B'', appears as a normalizing constant but it can be simplified by construction of the method.<br /> <br /> ====='''Example'''=====<br /> <br /> &lt;math&gt;\,f(x)=\frac{1}{\pi^{2}}\frac{1}{1+x^{2}}&lt;/math&gt;, where &lt;math&gt;\frac{1}{\pi^{2}} &lt;/math&gt; is normalization factor and &lt;math&gt;\frac{1}{1+x^{2}} &lt;/math&gt; is target distribution. &lt;br&gt;<br /> Then, we have &lt;math&gt;\,f(x)\propto\frac{1}{1+x^{2}}&lt;/math&gt;.&lt;br&gt;<br /> And let us take &lt;math&gt;\,q(x|y)=\frac{1}{\sqrt{2\pi}b}e^{-\frac{1}{2b^{2}}(y-x)^{2}}&lt;/math&gt;.&lt;br&gt;<br /> Then &lt;math&gt;\,q(x|y)&lt;/math&gt; is symmetric since &lt;math&gt;\,(y-x)^{2} = (x-y)^{2}&lt;/math&gt;.&lt;br&gt;<br /> Therefore Y can be simplified.<br /> <br /> <br /> We get :<br /> <br /> &lt;math&gt;\,\begin{align}<br /> \displaystyle r(x,y) <br /> &amp; =min\left\{\frac{f(y)}{f(x)}\frac{q(x|y)}{q(y|x)},1\right\} \\<br /> &amp; =min\left\{\frac{f(y)}{f(x)},1\right\} \\<br /> &amp; =min\left\{ \frac{ \frac{1}{1+y^{2}} }{ \frac{1}{1+x^{2}} },1\right\}\\<br /> &amp; =min\left\{ \frac{1+x^{2}}{1+y^{2}},1\right\}\\<br /> \end{align}<br /> &lt;/math&gt;.<br /> <br /> &lt;br/&gt;<br /> &lt;math&gt;\pi=[0.1\,0.1\,...] &lt;/math&gt; stands for probility;&lt;br/&gt;<br /> &lt;math&gt;\pi \propto [3\,2\, 10\, 100\, 1.5] &lt;/math&gt; is not brobility, so we take:&lt;br/&gt;<br /> &lt;math&gt;\Rightarrow \pi=1/c \times [3\, 2\, 10\, 100\, 1.5]&lt;/math&gt; is probility where&lt;br/&gt;<br /> &lt;math&gt;\Rightarrow c=3+2+10+100+1.5 &lt;/math&gt;&lt;br/&gt; <br /> &lt;br/&gt;<br /> &lt;br/&gt;<br /> <br /> In practice, if elements of &lt;math&gt;\pi&lt;/math&gt; are functions or random variables, we need c to be the normalization factor, the summation/integration over all members of &lt;math&gt;\pi&lt;/math&gt;. This is usually very difficult. Since we are taking ratios, with the Metropolis-Hasting algorithm, it is not necessary to do this. <br /> <br /> &lt;br&gt;<br /> For example, to find the relationship between weather temperature and humidity, we only have a proportional function instead of a probability function. To make it into a probability function, we need to compute c, which is really difficult. However, we don't need to compute c as it will be cancelled out during calculation of r.&lt;br&gt;<br /> <br /> ======'''MATLAB'''======<br /> The Matlab code of the algorithm is the following :<br /> &lt;pre style=&quot;font-size:12px&quot;&gt;<br /> clear all<br /> close all<br /> clc<br /> b=2;<br /> x(1)=0;<br /> for i=2:10000<br /> y=b*randn+x(i-1);<br /> r=min((1+x(i-1)^2)/(1+y^2),1);<br /> u=rand;<br /> if u&lt;r<br /> x(i)=y;<br /> else<br /> x(i)=x(i-1);<br /> end<br /> <br /> end<br /> hist(x,100);<br /> %The Markov Chain usually takes some time to converge and this is known as the &quot;burning time&quot;.<br /> &lt;/pre&gt;<br /> [[File:MH_example2.jpg|300px]]<br /> <br /> However, while the data does approximately fit the desired distribution, it takes some time until the chain gets to the stationary distribution. To generate a more accurate graph, we modify the code to ignore the initial points.&lt;br&gt;<br /> <br /> '''MATLAB'''<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> b=2;<br /> x(1)=0;<br /> for ii=2:10500<br /> y=b*randn+x(ii-1);<br /> r=min((1+x(ii-1)^2)/(1+y^2),1);<br /> u=rand;<br /> if u&lt;=r<br /> x(ii)=y;<br /> else<br /> x(ii)=x(ii-1);<br /> end<br /> end<br /> xx=x(501:end) %we don't display the first 500 points because they don't show the limiting behaviour of the Markov Chain<br /> hist(xx,100)<br /> &lt;/pre&gt;<br /> [[File:MH_Ex.jpg|300px]]<br /> &lt;br&gt;<br /> '''If a function f(x) can only take values from &lt;math&gt;[0,\infty)&lt;/math&gt;, but we need to use normal distribution as the candidate distribution, then we can use &lt;math&gt;q=\frac{2}{\sqrt{2\pi}}*exp(\frac{-(y-x)^2}{2})&lt;/math&gt;, where y is from &lt;math&gt;[0,\infty)&lt;/math&gt;. &lt;br&gt;(This is essentially the pdf of the absolute value of a normal distribution centered around x)'''&lt;br&gt;&lt;br&gt;<br /> <br /> Example:&lt;br&gt;<br /> We want to sample from &lt;math&gt;exp(2), q(y|x)~\sim~N(x,b^2)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;r=\frac{f(y)}{f(x)}=\frac{2*exp^(-2y)}{2*exp^(-2x)}=exp(2*(x-y))&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;r=min(exp(2*(x-y)),1)&lt;/math&gt;&lt;br&gt;<br /> <br /> '''MATLAB'''<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> x(1)=0;<br /> for ii=2:100<br /> y=2*(randn*b+abs(x(ii-1)))<br /> r=min(exp(2*(x-y)),1);<br /> u=rand;<br /> if u&lt;=r<br /> x(ii)=y;<br /> else<br /> x(ii)=x(ii-1);<br /> end<br /> end<br /> &lt;/pre&gt;<br /> &lt;br&gt;<br /> <br /> '''Definition of Burn in:''' <br /> <br /> Typically in a MH Algorithm, a set of values generated at at the beginning of the sequence are &quot;burned&quot; (discarded) after which the chain is assumed to have converged to its target distribution. In the first example listed above, we &quot;burned&quot; the first 500 observations because we believe the chain has not quite reached our target distribution in the first 500 observations. 500 is not a set threshold, there is no right or wrong answer as to what is the exact number required for burn-in. Theoretical calculation of the burn-in is rather difficult, in the above mentioned example, we chose 500 based on experience and quite arbitrarily. <br /> <br /> Burn-in time can also be thought of as the time it takes for the chain to reach its stationary distribution. Therefore, in this case you will disregard everything uptil the burn-in period because the chain is not stabilized yet. <br /> <br /> The Metropolis–Hasting Algorithm is started from an arbitrary initial value &lt;math&gt;x_0&lt;/math&gt; and the algorithm is run for many iterations until this initial state is &quot;forgotten&quot;. These samples, which are discarded, are known as ''burn-in''. The remaining <br /> set of accepted values of &lt;math&gt;x&lt;/math&gt; represent a sample from the distribution f(x).(http://en.wikipedia.org/wiki/Metropolis%E2%80%93Hastings_algorithm)&lt;br/&gt;<br /> <br /> Burn-in time can also be thought of as the time it takes for the process to reach the stationary distribution pi. Suppose it takes 5 samples after which you reach the stationary distribution. You should disregard the first five samples and consider the remaining samples as representing your target distribution f(x). &lt;br&gt;<br /> <br /> Several extensions have been proposed in the literature to speed up the convergence and reduce the so called “burn-in” period.<br /> One common suggestion is to match the first few moments of q(y|x) to f(x).<br /> <br /> '''Aside''': The algorithm works best if the candidate density q(y|x) matches the shape of the target distribution f(x). If a normal distribution is used as a candidate distribution, the variance parameter b&lt;sup&gt;2&lt;/sup&gt; has to be tuned during the burn-in period. &lt;br/&gt;<br /> <br /> 1. If b is chosen to be too small, the chain will mix slowly (smaller proposed move, the acceptance rate will be high and the chain will converge only slowly the f(x)). <br /> <br /> 2. If b is chosen to be too large, the acceptance rate will be low (larger proposed move and the chain will converge only slowly the f(x)).<br /> <br /> <br /> <br /> '''Note''': <br /> The histogram looks much nicer if we reject the points within the burning time.&lt;br&gt;<br /> <br /> <br /> Example: Use M-H method to generate sample from f(x)=2x<br /> 0&lt;x&lt;1, 0 otherwise.<br /> <br /> 1) Initialize the chain with &lt;math&gt;x_i&lt;/math&gt; and set &lt;math&gt;i=0&lt;/math&gt;<br /> <br /> 2)&lt;math&gt;Y~\sim~q(y|x_i)&lt;/math&gt;<br /> where our proposal function would be uniform [0,1] since it matches our original ones support.<br /> =&gt;&lt;math&gt;Y~\sim~Unif[0,1]&lt;/math&gt;<br /> <br /> 3)consider &lt;math&gt;\frac{f(y)}{f(x)}=\frac{y}{x}&lt;/math&gt;, <br /> &lt;math&gt;r(x,y)=min (\frac{y}{x},1)&lt;/math&gt; since q(y|x&lt;sub&gt;i&lt;/sub&gt;) and q(x&lt;sub&gt;i&lt;/sub&gt;|y) can be cancelled together.<br /> <br /> 4)&lt;math&gt;X_{i+1}=Y&lt;/math&gt; with prob &lt;math&gt;r(x,y)&lt;/math&gt;, <br /> &lt;math&gt;X_{i+1}=X_i&lt;/math&gt;, otherwise<br /> <br /> 5)&lt;math&gt;i=i+1&lt;/math&gt;, go to 2<br /> <br /> &lt;br&gt;<br /> <br /> Example form wikipedia<br /> <br /> ===Step-by-step instructions===<br /> <br /> Suppose the most recent value sampled is &lt;math&gt;x_t\,&lt;/math&gt;. To follow the Metropolis–Hastings algorithm, we next draw a new proposal state &lt;math&gt;x'\,&lt;/math&gt; with probability density &lt;math&gt;Q(x'\mid x_t)\,&lt;/math&gt;, and calculate a value<br /> <br /> :&lt;math&gt;<br /> a = a_1 a_2\,<br /> &lt;/math&gt;<br /> <br /> where<br /> <br /> :&lt;math&gt;<br /> a_1 = \frac{P(x')}{P(x_t)} \,\!<br /> &lt;/math&gt;<br /> <br /> is the likelihood ratio between the proposed sample &lt;math&gt;x'\,&lt;/math&gt; and the previous sample &lt;math&gt;x_t\,&lt;/math&gt;, and<br /> <br /> :&lt;math&gt;<br /> a_2 = \frac{Q(x_t \mid x')}{Q(x'\mid x_t)}<br /> &lt;/math&gt;<br /> <br /> is the ratio of the proposal density in two directions (from &lt;math&gt;x_t\,&lt;/math&gt; to &lt;math&gt;x'\,&lt;/math&gt; and ''vice versa'').<br /> This is equal to 1 if the proposal density is symmetric.<br /> Then the new state &lt;math&gt;\displaystyle x_{t+1}&lt;/math&gt; is chosen according to the following rules.<br /> <br /> :&lt;math&gt;<br /> \begin{matrix}<br /> \mbox{If } a \geq 1: &amp; \\<br /> &amp; x_{t+1} = x',<br /> \end{matrix}<br /> &lt;/math&gt;<br /> :&lt;math&gt;<br /> \begin{matrix}<br /> \mbox{else} &amp; \\<br /> &amp; x_{t+1} = \left\{<br /> \begin{array}{lr}<br /> x' &amp; \mbox{ with probability }a \\<br /> x_t &amp; \mbox{ with probability }1-a.<br /> \end{array}<br /> \right.<br /> \end{matrix}<br /> &lt;/math&gt;<br /> <br /> The Markov chain is started from an arbitrary initial value &lt;math&gt;\displaystyle x_0&lt;/math&gt; and the algorithm is run for many iterations until this initial state is &quot;forgotten&quot;. <br /> These samples, which are discarded, are known as ''burn-in''. The remaining set of accepted values of &lt;math&gt;x&lt;/math&gt; represent a sample from the distribution &lt;math&gt;P(x)&lt;/math&gt;.<br /> <br /> The algorithm works best if the proposal density matches the shape of the target distribution &lt;math&gt;\displaystyle P(x)&lt;/math&gt; from which direct sampling is difficult, that is &lt;math&gt;Q(x'\mid x_t) \approx P(x') \,\!&lt;/math&gt;.<br /> If a Gaussian proposal density &lt;math&gt;\displaystyle Q&lt;/math&gt; is used the variance parameter &lt;math&gt;\displaystyle \sigma^2&lt;/math&gt; has to be tuned during the burn-in period.<br /> This is usually done by calculating the ''acceptance rate'', which is the fraction of proposed samples that is accepted in a window of the last &lt;math&gt;\displaystyle N&lt;/math&gt; samples.<br /> The desired acceptance rate depends on the target distribution, however it has been shown theoretically that the ideal acceptance rate for a one dimensional Gaussian distribution is approx 50%, decreasing to approx 23% for an &lt;math&gt;\displaystyle N&lt;/math&gt;-dimensional Gaussian target distribution.&lt;ref name=Roberts/&gt;<br /> <br /> If &lt;math&gt;\displaystyle \sigma^2&lt;/math&gt; is too small the chain will ''mix slowly'' (i.e., the acceptance rate will be high but successive samples will move around the space slowly and the chain will converge only slowly to &lt;math&gt;\displaystyle P(x)&lt;/math&gt;). On the other hand,<br /> if &lt;math&gt;\displaystyle \sigma^2&lt;/math&gt; is too large the acceptance rate will be very low because the proposals are likely to land in regions of much lower probability density, so &lt;math&gt;\displaystyle a_1&lt;/math&gt; will be very small and again the chain will converge very slowly.<br /> <br /> == Class 19 - Tuesday July 9th 2013 ==<br /> '''Recall: Metropolis–Hasting Algorithm'''<br /> <br /> 1) &lt;math&gt;X_i&lt;/math&gt; = State of chain at time i. Set &lt;math&gt;X_0&lt;/math&gt; = 0&lt;br&gt;<br /> 2) Generate proposal distribution: Y ~ q(y|x) &lt;br&gt;<br /> 3) Set &lt;math&gt;\,r=min[\frac{f(y)}{f(x)}\,\frac{q(x|y)}{q(y|x)}\,,1]&lt;/math&gt;&lt;br&gt;<br /> 4) Generate U ~ U(0,1)&lt;br&gt;<br /> If &lt;math&gt;U&lt;r&lt;/math&gt;, then&lt;br&gt;<br /> &lt;math&gt;X_{i+1} = Y&lt;/math&gt; % i.e. we accept Y as the next point in the Markov Chain &lt;br&gt;<br /> else &lt;br&gt;<br /> &lt;math&gt;X_{i+1}&lt;/math&gt; = &lt;math&gt;X_i&lt;/math&gt;&lt;br&gt;<br /> End if&lt;br&gt;<br /> 5) Set i = i + 1. Return to Step 2. &lt;br&gt;<br /> <br /> <br /> Why can we use this algorithm to generate a Markov Chain?&lt;br&gt;<br /> <br /> &lt;math&gt;\,Y&lt;/math&gt;~&lt;math&gt;\,q(y|x)&lt;/math&gt; satisfies the Markov Property, as the current state does not depend on previous trials. Note that Y does not '''''have''''' to depend on X&lt;sub&gt;t-1&lt;/sub&gt;; the Markov Property is satisfied as long as Y is not dependent on X&lt;sub&gt;0&lt;/sub&gt;, X&lt;sub&gt;1&lt;/sub&gt;,..., X&lt;sub&gt;t-2&lt;/sub&gt;. Thus, time t will not affect the choice of state.&lt;br&gt; <br /> <br /> <br /> ==='''Choosing b: 3 cases'''===<br /> If y and x have the same domain, say R, we could use normal distribution to model &lt;math&gt;q(y|x)&lt;/math&gt;. &lt;math&gt;q(x|y)~normal(y,b^2), and q(y|x)~normal(x,b^2)&lt;/math&gt;.<br /> In the continuous case of MCMC, &lt;math&gt;q(y|x)&lt;/math&gt; is the probability of observing y, given you are observing x. We normally assume &lt;math&gt;q(y|x)&lt;/math&gt; ~ N(x,b^2). A reasonable choice of b is important to ensure the MC does indeed converges to the target distribution f. If b is too small it is not possible to explore the whole support because the jumps are small. If b is large than the probability of accepting the proposed state y is small, and it is very likely that we reject the possibilities of leaving the current state, hence the chain will keep on producing the initial state of the Markov chain. <br /> <br /> To be precise, we are discussing the choice of variance for the proposal distribution.Large b simply implies larger variance for our choice of proposal distribution (Gaussian) in this case. Therefore, many points will be rejected and we will generate same points many times since there are many points that have been rejected.&lt;br&gt;<br /> <br /> In this example, &lt;math&gt;q(y|x)=N(x, b^2)&lt;/math&gt;&lt;br&gt;<br /> <br /> Demonstrated as follows, the choice of b will be significant in determining the quality of the Metropolis algorithm. &lt;br&gt;<br /> <br /> This parameter affects the probability of accepting the candidate states, and the algorithm will not perform well if the acceptance probability is too large or too small, it also affects the size of the &quot;jump&quot; between the sampled &lt;math&gt;Y&lt;/math&gt; and the previous state x&lt;sub&gt;i+1&lt;/sub&gt;, as a larger variance implies a larger such &quot;jump&quot;.&lt;br&gt;<br /> <br /> If the jump is too large, we will have to repeat the previous stage; thus, we will repeat the same point for many times.&lt;br&gt;<br /> <br /> '''MATLAB b=2, b= 0.2, b=20 '''<br /> &lt;pre style=&quot;font-size:12px&quot;&gt;<br /> clear all<br /> close all<br /> clc<br /> b=2 % b=0.2 b=20;<br /> x(1)=0;<br /> for i=2:10000<br /> y=b*randn+x(i-1);<br /> r=min((1+x(i-1)^2)/(1+y^2),1);<br /> u=rand;<br /> if u&lt;r<br /> x(i)=y;<br /> else<br /> x(i)=x(i-1);<br /> end<br /> <br /> end<br /> figure(1);<br /> hist(x(5000:end,100));<br /> figure(2);<br /> plot(x(5000:end));<br /> %The Markov Chain usually takes some time to converge and this is known as the &quot;burning time&quot;<br /> %Therefore, we don't display the first 5000 points because they don't show the limiting behaviour of the Markov Chain<br /> <br /> generate the Markov Chain with 10000 random variable, using a large b and a small b.<br /> &lt;/pre&gt;<br /> <br /> b tells where the next point is going to be. The appropriate b is supposed to explore all the support area. <br /> <br /> f(x) is the stationary distribution list of the chain in MH. We generating y using q(y|x) and accept it with respect to r.<br /> <br /> ===='''b too small====<br /> If &lt;math&gt;b = 0.02&lt;/math&gt;, the chain takes small steps so the chain doesn't explore enough of sample space.<br /> <br /> If &lt;math&gt;b = 20&lt;/math&gt;, jumps are very unlikely to be accepted; i.e. &lt;math&gt; y &lt;/math&gt; is rejected as &lt;math&gt; u&gt; r &lt;/math&gt; and &lt;math&gt; Xt+1 = Xt&lt;/math&gt;.<br /> i.e &lt;math&gt;\frac {f(y)}{f(x)}&lt;/math&gt; and consequent &lt;math&gt; r &lt;/math&gt; is very small and very unlikely that &lt;math&gt; u &lt; r &lt;/math&gt;, so the current value will be repeated.<br /> <br /> ==='''Detailed Balance Holds for Metropolis-Hasting'''===<br /> <br /> In metropolis-hasting, we generate y using q(y|x) and accept it with probability r, where &lt;br&gt;<br /> <br /> &lt;math&gt;r(x,y) = min\left\{\frac{f(y)}{f(x)}\frac{q(x|y)}{q(y|x)},1\right\} = min\left\{\frac{f(y)}{f(x)},1\right\}&lt;/math&gt;&lt;br&gt;<br /> <br /> Without loss of generality we assume &lt;math&gt;\frac{f(y)}{f(x)}\frac{q(x|y)}{q(y|x)} &gt; 1&lt;/math&gt;&lt;br&gt;<br /> <br /> Then r(x,y) (probability of accepting y given we are currently in x) is &lt;br&gt;<br /> <br /> &lt;math&gt;r(x,y) = min\left\{\frac{f(y)}{f(x)}\frac{q(x|y)}{q(y|x)},1\right\} = \frac{f(y)}{f(x)}\frac{q(x|y)}{q(y|x)}&lt;/math&gt;&lt;br&gt;<br /> <br /> Now suppose that the current state is y and we are generating x; the probability of accepting x given that we are currently in state y is &lt;br&gt;<br /> <br /> &lt;math&gt;r(x,y) = min\left\{\frac{f(x)}{f(y)}\frac{q(y|x)}{q(x|y)},1\right\} = 1 &lt;/math&gt;&lt;br&gt;<br /> <br /> This is because &lt;math&gt;\frac{f(y)}{f(x)}\frac{q(x|y)}{q(y|x)} &lt; 1 &lt;/math&gt; and its reverse &lt;math&gt;\frac{f(x)}{f(y)}\frac{q(y|x)}{q(x|y)} &gt; 1 &lt;/math&gt;. Then &lt;math&gt;r(x,y) = 1&lt;/math&gt;.&lt;br&gt;<br /> We are interested in the probability of moving from from x to y in the Markov Chain generated by MH algorithm: &lt;br&gt;<br /> P(y|x) depends on two probabilities:<br /> 1. Probability of generating y, and&lt;br&gt;<br /> 2. Probability of accepting y. &lt;br&gt;<br /> <br /> &lt;math&gt;P(y|x) = q(y|x)*r(x,y) = q(y|x)*{\frac{f(y)}{f(x)}\frac{q(x|y)}{q(y|x)}} = \frac{f(y)*q(x|y)}{f(x)} &lt;/math&gt; &lt;br&gt;<br /> <br /> The probability of moving to x given the current state is y:<br /> <br /> &lt;math&gt;P(x|y) = q(x|y)*r(y,x) = q(x|y)&lt;/math&gt;&lt;br&gt;<br /> <br /> So does detailed balance hold for MH? &lt;br&gt;<br /> <br /> If it holds we should have &lt;math&gt;f(x)*P(y|x) = f(y)*P(x|y)&lt;/math&gt;.&lt;br&gt;<br /> <br /> Left-hand side: &lt;br&gt;<br /> <br /> &lt;math&gt;f(x)*P(y|x) = f(x)*{\frac{f(y)*q(x|y)}{f(x)}} = f(y)*q(x|y)&lt;/math&gt;&lt;br&gt;<br /> <br /> Right-hand side: &lt;br&gt;<br /> <br /> &lt;math&gt;f(y)*P(x|y) = f(y)*q(x|y)&lt;/math&gt;&lt;br&gt;<br /> <br /> Thus LHS and RHS are equal and the detailed balance holds for MH algorithm. &lt;br&gt;<br /> Therefore, f(x) is the stationary distribution of the chain.&lt;br&gt;<br /> <br /> == Class 20 - Thursday July 11th 2013 ==<br /> === Simulated annealing ===<br /> &lt;br /&gt;<br /> '''Definition:''' Simulated annealing (SA) is a generic probabilistic metaheuristic for the global optimization problem of locating a good approximation to the global optimum of a given function in a large search space. It is often used when the search space is discrete (e.g., all tours that visit a given set of cities). &lt;br /&gt;<br /> (http://en.wikipedia.org/wiki/Simulated_annealing) &lt;br /&gt;<br /> &quot;Simulated annealing is a popular algorithm in simulation for minimizing functions.&quot; (from textbook)&lt;br /&gt;<br /> <br /> Simulated annealing is developed to solve the traveling salesman problem: finding the optimal path to travel all the cities needed&lt;br/&gt;<br /> <br /> It is called &quot;Simulated annealing&quot; because it mimics the process undergone by misplaced atoms in a metal when&lt;br /&gt; <br /> its heated and then slowly cooled.&lt;br /&gt;<br /> (http://mathworld.wolfram.com/SimulatedAnnealing.html)&lt;br /&gt;<br /> <br /> It is a probabilistic method proposed in Kirkpatrick, Gelett and Vecchi (1983) and Cerny (1985) for finding the global minimum of a function that may have multiple local minimums.&lt;br /&gt;<br /> (http://www.mit.edu/~dbertsim/papers/Optimization/Simulated%20annealing.pdf)&lt;br /&gt;<br /> <br /> Simulated annealing was developed as an approach for finding the minimum of complex functions &lt;br /&gt;<br /> with multiple peaks; where standard hill-climbing approaches may trap the algorithm at a less that optimal peak.&lt;br /&gt;<br /> <br /> Suppose we generated a point &lt;math&gt; x &lt;/math&gt; by an existing algorithm, and we would like to get a &quot;better&quot; point. &lt;br&gt;<br /> (eg. If we have generated a local min of a function and we want the global min) &lt;br&gt;<br /> Then we would use simulated annealing as a method to &quot;perturb&quot; &lt;math&gt; x &lt;/math&gt; to obtain a better solution. &lt;br&gt;<br /> <br /> Suppose we would like to min &lt;math&gt; h(x)&lt;/math&gt;, for any arbitrary constant &lt;math&gt; T &gt; 0&lt;/math&gt;, this problem is equivalent to max &lt;math&gt;e^{-h(x)/T}&lt;/math&gt;&lt;br /&gt;<br /> Note that the exponential function is monotonic. &lt;br /&gt;<br /> Consider f proportional to e&lt;sup&gt;-h(x)/T&lt;/sup&gt;, sample of this distribution when T is small and <br /> close to the optimal point of h(x). Based on this observation, SA algorithm is introduced as :&lt;br /&gt;<br /> &lt;b&gt;1.&lt;/b&gt; Set T to be a large number&lt;br /&gt;<br /> &lt;b&gt;2.&lt;/b&gt; Initialize the chain: set &lt;math&gt;\,X_{t} (ie. i=0, x_0=s)&lt;/math&gt;&lt;br /&gt;<br /> &lt;b&gt;3.&lt;/b&gt; &lt;math&gt;\,y&lt;/math&gt;~&lt;math&gt;\,q(y|x)&lt;/math&gt;&lt;br/&gt; <br /> (q should be symmetric)&lt;br /&gt;<br /> &lt;b&gt;4.&lt;/b&gt; &lt;math&gt;r = \min\{\frac{f(y)}{f(x)},1\}&lt;/math&gt;&lt;br /&gt;<br /> &lt;b&gt;5.&lt;/b&gt; U ~ U(0,1)&lt;br /&gt;<br /> &lt;b&gt;6.&lt;/b&gt; If U &lt; r, &lt;math&gt;X_{t+1}=y&lt;/math&gt; &lt;br/&gt;<br /> else, &lt;math&gt;X_{t+1}=X_t&lt;/math&gt;&lt;br/&gt;<br /> &lt;b&gt;7.&lt;/b&gt; end decrease T, and let i=i+1. Go back to 3. (This is where the difference lies between SA and MH. &lt;br /&gt;<br /> (repeat the procedure until T is very small)&lt;br/&gt;<br /> &lt;br/&gt;<br /> &lt;b&gt;Note&lt;/b&gt;: q(y|x) does not have to be symmetric. If q is non-symmetric, then the original MH formula is used.&lt;br /&gt;<br /> <br /> The significance of T &lt;br /&gt; <br /> Initially we set T to be large when initializing the chain so as to explore the entire sample space and to avoid the possibility of getting stuck/trapped in one region of the sample space. Then we gradually start decreasing T so as to get closer and closer to the actual solution. <br /> <br /> Notice that we have: <br /> &lt;math&gt; r = \min\{\frac{f(y)}{f(x)},1\} &lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt; = \min\{\frac{e^{\frac{-h(y)}{T}}}{e^{\frac{-h(x)}{T}}},1\} &lt;/math&gt; &lt;br/&gt;<br /> &lt;math&gt; = \min\{e^{\frac{h(x)-h(y)}{T}},1\} &lt;/math&gt;&lt;br/&gt;<br /> <br /> Reasons we start with a large T but not a small T at the beginning:&lt;br /&gt;<br /> <br /> &lt;ul&gt;&lt;li&gt;A point in the tail when T is small would be rejected &lt;br /&gt;<br /> &lt;/li&gt;&lt;li&gt;Chances that we reject points get larger as we move from large T to small T &lt;br /&gt;<br /> &lt;/li&gt;&lt;li&gt;Large T helps get to the mode of maximum value&lt;br /&gt;<br /> &lt;/li&gt;&lt;/ul&gt;<br /> <br /> Assume T is large &lt;br /&gt;<br /> 1. h(y) &lt; h(x), e&lt;sup&gt;(h(x)-h(y))/T &lt;/sup&gt; &gt; 1, then r = 1, y will always be accepted.&lt;br /&gt;<br /> 2. h(y) &gt; h(x), e&lt;sup&gt;(h(x)-h(y))/T &lt;/sup&gt;&lt; 1, then r &lt; 1, y will be accepted with probability r. '''Remark:'''this will help to scape from local minimum, because the algorithm prevents it from reaching and staying in the local minimum forever. &lt;br /&gt;<br /> Assume T is small&lt;br /&gt;<br /> 1. h(y) &lt; h(x), then r = 1, y will always be accepted.&lt;br /&gt;<br /> 2. h(y) &gt; h(x), e&lt;sup&gt;(h(x)-h(y))/T &lt;/sup&gt; approaches to 0, then r goes to 0 and y will almost never be accepted.<br /> <br /> &lt;p&gt;&lt;br /&gt; All in all, choose a large T to start off with in order for a higher chance that the points can explore. &lt;br /&gt;<br /> <br /> '''Note''': The variable T is known in practice as the &quot;Temperature&quot;, thus the higher T is, the more variability there is in terms of the expansion and contraction of materials. The term &quot;Annealing&quot; follows from here, as annealing is the process of heating materials and allowing them to cool slowly.&lt;br /&gt;<br /> <br /> Asymptotically this algorithm is guaranteed to generate the global optimal answer, however in practice, we never sample forever and this may not happen.<br /> <br /> &lt;/p&gt;&lt;p&gt;&lt;br /&gt;<br /> &lt;/p&gt;&lt;p&gt;Example: Consider &lt;math&gt;h(x)=3x^2&lt;/math&gt;, 0&amp;lt;x&amp;lt;1<br /> &lt;/p&gt;&lt;p&gt;&lt;br /&gt;1) Set T to be large, for example, T=100&lt;br /&gt;<br /> &lt;br /&gt;2) Initialize the chain&lt;br /&gt;<br /> &lt;br /&gt;3) Set &lt;math&gt;q(y|x)~\sim~Unif[0,1]&lt;/math&gt;&lt;br /&gt;<br /> &lt;br /&gt;4) &lt;math&gt;r=min(exp(\frac{(3x^2-3y^2)}{100}),1)&lt;/math&gt;&lt;br /&gt;<br /> &lt;br /&gt;5) &lt;math&gt;U~\sim~U[0,1]&lt;/math&gt;&lt;br /&gt;<br /> &lt;br /&gt;6) If &lt;i&gt;U&lt;/i&gt; &amp;lt; &lt;i&gt;r&lt;/i&gt; then &lt;i&gt;X&lt;/i&gt;&lt;sub&gt;&lt;i&gt;t&lt;/i&gt; + 1&lt;/sub&gt; = &lt;i&gt;y&lt;/i&gt; &lt;br&gt; <br /> &lt;i&gt;e&lt;/i&gt;&lt;i&gt;l&lt;/i&gt;&lt;i&gt;s&lt;/i&gt;&lt;i&gt;e&lt;/i&gt;,&lt;i&gt;X&lt;/i&gt;&lt;sub&gt;&lt;i&gt;t&lt;/i&gt; + 1&lt;/sub&gt; = &lt;i&gt;x&lt;/i&gt;&lt;sub&gt;&lt;i&gt;t&lt;/i&gt;&lt;/sub&gt;&lt;br /&gt;<br /> &lt;br /&gt;7) Decrease T, go back to 3&lt;br /&gt;<br /> &lt;/p&gt;<br /> &lt;div style=&quot;border:1px red solid&quot;&gt;<br /> &lt;p&gt;&lt;b&gt;MATLAB &lt;/b&gt;<br /> &lt;/p&gt;<br /> &lt;pre style=&quot;font-size:12px&quot;&gt;<br /> Syms x<br /> Ezplot('(x-3)^2',[-6,12])<br /> Ezplot('exp(-((x-3)^2))', [-6, 12])<br /> &lt;/pre&gt;<br /> <br /> [[File:Snip2013.png|350px]]<br /> <br /> [[File:Snip20131.png|350px]]<br /> <br /> [[File:STAT_340.JPG]]<br /> http://www.wolframalpha.com/input/?i=graph+exp%28-%28x-3%29%5E2%2F10%29<br /> &lt;b&gt;MATLAB &lt;/b&gt;<br /> <br /> Note that when T is small, the graph consists of a much higher bump; when T is large, the graph is flatter. <br /> <br /> &lt;pre style=&quot;font-size:14px&quot;&gt;<br /> <br /> clear all<br /> close all<br /> T=100;<br /> x(1)=randn;<br /> ii=1;<br /> b=1;<br /> while T&amp;gt;0.001<br /> y=b*randn+x(ii);<br /> r=min(exp((H(x(ii))-H(y))/T),1);<br /> u=rand;<br /> if u&amp;lt;r<br /> x(ii+1)=y;<br /> else<br /> x(ii+1)=x(ii);<br /> end<br /> <br /> T=0.99*T;<br /> ii=ii+1;<br /> end<br /> plot(x)<br /> <br /> &lt;/pre&gt;<br /> [[File:SA_example.jpg|350px]]<br /> &lt;/div&gt;<br /> &lt;p&gt;Helper function:<br /> &lt;/p&gt;&lt;p&gt;an example is for H(x)=(x-3)^2<br /> &lt;/p&gt;<br /> &lt;pre style=&quot;font-size:12px&quot;&gt;<br /> function c=H(x)<br /> c=(x-3)^2;<br /> end<br /> &lt;/pre&gt;<br /> &lt;p&gt;&lt;b&gt;Another Example:&lt;/b&gt;<br /> &lt;span class=&quot;texhtml&quot;&gt;&lt;i&gt;h&lt;/i&gt;(&lt;i&gt;x&lt;/i&gt;) = ((&lt;i&gt;x&lt;/i&gt; &amp;minus; 2)&lt;sup&gt;2&lt;/sup&gt; &amp;minus; 4)((&lt;i&gt;x&lt;/i&gt; &amp;minus; 4)&lt;sup&gt;2&lt;/sup&gt; &amp;minus; 8)&lt;/span&gt;<br /> &lt;/p&gt;<br /> &lt;pre style=&quot;font-size:12px&quot;&gt;<br /> &amp;gt;&amp;gt;syms x<br /> &amp;gt;&amp;gt;ezplot(((x-2)^2-4)*((x-4)^2-8),[-1,8])<br /> &lt;/pre&gt;<br /> &lt;pre style=&quot;font-size:12px&quot;&gt;<br /> function c=H(x)<br /> c=((x-2)^2-4)*((x-4)^2-8);<br /> end<br /> &lt;/pre&gt;<br /> [[File:SA_example2.jpg|350px]]<br /> &lt;p&gt;Run earlier code with the new H(x) function<br /> &lt;/p&gt;<br /> &lt;h3&gt; &lt;span class=&quot;mw-headline&quot; id=&quot;Motivation:_Simulated_Annealing_and_the_Travelling_Salesman_Problem&quot;&gt; Motivation: Simulated Annealing and the Travelling Salesman Problem &lt;/span&gt;&lt;/h3&gt;<br /> &lt;p&gt;The Travelling Salesman Problem asks: &lt;br /&gt;<br /> Given n numbers of cities and the distances between each pair of cities, what is the shortest possible route that visits each city exactly once and returns to the original city? By letting two permutations if one results from an interchange of two of the coordinates of the other, we can use simulated annealing to approximate the best path.<br /> &lt;p&gt;[[File:Salesman n5.png|350px]]<br /> &lt;/p&gt;<br /> &lt;ul&gt;&lt;li&gt;An example of a solution of a travelling salesman problem on n=5. This is only one of many solutions, but we want to ensure we find the optimal solution.<br /> &lt;/li&gt;&lt;/ul&gt;<br /> <br /> &lt;ul&gt;&lt;li&gt;Given n=5 cities, we search for the best route with the minimum distance to visit all cities and return to the starting city.<br /> &lt;/li&gt;&lt;/ul&gt;<br /> &lt;p&gt;&lt;b&gt;The idea of using Simulated Annealing algorithm&lt;/b&gt;&amp;nbsp;:<br /> Let Y (let Y be all possible combinations of route in terms of cities index) be generated by permutation of all cities. Let the target or objective distribution (f(x)) be the distance of the route given Y.<br /> Then use the Simulated Annealing algorithm to find the minimum value of f(x).&lt;br /&gt;<br /> &lt;/p&gt;&lt;p&gt;&lt;b&gt;Note&lt;/b&gt;: in this case, Q is the permutation of the numbers. There will be may possible paths, especially when n is large. If n is very large, then it will take forever to check all the combination of routes.<br /> &lt;/p&gt;<br /> &lt;ul&gt;&lt;li&gt;This sort of knowledge would be very useful for those in a situation where they are on a limited budget or must visit many points in a short period of time. For example, a truck driver may have to visit multiple cities in southern Ontario and make it back to his original starting point within a 6-hour period. &lt;br /&gt;<br /> &lt;/li&gt;&lt;/ul&gt;<br /> <br /> '''Disadvantages of Simulated Annealing:'''&lt;br/&gt;<br /> 1. This method converges very slowly, and therefore very expensive.&lt;br/&gt;<br /> 2. This algorithm cannot tell whether it has found the global minimum.&lt;br/&gt;&lt;ref&gt;<br /> Reference: http://cs.adelaide.edu.au/~paulc/teaching/montecarlo/node140.html<br /> &lt;/ref&gt;<br /> <br /> == Class 21 - Tuesday July 16, 2013 ==<br /> === Gibbs Sampling===<br /> '''Definition'''&lt;br&gt;<br /> In statistics and in statistical physics, Gibbs sampling or a Gibbs sampler is a Markov chain Monte Carlo (MCMC) algorithm for obtaining a sequence of observations which are approximately from a specified multivariate probability distribution (i.e. from the joint probability distribution of two or more random variables), when direct sampling is difficult.&lt;br/&gt;<br /> (http://en.wikipedia.org/wiki/Gibbs_sampling)&lt;br/&gt;<br /> <br /> The Gibbs sampling method was originally developed by Geman and Geman . It was later brought into mainstream statistics by Gelfand and Smith  and Gelfand, et al. &lt;br/&gt;<br /> Source: https://www.msu.edu/~blackj/Scan_2003_02_12/Chapter_11_Markov_Chain_Monte_Carlo_Methods.pdf&lt;br/&gt;<br /> <br /> Gibbs sampling is a general method for probabilistic inference which is often used when dealing with incomplete information. However, generality comes at some computational cost, and for many applications including those involving missing information, there are often alternative methods that have been shown to be more efficient in practice. Suppose we want to sample from a joint distribution &lt;math&gt;p(x_1,...,x_k)&lt;/math&gt; (i.e. a posterior distribution). If we knew the full conditional distributions for each parameter (i.e. &lt;math&gt;p(x_i|x_1,x_2,...,x_{i-1},x_{i+1},...,x_k)&lt;/math&gt;), we can use the Gibbs sampler to sample from these conditional distributions. &lt;br&gt;<br /> <br /> When utilizing the Gibbs sampler, the candidate state is always accepted as the nect state of the chain.(from text book)&lt;br/&gt;<br /> <br /> *Another Markov Chain Monte Carlo (MCMC) method (first MCMC method introduced in this course is the MH Algorithm) &lt;br/&gt;<br /> *a special case of Metropolis-Hastings sampling where the random value is always accepted, i.e. as long as a point is proposed, it is accepted. &lt;br/&gt; <br /> * useful and make it simple and easier for sampling a d-dimensional random vector &lt;math&gt;\vec{x} = (x_1, x_2,...,x_d)&lt;/math&gt;&lt;br /&gt;<br /> * then the observations of d-dimensional random vectors &lt;math&gt;{\vec{x_1}, \vec{x_2}, ... , \vec{x_n}}&lt;/math&gt; form a d-dimensional Markov Chain and the joint density &lt;math&gt;f(x_1, x_2, ... , x_d)&lt;/math&gt; is an invariant distribution for the chain. i.e. for sampling multivariate distributions.&lt;br /&gt;<br /> * useful if sampling from conditional pdf, since they are easier to sample, in comparison to the joint distribution.&lt;br/&gt;<br /> *Definition of univariate conditional distribution: all the random variables are fixed except for one; we need to use n such univariate conditional distributions to simulate n random variables. <br /> <br /> '''Difference between Gibbs Sampling &amp; MH'''&lt;br&gt;<br /> Gibbs Sampling generates new value based on the conditional distribution of other components (unlike MH, which does not require conditional distribution).&lt;br/&gt;<br /> eg. We are given the following about &lt;math&gt; f(x_1,x_2) , f(x_1|x_2),f(x_2|x_1) &lt;/math&gt;&lt;br/&gt;<br /> 1. let &lt;math&gt;x^*_1 \sim f(x_1|x_2)&lt;/math&gt;&lt;br/&gt;<br /> 2. &lt;math&gt;x^*_2 \sim f(x^*_1|x_2)&lt;/math&gt;&lt;br/&gt;<br /> 3. substitute &lt;math&gt;x^*_2&lt;/math&gt; back into first step and repeat the process. &lt;br/&gt;<br /> <br /> Also, for Gibbs sampling, we will &quot;always accept a candidate point&quot;, unlike MH&lt;br/&gt;<br /> Source: https://www.msu.edu/~blackj/Scan_2003_02_12/Chapter_11_Markov_Chain_Monte_Carlo_Methods.pdf&lt;br/&gt;<br /> <br /> &lt;div style = &quot;align:left; background:#F5F5DC; font-size: 120%&quot;&gt;<br /> '''Gibbs Sampling as a special form of the Metropolis Hastings algorithm'''&lt;br&gt;<br /> <br /> The Gibbs Sampler is simply a case of the Metropolis Hastings algorithm&lt;br&gt;<br /> <br /> here, the proposal distribution is &lt;math&gt;q(Y|X)=f(X^j|X^*_i, i\neq j)=\frac{f(Y)}{f(X_i, i\neq j)}&lt;/math&gt; for &lt;math&gt;X=(X_1,...,X_n)&lt;/math&gt;, &lt;br&gt;<br /> which is simply the conditional distribution of each element conditional on all the other elements in the vector. &lt;br&gt;<br /> similarly &lt;math&gt;q(X|Y)=f(X|Y^*_i, i\neq j)=\frac{f(X)}{f(Y_i, i\neq j)}&lt;/math&gt;&lt;br&gt;<br /> notice that &lt;math&gt;(Y_i, i\neq j)&lt;/math&gt; and &lt;math&gt;(X_i, i\neq j)&lt;/math&gt; are identically distributed. &lt;br&gt;<br /> <br /> the distribution we wish to simulate from is &lt;math&gt;p(X) = f(X) &lt;/math&gt;<br /> also, &lt;math&gt;p(Y) = f(Y) &lt;/math&gt;<br /> <br /> Hence, the acceptance ratio in the Metropolis-Hastings algorithm is: &lt;br&gt;<br /> &lt;math&gt;r(x,y) = min\left\{\frac{f(x)}{f(y)}\frac{q(y|x)}{q(x|y)},1\right\} = min\left\{\frac{f(x)}{f(y)}\frac{f(y)}{f(x)},1\right\} = 1 &lt;/math&gt;&lt;br&gt;<br /> so the new point will always be accepted, and no points are rejected and the Gibbs Sampler is an efficient algorithm in that aspect. &lt;br&gt;<br /> &lt;/div&gt;<br /> <br /> &lt;b&gt;Advantages &lt;/b&gt;&lt;ref&gt;<br /> http://wikicoursenote.com/wiki/Stat341#Gibbs_Sampling_-_June_30.2C_2009<br /> &lt;/ref&gt;<br /> <br /> *The algorithm has an acceptance rate of 1. Thus, it is efficient because we keep all the points that we sample from.<br /> *It is simple and straightforward if and only if we know the conditional pdf. <br /> *It is useful for high-dimensional distributions. (ie. for sampling multivariate PDF)<br /> *It is useful if sampling from conditional PDF are easier than sampling from the joint. <br /> <br /> &lt;br /&gt;<br /> &lt;b&gt;Disadvantages&lt;/b&gt;&lt;ref&gt;<br /> http://wikicoursenote.com/wiki/Stat341#Gibbs_Sampling_-_June_30.2C_2009<br /> &lt;/ref&gt;<br /> <br /> *We rarely know how to sample from the conditional distributions.<br /> *The probability functions of the conditional probability are usually unknown or hard to sample from. <br /> *The algorithm can be extremely slow to converge.<br /> *It is often difficult to know when convergence has occurred.<br /> *The method is not practical when there are relatively small correlations between the random variables.<br /> <br /> '''Gibbs Sampler Steps:'''&lt;br\&gt;&lt;ref&gt;<br /> http://www.people.fas.harvard.edu/~plam/teaching/methods/mcmc/mcmc.pdf<br /> &lt;/ref&gt;<br /> Let's suppose that we are interested in sampling from the posterior p(x|y), where x is a vector of three parameters, x1, x2, x3. &lt;br\&gt;<br /> The steps to a Gibbs Sampler are:&lt;br\&gt;<br /> 1. Pick a vector of starting value x(0). Any x(0) will converge eventually, but it can be chosen to take fewer iterations&lt;br\&gt;<br /> 2. Start with any x(order does not matter, but I will start with x1 for convenience). Draw a value x1(1)from the full conditional p(x1|x2(0),x3(0),y)&lt;br\&gt;<br /> 3. Draw a value x2(1) from the full conditional p(x2|x1(1),x3(0),y). Note that we must use the updated value of x1(1).&lt;br\&gt;<br /> 4. Draw a value x3(1) from the full conditional p(x3|x1(1),x2(1),y) using both updated values.&lt;br\&gt;<br /> 5. Draw x2 using x1 and continually using the most updated values. &lt;br\&gt;<br /> 6. Repeat until we get M draws, we each draw being a vector x(t).&lt;br\&gt;<br /> 7. Optional burn-in or thinning.&lt;br\&gt;<br /> Our result is a Markov chain with a bunch of draws of x that are approximately from our posterior. <br /> <br /> '''The Basic idea:''' &lt;br&gt;<br /> The distinguishing feature of Gibbs sampling is that the underlying Markov chain is constructed from a sequence of conditional distributions. The essential idea is updating one part of the previous element while keeping the other parts fixed - it is useful in many instances where the state variable is a random variable taking values in a general space, not just in R&lt;sup&gt;n&lt;/sup&gt;. (Simulation and the Monte Carlo Method, Reuven Y. Rubinstein)<br /> <br /> '''Note:''' &lt;br&gt;<br /> 1.Other optimizing algorithms introduced such as Simulated Annealing settles on a minimum eventually,which means that if we generate enough observations and plot them in a time series plot, the plot will eventually flatten at the optimal value.&lt;br\&gt; <br /> 2.For Gibbs Sampling however, when convergence is achieved, instead of staying at the optimal value, the Gibbs Sampler continues to wonder through the target distribution (i.e. will not stay at the optimal point) forever.&lt;br\&gt; <br /> '''Special Example'''&lt;br\&gt;<br /> &lt;pre&gt;<br /> function gibbs2(n, thin)<br /> x_samp = zeros(n,1)<br /> y_samp = zeros(n,1)<br /> x=0.0<br /> y=0.0<br /> for i=1:n<br /> for j=1:thin<br /> x=(y^2+4)*randg(3)<br /> y=1/(1+x)+randn()/sqrt(2*x+2)<br /> end<br /> x_samp[i] = x<br /> y_samp[i] = y<br /> end<br /> return x_samp, y_samp<br /> end<br /> 1<br /> 2<br /> julia&gt; @elapsed gibbs2(50000,1000)<br /> 7.6084020137786865<br /> &lt;/pre&gt;<br /> <br /> '''Theoretical Example''' &lt;br/&gt;<br /> <br /> Gibbs Sampler Application (Inspired by Example 10b in the Ross Simulation (4th Edition Textbook))<br /> <br /> Suppose we are a truck driver who randomly puts n basketballs into a 3D storage cube sized so that each edge of the cube is 300cm in length. The basket balls are spherical and have a radius of 25cm each.<br /> <br /> Because the basketballs have a radius of 25cm, the centre of each basketball must be at least 50cm away from the centre of another basketball. That is to say, if two basketballs are touching (as close together as possible) their centres will be 50cm apart.<br /> <br /> Clearly the distribution of n basketballs will need to conditioned on the fact that no basketball is placed so that its centre is closer than 50cm to another basketball.<br /> <br /> This gives:<br /> <br /> Beta = P{the centre of no two basketballs are within 50cm of each other}<br /> <br /> That is to say, the placement of basketballs is conditioned on the fact that two balls cannot overlap.<br /> <br /> This distribution of n balls can be modelled using the Gibbs sampler.<br /> <br /> 1. Start with n basketballs positioned in the cube so that no two centres are within 50cm of each other&lt;br /&gt;<br /> 2. Generate a random number U and let I = floor(n*U) + 1&lt;br /&gt;<br /> 3. Generate another random point &lt;math&gt;X_k&lt;/math&gt; in the storage box.&lt;br /&gt;<br /> 4. If &lt;math&gt;X_k&lt;/math&gt; is not within 50cm of any other point, excluding point &lt;math&gt;X_I&lt;/math&gt;: &lt;br /&gt;<br /> then replace &lt;math&gt;X_I&lt;/math&gt; by this new point. &lt;br /&gt;<br /> Otherwise: return to step 3.&lt;br /&gt;<br /> <br /> After many iterations, the set of n points will approximate the distribution.<br /> <br /> <br /> '''Example1''' &lt;br/&gt;<br /> We want to sample from a target joint distribution f(x&lt;sub&gt;1&lt;/sub&gt;, x&lt;sub&gt;2&lt;/sub&gt;), which is not easy to sample from but the conditional pdfs f(x&lt;sub&gt;1&lt;/sub&gt;|x&lt;sub&gt;2&lt;/sub&gt;) &amp; f(x&lt;sub&gt;2&lt;/sub&gt;|x&lt;sub&gt;1&lt;/sub&gt;) are very easy to sample from. We can find the stationary distribution (target distribution) using Gibbs sampling: &lt;br/&gt;<br /> 1. x&lt;sub&gt;1&lt;/sub&gt;* ~ f(x&lt;sub&gt;1&lt;/sub&gt;|x&lt;sub&gt;2&lt;/sub&gt;) (here x&lt;sub&gt;2&lt;/sub&gt; is given) =&gt; x = [x&lt;sub&gt;1&lt;/sub&gt;* x&lt;sub&gt;2&lt;/sub&gt;] &lt;br/&gt;<br /> 2. x&lt;sub&gt;2&lt;/sub&gt;* ~ f(x&lt;sub&gt;2&lt;/sub&gt;|x&lt;sub&gt;1&lt;/sub&gt;*) (here x&lt;sub&gt;1&lt;/sub&gt;* is generated from above) =&gt; x = [x&lt;sub&gt;1&lt;/sub&gt;* x&lt;sub&gt;2&lt;/sub&gt;*] &lt;br/&gt;<br /> 3. x&lt;sub&gt;1&lt;/sub&gt;* ~ f(x&lt;sub&gt;1&lt;/sub&gt;*|x&lt;sub&gt;2&lt;/sub&gt;*) (here x&lt;sub&gt;2&lt;/sub&gt;* is generated from above) =&gt; x = [x&lt;sub&gt;1&lt;/sub&gt;* x&lt;sub&gt;2&lt;/sub&gt;* ] &lt;br/&gt;<br /> 4. x&lt;sub&gt;2&lt;/sub&gt;* ~ f(x&lt;sub&gt;2&lt;/sub&gt;*|x&lt;sub&gt;1&lt;/sub&gt;*) &lt;br/&gt;<br /> 5. Repeat steps 3 and 4 until the chain reaches its stationary distribution [x&lt;sub&gt;1&lt;/sub&gt;* x&lt;sub&gt;2&lt;/sub&gt;*]. &lt;br/&gt;<br /> <br /> <br /> Suppose we want to sample from multivariate pdf f(x), where &lt;math&gt;\vec{x} = (x_1, x_2,...,x_d)&lt;/math&gt; is a d-dimentional vector.&lt;br/&gt;<br /> Suppose &lt;math&gt;\vec{x} _t = (x_t,_1, x_t,_2,...,x_t,_d)&lt;/math&gt; is the current value. &lt;br/&gt; <br /> <br /> Suppose &lt;math&gt;\vec{y} = (y_1, y_2,...,y_d)&lt;/math&gt; is the proposed point. &lt;br/&gt;<br /> &lt;math&gt;\vec{x} _{t+1} = \vec{y} &lt;/math&gt;&lt;br /&gt;&lt;br/&gt;<br /> <br /> Let &lt;math&gt;\displaystyle f(x_i|x_1, x_2,...,x_{i-1},....x_d)&lt;/math&gt; represents the conditional pdf of component x&lt;sub&gt;i&lt;/sub&gt;, given other components. &lt;br/&gt;<br /> Then Gibbs sampler is as follows:&lt;br/&gt;<br /> # &lt;math&gt;\displaystyle y_1 \sim f(x_1 | x_{t,2}, x_{t,3}, ..., x_{t,d})&lt;/math&gt;<br /> # &lt;math&gt;\displaystyle y_i \sim f(x_i | y_1, ...., y_{i-1}, x_{t,i+1} , ..., x_{t,d})&lt;/math&gt; <br /> # &lt;math&gt;\displaystyle y_d \sim f(x_d | y_1, ... , y_{d-1})&lt;/math&gt;<br /> # &lt;math&gt;\displaystyle \vec{Y} = (y_1,y_2, ...,y_d)&lt;/math&gt;&lt;br&gt;<br /> <br /> <br /> '''A simpler illustration of the above example'''<br /> Consider four variables (w,x,y,z), the sampler becomes&lt;br/&gt;<br /> # &lt;math&gt;\displaystyle w_i \sim p(w | x = x_{i - 1}, y = y_{i - 1},z = z_{i - 1} )&lt;/math&gt;<br /> # &lt;math&gt;\displaystyle x_i \sim p(x | w = w_i, y = y_{i - 1},z = z_{i - 1} )&lt;/math&gt;<br /> # &lt;math&gt;\displaystyle y_i \sim p(y | w = w_i, x = x_i,z = z_{i - 1} )&lt;/math&gt;<br /> # &lt;math&gt;\displaystyle z_i \sim p(z | w = w_i, x = x_i,y = y_i)&lt;/math&gt;<br /> The reference is here&lt;br/&gt;<br /> http://web.mit.edu/~wingated/www/introductions/mcmc-gibbs-intro.pdf<br /> <br /> '''Example2'''&lt;br&gt;<br /> Suppose we want to sample from a bivariate normal distribution. &lt;br/&gt; &lt;math&gt;\mu =<br /> \left [ \begin{matrix}<br /> 1 \\<br /> 2 \end{matrix} \right] &lt;/math&gt;<br /> <br /> &lt;math&gt;\Sigma=<br /> \left [ \begin{matrix}<br /> 1 &amp; \rho \\<br /> \rho &amp; 1 \end{matrix} \right] &lt;/math&gt; (the covariance matrix)<br /> <br /> where &lt;math&gt;\rho&lt;/math&gt;= 0.9. Then it can be shown that all conditionals are normal of this form: &lt;br/&gt;<br /> f(x&lt;sub&gt;1&lt;/sub&gt;|x&lt;sub&gt;2&lt;/sub&gt;) = N (u&lt;sub&gt;1&lt;/sub&gt; + r(x&lt;sub&gt;2&lt;/sub&gt;-u&lt;sub&gt;2&lt;/sub&gt;), 1-r&lt;math&gt;^2&lt;/math&gt;) &lt;br/&gt;<br /> f(x&lt;sub&gt;2&lt;/sub&gt;|x&lt;sub&gt;1&lt;/sub&gt;) = N (u&lt;sub&gt;2&lt;/sub&gt; + r(x&lt;sub&gt;1&lt;/sub&gt;-u&lt;sub&gt;1&lt;/sub&gt;), 1-r&lt;math&gt;^2&lt;/math&gt;) &lt;br/&gt;&lt;br/&gt;<br /> <br /> '''Matlab Code'''<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> close all<br /> clear all<br /> mu = [1;2]; <br /> x(:,1) = [1;1]; % covariance matrix<br /> r = 0.9; % covariance matrix<br /> for ii = 1:1000<br /> x(1, ii+1) = sqrt(1-r^2)*randn + (mu(1) + r*(x(2,ii) - mu(2))); % N (u1 + r(x2-u2), 1-r2) <br /> x(2, ii+1) = sqrt(1-r^2)*randn + (mu(2) + r*(x(1,ii+1) - mu(1))); % N (u2 + r(x1-u1), 1-r2)<br /> end<br /> plot(x(1,:),x(2,:),'.')<br /> &lt;/pre&gt;&lt;br&gt;<br /> <br /> '''Example3''' &lt;br&gt;<br /> Consider the flowing bivariate normal distribution. &lt;br/&gt; <br /> &lt;math&gt;\mu = \left[\begin{matrix}0\\0 \end{matrix}\right] \qquad \Sigma=\left [ \begin{matrix}1 &amp; \rho \\ \rho &amp; 1 \end{matrix} \right] &lt;/math&gt; (the covariance matrix)<br /> <br /> where &lt;math&gt;\rho&lt;/math&gt;= 0.5. Then it can be shown that all conditionals are normal of this form: &lt;br/&gt;<br /> &lt;math&gt; x_{1,t+1}|x_{2,t} \sim N(\rho x_{2,t},1-\rho^2&lt;/math&gt;) &lt;br/&gt;<br /> &lt;math&gt; x_{2,t+1}|x_{1,t} \sim N(\rho x_{1,t},1-\rho^2&lt;/math&gt;) &lt;br/&gt;&lt;br/&gt;<br /> <br /> <br /> '''Matlab Code:'''<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> close all<br /> clear all<br /> mu = [0;0];<br /> x(:,1) = [1;1];<br /> r = 0.5;<br /> for ii = 1:1000<br /> x(1, ii+1) = sqrt(1-r^2)*randn + r*(x(2,ii));<br /> x(2, ii+1) = sqrt(1-r^2)*randn + r*(x(1,ii+1));<br /> end<br /> z=x(:,501:end);<br /> hist(z(:),100);<br /> &lt;/pre&gt;<br /> [[File:GibbExample.jpg]]<br /> <br /> &lt;div style = &quot;align:left; background:#F5F5DC; font-size: 120%&quot;&gt; &lt;br /&gt;<br /> <br /> <br /> '''Additional Example''' (Adapted from Assignment 5 Question 2)&lt;br /&gt;<br /> Suppose we want to sample from the following two dimensional pdf:&lt;br /&gt;<br /> &lt;math&gt;\, f(x_1,x_2) = c \times e^{\frac{-(x_1^2 x_2^2+x_1^2+x_2^2-8 x_1-8 x_2)}2}&lt;/math&gt;&lt;br /&gt;<br /> One can show that &lt;math&gt;c=~\frac{1}{20216.3359}&lt;/math&gt;, is a normalize constant , but is not required. &lt;br /&gt;<br /> <br /> <br /> '''''Method 1''''' - apply Metropolis-Hastings&lt;br /&gt;<br /> <br /> A simple choice of the proposal distribution is &lt;math&gt;q(y|x)~\sim~N(x,a^2 l_2)&lt;/math&gt; for some parameter &lt;math&gt;a &gt; 0&lt;/math&gt;, and &lt;math&gt;l_2&lt;/math&gt; is the identity matrix of dimension 2.&lt;br /&gt;<br /> i.e., A random walk sampler : Y= x + Z, where &lt;math&gt;Z~\sim~N_2(0,a^2 l_2)&lt;/math&gt;&lt;br /&gt;<br /> Since q(.) is symmetric, then we have&lt;br /&gt;<br /> <br /> &lt;math&gt; r = min(\frac {f(x)}{f(y)},1)&lt;/math&gt;&lt;br /&gt;<br /> Simply put it , given &lt;math&gt;\,x = (x_1,x_2)&lt;/math&gt;:<br /> &lt;math&gt;\,y_1 = x_1+Z_1&lt;/math&gt; and &lt;math&gt;\,y_2 = x_2+Z_2&lt;/math&gt;&lt;br /&gt;<br /> where &lt;math&gt;\Z_i~\sim~N(0,a^2)&lt;/math&gt;<br /> Using &lt;math&gt;a = 2&lt;/math&gt; (moderate tuning parameter)<br /> <br /> [[Algorithm]]<br /> &lt;br /&gt;1. Initiate &lt;math&gt;\,x_0 = (x_{0 1}, x_{0 2})&lt;/math&gt;&lt;br /&gt;<br /> <br /> 2. Generate &lt;math&gt;Z_1,Z_2 ~\sim~N(0,1)&lt;/math&gt; independently,&lt;math&gt;\,Z=(Z_1,Z_2)&lt;/math&gt;, and &lt;math&gt;\,y = x+2Z &lt;/math&gt; for the nth steps.&lt;br /&gt;<br /> <br /> 3. Calculate &lt;math&gt;r = min(\frac {f(x)}{f(y)},1)&lt;/math&gt; &lt;br /&gt;<br /> <br /> 4. Generate &lt;math&gt;U~\sim~Unif(0,1)&lt;/math&gt;, if &lt;math&gt;\, U &lt; r &lt;/math&gt;, return &lt;math&gt;\,x_n = y&lt;/math&gt; , else &lt;math&gt;\,x_n = x_{n-1}&lt;/math&gt;. &lt;br /&gt;<br /> <br /> <br /> '''''Method 2''''' - apply Metropolis-Hastings - Gibbs sampling &lt;br /&gt;<br /> Note that we can rearrange the function as follow:<br /> &lt;math&gt;\,f(x_1,x_2) = c e^{-(1+x_2^2)(x_1-(\frac{4}{1+x_2^2}))^2/2}&lt;/math&gt; &lt;br /&gt;<br /> where c is a function of &lt;math&gt;\,x_2&lt;/math&gt;&lt;br /&gt;<br /> Similarly, we can express the function as :<br /> &lt;math&gt;\,f(x_1,x_2) = c e^{-(1+x_1^2)(x_2-(\frac{4}{1+x_1^2}))^2/2}&lt;/math&gt; &lt;br /&gt;<br /> where c is a function of &lt;math&gt;\,x_1&lt;/math&gt;&lt;br /&gt;<br /> Now, we can see that<br /> &lt;math&gt;\,f(x_1|x_2)~\sim~N(\frac{4}{1+x_2^2},\frac{1}{1+x_2^2})&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;\,f(x_2|x_1) ~\sim~N(\frac{4}{1+x_1^2},\frac{1}{1+x_1^2})&lt;/math&gt;&lt;br /&gt;<br /> <br /> [[Algorithm]]<br /> &lt;br /&gt;1. sampling from &lt;math&gt;x_1: y_1~\sim~f(x_1|x_{t 2})&lt;/math&gt;&lt;br /&gt;<br /> 2. &lt;math&gt;y_2~\sim~f(x_2|Y_1)&lt;/math&gt; and repeat the procedures.&lt;br /&gt;<br /> 3. &lt;math&gt;\vec{Y} = (y_1,y_2) &lt;/math&gt;<br /> <br /> '''Matlab Code:''' &lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> n=10^4; %% generate 10^4 chains<br /> x1(1) =1; x2(1) = 0 ; %% initialize the chain<br /> %%Note that we take steps of 2<br /> %%This is so that we can store the initial result, and the improved result<br /> for i = 2 :2: n; <br /> sig_x1 = sqrt(1/(1+x2(i-1)^2));<br /> mu_x1 = 4/(1+x2(i-1)^2);<br /> x1(i) = normrnd(mu_x1,sig_x1); %% generate from the conditional density <br /> x2(i) = x2(i-1);<br /> <br /> sig_x2 = sqrt(1/(1+x1(i)^2));<br /> mu_x2 = 4/(1+x1(i)^2);<br /> i=i+1;<br /> x2(i) = normrnd(mu_x2, sig_x2); <br /> x1(i) = x1(i-1);<br /> end<br /> scatter(x1(1000:n),(x2(1000:n)),'.'); hold on;<br /> [ x , y ] = meshgrid( -1:.2:7 , -1:0.2:7); <br /> c = 1/202126.335877;<br /> z = c .* exp( -( x .^2+ y .^2+ y .^2 .* x .^2 -8.* x -8.* y ) /2);<br /> contour( x , y , z );<br /> &lt;/pre&gt;<br /> <br /> Result:<br /> <br /> [[File:BivariateGibbsContour.png]]<br /> &lt;/div&gt;<br /> <br /> ==Class 22, Thursday, July 18, 2013==<br /> ===Assignment Hint: Question 2 ===<br /> Matlab Code &lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> syms x<br /> syms y<br /> ezplot(x^2 +2)<br /> ezsurf(exp(-x given ~ 2 ...)/i) this gives a n dimensional plot <br /> &lt;/pre&gt;<br /> <br /> ezsurf(fun) creates a graph of fun(x,y) using the surf function. fun is plotted over the default domain: -2π &lt; x &lt; 2π, -2π &lt; y &lt; 2π.<br /> http://www.mathworks.com/help/matlab/ref/ezsurf.html&lt;br&gt;<br /> <br /> '''Example:''' ezsurf((x+y)^2+(x-y)^3)&lt;br/&gt;<br /> [[File:Ezplot.jpg|300px]][[File:Ezsurf.jpg|300px]]<br /> <br /> ====Generate CDF of N(0,1) distribution====<br /> Define &lt;math&gt;h (x)&lt;/math&gt; to be an indicator function such that &lt;math&gt;h (X&lt;x)&lt;/math&gt; = 1 and 0 otherwise.&lt;br /&gt;<br /> '''Example:''' Face recognition &lt;br /&gt;<br /> <br /> X is a greyscale image of the person and Y is the person.&lt;br /&gt;<br /> Here,We have a 100 x 100 grid where each cell is a number from 0 to 255 representing the darkness of the cell (from white to black).&lt;br&gt;<br /> Let x be a vector of length 100*100=10,000 and y be a vector with each element being a picture of a person's face.&lt;br&gt;<br /> Compare Pr{x|y} and Pr{y|x}.&lt;br /&gt;<br /> <br /> &lt;br /&gt;[[Frequentist approach]]&lt;br /&gt;<br /> *A frequentist would say X is a random variable and Y is not, so they would use Pr{x|y} (given that y is Tom, how likely is it that x is an image of Tom?).<br /> <br /> &lt;math&gt;\displaystyle P(X|Y)&lt;/math&gt;, y is person and x is how likely the picture is of this person. Here, y is known. &lt;br/&gt; <br /> *Frequentist: probability is objective quantity which is proportional to events. &lt;br&gt;<br /> i.e. Flip a coin many times, half of the time, it will be heads, and the other half it will be tails. (Physics) &lt;br&gt;<br /> <br /> &lt;br/&gt;[[Bayesian approach]] &lt;br/&gt;<br /> A Bayesian would ask, given some image, how likely is it that the person in the image is Tom? They would use P(Y|X).<br /> <br /> &lt;math&gt;P(Y|X) = \frac {P(x|y)P(y)}{\int P(x|y)P(y)dy}&lt;/math&gt; Here, everything is a random variable.&lt;br&gt; <br /> Proof:&lt;br/&gt;<br /> &lt;math&gt;P(y|x)P(x) = P(x,y)= P(x|y)P(y) P(x) = \int P(x|y)P(y)dy&lt;/math&gt;<br /> <br /> *Bayesian: Probability is subjective, which states someone's belief. &lt;br&gt;<br /> i.e. The chance of raining tomorrow is 40%. (A Frequentist would not say this because no one can observe tomorrow a thousand times.)<br /> <br /> ===Generating Normally Distributed Random Number(MATLAB)===<br /> y = randn(m,n) returns &quot;m x n&quot; matrix of random values from standard normal distribution. &lt;br&gt;<br /> y = randn(n) returns &quot;n x n&quot; matrix of random values instead. &lt;br&gt;<br /> '''Note:''' m &amp; n must be positive values; otherwise, negative numbers will be treated as 0. &lt;br&gt;<br /> http://www.mathworks.com/help/matlab/ref/randn.html &lt;br&gt;<br /> <br /> '''Matlab'''&lt;br/&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> y = randn(2,4)<br /> ans =<br /> <br /> 0.5377 -2.2588 0.3188 -0.4336<br /> 1.8339 0.8622 -1.3077 0.3426<br /> &lt;/pre&gt;<br /> &lt;br&gt;<br /> <br /> ===Randsample (MATLAB)===<br /> y = randsample(n,k,true,w) or y = randsample(population,k,true,w) returns a weighted sample which is taken with replacement, using a vector of positive weights w with length n. The probability that the integer i is selected for an entry of y is w(i)/sum(w). Usually, w is a vector of probabilities. randsample does not support weighted sampling without replacement.<br /> http://www.mathworks.com/help/stats/randsample.html<br /> <br /> Matlab: &lt;br/&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> y = randsample(8,1,true,w)<br /> &gt;&gt; [1 3 5 2 8 7 4 6]<br /> &lt;/pre&gt;<br /> &lt;br&gt;<br /> <br /> ===Variance reduction===<br /> &lt;br /&gt;<br /> '''Definition'''&lt;br/&gt;<br /> *Variance reduction is a procedure used to increase the precision of the estimates that can be obtained for a given number of iterations. Every output random variable from the simulation is associated with a variance which limits the precision of the simulation results. &lt;br/&gt;<br /> *In order to make a simulation statistically efficient,(i.e. to obtain a greater precision and smaller confidence intervals for the output random variable of interest),variance reduction techniques can be used. The main ones are: Common random numbers, antithetic variates, control variates, importance sampling and stratified sampling), We will only be learning one of the methods - importance sampling. http://en.wikipedia.org/wiki/Variance_reduction<br /> *&lt;br /&gt;It can be seen that the integral &lt;math&gt;\displaystyle\int \frac{h(x)f(x)}{g(x)}g(x)\,dx = \int \frac{f(x)}{g(x)}h(x) g(x)\,dx&lt;/math&gt; is just &lt;math&gt; \displaystyle E_g(h(x)) \rightarrow&lt;/math&gt;the expectation of h(x) with respect to g(x), where &lt;math&gt;\displaystyle \frac{f(x)}{g(x)} &lt;/math&gt; is a weight &lt;math&gt;\displaystyle\beta(x)&lt;/math&gt;. In the case where &lt;math&gt;\displaystyle f &gt; g&lt;/math&gt;, a greater weight for &lt;math&gt;\displaystyle\beta(x)&lt;/math&gt; will be assigned. &lt;br /&gt;&lt;ref&gt;<br /> http://wikicoursenote.com/wiki/Stat341#Importance_Sampling_2<br /> &lt;/ref&gt;<br /> *Variance reduction uses the fact that the variance of a finite integral is zero. &lt;br/&gt;<br /> <br /> We wish to use simulation for this algorithm. We can utilize Monte Carlo Integration framework from previous classes.<br /> &lt;math&gt;E_f [h(x)] = \int h(x)f(x) dx&lt;/math&gt;. The motivation is that a lot of integrals need to be calculated. &lt;br/&gt;&lt;br/&gt;<br /> '''Some addition knowledge:''' &lt;br/&gt;<br /> Common Random Numbers: The common random numbers variance reduction technique is a popular and useful variance reduction technique which applies when we are comparing two or more alternative configurations (of a system) instead of investigating a single configuration. &lt;br/&gt; &lt;br/&gt;<br /> '''Case 1 Basic Monte Carlo Integration''' &lt;br/&gt;<br /> '''Idea:'''Evaluating an integral means calculating the area under the desired curve f(x).&lt;br/&gt;<br /> The Monte Carlo Integration method evaluates the area under the curve by computing the area randomly many times and then take average of the results. &lt;ref&gt;<br /> http://www.cs.dartmouth.edu/~fabio/teaching/graphics08/lectures/15_MonteCarloIntegration_Web.pdf<br /> &lt;/ref&gt;<br /> &lt;br /&gt; <br /> <br /> '''Detailed Explanation:'''<br /> &lt;br /&gt;The original '''Monte Carlo''' approach was a method developed by physicists to use random number generation to compute integrals. Suppose we wish to compute a complex integral&lt;br /&gt;<br /> &lt;math&gt;\int_a ^b h(x)dx&lt;/math&gt;&lt;br /&gt;<br /> If we can decompose h(x) into the product of a function f(x) and a probability density function p(x)defined over the interval (a,b), then we note that&lt;br /&gt;<br /> &lt;math&gt;\int_a ^b h(x)dx=\int_a ^b f(x)p(x)dx=E_p(x)[f(x)]&lt;/math&gt;&lt;br /&gt;<br /> so that the integral can be expressed as an expectation of f(x) over the density p(x). Thus, if we draw a large number x1, x2,..,xn of random variables from the density p(x), then&lt;br /&gt;<br /> &lt;math&gt;\int_a ^b h(x)dx=E_p(x)[f(x)]= \frac{1}{n} \sum_{i = 1}^{n} f({x_{i})}&lt;/math&gt;&lt;br /&gt;<br /> This is referred to as '''Monte Carlo Integration.''' http://web.mit.edu/~wingated/www/introductions/mcmc-gibbs-intro.pdf<br /> <br /> Suppose we have an integral of this form&lt;br /&gt;<br /> &lt;math&gt;I = \int_a ^b h(x)dx =\int_a ^b h(x) (\frac {b-a}{b-a} )dx =\int_a ^b h(x)(b-a) (\frac {1}{b-a} )dx =\int_a ^b w(x) f(x)dx &lt;/math&gt;&lt;br /&gt;<br /> <br /> where &lt;math&gt;w(x) = h(x)(b-a)&lt;/math&gt; , and &lt;math&gt;f(x) =(\frac {1}{b-a})&lt;/math&gt;&lt;br/&gt;<br /> <br /> '''Note:''' &lt;math&gt;f(x)&lt;/math&gt; is the pdf of a uniform distribution &lt;math&gt;~\sim U(a, b)&lt;/math&gt;.<br /> &lt;br/&gt;<br /> <br /> Therefore, we can estimate I by&lt;br/&gt;<br /> &lt;math&gt;\widehat{I} = \frac{1}{n} \cdot \sum_{i = 1}^{n} w({x_{i})}&lt;/math&gt; where &lt;math&gt; x_{i} ~\sim UNIF[a,b]&lt;/math&gt;&lt;br /&gt;<br /> <br /> As n approaches infinity,&lt;math&gt; \widehat{I}&lt;/math&gt; approches I <br /> <br /> This idea is illustrated as follows:&lt;br/&gt;<br /> [[File:MCintegration.jpg]]&lt;br/&gt;<br /> <br /> In the illustration, since we are using uniform distribution as our p(x), we have p(x&lt;sub&gt;i&lt;/sub&gt;)=&lt;math&gt;1/(b-a)&lt;/math&gt;.&lt;br/&gt;<br /> <br /> <br /> '''Example'''&lt;br /&gt;<br /> &lt;math&gt;I = \int_0 ^1 x^4 dx&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;I = (\frac {x^5}{5})\bigg|_0 ^1 = \frac{1}{5} - \frac{0}{5} =\frac{1}{5}&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;\widehat{I} = \frac{1}{n} \cdot \sum_{i = 1}^{n} w({x_{i})}&lt;/math&gt; where &lt;math&gt; x_{i} ~\sim UNIF[0,1]&lt;/math&gt;&lt;br /&gt;<br /> '''In this question, &lt;math&gt; w(x) = h(x)(b-a) = x^4(1-0) = x^4 &lt;/math&gt;&lt;br /&gt;'''<br /> <br /> Matlab Code:&lt;br/&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt; n = 1000;<br /> &gt;&gt; x = rand(1, n);<br /> &gt;&gt; w = x.^4;<br /> &gt;&gt; sum (w)/n<br /> <br /> ans = <br /> <br /> 0.2051<br /> &lt;/pre&gt;<br /> &lt;br /&gt;<br /> '''Example'''&lt;br /&gt;<br /> &lt;math&gt;I = \int_2 ^4 \frac{\sin(x)}{x} dx = \int_2 ^4 \frac{\sin(x)}{x} \frac{(4-2)}{(4-2)} dx &lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;\hat{I} = \frac{1}{n} \sum_{i = 1}^{n} w({x_{i})} = \frac{1}{n} \sum_{i = 1}^{n} 2 \frac{\sin(x_i)}{x_i} &lt;/math&gt; where &lt;math&gt; x_i ~\sim UNIF[2,4]&lt;/math&gt;&lt;br/&gt;<br /> <br /> Matlab Code:&lt;br/&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt; n = 1000;<br /> &gt;&gt; for i=1:n;<br /> x = 2*rand + 2; %xi~Unif(2,4) <br /> w(i) = 2*sin(x)/x<br /> end;<br /> &gt;&gt; sum(w)/n<br /> <br /> ans =<br /> <br /> 0.1382<br /> &lt;/pre&gt;<br /> &lt;br /&gt;<br /> <br /> '''Example'''&lt;br /&gt;<br /> Consider &lt;math&gt;I = \int_0 ^1 x^2+2x dx&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;\hat{I} = \frac{1}{n} \sum_{i = 1}^{n} w({x_{i})} = \frac{1}{n} \sum_{i = 1}^{n} (x_{i})^2+2(x_{i}) &lt;/math&gt; where &lt;math&gt; x_i ~\sim UNIF[0,1]&lt;/math&gt;&lt;br/&gt;<br /> It evaluates to 4/3, now to simulate this, here is the code:<br /> <br /> Matlab Code:&lt;br/&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt; n = 1000;<br /> &gt;&gt; x = rand(1, n);<br /> &gt;&gt; w = x.^2+2*x;<br /> &gt;&gt; sum (w)/n<br /> <br /> ans =<br /> <br /> 1.3717<br /> &lt;/pre&gt;<br /> Note: when n is larger then your answer will be more precise<br /> &lt;br/&gt;<br /> <br /> '''Example'''&lt;br /&gt;<br /> Consider &lt;math&gt;I = \int_0 ^1 e^x dx&lt;/math&gt;&lt;br /&gt;<br /> <br /> The exact answer is e - 1. <br /> Comparing to the simulation, the matlab code is as follows:<br /> <br /> Matlab Code:&lt;br/&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt; n = 100000;<br /> &gt;&gt; x = rand(1, n);<br /> &gt;&gt; w = exp(x);<br /> &gt;&gt; sum (w)/n<br /> <br /> ans =<br /> <br /> 1.7178<br /> &lt;/pre&gt;<br /> The answer 1.7178 is really close enough to the exact answer e - 1 = 1.71828182846. The accuracy will increase if n is larger, for example n=100000000.<br /> &lt;br/&gt;<br /> <br /> <br /> '''Multiple Variables Example'''&lt;br /&gt;<br /> Consider &lt;math&gt;I = \iint e^(x+y) dx&lt;/math&gt;&lt;br /&gt;<br /> <br /> The exact answer is (e - 1)^2. <br /> The matlab code is similar to the above example, with an additional variable:<br /> <br /> Matlab Code:&lt;br/&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt; n = 100000;<br /> &gt;&gt; x = rand(1, n);<br /> &gt;&gt; y = rand(1, n);<br /> &gt;&gt; w = exp(x+y);<br /> &gt;&gt; sum (w)/n<br /> <br /> ans =<br /> <br /> 2.9438<br /> &lt;/pre&gt;<br /> Note that this is close to the exact answer (e - 1)^2 = 2.95249.<br /> &lt;br/&gt;<br /> <br /> <br /> '''Case 2'''&lt;br /&gt;<br /> We can generalize this idea. Suppose we wish to compute <br /> &lt;math&gt;I = \int h(x)f(x)dx &lt;/math&gt;&lt;br /&gt;<br /> If f(x) is uniform, this will be same as case 1 for general f&lt;br /&gt;<br /> &lt;math&gt;\hat{I} = \frac {1}{n} \sum_{i=1}^{n}h(x_i)&lt;/math&gt;&lt;br /&gt;<br /> x&lt;sub&gt;i&lt;/sub&gt; ~ f(x)<br /> <br /> '''Note:''' &lt;math&gt;\hat{I}&lt;/math&gt; as n approaches infinity -&gt; &lt;math&gt;I&lt;/math&gt;&lt;br/&gt;<br /> &lt;br/&gt;<br /> <br /> '''Example'''. Find &lt;math&gt;E[\sqrt{x}]&lt;/math&gt; for &lt;math&gt;f = e^{-x}&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;I = E[\sqrt{x}] = \int \sqrt{x}e^{-x}dx&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;\widehat{I} = \frac{1}{n} \sum_{i = 1}^{n} \sqrt{x_{i}}&lt;/math&gt; where &lt;math&gt;x_{i} ~\sim f(x)&lt;/math&gt;<br /> <br /> Matlab Code:&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> n = 1000;<br /> u = rand(1,n);<br /> x = -log(u) %% Inverse transform method for generating exp(1);<br /> sum(sqrt(x))/n;<br /> &lt;/pre&gt;<br /> &lt;br/&gt;<br /> <br /> '''Tips:''' &lt;br /&gt;<br /> It is important to know when Case 2 is appropriate to be used when evaluating a integral using simulation. Normally case 2 can be distinguished from case 1 if the bounds of the integral are improper i.e either the lower, upper or both the bounds approach infinity. &lt;br /&gt;<br /> Now, when it is identified that Case 2 should be used, understand that f(x) must be a pdf. That is integral of f(x) should equal 1, when evaluating along the bounds of the integral. If this is not true we cannot use the summation formula and need to modify the integral to make sure we have a pdf inside the integral. &lt;br /&gt;<br /> <br /> '''Example'''. Use simulation to approximate the following integral &lt;math&gt; \int_{-2}^{2} e^{x+x^2}dx &lt;/math&gt;. The exact value for this integral is around 93.163.&lt;br/&gt;<br /> '''Solution'''&lt;br/&gt;<br /> &lt;math&gt; I = 4E[e^{x+x^2}] = 4 \int_{-2}^{2} \frac{1}{4}e^{x+x^2}dx&lt;/math&gt; where &lt;math&gt;x~\sim U[-2,2]&lt;/math&gt; &lt;br/&gt;<br /> &lt;math&gt;\widehat{I} = \frac{1}{n} \sum_{i = 1}^{n} e^{x_i+x_i^2}&lt;/math&gt; where &lt;math&gt;x_i~\sim U[-2,2]&lt;/math&gt;<br /> <br /> Matlab Code:&lt;br/&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> close all<br /> clear all<br /> n=10000;<br /> u=rand(1,n);<br /> %xi~U[-2,2]<br /> x=4*u-2;<br /> s=exp(x+x.^2);<br /> 4*sum(s)/n<br /> &gt;&gt;93.2680<br /> &lt;/pre&gt;<br /> &lt;br/&gt;<br /> <br /> '''Example'''&lt;br /&gt;<br /> Let &lt;math&gt;f\left( x\right) =\dfrac {1} {\sqrt {2\pi }}e^{-\dfrac {x^{2}} {2}}&lt;/math&gt;&lt;br/&gt;<br /> Compute cdf at point x=2.&lt;br/&gt;&lt;br/&gt;<br /> By definition, &lt;math&gt;F(x)=\int^2_{-\infty} \dfrac {1} {\sqrt {2\pi }}e^{-\dfrac {x^{2}} {2}} dx&lt;/math&gt; &lt;br/&gt;<br /> We only have two methods for simulating integration, one is a definite integral assuming f is uniform, and the other an indefinite integral for any f. Since we are already given the pdf, we have to use the second method. However, since we currently have a definite integral, we must define h(x) as an indicator function to obtain an indefinite integral (thereby allowing us to use the second method).&lt;br/&gt;<br /> If<br /> :&lt;math&gt;<br /> h(x) = \begin{cases}<br /> 1, &amp; \text{if } x &lt; 2 \\<br /> 0, &amp; \text{if } x \geq 2<br /> \end{cases}&lt;/math&gt;<br /> then &lt;math&gt;I=\int h\left( x\right) f\left( x\right) dx=\int^2_{-\infty} f(x)\,dx &lt;/math&gt;&lt;br/&gt;<br /> Now use &lt;math&gt;\widehat{I} = \frac{1}{n} \sum_{i = 1}^{n} h({x_{i})}&lt;/math&gt; where &lt;math&gt;x_{i} ~\sim f(x)&lt;/math&gt;.<br /> This gives &lt;math&gt;\widehat{I}= \frac{\text{number of samples }&lt; 2}{n}&lt;/math&gt;<br /> &lt;br /&gt;<br /> Matlab Code:&lt;br/&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> n = 1000<br /> x = randn(1,n)<br /> sum(x&lt;2)/n<br /> &lt;/pre&gt;<br /> Similarly, cdf at point &lt;math&gt;x=0&lt;/math&gt; is &lt;math&gt;\frac{1}{2}&lt;/math&gt;.&lt;br/&gt;<br /> '''Note:'''If we want to compute cdf when x has a small value, for example -3, the probability that h(x) equals 1 is small, &lt;br/&gt;<br /> so the variance can be large. As x gets smaller, we can increase the sample size to make our simulation more accurate.&lt;br/&gt;<br /> <br /> special example from:http://www.math.wsu.edu/faculty/genz/416/lect/l08-6.pdf<br /> <br /> https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-ash4/q71/s720x720/999682_413300238783048_1464937675_n.jpg<br /> <br /> '''What is the variance of estimation?'''&lt;br\&gt;<br /> &lt;math&gt;\begin{align}<br /> &amp; Var(x)= E(x^2)-[E(x)]^2 \\<br /> &amp; =E(w^2)-[E(w)]^2 \\<br /> \end{align}&lt;/math&gt;&lt;br\&gt;<br /> Suppose that f(x) is the function that we want to estimate and &lt;math&gt;\widehat{f(x)} = \frac{1}{n} \sum_{i = 1}^{n} w(x_i)&lt;/math&gt;&lt;br\&gt;<br /> The range for f(x) is from 0 to &lt;math&gt;\infty&lt;/math&gt; (e.g. if we take &lt;math&gt;x_i&lt;/math&gt;=-N to N where i from 1 to 2N)&lt;br\&gt;<br /> The variance of our estimate would be:&lt;br\&gt;<br /> &lt;math&gt;\begin{align}<br /> &amp; Var(f)= E(w^2)-[E(w)]^2 \\<br /> &amp; = \sum_{i = 1}^{2N} x_i^2*\widehat{f(x_i)} - (\sum_{i = 1}^{2N} x_i*\widehat{f(x_i)})^2 \\<br /> \end{align}&lt;/math&gt;&lt;br\&gt;<br /> <br /> == Class 23, Tuesday July 23 ==<br /> ===Importance Sampling===<br /> Start with<br /> &lt;math&gt;I = \int^{b}_{a} f(x)\,dx &lt;/math&gt;&lt;br/&gt; = &lt;math&gt;\int f(x)*(b-a) * \frac{1}{(b-a)}\,dx &lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;\widehat{I} = \frac{1}{n} \sum_{i = 1}^{n} w({x_{i})}&lt;/math&gt; where &lt;math&gt;w_{i} ~\sim Unif(a,b)&lt;/math&gt; <br /> <br /> Recall the definition of crude Monte Carlo Integration: &lt;br/&gt;<br /> &lt;math&gt;E[h(X)]=\int f(x)h(x)\,dx&lt;/math&gt;&lt;br/&gt;<br /> If &lt;math&gt;x~\sim U(0,1)&lt;/math&gt; and hence &lt;math&gt;\,f(x)=1&lt;/math&gt;, then we have the basis of other variance reduction techniques. Now we consider what happens if X is not uniformly distributed.<br /> <br /> In the control variate case, we change the formula b adding and subtracting a known function h(x): basically, by adding zero to the integral, retaining the unbiasedness and allowing us to solve it more easily. In importance sampling, we will instead multiply by 1. The known function in this case will be g(x), which is selected under a few assumptions.<br /> <br /> There are cases where another distribution gives a better fit to integral to approximate, and results in a more accurate estimate; importance sampling is useful here.<br /> Motivation:&lt;br/&gt;<br /> - Consider &lt;math&gt;I = \int h(x)f(x)\,dx &lt;/math&gt;&lt;br/&gt;<br /> - There are cases in which we do not know how to sample from f(x) because the distribution of f(x) is complex; or it's very difficult to sample from f.&lt;br/&gt;<br /> - There are cases in which h(x) is a rare event with respect to f.&lt;br/&gt;<br /> Importance sampling is useful to overcome these cases.&lt;br/&gt;<br /> - rare event is the event when you sample from its distribution, you rarely get an satisfied sample.&lt;br/&gt;<br /> &lt;br/&gt;<br /> *Importance sampling can solve the cases listed above. It makes use of some functions that are easier to sample from. &lt;br/&gt;<br /> *Importance sampling is a variance reduction technique that can be used in the Monte Carlo method. Although it is not exactly like a Markov Chain Monte Carlo (MCMC) algorithm, it also approximately samples a vector where the mass function is specified up to some constant.&lt;br/&gt;<br /> *The idea behind importance sampling is that, certain values of the input random variables in a simulation have more impact on the parameter being estimated than the others. If these &quot;important&quot; values are emphasized by being sampled more frequently, then the estimator variance can be reduced.&lt;br/&gt; <br /> *Hence, the basic methodology in importance sampling is to choose a distribution which &quot;encourages&quot; the important values. This use of &quot;biased&quot; distributions will result in a biased estimator if it is applied directly in the simulation. (http://en.wikipedia.org/wiki/Importance_sampling)&lt;br/&gt;<br /> *However, the simulation outputs are weighted to correct for the use of the biased distribution, and this ensures that the new importance sampling estimator is unbiased. (http://en.wikipedia.org/wiki/Importance_sampling)<br /> <br /> '''Example''':&lt;br/&gt;<br /> <br /> * Bit Error Rate on a channel.&lt;br/&gt;<br /> The bit error rate (BER) is the number of bit errors over the total number of bits during a specific time. BER has no unit associated to it. BER is often written as a percentage. &lt;br/&gt;<br /> * Failure Probability of a reliable system.&lt;br/&gt;<br /> * A well chosen distribution can result in saving huge amount of running-time for importance sampling algorithm. <br /> <br /> Recall &lt;math&gt;I = \int h(x)f(x)\,dx &lt;/math&gt;, where the preceding is an n-dimensional integral over all possible values of x.&lt;br/&gt;<br /> <br /> We have &lt;math&gt;I = \int \frac {h(x)f(x)}{g(x)} g(x)\, dx = \int w(x)g(x)\,dx&lt;/math&gt;, where &lt;math&gt;w(x)= \frac{h(x)f(x)}{g(x)}&lt;/math&gt;, and we know this integral since &lt;math&gt;g(x)&lt;/math&gt; is a known distribution (we can assume &lt;math&gt; g(x)=b-a&lt;/math&gt;) and &lt;math&gt;I&lt;/math&gt; is the expectation of &lt;math&gt; w(x) &lt;/math&gt; with respect to &lt;math&gt; g(x) &lt;/math&gt;, or &lt;math&gt;\hat{I} = \sum_{i=1}^{n} \frac{w(x)}{n} &lt;/math&gt;; &lt;math&gt;x ~\sim g(x)&lt;/math&gt; &lt;br/&gt;<br /> <br /> As n approaches infinity, &lt;math&gt;\hat{I}&lt;/math&gt; approaches &lt;math&gt;{I}&lt;/math&gt;<br /> <br /> '''Note''': &lt;br/&gt;<br /> <br /> Even though the uniform distribution sampling method only works for a definite integral, you can use still uniform distribution sampling method for I in the case of indefinite integral - this can be done by manipulating the function to adjust the integral range, such that the integral becomes definite.<br /> <br /> w(x) is called the Importance Function. &lt;br/&gt;<br /> *A good importance function will be large when the integrand is large and small otherwise.&lt;br/&gt;<br /> <br /> This is the importance sampling estimator of &lt;math&gt;I&lt;/math&gt;, and is unbiased. That is, the estimation procedure is to generate i.i.d. samples from &lt;math&gt;g(x)&lt;/math&gt;, and for each sample which exceeds the upper bound of the integral, the estimate is incremented by the weight W, evaluated at the sample value. The results are averaged over N trials. &lt;br/&gt;<br /> http://en.wikipedia.org/wiki/Importance_sampling &lt;br/&gt;<br /> <br /> Choosing a good fit biased distribution is the key of importance sampling.&lt;br/&gt; <br /> Note that &lt;math&gt; g(x) &lt;/math&gt; is selected under the following assumptions:&lt;br/&gt;<br /> 1. &lt;math&gt; g(x) &lt;/math&gt; (or at least a constant times &lt;math&gt; g(x) &lt;/math&gt;) is a pdf.&lt;br/&gt;<br /> 2. We have a way to generate from &lt;math&gt; g(x) &lt;/math&gt; (known function that we know how to generate using software). &lt;br/&gt;<br /> 3. &lt;math&gt; \frac{h(x)f(x)}{g(x)}&lt;/math&gt; ~ constant =&gt; hence small variability &lt;br/&gt;<br /> 4. g(x) should not be 0 at the same time as f(x) &quot;too often&quot; (From Stat340w13 and Course Note Material) &lt;br/&gt;<br /> 5. g(x) is another density function whose support is the same as that of f(x) &lt;br/&gt;<br /> 6. g(x) should have thicker tails compare to f to ensure f(x)/g(x) is reasonably small. &lt;br/&gt;<br /> 7. g(x) should have a similar shape to f(x) in general. &lt;br/&gt; <br /> <br /> <br /> <br /> '''Example 1:'''&lt;br/&gt;<br /> &lt;math&gt;I=\int^{-1}_{-\infty} f(x)\,dx&lt;/math&gt;, where &lt;math&gt;\displaystyle f(x) \sim N(0,1)&lt;/math&gt;&lt;br/&gt;<br /> Define<br /> &lt;math&gt;<br /> h(x) = \begin{cases}<br /> <br /> 1, &amp; \text{if } x \leq -1 \\<br /> 0, &amp; \text{if } x &gt; -1<br /> \end{cases}&lt;/math&gt;&lt;br/&gt;<br /> <br /> then,&lt;math&gt;I=\int h(x)*f(x)\,dx&lt;/math&gt;.&lt;br/&gt;<br /> <br /> Therefore,&lt;math&gt;\widehat{I} = \frac{1}{n} \sum_{i = 1}^{n} h({x_{i})}&lt;/math&gt; where &lt;math&gt;x_{i} ~\sim N(0,1)&lt;/math&gt; <br /> which gives &lt;math&gt;\widehat{I}= \frac{\text{number of observations }&lt;= -1}{n}&lt;/math&gt;&lt;br/&gt;<br /> &lt;br&gt; <br /> <br /> '''Note''': &lt;br/&gt;<br /> h(x) is acting as an indicator variable which follows a Bernoulli distribution with p = P(x&lt;=-1).&lt;br/&gt;<br /> h(x) is used to count the points greater than -1.<br /> <br /> <br /> <br /> <br /> '''Consider &lt;math&gt;I= \int h(x)f(x)\,dx &lt;/math&gt; again.''' &lt;br/&gt;<br /> <br /> Importance sampling is used to overcome the following two cases: &lt;br/&gt;<br /> *cases we don't know how to sample from f(x), because f(x) is a complicated distribution. &lt;br/&gt;<br /> *cases in which h(x) corresponds to a rare event over f (e.g. less than -3 in a standard normal distribution). &lt;br/&gt;<br /> ---- In the second case, using the basic method without importance sampling will result in high variability in the simulated results (which goes against the purpose of variance reduction) &lt;br/&gt;<br /> <br /> <br /> <br /> &lt;math&gt;\begin{align}<br /> I &amp;= \int h(x)f(x)dx \\<br /> <br /> &amp;= \int h(x)f(x) \frac{g(x)}{g(x)} dx, \text{ where g(x) is a pdf easy to sample from and f(x) is not.} \\<br /> <br /> &amp;= \int \frac{h(x)f(x)}{g(x)} g(x) dx \\<br /> <br /> &amp;= \int w(x)g(x) dx \text{ where } w(x) = \frac{h(x)f(x)}{g(x)}<br /> <br /> \end{align}&lt;/math&gt;<br /> <br /> So &lt;math&gt;\hat{I} = \frac{1}{n} \sum_{i=1}^{n}w(x_i), x_i &lt;/math&gt; from &lt;math&gt;g(x)&lt;/math&gt;&lt;br /&gt;<br /> <br /> One can think of &lt;math&gt;\frac{f(x)h(x)}{g(x)}&lt;/math&gt; as weights. We sample from &lt;math&gt;g(x)&lt;/math&gt;, and then re-weight our samples based on their importance.<br /> <br /> Note that &lt;math&gt;\hat{I}&lt;/math&gt; is an unbiased estimator for &lt;math&gt;I&lt;/math&gt; as &lt;math&gt;\ E_x(\hat{I}) = E_x(\frac{1}{n} \sum_{i = 1}^{n} w(X_i)) = \frac{1}{n} \sum_{i = 1}^{n} E_x(\frac{h(X_i)f(X_i)}{g(X_i)}) = \frac{1}{n} \sum_{i = 1}^{n} \int \frac{h(x)f(x)}{g(x)}g(x)dx = \frac{1}{n} \sum_{i = 1}^{n} I = I&lt;/math&gt;<br /> <br /> ''''''[[Problem:]]''''''The variance of &lt;math&gt; \widehat{I}&lt;/math&gt; could be very large with bad choice of g. &lt;br/&gt;<br /> <br /> '''Advice 1''': &lt;br/&gt;<br /> Choose g such that g has thicker tails compare to f. &lt;br/&gt;<br /> In general, if over a set A, g is small but f is large, then f(x)/g(x) could be large. ie: the variance could be large. (the values for which h(x) is exceedingly small) &lt;br/&gt;<br /> <br /> '''Advice 2'''：<br /> Choose g to have similar shape with f. &lt;br/&gt;<br /> In general, it is better to choose g such that it is similar to f in terms of shape, but has thicker tails. &lt;br/&gt;<br /> <br /> &lt;br&gt;&lt;br&gt;<br /> <br /> &lt;b&gt;'''Procedure'''&lt;/b&gt;&lt;br&gt;<br /> <br /> 1. Sample &lt;math&gt; x_{1}, x_{2}, ..., x_{n} ~\sim g(x) &lt;/math&gt; &lt;br /&gt;&lt;br /&gt;<br /> 2. &lt;math&gt;\widehat{I} = \frac{1}{n} \sum_{i = 1}^{n} w({x_{i})}&lt;/math&gt; where &lt;math&gt; w(x_i) = \frac{h(x_i)f(x_i)}{g(x_i)} &lt;/math&gt; for &lt;math&gt;i=1\dots n&lt;/math&gt;&lt;br /&gt;<br /> <br /> <br /> <br /> <br /> '''Example 2'''<br /> <br /> &lt;math&gt;I=\int^{-3}_{-\infty} f(x)\,dx =\int^{\infty}_{-\infty} h\left( x\right) f\left( x\right) dx &lt;/math&gt;&lt;br/&gt;<br /> where <br /> &lt;math&gt;<br /> h(x) = \begin{cases}<br /> 1, &amp; \text{if } x &lt; -3 \\<br /> 0, &amp; \text{if } x &gt; -3<br /> \end{cases}&lt;/math&gt;<br /> <br /> Now we have to compute the expectation of h which is:<br /> <br /> &lt;math&gt;\widehat{I} = \frac{1}{n} \sum_{i = 1}^{n} h({x_{i})}&lt;/math&gt; where &lt;math&gt;x_{i} ~\sim N(0,1)&lt;/math&gt; <br /> which gives &lt;math&gt;\widehat{I}= \frac{\text{number of observations }&lt; -3}{n}&lt;/math&gt;&lt;br/&gt;<br /> <br /> <br /> <br /> '''Matlab Code:''' &lt;br/&gt;<br /> <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> n = 200;<br /> x = randn(1,n);<br /> I= sum(x&gt;3)./n;<br /> <br /> &gt;&gt; mean(I)<br /> &gt;&gt; var(I) % to calculate the variance of the estimates<br /> &lt;/pre&gt;<br /> &lt;br&gt;<br /> '''Comments on Example 2''':&lt;br/&gt;<br /> <br /> *Since observations less than -3 are a relatively rare event, this method will give us a relatively high variance. &lt;br/&gt;<br /> *To illustrate this, suppose we sample 100 points each time for many times, we will be getting mostly 0's and some 1's and occasionally 2's. This data has large variances.&lt;br/&gt;<br /> <br /> '''Note''' ： h(x) is counting the number of observations that are less than -3.<br /> <br /> <br /> <br /> '''Remarks'''：<br /> <br /> *We can actually compute the form of &lt;math&gt;\displaystyle g(x)&lt;/math&gt; to have optimal variance. &lt;br&gt;Mathematically, it is to find &lt;math&gt;\displaystyle g(x)&lt;/math&gt; subject to &lt;math&gt;\displaystyle \min_g [\ E_g([y(x)]^2) - (E_g[y(x)])^2\ ]&lt;/math&gt;&lt;br&gt;<br /> <br /> It can be shown that the optimal &lt;math&gt;\displaystyle g(x)&lt;/math&gt; is &lt;math&gt;\displaystyle {|h(x)|f(x)}&lt;/math&gt;. Using the optimal &lt;math&gt;\displaystyle g(x)&lt;/math&gt; will minimize the variance of estimation in Importance Sampling. This is of theoretical interest but not useful in practice. As we can see, if we can actually show the expression of g(x), we must first have the value of the integration---which is what we want in the first place. &lt;br/&gt;<br /> <br /> In practice, we shall choose &lt;math&gt;\displaystyle g(x)&lt;/math&gt; which has similar shape as &lt;math&gt;\displaystyle f(x)&lt;/math&gt; but with a thicker tail than &lt;math&gt;\displaystyle f(x)&lt;/math&gt; in order to avoid the problem mentioned above.&lt;br&gt;<br /> <br /> The case when &lt;math&gt; g(x) &lt;/math&gt; is important it should have the same support. If &lt;math&gt; g(x) &lt;/math&gt; does not have the same support then it may not be able to sample from &lt;math&gt; f &lt;/math&gt; like before. Also, if &lt;math&gt; g(x) &lt;/math&gt; is not a good choice then it increases the variance very badly. &lt;br/&gt;<br /> <br /> <br /> '''Note:''' <br /> Normalized imporatance sampling is biased, but it is asymptotically unbiased.&lt;br/&gt;<br /> <br /> &lt;math&gt;I=\int h(x)f(x)dx&lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt;I=\int \frac{h(x)f(x)}{g(x)} g(x) dx &lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;I = \frac{1}{n} \sum_{i = 1}^{n} h({x_{i})b_i}&lt;/math&gt;<br /> <br /> and the second I, &lt;br&gt;<br /> <br /> &lt;math&gt;I=\int \frac{h(x)f(x)dx}{\int f(s)ds}&lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt;I = \frac{1}{n} \sum_{i = 1}^{n} h({x_{i}){b_i}^{*}}&lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt;{b_i}^{*}= \frac {b_i}{\sum_{i = 1}^{n} b_i}&lt;/math&gt;<br /> <br /> [[File:IMP ex part 1.png|600px]] <br /> [[File:IMP ex part 2.png|600px]] &lt;br \&gt;<br /> Source: STAT 340 Spring 2010 Course Notes &lt;br&gt;<br /> <br /> <br /> <br /> '''Example:'''&lt;br&gt;<br /> Suppose &lt;math&gt;I=\int^{\infty}_{0} \frac{1}{(1+x)^2} dx&lt;/math&gt; &lt;br&gt;<br /> Since the range is from 0 to &lt;math&gt;\infty&lt;/math&gt; here, we can use &lt;math&gt; g(x) = e^{-x} &lt;/math&gt; ; x&gt;0&lt;br&gt;<br /> So &lt;math&gt;I=\int^{\infty}_{0} w(x)g(x) dx&lt;/math&gt; where &lt;math&gt;w(x) = \frac{f(x)}{g(x)} = \frac{e^x}{(1+x)^2}&lt;/math&gt;&lt;br&gt;<br /> <br /> '''Algorithm:'''&lt;br&gt;<br /> 1) Generate n number of U&lt;sub&gt;i&lt;/sub&gt;~U(0,1)&lt;br&gt;<br /> 2) Set &lt;math&gt;X_i=-log(1-U_i) &lt;/math&gt; for i=1,...,n&lt;br&gt;<br /> 3) Set &lt;math&gt;W(X_i)= \frac{e^{X_i}}{(1+X_i)^2}&lt;/math&gt;&lt;br&gt;<br /> 4) &lt;math&gt;\widehat{I} = \frac{1}{n} \sum_{i = 1}^{n} W({X_i)}&lt;/math&gt;&lt;br&gt;<br /> Actual value of the integral is 1 &lt;br&gt;<br /> <br /> '''Matlab Code''':&lt;br&gt;<br /> <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt; clear all<br /> &gt;&gt; close all<br /> &gt;&gt; n=1000;<br /> &gt;&gt; u=rand(1,n);<br /> &gt;&gt; x=-log(u); % Generates number from exponential distribution using inverse transformation method <br /> &gt;&gt; w=(1./(1+x).^2).*exp(x);<br /> &gt;&gt; sum(w)/n<br /> ans = 0.8884<br /> <br /> Similarly for n=1000000, we get 0.9376 which is even closer to 1.<br /> &lt;/pre&gt;<br /> <br /> '''Another Method'''&lt;br /&gt;<br /> <br /> By changing the variable so that the bounds is (0,1), we can apply the Unif(0,1) method: &lt;br /&gt;<br /> <br /> Let &lt;math&gt;y= \frac{1}{x+1}, dy= \frac{-1}{(x+1)^2}dx =-y^2dx&lt;/math&gt;&lt;br /&gt;<br /> <br /> We can express the integral as &lt;br /&gt;<br /> &lt;math&gt;\int^{1}_{0} \frac {1}{y^2} y^2 dy =\int^{1}_{0} 1 dy &lt;/math&gt;&lt;br /&gt;<br /> which we recognise that it is just a &lt;math&gt;Unif(0,1)&lt;/math&gt; and the result follows. &lt;br /&gt;<br /> &lt;br /&gt;<br /> '''The following are general forms for the change of variable method for different cases'''<br /> :&lt;math&gt;<br /> \int_a^{+\infty}f(x) \, dx =\int_0^1 f\left(a + \frac{u}{1-u}\right) \frac{du}{(1-u)^2} &lt;/math&gt;<br /> <br /> :&lt;math&gt;<br /> \int_{-\infty}^a f(x) \, dx = \int_0^1 f\left(a - \frac{1-u}{u}\right) \frac{du}{u^2}&lt;/math&gt;<br /> <br /> :&lt;math&gt;<br /> \int_{-\infty}^{+\infty} f(x) \, dx = \int_{-1}^{+1} f\left( \frac{u}{1-u^2} \right) \frac{1+u^2}{(1-u^2)^2} \, du,<br /> &lt;/math&gt;<br /> Source: Wikipedia Numerical Integration<br /> <br /> &lt;math&gt;Insert formula here&lt;/math&gt;===Problem of Importance Sampling===<br /> The variance of &lt;math&gt;\hat{I}&lt;/math&gt; '''could be very large''' (infinitely large) with a bad choice of &lt;math&gt;g&lt;/math&gt; &lt;br&gt;<br /> <br /> &lt;math&gt;\displaystyle Var(w) = E(w^2) - (E(w))^2 &lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt; \begin{align}<br /> E(w^2) &amp;= \int (\frac{h(x)f(x)}{g(x)})^2 g(x) dx , where w = (\frac{h(x)f(x)}{g(x)})\\<br /> &amp;= \int (\frac{h^2(x)f^2(x)}{g^2(x)}) g(x) dx \\<br /> &amp;= \int (\frac{h^2(x)f^2(x)}{g(x)}) dx<br /> \end{align}&lt;/math&gt;&lt;br&gt;<br /> when &lt;math&gt;g(x)&lt;/math&gt; is very small, then &lt;math&gt;E(w^2)&lt;/math&gt; can be (infinitely) large. This leads to &lt;math&gt;Var(x)&lt;/math&gt; being (infinitely) large too.<br /> <br /> Consider the term &lt;math&gt;\frac{f(x)}{g(x)}&lt;/math&gt;.&lt;br&gt;<br /> • If &lt;math&gt;g(x)&lt;/math&gt; has thinner tails compared to &lt;math&gt;f(x)&lt;/math&gt;, then &lt;math&gt;\frac{f(x)}{g(x)}&lt;/math&gt; could be infinitely large.<br /> i.e. &lt;math&gt;E(w^2)&lt;/math&gt; is infinitely large and so is variance.<br /> <br /> &lt;gallery&gt;<br /> Image:Comparetail.png|At the tails, f(x)/g(x) may become very large.<br /> Image:ProblematicShapes.png|f(x) and g(x) should have similar shapes. This scenario could be problematic with importance sampling.<br /> &lt;/gallery&gt;<br /> <br /> <br /> A bad choice for g(x) can cause a problem and a good choice can reduce the variance. Therefore we need to have criteria for choosing good &lt;math&gt;g&lt;/math&gt;: &lt;br&gt;&lt;br&gt;<br /> &lt;b&gt;Advice 1:&lt;/b&gt; Choose &lt;math&gt;g&lt;/math&gt; such that &lt;math&gt;g&lt;/math&gt; has thicker tails compared to &lt;math&gt;f&lt;/math&gt;&lt;br&gt;<br /> - Also, if over a set &lt;math&gt;A&lt;/math&gt;, &lt;math&gt;g&lt;/math&gt; is small but &lt;math&gt;f&lt;/math&gt; is large, then &lt;math&gt;\frac {f(x)}{g(x)}&lt;/math&gt; could be large. (i.e. the variance could be large.)<br /> <br /> &lt;b&gt;Advice 2:&lt;/b&gt; Choose &lt;math&gt;g&lt;/math&gt; to have similar shape with &lt;math&gt;f&lt;/math&gt;&lt;br&gt;<br /> -In general, it is better to choose &lt;math&gt;g&lt;/math&gt; such that it is similar to &lt;math&gt;f&lt;/math&gt; in terms of shape but with thicker tails.<br /> <br /> &lt;div style=&quot;border:5px solid pink;&quot;&gt;<br /> &lt;b&gt;Example List&lt;/b&gt;<br /> Estimate &lt;math&gt;\displaystyle I = Pr(Z&gt;3),\ \text{ where }\ Z \sim N(0,1)&lt;/math&gt;&lt;br&gt;&lt;br&gt;<br /> <br /> '''Note''':&lt;math&gt;\displaystyle Pr(Z&gt;3)=Pr(Z&lt;-3)&lt;/math&gt; due to the symmetric property of normal distribution. The occurrence of Z&gt;3 is a rare event since &lt;math&gt;\displaystyle Pr(Z&gt;3) &lt;/math&gt; is roughly 0.13% (this is obtained from a normal probability table).<br /> stat 231 note.<br /> <br /> <br /> &lt;b&gt;Method 1: Basic Sampling&lt;/b&gt; &lt;br&gt;<br /> <br /> We let &lt;math&gt;\displaystyle f(x) &lt;/math&gt; be the pdf of the standard normal. We want to compute<br /> <br /> &lt;math&gt;I=\int^{\infty}_3 f(x)\,dx =\int_{- \infty}^{\infty} h\left( x\right) f\left( x\right) dx &lt;/math&gt;&lt;br/&gt;<br /> &lt;br /&gt;<br /> where <br /> &lt;math&gt;<br /> h(x) = \begin{cases}<br /> 1, &amp; \text{if } x &gt; 3 \\<br /> 0, &amp; \text{if } x \leq 3<br /> \end{cases}&lt;/math&gt; <br /> &lt;math&gt;\ ,\ f(x) = \frac{1}{\sqrt{2\pi}}e^{-\frac{1}{2}x^2}&lt;/math&gt;&lt;br/&gt;<br /> This gives &lt;math&gt;\widehat{I} = \frac{1}{n} \sum_{i = 1}^{n} h({x_{i})}&lt;/math&gt; where &lt;math&gt;x_{i} ~\sim N(0,1)&lt;/math&gt; <br /> which is equivalent to &lt;math&gt;\widehat{I}= \frac{\text{number of observations }&gt; 3}{n}&lt;/math&gt;&lt;br/&gt;<br /> <br /> <br /> '''Note''' :we could also formulate &lt;math&gt; h(x) &lt;/math&gt; to be the following because the density at a single point (3 in this case) is 0 anyways.<br /> &lt;math&gt;<br /> h(x) = \begin{cases}<br /> 1, &amp; \text{if } x \geq 3 \\<br /> 0, &amp; \text{if } x &lt; 3<br /> \end{cases}&lt;/math&gt;<br /> <br /> MATLAB Code<br /> &lt;pre style=&quot;font-size:14px&quot;&gt;<br /> x=randn(1,100);<br /> sum(x&gt;3)/100;<br /> <br /> clc<br /> clear all<br /> close all<br /> n = 100;<br /> for ii = 1:200;<br /> x = randn(1,n);<br /> I(ii) = sum(x&gt;3)/n %%sums values in x vector greater than 3 and divides by n<br /> end<br /> &lt;/pre&gt;<br /> <br /> '''Note:''' (x&gt;3) is an indicator function in Matlab. &lt;br&gt;<br /> It will provide answers in the form of boolean.&lt;br&gt;<br /> In this example, <br /> We see that if we repeat I(ii) several times, we get 0, 0, 0.01, 0, 0, 0.<br /> This is considered a very good result as 5 out of 6 times you get the actual mean of the distribution.<br /> <br /> &lt;b&gt;Method 2: Importance Sampling&lt;/b&gt; &lt;br&gt;<br /> &lt;math&gt;I=\int h\left( x\right) f\left( x\right) dx= \int \frac{h(x)f(x)}{g(x)} g(x) dx &lt;/math&gt; &lt;br&gt;<br /> where &lt;math&gt;\frac{h(x)f(x)}{g(x)} = w(x)&lt;/math&gt; Choose &lt;math&gt;g(x)&lt;/math&gt; from &lt;math&gt;N(4,1)&lt;/math&gt;&lt;br&gt;<br /> <br /> '''Note''' : &lt;br/&gt;<br /> *&lt;math&gt;N(4,1)&lt;/math&gt; was chosen according to our advice mentioned earlier. &lt;math&gt;N(4,1)&lt;/math&gt; has the same shape (actually the exact same shape) as &lt;math&gt;N(0,1)&lt;/math&gt;. Hence it will not increase the variance of our simulation.&lt;br/&gt;<br /> <br /> *The reason why we do not choose uniform distribution for this case is because the uniform distribution only distributed over a finite interval from a to b, where the required &lt;math&gt;N(4,1)&lt;/math&gt; distributed over all x.&lt;br/&gt;<br /> <br /> *To be more precise, we can see that choosing a distribution centered at 3 or nearby points (i.e. 2 or 4) will help us generate more points which are greater than 3. Thus the size of variance between different samples will be reduced. The reason behind this can be explained as follows: &lt;br/&gt;<br /> Eg: If we take a sample of 1000 points and very few points are above three and we take the sample again, we will have a huge variance as the probability of samples greater than 3 is low. We may even get 0 as our simulated answer as shown in class which is not the case. Thus using this method helps us overcome the problem of sampling from rare events. <br /> <br /> &lt;math&gt;\widehat{I} = \frac{1}{n} \sum_{i = 1}^{n} w({x_{i})}&lt;/math&gt; where &lt;math&gt;x_{i} ~\sim N(4,1)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt; \frac{f(x)}{g(x)}=\dfrac {e^{-\dfrac{x^{2}} {2} }} {e^{-\dfrac{{(x-4)}^{2}}{2} }} =e^{8-4x}&lt;/math&gt;&lt;br&gt;<br /> <br /> This gives &lt;math&gt;\displaystyle w(x)=h(x) e^{8-4x}&lt;/math&gt;&lt;br&gt;<br /> <br /> <br /> '''MATLAB'''<br /> <br /> &lt;pre style=&quot;font-size:14px&quot;&gt;<br /> clear all<br /> close all<br /> clc %% clc clears all input and output from the Command Window display, giving you a &quot;clean screen.&quot;<br /> n=100;<br /> for ii=1:200<br /> x=randn(1,n);<br /> lb(ii)=sum(x&gt;3)/n; %sums values in x vector greater than 3 and divides by n<br /> <br /> x=randn(1,n)+4;<br /> ls(ii)=sum((x&gt;3).*exp(8-4*x))/n; %w(x)/n<br /> end<br /> var(lb)<br /> var(ls)<br /> var(ls)/var(lb)<br /> hist(ls,50)<br /> &lt;/pre&gt;<br /> <br /> [[File:hist(ls).jpg|450px]]<br /> <br /> '''Note:'''<br /> The helper g(x) needs to be a valid pdf.<br /> &lt;br&gt;<br /> <br /> <br /> '''Example3''' &lt;br&gt;<br /> If &lt;math&gt; g(x)=x &lt;/math&gt; for x belongs to &lt;math&gt;[0,1]&lt;/math&gt;, the integral of this function is not 1.&lt;br/&gt;<br /> So, we need to add a constant number to make it a valid pdf.&lt;br/&gt;<br /> Therefore, we '''change it to &lt;math&gt; g(x)=2x &lt;/math&gt; for 0&lt;x&lt;1 '''<br /> <br /> This code is for calculating the variance.&lt;br/&gt;<br /> The first method produced a variance with a power of 10&lt;sup&gt;-5&lt;/sup&gt;, while the second method produced a variance with a power of 10&lt;sup&gt;-8&lt;/sup&gt;. Hence, a clear variance reduction is evident. &lt;br/&gt;<br /> <br /> '''Side Note:''' &lt;br/&gt; <br /> *The most effective variance reduction technique is to '''increase the sample size'''. For instance, in the above example, by using Importance Sampling, we are able to reduce the variance by 3 degrees of power. &lt;br/&gt; <br /> *However if we used Method 1 while increasing the sample size from 200 to 1,000,000 or more, we are able to decrease the variance by 4 or more degrees of power. &lt;br/&gt; <br /> *Also, note that since there is a large variance, it is problematic. So by choosing a different distribution that is not centered around 0, a distribution that is centered at 4 for example would result in less variation. For example, choose &lt;math&gt;\displaystyle g(x)\sim N(4,1)&lt;/math&gt; &lt;br/&gt;<br /> <br /> '''Important Notes on selection of g(x):''' <br /> <br /> *g(x) must have the same support as f(x) in order for accurate sampling&lt;br/&gt;<br /> *g(x) must be such that it encourages the occurrence of rare points (rare h(x))&lt;br/&gt;<br /> *Selection of g(x) greatly affects E[w&lt;sup&gt;2&lt;/sup&gt;] therefore affects the variance. A poor choice of g(x) can cause a significant increase in the variance, thus defeating the purpose of Importance Sampling.&lt;br /&gt;<br /> *Specifically, it is recommended that g(x) have the following properties&lt;ref&gt;<br /> http://ib.berkeley.edu/labs/slatkin/eriq/classes/guest_lect/mc_lecture_notes.pdf<br /> &lt;/ref&gt;<br /> &lt;blockquote&gt;<br /> 1) It is greater than 0 whenever the function in question is not zero.&lt;br /&gt;<br /> 2) It should be close to being proportional to the absolute value of the function in question.&lt;br /&gt;<br /> 3) It should be easy to simulate.&lt;br /&gt;<br /> 4) It should be easy to compute for any realizable x.&lt;br /&gt;<br /> &lt;/blockquote&gt;<br /> <br /> <br /> *Although there are many methods of variance reduction, the best way is to '''increase n'''. The larger n is, the closer your value is to the exact value.<br /> *Using various computer software is the most effective method of reducing a variance.<br /> &lt;/div&gt;<br /> <br /> ==Class 24, Thursday, July 25, 2013==<br /> ===Importance Sampling===<br /> Importance Sampling is the most fundamental variance reduction technique, and often leads to a dramatic variance reduction. &lt;br /&gt; <br /> Importance sampling involves choosing a sampling distribution that favor important samples*.(Simulation and the Monte Carlo Method, Reuven Y. Rubinstein) &lt;br /&gt;<br /> <br /> * Here &quot;favor important samples&quot; means to encourage the occurrence of the desired event or part of the desired event. For instance, if the event of interest is rare (probability of occurrence is close to zero), we &quot;favor important samples&quot; by choosing a sampling distribution such that the event has higher probability of occurrence. <br /> <br /> Definition of importance sampling from Wikipedia:&lt;br&gt;<br /> Importance sampling is a general technique for estimating properties of a particular distribution, while samples are generated from a different distribution other than the distribution of interest. It is related to umbrella sampling in computational physics. Depending on the application, the term may refer to the process of sampling from this alternative distribution, the process of inference, or both.<br /> &lt;br&gt;<br /> Recall that using importance sampling,we have the following:&lt;br/&gt;<br /> <br /> &lt;math&gt;I=\int_{a}^{b}f(x)dx = \int_{a}^{b}f(x)(b-a) \times \frac{1}{b-a}dx&lt;/math&gt; &lt;br /&gt;<br /> <br /> <br /> If g(x) is another probability density function, &lt;br /&gt;<br /> note: in summary, a good importance sampling function g(x) should satisfies:&lt;br /&gt;<br /> <br /> 1. g(x) &gt; 0 whenver f(x)not equal to 0&lt;br /&gt;<br /> 2. g(x) should be equal or close to the absolute value of f(x)&lt;br /&gt;<br /> 3. easy to simulate values from g(x)&lt;br /&gt;<br /> 4. easy to compute the density of g(x)&lt;br /&gt;<br /> <br /> original source is here&lt;br /&gt; http://ib.berkeley.edu/labs/slatkin/eriq/classes/guest_lect/mc_lecture_notes.pdf&lt;br /&gt;<br /> <br /> then we have: &lt;br /&gt;<br /> &lt;math&gt;I = \int h(x)f(x)\,dx =\int\frac{h(x)f(x)}{g(x)}\times g(x)\,dx&lt;/math&gt;, where &lt;math&gt;w(x) = \frac{h(x)f(x)}{g(x)}&lt;/math&gt;&lt;br /&gt;<br /> <br /> <br /> In order to estimate I we have:&lt;br/&gt;<br /> <br /> &lt;math&gt;\widehat{I}=\frac{1}{n}\sum_{i=1}^{n}w(x)&lt;/math&gt; and &lt;math&gt;g^{*}(x) = \frac{|h(x)|f(x)}{\int |h(x)|f(x)dx}&lt;/math&gt;, where &lt;math&gt; h(x)&gt;=0 &lt;/math&gt; for all x &lt;br&gt;<br /> <br /> Higher values of n correspond to values of &lt;math&gt;\widehat{I}&lt;/math&gt; closer to &lt;math&gt;{I}&lt;/math&gt;, which approaches &lt;math&gt;\widehat{I}&lt;/math&gt; as n approaches infinity.<br /> <br /> '''Note:''' g(x) should be chosen carefully. It should be easy to sample from. Also, since this method is for minimizing the variance, g(x) should be chosen in a manner such that the variance is minimized. g*(x) is the distribution that minimizes the variance.<br /> <br /> <br /> *In assignment 6, we need to prove that &quot;the choice of g that minimizes the variance of I is g*(x)&quot;. Furthermore, we were asked to minimize the variance of I which requires us to minimize the variance of w (as seen below). For simplicity, we assume that h(x) is greater than or equal to 0 for all x. In reality, h(x) can be positive, negative or 0. <br /> <br /> :&lt;math&gt;\, Var(x) = E(x^2) - (E(x))^2 &lt;/math&gt; &lt;br&gt;<br /> <br /> <br /> :&lt;math&gt;\, Var(I) = Var(\frac{1}{n} \sum_{i = 1}^{n} w({x_{i})})= Var(w)/n &lt;/math&gt; &lt;br&gt;<br /> <br /> Note:<br /> This expression has equivalent to the summation of all variances of W, because W’s are independent, hence covariance terms are zero.<br /> <br /> <br /> :&lt;math&gt;\, Var(w) = E(w^2) - (E(w))^2 &lt;/math&gt; &lt;br&gt;<br /> <br /> <br /> :&lt;math&gt; = \int(\frac{h(x)f(x)}{g(x)})^2 g(x) dx - (\int\frac{h(x)f(x)}{g(x)} g(x) dx)^2 &lt;/math&gt;&lt;br&gt;<br /> <br /> <br /> :&lt;b&gt;The Second Term&lt;/b&gt; &lt;br&gt;<br /> <br /> <br /> :&lt;math&gt;\left(\int \frac{h(x)f(x)}{g(x)}g(x)dx\right)^2=\left(\underbrace{\int h(x)f(x)dx}_I\right)^2=I^2&lt;/math&gt;&lt;br&gt;<br /> <br /> <br /> :'''Note''' ： No matter what g is, the second term is always constant with respect to g at &lt;math&gt; I^2&lt;/math&gt;.&lt;br&gt; <br /> since &lt;math&gt; I^2 &lt;/math&gt; is constant with respect to g, if we want to minimize the variance, we only need to consider the first term.&lt;br&gt; <br /> <br /> So, we need to minimize the first term.&lt;br /&gt;<br /> <br /> <br /> :&lt;b&gt; The First Term &lt;/b&gt;&lt;br&gt;<br /> <br /> &lt;math&gt; \int(\frac{h(x)f(x)}{g(x)})^2 g(x) dx &lt;/math&gt; &lt;br&gt;<br /> <br /> <br /> &lt;math&gt; = \int\frac{h(x)^2f(x)^2}{g(x)} dx &lt;/math&gt; &lt;br&gt;<br /> <br /> <br /> If &lt;math&gt;h(x) \geq 0&lt;/math&gt;, then &lt;math&gt;g^*(x)= \frac{h(x)f(x)}{\int h(x)f(x) dx} = \frac{h(x)f(x)}{I}&lt;/math&gt; where &lt;math&gt; I = \int h(x)f(x) dx&lt;/math&gt; &lt;br&gt;<br /> <br /> &lt;math&gt; = \int\frac{h(x)^2f(x)^2}{\frac{h(x)f(x)}{I}} dx &lt;/math&gt; &lt;br&gt;<br /> <br /> &lt;math&gt; = \int\frac{I \times h(x)^2f(x)^2}{h(x)f(x)} dx &lt;/math&gt; &lt;br&gt;<br /> <br /> &lt;math&gt; = \int I \times h(x)f(x) dx &lt;/math&gt; &lt;br&gt;<br /> <br /> &lt;math&gt; = I\times \left(\underbrace{\int h(x)f(x)dx}_I\right) &lt;/math&gt; &lt;br&gt;<br /> <br /> &lt;math&gt; = I^2 &lt;/math&gt; &lt;br&gt; (because we choose g(x)=g*(x))<br /> <br /> &lt;math&gt;I = \int h(x)f(x) dx \leq \int |h(x)|f(x) dx&lt;/math&gt;&lt;br /&gt;<br /> <br /> since &lt;math&gt;f(x) \geq 0&lt;/math&gt;, &lt;math&gt;|h(x)| \geq h(x)&lt;/math&gt; &lt;br /&gt;<br /> <br /> Therefore, at &lt;math&gt;\,g(x), Var(w)=I^2 -I^2=0&lt;/math&gt;&lt;br /&gt;<br /> '''Note that although this proof uses the assumption of h(x) ≥ 0, the result still holds for functions h(x) that are not always non-negative (however, the variance will not be 0)&lt;br/&gt;<br /> More specifically, since &lt;math&gt;\int |h(x)|f(x) dx \geq \int h(x)f(x) dx = I&lt;/math&gt; where &lt;math&gt;h(x)&lt;/math&gt; can be negative, so &lt;math&gt;\, Var(w) \geq I^2-I^2 = 0 &lt;/math&gt;, and as a result Var(w) will always be non-negative.&lt;br/&gt;<br /> '''<br /> Remark: Since &lt;math&gt; I^2 &lt;/math&gt; is constant of g, we only consider minimizing the first term to minimize the variance.<br /> '''<br /> <br /> === Normalized Importance Sampling ===<br /> <br /> &lt;math&gt;I= \frac{\int h(x)f(x) dx}{\int f(x) dx}&lt;/math&gt; since f(x) is a pdf, and the integral is just equal to 1&lt;br /&gt; <br /> <br /> &lt;math&gt;=\frac{\int\frac{ h(x)f(x)}{g(x)}g(x)dx}{\int \frac{f(x)g(x)}{g(x)}dx}&lt;/math&gt;<br /> &lt;br /&gt;<br /> &lt;br /&gt;<br /> &lt;math&gt;\hat{I}= \frac{1}{n}\sum_{i=1}^{n}\frac{h(x_i)f(x_i)}{g(x_i)} =\frac{1}{n}\sum_{i=1}^{n}h(x_i)\beta_i^*&lt;/math&gt; where &lt;math&gt;\beta_i^* = \frac{\beta_i}{\sum_{i=i}^{n}\beta_i}&lt;/math&gt; and &lt;math&gt;\beta_i = \frac{f(x_i)}{g(x_i)}&lt;/math&gt; &lt;br /&gt;<br /> <br /> Where &lt;math&gt;\frac{f(x)}{g(x)}&lt;/math&gt; corresponds to a weight. &lt;br&gt;<br /> <br /> &lt;math&gt;\,\beta = [\beta_1,\beta_2, ......, \beta_n]&lt;/math&gt; xi~&lt;math&gt;g(x)&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;\beta_i^*= \biggl[ \frac{\beta_1}{\beta_1+...+\beta_n}, \frac{\beta_2}{\beta_1+...+\beta_n}, ... , \frac{\beta_n}{\beta_1+...+\beta_n} \biggr]&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;br/&gt;<br /> Note: Above is not included in exam.<br /> <br /> :'''Note:''' <br /> :*One advantage of using Normalized Importance Sampling is that we don't need to know the normalization factor of distribution f(x). The normalization will be applied to the individual &lt;math&gt;\beta_i&lt;/math&gt;, and so the sum of the &lt;math&gt;\beta&lt;/math&gt;'s will be of the same proportion.&lt;br&gt;<br /> :*The normalization factor will be cancelled out when we calculate the weights. Note that this is the same advantage as that of Metropolis Hastings. It is a powerful advantage because in practice, determining the normalizing constant can be very difficult. This advantage does not hold when using &lt;math&gt;\beta&lt;/math&gt; only &lt;math&gt;\beta^*&lt;/math&gt;<br /> :*Normalized Importance Sampling however, performs worse than regular importance sampling as we are approximating the normalizing constant<br /> <br /> [http://www.youtube.com/watch?v=gYvlnu5AAzE Here is a video explaining normalized importance sampling sightly differently.]<br /> <br /> ===Final Exam Review===<br /> <br /> '''Summary of Final Exam Topics:'''<br /> ''Pre-Midterm:''<br /> • Multiplicative Congruential Algorithm<br /> • Inverse Transform Method<br /> • Acceptance Rejection Method<br /> • Multivariate Random Variable Generation<br /> • Vector Acceptance Rejection Method<br /> ''Post-Midterm:''<br /> • Poisson Process<br /> • Markov Chains (MC)<br /> • Page Rank (MC Application)<br /> • Markov Chain Monte Carlo (MCMC)<br /> • Metropolis-Hasting Algorithm (MCMC Application)<br /> • Simulated Annealing (MCMC Application)<br /> • Gibbs Sampling (Metropolis-Hasting adaptation)<br /> • Monte Carlo Integration<br /> • Importance Sampling<br /> <br /> <br /> Only review of material not covered on the midterm, the final will be cumulative&lt;br/&gt;<br /> For review of material covered on the midterm refer to class 12 - June 13th.<br /> '''Stochastic Processes''' (we learned Poisson Process and Markov Chain).<br /> <br /> *{X&lt;sub&gt;t&lt;/sub&gt; | t in T}<br /> where x&lt;sub&gt;t&lt;/sub&gt; is an element of state space X and T is the index set.&lt;br/&gt;<br /> *A collection of random variables.&lt;br/&gt;<br /> *The two most important stochastic processes we looked at in this term are Poission Process and Markov Chain.&lt;br/&gt;<br /> <br /> ===Poisson Process=== (useful for counting number of arrivals): &lt;br/&gt;<br /> - two assumptions: &lt;br/&gt;<br /> # the number of arrivals in non-overlapping intervals are independent &lt;br/&gt;<br /> # the number of arrivals in an interval I is Poisson distributed.&lt;br/&gt;<br /> - the mean of the Poisson Process is &lt;math&gt;\lambda \times length(I)&lt;/math&gt; &lt;br/&gt;<br /> - Can be generated using the exponential distribution &lt;br/&gt;<br /> <br /> '''Algorithm'''&lt;br\&gt;<br /> *1. Set n=1, a=1&lt;br\&gt;<br /> *2. &lt;math&gt;U_n&lt;/math&gt; ~ &lt;math&gt;U[0,1]&lt;/math&gt; and set &lt;math&gt;a=aU_n&lt;/math&gt;&lt;br\&gt;<br /> *3. If &lt;math&gt;a \geq e^{-\lambda}&lt;/math&gt; then: n=n+1 and go to Step 2. Else set X=n-1.<br /> <br /> Acknowledgments: from Spring 2012 stat 340 coursenotes<br /> <br /> Matlab code review: &lt;br/&gt;<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> T(1)=0;<br /> ii=1;<br /> l=2;<br /> TT=5;<br /> while T(ii)&lt;=TT<br /> u=rand;<br /> ii=ii+1;<br /> T(ii)=T(ii-1) - (1/l)*log(u); <br /> end<br /> plot(T, '.')<br /> &lt;/pre&gt;<br /> <br /> <br /> '''Markov Chain''': &lt;br/&gt;<br /> Recall that:&lt;br /&gt;<br /> *A Markov Chain is a discrete random process which transits from one state to another. The number of states in a Markov Chain can be finite or countable.&lt;br /&gt;<br /> <br /> *A Markov Chain has the Memoriless Property:<br /> &lt;math&gt;Pr(X_t=x_t|X_{t-1}=x_{t-1},..., X_1=x_1)= Pr(X_t=x_t|X_{t-1}=x_{t-1})&lt;/math&gt;.<br /> &lt;br&gt;In other words, the current state only depends on the previous state and no other prior states. This property is also called the &quot;Markov property&quot;.&lt;br /&gt;<br /> <br /> <br /> *The possible values of X&lt;sub&gt;i&lt;/sub&gt; are called the &quot;state space&quot; of the chain.<br /> <br /> *Transition probability P&lt;sub&gt;ij&lt;/sub&gt; = Pr {x&lt;sub&gt;t+1&lt;/sub&gt;=j | x&lt;sub&gt;t&lt;/sub&gt; = i} = P(i,j)&lt;br/&gt;<br /> <br /> *Transition matrix P = [P&lt;sub&gt;11&lt;/sub&gt; ... P&lt;sub&gt;1n&lt;/sub&gt; ; ... ; P&lt;sub&gt;n1&lt;/sub&gt; ... P&lt;sub&gt;nn&lt;/sub&gt;]. where P&lt;sub&gt; ij&lt;/sub&gt; &gt;= 0, row sum = 1 &lt;br/&gt;<br /> <br /> *N-step transition matrix P&lt;sub&gt;n(i,j)&lt;/sub&gt; = Pr {x&lt;sub&gt;t+n&lt;/sub&gt;=j | x&lt;sub&gt;t&lt;/sub&gt; = i}, P&lt;sub&gt;n&lt;/sub&gt; = P&lt;sup&gt;n&lt;/sup&gt; &lt;br/&gt;<br /> <br /> *Marginal distribution:&lt;math&gt;\mu_1~ = \mu_0P&lt;/math&gt; &lt;br&gt;<br /> In general, &lt;math&gt;\mu_n~ = \mu_0P^n&lt;/math&gt;&lt;br /&gt;<br /> <br /> *'''Stationary distribution''': &lt;math&gt;\pi&lt;/math&gt; = &lt;math&gt;\pi&lt;/math&gt; P &lt;br/&gt;<br /> <br /> *'''Limiting distribution''': <br /> &lt;math&gt;\lim_{n\to \infty} P^n= \left[ {\begin{array}{ccc}<br /> \pi_1 \\<br /> \vdots \\<br /> \pi_n \\<br /> \end{array} } \right]&lt;/math&gt; &lt;br/&gt;<br /> <br /> - '''Detailed Balance''': &lt;br/&gt;<br /> <br /> Detailed Balance &lt;br/&gt;<br /> if &lt;math&gt;\pi&lt;/math&gt;&lt;sub&gt;i&lt;/sub&gt; P&lt;sub&gt;ij&lt;/sub&gt; = &lt;math&gt;\pi&lt;/math&gt;&lt;sub&gt;j&lt;/sub&gt; P&lt;sub&gt;ji&lt;/sub&gt;, <br /> &lt;math&gt; [\pi P][\pi P]j = \sum \pi&lt;/math&gt; &lt;sub&gt;i&lt;/sub&gt; P&lt;sub&gt;ij&lt;/sub&gt; &lt;math&gt;= \sum \pi &lt;/math&gt;&lt;sub&gt;j&lt;/sub&gt;P&lt;sub&gt;ji&lt;/sub&gt; &lt;math&gt;= \pi &lt;/math&gt;&lt;sub&gt;j&lt;/sub&gt;&lt;math&gt; \sum P&lt;/math&gt;&lt;sub&gt;ji&lt;/sub&gt; &lt;math&gt;= \pi &lt;/math&gt;&lt;sub&gt;j &lt;/sub&gt;<br /> <br /> <br /> '''Proof:''' We will look at a single row from <br /> <br /> <br /> &lt;math&gt;\; \pi P &lt;/math&gt;, denoted by &lt;math&gt;\; [\pi P]_j &lt;/math&gt; &lt;br/&gt;<br /> <br /> &lt;math&gt;\; [\pi P]_j = \sum_i \pi_i P_{ij} =\sum_i P_{ji}\pi_j =\pi_j\sum_i P_{ji} =\pi_j ,\forall j&lt;/math&gt; &lt;br/&gt;<br /> note: &lt;math&gt;\sum_i P_{ji}=1.&lt;/math&gt; &lt;br/&gt;<br /> <br /> <br /> -''' Application of Markov Chain''' - '''PageRank''': &lt;br/&gt;<br /> <br /> &lt;math&gt;P_i= (1-d) + d\cdot \sum_j \frac {L_{ij}P_j}{c_j}&lt;/math&gt;, where 0 &lt; d &lt; 1 is constant &lt;br/&gt;<br /> where &lt;math&gt; L_{ij} &lt;/math&gt; is 1 if j has link to i, and 0 otherwise; <br /> &lt;math&gt; C_j = \sum_i L_{ij} &lt;/math&gt; &lt;br/&gt;<br /> Note: we solved this using systems of equations or eigenvalues and eigenvectors<br /> <br /> '''Matrix form:''' &lt;br/&gt;<br /> &lt;math&gt;P=[(1-d)~\frac{ee^T}{N}+dLD^{-1}]P&lt;/math&gt; &lt;br/&gt;&lt;br/&gt; (ee^T is a matrix of all 1s)<br /> <br /> &lt;math&gt;P=AP , where A=[(1-d)~\frac{ee^T}{N}+dLD^{-1}]&lt;/math&gt; &lt;br/&gt;&lt;br/&gt;<br /> The matrix has column summation equals to one and has eigenvalues equal to one.<br /> <br /> <br /> '''Markov Chain Monte Carlo (MCMC)''': &lt;br/&gt; <br /> -Recall that MCMC is a special form of stochastic process where X&lt;sub&gt;t&lt;/sub&gt; depends only on X&lt;sub&gt;t-1&lt;/sub&gt;&lt;br/&gt;&lt;br/&gt;<br /> -The two applications of MCMC are Metropolis–Hasting algorithm and Simulated Annealing.&lt;br/&gt;<br /> <br /> - ''''''Metropolis–Hasting Algorithm'''''': &lt;br/&gt;<br /> If we have target distribution f, which we want to sample from, then&lt;br/&gt;<br /> decide to accept the sample or reject. &lt;br/&gt;<br /> <br /> 1) X&lt;sub&gt;0&lt;/sub&gt;= state of chain at time 0. Set i = 0&lt;br/&gt;<br /> <br /> 2) &lt;math&gt;Y&lt;/math&gt;~&lt;math&gt;q(y|x)&lt;/math&gt;&lt;br/&gt;<br /> <br /> 3) &lt;math&gt;r(x,y)=\min\{\frac{f(y)}{f(x)}\frac{q(x|y)}{q(y|x)},1\}&lt;/math&gt;&lt;br/&gt;<br /> <br /> 4) &lt;math&gt;U&lt;/math&gt;~&lt;math&gt;U(0,1)&lt;/math&gt;&lt;br/&gt;<br /> <br /> 5) If &lt;math&gt;U&lt;r&lt;/math&gt;, &lt;math&gt;X_{t+1}=Y&lt;/math&gt;; else &lt;math&gt;X_{t+1}=X_t&lt;/math&gt;&lt;br/&gt;<br /> <br /> 6) i = i + 1. Return to Step 2. &lt;br/&gt;<br /> <br /> <br /> '''Note''' :for just the Metropolis Algorithm everything is the same as in Metropolis-Hasting Algorithm except that step 3 is: &lt;br/&gt;<br /> &lt;math&gt;r(x,y)=\min\{\frac{f(y)}{f(x)},1\}&lt;/math&gt; This is because q is symmetric in the Metropolis Algorithm.&lt;br/&gt;<br /> &lt;br/&gt;<br /> <br /> -''' Application of M.H. Algorithm''' - '''Simulated Annealing''': &lt;br/&gt;<br /> min h(x) = max exp(-h(x)/T) &lt;br /&gt;<br /> <br /> Simulated Annealing Algorithm: &lt;br/&gt;<br /> 1) Set T to be a large number, Set i = 0, &lt;math&gt;X_{t}&lt;/math&gt; = 0&lt;br /&gt;<br /> <br /> 2) &lt;math&gt;Y&lt;/math&gt;~&lt;math&gt;Q(Y|X)&lt;/math&gt;&lt;br/&gt; <br /> <br /> 3) &lt;math&gt;r(x,y) = \min\{\frac{f(y)}{f(x)},1\}&lt;/math&gt;&lt;br /&gt; Since q(.) is symetric<br /> <br /> 4) &lt;math&gt;U&lt;/math&gt;~&lt;math&gt;U(0,1)&lt;/math&gt;&lt;br/&gt;<br /> <br /> 5) If &lt;math&gt;U&lt;r&lt;/math&gt;, &lt;math&gt;X_{t+1}=Y&lt;/math&gt;; else &lt;math&gt;X_{t+1}=X_t&lt;/math&gt;&lt;br/&gt; <br /> <br /> 6) Decrease T. Return to Step 2.&lt;br/&gt;&lt;br/&gt;<br /> <br /> * note: popular candidates for Q(Y|X) are uniform distribution and normal distribution.(symmetric)<br /> <br /> <br /> - '''Proof of MH Algorithm (convergence):''' &lt;br/&gt;<br /> Detailed Balance: &lt;math&gt;f(x) P(y|x) = f(y) P(x|y)&lt;/math&gt;&lt;br/&gt;<br /> <br /> 1) &lt;math&gt;\frac {f(y) q(x|y)}{f(x) q(y|x)}&lt;1&lt;/math&gt; &lt;br&gt;<br /> <br /> =&gt; r(x,y) = &lt;math&gt;\frac {f(y)q(x|y)}{f(x)q(y|x)}&lt;/math&gt;&lt;br&gt;<br /> <br /> 2) &lt;math&gt;\frac {f(y)q(x|y)}{f(x)q(y|x)}&gt;1&lt;/math&gt; &lt;br&gt;<br /> <br /> =&gt; r(x,y) = 1&lt;br&gt;<br /> <br /> LHS: &lt;math&gt;f(x)P(y|x)= f(x)q(y|x)r(x,y)&lt;/math&gt; <br /> =f(x)q(y|x)&lt;math&gt;\frac {f(y)q(x|y)}{f(x)q(y|x)}&lt;/math&gt; <br /> =f(y)q(x|y)&lt;br/&gt;<br /> <br /> <br /> &lt;math&gt;\begin{align}<br /> \text{RHS} &amp; = f(x)P(x|y)= f(x)q(x|y)r(y,x) \\<br /> &amp; =f(x)q(x|y)*1 \\<br /> &amp; =f(y)q(x|y) = \text{LHS}<br /> \end{align}&lt;/math&gt;&lt;br&gt;<br /> <br /> <br /> *Therefore, detailed balance is satisfied, so f(x) is a stationary distribution! &lt;br&gt;<br /> *We can also prove similarly for Metropolis Hasting and Simulated Annealing (even easier since they don't have q(x|y)/q(y|x) when calculating r) &lt;br&gt;<br /> <br /> - '''Proof of Simulated Annealing Algorithm (convergence):''' &lt;br/&gt;<br /> Detailed Balance: &lt;math&gt;\,f(x) P(y|x) = f(y) P(x|y)&lt;/math&gt;&lt;br/&gt;<br /> <br /> Since q(y|x) is symmetric -&gt; q(y|x)=q(x|y)&lt;br/&gt;<br /> <br /> 1) &lt;math&gt;\frac {f(y)}{f(x)}&lt;1&lt;/math&gt; &lt;br&gt;<br /> <br /> =&gt; &lt;math&gt;r(x,y) = \frac {f(y)}{f(x)}&lt;/math&gt;&lt;br&gt;<br /> <br /> 2) &lt;math&gt;\frac {f(y)}{f(x)}&gt;1&lt;/math&gt; &lt;br&gt;<br /> <br /> =&gt; &lt;math&gt;\, r(x,y) = 1&lt;/math&gt;&lt;br&gt;<br /> <br /> &lt;math&gt;\begin{align}<br /> \text{LHS} &amp; = f(x)P(y|x)= f(x)q(y|x)r(x,y) \\ <br /> &amp; =f(x)q(y|x)\frac {f(y)}{f(x)} \\<br /> &amp; =f(y)q(x|y) \end{align}&lt;/math&gt;&lt;br&gt;<br /> <br /> <br /> &lt;math&gt;\begin{align}<br /> \text{RHS} &amp; = f(y)P(x|y)= f(y)q(x|y)r(y,x) \\<br /> &amp; =f(y)q(x|y)\times 1 \\<br /> &amp; =f(y)q(x|y) = \text{LHS}<br /> \end{align}&lt;/math&gt;&lt;br&gt;<br /> <br /> <br /> '''Gibbs Sampling''':&lt;br&gt;<br /> The most widely used version of the Metropolis-Hastings algorithm is the Gibbs sampler. &lt;br /&gt; <br /> This sampling method is useful when dealing with multivariable distributions.&lt;br&gt;<br /> <br /> &lt;math&gt;f(x_1, x_2, ..., x_d)&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;x = (x_1, ..., x_d)&lt;/math&gt;&lt;br /&gt;<br /> <br /> *Suppose &lt;math&gt;x_t = (x_{t_1}, ..., x_{t_d})&lt;/math&gt; are the initial values.&lt;br /&gt;<br /> &lt;br /&gt;<br /> <br /> Start by sampling from &lt;math&gt;x_1&lt;/math&gt;:<br /> &lt;math&gt;\displaystyle Y_1 \sim f(x_1 | x_{t_2}, ..., x_{t_d})&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;\displaystyle Y_i \sim f(x_i | Y_1, ..., Y_{i-1}, x_{t_{i+1}}, ..., x_{t_d})&lt;/math&gt;, where &lt;math&gt;i=2, ..., d&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;\displaystyle Y_d \sim f(x_d | Y_1, ..., Y_{d-1})&lt;/math&gt;&lt;br /&gt;<br /> <br /> ===Example:===<br /> <br /> <br /> Consider a biased die<br /> &lt;math&gt;\pi&lt;/math&gt;= [0.1, 0.1, 0.3, 0.3, 0.1, 0.1]<br /> <br /> We use &lt;math&gt;6 x 6 &lt;/math&gt; matrix &lt;math&gt; \mathbf{Q} &lt;/math&gt; as the proposal distribution &lt;br&gt;<br /> And we use U(0,1) distribution.<br /> <br /> &lt;math&gt; \mathbf{Q} = <br /> \begin{bmatrix}<br /> 1/6 &amp; 1/6 &amp; \cdots &amp; 1/6 \\<br /> 1/6 &amp; 1/6 &amp; \cdots &amp; 1/6 \\<br /> \vdots &amp; \vdots &amp; \ddots &amp; \vdots \\<br /> 1/6 &amp; 1/6 &amp; \cdots &amp; 1/6<br /> \end{bmatrix}<br /> &lt;/math&gt; &lt;br/&gt;<br /> <br /> <br /> '''Algorithm''' &lt;br&gt;<br /> 1. &lt;math&gt;x_t=5&lt;/math&gt; <br /> 2. Y~unif[1,2,...,6]&lt;br /&gt;<br /> 3. &lt;math&gt; r_{ij} = \min \{\frac{\pi_j q_{ji}}{\pi_i q_{ij}}, 1\} = \min \{\frac{\pi_j 1/6}{\pi_i 1/6}, 1\} = \min \{\frac{\pi_j}{\pi_i}, 1\}&lt;/math&gt;&lt;br&gt;<br /> 4. U~Unif(0,1)&lt;br/&gt;<br /> if &lt;math&gt;u \leq r_{ij}&lt;/math&gt;, X&lt;sub&gt;t+1&lt;/sub&gt;=Y&lt;br /&gt;<br /> else X&lt;sub&gt;t+1&lt;/sub&gt;=X&lt;sub&gt;t&lt;/sub&gt;&lt;br /&gt;<br /> go back to 2&lt;br&gt;<br /> <br /> ===Monte Carlo Integration===<br /> *It is a technique used for numerical integration using random numbers.&lt;br/&gt;<br /> *This method is one of the Monte Carlo methods that numerically computes definite integrals. &lt;br/&gt;<br /> <br /> *The above integral can be rewritten as following:&lt;br&gt;<br /> &lt;math&gt;I = \int_a^b h(x)dx = \int_a^b h(x) \frac{b-a}{b-a} dx = \int_a^b \frac{h(x)}{b-a} (b-a) dx &lt;/math&gt; where &lt;math&gt; U(a,b) = 1/(b-a) &lt;/math&gt; &lt;br/&gt;<br /> <br /> <br /> So we have &lt;math&gt; w(x)= \frac{h(x)}{b-a} &lt;/math&gt; and &lt;math&gt;\hat{I} = \frac{1}{n} \sum_{i=1}^n w(x_i),x_i \sim U(a,b)&lt;/math&gt;&lt;br /&gt;<br /> <br /> <br /> For the case where we do not have finite bounds on the integration, we have<br /> &lt;math&gt;I = \int h(x)f(x)dx&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;\hat{I} = \frac{1}{n} \sum _{i=1}^n h(x_i) , \text{where} \ x_i \sim f&lt;/math&gt;<br /> <br /> ===Importance Sampling===<br /> Importance Sampling is a useful technique for variance reduction.&lt;br /&gt;<br /> <br /> Using importance sampling, we have:&lt;br/&gt;<br /> <br /> &lt;math&gt;I=\int_{a}^{b}f(x)dx = \int_{a}^{b}f(x)(b-a) \times \frac{1}{b-a}dx&lt;/math&gt; &lt;br /&gt;<br /> <br /> If g(x) is another probability density function, &lt;br /&gt;<br /> <br /> &lt;math&gt;I = \int h(x)f(x)\,dx =\int\frac{h(x)f(x)}{g(x)}\times g(x)\,dx&lt;/math&gt;, where &lt;math&gt;w(x) = \frac{h(x)f(x)}{g(x)}&lt;/math&gt;&lt;br /&gt;<br /> <br /> To approximate I,&lt;br/&gt;<br /> <br /> &lt;math&gt;\widehat{I}=\frac{1}{n}\sum_{i=1}^{n}w(x)&lt;/math&gt; and &lt;math&gt;g^{*}(x) = \frac{|h(x)|f(x)}{\int |h(x)|f(x)dx}&lt;/math&gt;, where &lt;math&gt; h(x)&gt;=0 &lt;/math&gt; for all x &lt;br&gt;<br /> <br /> '''Note:''' g(x) should be chosen carefully so that its distribution would minimize the variance.</div> Zhchong http://wiki.math.uwaterloo.ca/statwiki/index.php?title=stat340s13&diff=22690 stat340s13 2013-07-31T04:40:38Z <p>Zhchong: /* Gibbs Sampling */</p> <hr /> <div>&lt;div style = &quot;align:left; background:#00ffff; font-size: 150%&quot;&gt;<br /> If you<br /> use ideas, plots, text, code and other intellectual property developed by someone else<br /> in your wikicoursenote' contribution , you have to cite the<br /> original source. If you copy a sentence or a paragraph from work done by someone<br /> else, in addition to citing the original source you have to use quotation marks to<br /> identify the scope of the copied material. Evidence of copying or plagiarism will<br /> cause a failing mark in the course.<br /> <br /> Example of citing the original source<br /> <br /> Assumptions Underlying Principal Component Analysis can be found here&lt;ref&gt;http://support.sas.com/publishing/pubcat/chaps/55129.pdf&lt;/ref&gt;<br /> <br /> &lt;/div&gt;<br /> <br /> ==Important Notes==<br /> &lt;span style=&quot;color:#ff0000;font-size: 200%&quot;&gt; To make distinction between the material covered in class and additional material that you have add to the course, use the following convention. For anything that is not covered in the lecture write:&lt;/span&gt;<br /> <br /> &lt;div style = &quot;align:left; background:#F5F5DC; font-size: 120%&quot;&gt;<br /> In the news recently was a story that captures some of the ideas behind PCA. Over the past two years, Scott Golder and Michael Macy, researchers from Cornell University, collected 509 million Twitter messages from 2.4 million users in 84 different countries. The data they used were words collected at various times of day and they classified the data into two different categories: positive emotion words and negative emotion words. Then, they were able to study this new data to evaluate subjects' moods at different times of day, while the subjects were in different parts of the world. They found that the subjects generally exhibited positive emotions in the mornings and late evenings, and negative emotions mid-day. They were able to &quot;project their data onto a smaller dimensional space&quot; using PCS. Their paper, &quot;Diurnal and Seasonal Mood Vary with Work, Sleep, and Daylength Across Diverse Cultures,&quot; is available in the journal Science.&lt;ref&gt;http://www.pcworld.com/article/240831/twitter_analysis_reveals_global_human_moodiness.html&lt;/ref&gt;.<br /> <br /> Assumptions Underlying Principal Component Analysis can be found here&lt;ref&gt;http://support.sas.com/publishing/pubcat/chaps/55129.pdf&lt;/ref&gt;<br /> <br /> &lt;/div&gt;<br /> <br /> == Introduction, Class 1 - Tuesday, May 7 ==<br /> <br /> <br /> <br /> === Course Instructor: Ali Ghodsi ===<br /> &lt;!-- br tag for spacing--&gt;<br /> Lecture: &lt;br /&gt;<br /> 001: T/Th 8:30-9:50am MC1085 &lt;br /&gt;<br /> 002: T/Th 1:00-2:20pm DC1351 &lt;br /&gt;<br /> Tutorial: &lt;br /&gt;<br /> 2:30-3:20pm Mon M3 1006 &lt;br /&gt;<br /> Office Hours: &lt;br /&gt;<br /> Friday at 10am, M3 4208 &lt;br /&gt;<br /> <br /> === Midterm ===<br /> Monday June 17,2013 from 2:30pm-3:20pm<br /> <br /> === Final ===<br /> Saturday August 10,2013 from 7:30pm-10:00pm<br /> <br /> === TA(s): ===<br /> &lt;!-- br tag for spacing--&gt;<br /> {| class=&quot;wikitable&quot;<br /> |-<br /> ! TA<br /> ! Day<br /> ! Time<br /> ! Location<br /> |-<br /> | Lu Cheng<br /> | Monday<br /> | 3:30-5:30 pm<br /> | M3 3108, space 2<br /> |-<br /> | Han ShengSun<br /> | Tuesday<br /> | 4:00-6:00 pm<br /> | M3 3108, space 2<br /> |-<br /> | Yizhou Fang<br /> | Wednesday<br /> | 1:00-3:00 pm<br /> | M3 3108, space 1<br /> |-<br /> | Huan Cheng<br /> | Thursday<br /> | 3:00-5:00 pm<br /> | M3 3111, space 1<br /> |-<br /> | Wu Lin<br /> | Friday<br /> | 11:00-1:00 pm<br /> | M3 3108, space 1<br /> |}<br /> <br /> === Four Fundamental Problems ===<br /> &lt;!-- br tag for spacing--&gt;<br /> 1 Classification: Given input object X, we have a function which will take this input X and identify which 'class (Y)' it belongs to (Discrete Case) &lt;br /&gt;<br /> &lt;font size=&quot;3&quot;&gt;i.e taking value from x, we could predict y.&lt;/font&gt;<br /> (For example, if you have 40 images of oranges and 60 images of apples (represented by x), you can estimate a function that takes the images and states what type of fruit it is - note Y is discrete in this case.) &lt;br /&gt;<br /> 2 Regression: Same as classification but in the continuous case except y is non discrete. Results from regression are often used for prediction,forecasting and etc. (Example of stock prices, height, weight, etc.) &lt;br /&gt;<br /> (A simple practice might be investigating the hypothesis that higher levels of education cause higher levels of income.) &lt;br /&gt;<br /> 3 Clustering: Use common features of objects in same class or group to form clusters.(in this case, x is given, y is unknown; For example, clustering by provinces to measure average height of Canadian men.) &lt;br /&gt;<br /> 4 Dimensionality Reduction (also known as Feature extraction, Manifold learning): Used when we have a variable in high dimension space and we want to reduce the dimension &lt;br /&gt;<br /> <br /> === Applications ===<br /> <br /> Most useful when structure of the task is not well understood but can be characterized by a dataset with strong statistical regularity&lt;br /&gt;<br /> Examples: &lt;br /&gt;<br /> *Computer Vision, Computer Graphics, Finance (fraud detection), Machine Learning &lt;br /&gt;<br /> *Search and recommendation (eg. Google, Amazon) &lt;br /&gt;<br /> *Automatic speech recognition, speaker verification &lt;br /&gt;<br /> *Text parsing &lt;br /&gt;<br /> *Face identification &lt;br /&gt;<br /> *Tracking objects in video&lt;br /&gt;<br /> *Financial prediction(e.g. credit cards) &lt;br /&gt;<br /> *Fraud detection &lt;br /&gt;<br /> *Medical diagnosis &lt;br /&gt;<br /> <br /> === Course Information ===<br /> <br /> '''Prerequisite:''' (One of CS 116, 126/124, 134, 136, 138, 145, SYDE 221/322) and (STAT 230 with a grade of at least 60% or STAT 240) and (STAT 231 or 241)<br /> <br /> '''Antirequisite:''' CM 361/STAT 341, CS 437, 457 &lt;!--- Moved these down to declutter table of contents ---&gt;<br /> <br /> '''General Information'''<br /> *No required textbook<br /> *Recommended: &quot;Simulation&quot; by Sheldon M. Ross<br /> *Computing parts of the course will be done in Matlab, but prior knowledge of Matlab is not essential (will have a tutorial on it)<br /> *First midterm will be held on Monday, June 17 from 2:30 to 3:30<br /> *Announcements and assignments will be posted on Learn.<br /> *Other course material on: http://wikicoursenote.com/wiki/<br /> *Log on to both Learn and wikicoursenote frequently.<br /> *Email all questions and concerns to UWStat340@gmail.com. Do not use your personal email address! Do not email instructor or TAs about the class directly to their personal accounts!<br /> <br /> '''Wikicourse note (complete at least 12 contributions to get 10% of final mark):'''<br /> When applying for an account in the wikicourse note, please use the quest account as your login name while the uwaterloo email as the registered email. This is important as the quest id will be used to identify the students who make the contributions.<br /> Example:&lt;br/&gt;<br /> User: questid&lt;br/&gt;<br /> Email: questid@uwaterloo.ca&lt;br/&gt;<br /> After the student has made the account request, do wait for several hours before students can login into the account using the passwords stated in the email. During the first login, students will be ask to create a new password for their account. <br /> <br /> '''As a technical/editorial contributor''': Make contributions within 1 week and do not copy the notes on the blackboard.<br /> <br /> ''All contributions are now considered general contributions you must contribute to 50% of lectures for full marks''<br /> <br /> *A general contribution can be correctional (fixing mistakes) or technical (expanding content, adding examples, etc.) but at least half of your contributions should be technical for full marks.<br /> <br /> Do not submit copyrighted work without permission, cite original sources.<br /> Each time you make a contribution, check mark the table. Marks are calculated on an honour system, although there will be random verifications. If you are caught claiming to contribute but have not, you will not be credited.<br /> <br /> '''Wikicoursenote contribution form''' : https://docs.google.com/forms/d/1Sgq0uDztDvtcS5JoBMtWziwH96DrBz2JiURvHPNd-xs/viewform<br /> <br /> - you can submit your contributions multiple times.&lt;br /&gt;<br /> - you will be able to edit the response right after submitting&lt;br /&gt;<br /> - send email to make changes to an old response : uwstat340@gmail.com&lt;br /&gt;<br /> <br /> === Tentative Topics ===<br /> - Random variable and stochastic process generation&lt;br /&gt;<br /> - Discrete-Event Systems&lt;br /&gt;<br /> - Variance reduction&lt;br /&gt;<br /> - Markov Chain Monte Carlo<br /> <br /> ==Class 2 - Thursday, May 9==<br /> ===Generating Random Numbers===<br /> ==== Introduction ====<br /> Simulation is the imitation of a process or system over time. Computational power has introduced the possibility of using simulation study to analyze models used to describe a situation.<br /> <br /> In order to perform a simulation study, we should:<br /> &lt;br\&gt; 1 Use a computer to generate (pseudo*) random numbers (rand in MATLAB).&lt;br&gt;<br /> 2 Use these numbers to generate values of random variable from distributions: for example, set a variable in terms of uniform u ~ U(0,1).&lt;br&gt;<br /> 3 Using the concept of discrete events, we show how the random variables can be used to generate the behavior of a stochastic model over time. (Note: A stochastic model is the opposite of deterministic model, where there are several directions the process can evolve to)&lt;br&gt;<br /> 4 After continually generating the behavior of the system, we can obtain estimators and other quantities of interest.&lt;br&gt;<br /> <br /> The building block of a simulation study is the ability to generate a random number. This random number is a value from a random variable distributed uniformly on (0,1). There are many different methods of generating a random number: &lt;br&gt;<br /> &lt;br&gt;&lt;font size=&quot;3&quot;&gt;Physical Method: Roulette wheel, lottery balls, dice rolling, card shuffling etc. &lt;br&gt;<br /> &lt;br&gt;Numerically/Arithmetically: Use of a computer to successively generate pseudorandom numbers. The &lt;br /&gt;sequence of numbers can appear to be random; however they are deterministically calculated with an &lt;br /&gt;equation which defines pseudorandom. &lt;br&gt;&lt;/font&gt;<br /> <br /> (Source: Ross, Sheldon M., and Sheldon M. Ross. Simulation. San Diego: Academic, 1997. Print.)<br /> <br /> *We use the prefix pseudo because computer generates random numbers based on algorithms, which suggests that generated numbers are not truly random. Therefore pseudo-random numbers is used.<br /> <br /> In general, a deterministic model produces specific results given certain inputs by the model user, contrasting with a '''stochastic''' model which encapsulates randomness and probabilistic events.<br /> [[File:Det_vs_sto.jpg]]<br /> &lt;br&gt;A computer cannot generate truly random numbers because computers can only run algorithms, which are deterministic in nature. They can, however, generate Pseudo Random Numbers&lt;br&gt;<br /> <br /> '''Pseudo Random Numbers''' are the numbers that seem random but are actually determined by a relative set of original values. It is a chain of numbers pre-set by a formula or an algorithm, and the value jump from one to the next, making it look like a series of independent random events. The flaw of this method is that, eventually the chain returns to its initial position and pattern starts to repeat, but if we make the number set large enough we can prevent the numbers from repeating too early. Although the pseudo random numbers are deterministic, these numbers have a sequence of value and all of them have the appearances of being independent uniform random variables. Being deterministic, pseudo random numbers are valuable and beneficial due to the ease to generate and manipulate.<br /> <br /> When people repeat the test many times, the results will be the closed express values, which make the trials look deterministic. However, for each trial, the result is random. So, it looks like pseudo random numbers.<br /> <br /> ==== Mod ====<br /> Let &lt;math&gt;n \in \N&lt;/math&gt; and &lt;math&gt;m \in \N^+&lt;/math&gt;, then by Division Algorithm, <br /> &lt;math&gt;\exists q, \, r \in \N \;\text{with}\; 0\leq r &lt; m, \; \text{s.t.}\; n = mq+r&lt;/math&gt;, <br /> where &lt;math&gt;q&lt;/math&gt; is called the quotient and &lt;math&gt;r&lt;/math&gt; the remainder. Hence we can define a binary function<br /> &lt;math&gt;\mod : \N \times \N^+ \rightarrow \N &lt;/math&gt; given by &lt;math&gt;r:=n \mod m&lt;/math&gt; which returns the remainder after division by m. <br /> &lt;br /&gt;<br /> Generally, mod means taking the reminder after division by m.<br /> &lt;br /&gt;<br /> We say that n is congruent to r mod m if n = mq + r, where m is an integer. <br /> Values are between 0 and m-1 &lt;br /&gt;<br /> if y = ax + b, then &lt;math&gt;b:=y \mod a&lt;/math&gt;. &lt;br /&gt;<br /> <br /> '''Example 1:'''&lt;br /&gt;<br /> <br /> &lt;math&gt;30 = 4 \cdot 7 + 2&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;2 := 30\mod 7&lt;/math&gt;&lt;br /&gt;<br /> &lt;br /&gt;<br /> &lt;math&gt;25 = 8 \cdot 3 + 1&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;1: = 25\mod 3&lt;/math&gt;&lt;br /&gt;<br /> &lt;br /&gt;<br /> &lt;math&gt;-3=5\cdot (-1)+2&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;2:=-3\mod 5&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;br /&gt;<br /> '''Example 2:'''&lt;br /&gt;<br /> <br /> If &lt;math&gt;23 = 3 \cdot 6 + 5&lt;/math&gt; &lt;br /&gt;<br /> <br /> Then equivalently, &lt;math&gt;5 := 23\mod 6&lt;/math&gt;&lt;br /&gt;<br /> &lt;br /&gt;<br /> If &lt;math&gt;31 = 31 \cdot 1&lt;/math&gt; &lt;br /&gt;<br /> <br /> Then equivalently, &lt;math&gt;0 := 31\mod 31&lt;/math&gt;&lt;br /&gt;<br /> &lt;br /&gt;<br /> If &lt;math&gt;-37 = 40\cdot (-1)+ 3&lt;/math&gt; &lt;br /&gt;<br /> <br /> Then equivalently, &lt;math&gt;3 := -37\mod 40&lt;/math&gt;&lt;br /&gt;<br /> <br /> '''Example 3:'''&lt;br /&gt;<br /> &lt;math&gt;77 = 3 \cdot 25 + 2&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;2 := 77\mod 3&lt;/math&gt;&lt;br /&gt;<br /> &lt;br /&gt;<br /> &lt;math&gt;25 = 25 \cdot 1 + 0&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;0: = 25\mod 25&lt;/math&gt;&lt;br /&gt;<br /> &lt;br /&gt;<br /> <br /> <br /> <br /> <br /> '''Note:''' &lt;math&gt;\mod&lt;/math&gt; here is different from the modulo congruence relation in &lt;math&gt;\Z_m&lt;/math&gt;, which is an equivalence relation instead of a function.<br /> <br /> The modulo operation is useful for determining if an integer divided by another integer produces a non-zero remainder. But both integers should satisfy &lt;math&gt;n = mq + r&lt;/math&gt;, where &lt;math&gt;m&lt;/math&gt;, &lt;math&gt;r&lt;/math&gt;, &lt;math&gt;q&lt;/math&gt;, and &lt;math&gt;n&lt;/math&gt; are all integers, and &lt;math&gt;r&lt;/math&gt; is smaller than &lt;math&gt;m&lt;/math&gt;. The above rules also satisfy when any of &lt;math&gt;m&lt;/math&gt;, &lt;math&gt;r&lt;/math&gt;, &lt;math&gt;q&lt;/math&gt;, and &lt;math&gt;n&lt;/math&gt; is negative integer, see the third example.<br /> <br /> ==== Mixed Congruential Algorithm ====<br /> We define the Linear Congruential Method to be &lt;math&gt;x_{k+1}=(ax_k + b) \mod m&lt;/math&gt;, where &lt;math&gt;x_k, a, b, m \in \N, \;\text{with}\; a, m \neq 0&lt;/math&gt;. Given a '''seed''' (i.e. an initial value &lt;math&gt;x_0 \in \N&lt;/math&gt;), we can obtain values for &lt;math&gt;x_1, \, x_2, \, \cdots, x_n&lt;/math&gt; inductively. The Multiplicative Congruential Method, invented by Berkeley professor D. H. Lehmer, may also refer to the special case where &lt;math&gt;b=0&lt;/math&gt; and the Mixed Congruential Method is case where &lt;math&gt;b \neq 0&lt;/math&gt; &lt;br /&gt;. Their title as &quot;mixed&quot; arises from the fact that it has both a multiplicative and additive term. <br /> <br /> An interesting fact about '''Linear Congruential Method''' is that it is one of the oldest and best-known pseudo random number generator algorithms. It is very fast and requires minimal memory to retain state. However, this method should not be used for applications that require high randomness. They should not be used for Monte Carlo simulation and cryptographic applications. (Monte Carlo simulation will consider possibilities for every choice of consideration, and it shows the extreme possibilities. This method is not precise enough.)&lt;br /&gt;<br /> <br /> [[File:Linear_Congruential_Statment.png‎|600px]] &quot;Source: STAT 340 Spring 2010 Course Notes&quot;<br /> <br /> '''First consider the following algorithm'''&lt;br /&gt;<br /> &lt;math&gt;x_{k+1}=x_{k} \mod m&lt;/math&gt; &lt;br /&gt;<br /> <br /> such that: if &lt;math&gt;x_{0}=5(mod 150)&lt;/math&gt;, &lt;math&gt;x_{n}=3x_{n-1}&lt;/math&gt;, find &lt;math&gt;x_{1},x_{8},x_{9}&lt;/math&gt;. &lt;br /&gt;<br /> &lt;math&gt;x_{n}=(3^n)*5(mod 150)&lt;/math&gt; &lt;br /&gt;<br /> &lt;math&gt;x_{1}=45,x_{8}=105,x_{9}=15&lt;/math&gt; &lt;br /&gt; <br /> <br /> <br /> <br /> '''Example'''&lt;br /&gt;<br /> &lt;math&gt;\text{Let }x_{0}=10,\,m=3&lt;/math&gt;&lt;br //&gt;<br /> <br /> :&lt;math&gt;\begin{align}<br /> <br /> x_{1} &amp;{}= 10 &amp;{}\mod{3} = 1 \\<br /> <br /> x_{2} &amp;{}= 1 &amp;{}\mod{3} = 1 \\<br /> <br /> x_{3} &amp;{}= 1 &amp;{}\mod{3} =1 \\<br /> \end{align}&lt;/math&gt;<br /> &lt;math&gt;\ldots&lt;/math&gt;&lt;br /&gt;<br /> <br /> Excluding &lt;math&gt;x_{0}&lt;/math&gt;, this example generates a series of ones. In general, excluding &lt;math&gt;x_{0}&lt;/math&gt;, the algorithm above will always generate a series of the same number less than M. Hence, it has a period of 1. The '''period''' can be described as the length of a sequence before it repeats. We want a large period with a sequence that is random looking. We can modify this algorithm to form the Multiplicative Congruential Algorithm. &lt;br /&gt;<br /> <br /> <br /> <br /> &lt;math&gt;x_{k+1}=(a \cdot x_{k} + b) \mod m &lt;/math&gt;(a little tip: &lt;math&gt;(a \cdot b)\mod c = (a\mod c)\cdot(b\mod c))&lt;/math&gt;&lt;br/&gt;<br /> <br /> '''Example'''&lt;br /&gt;<br /> &lt;math&gt;\text{Let }a=2,\, b=1, \, m=3, \, x_{0} = 10&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;\begin{align}<br /> \text{Step 1: } 0&amp;{}=(2\cdot 10 + 1) &amp;{}\mod 3 \\<br /> \text{Step 2: } 1&amp;{}=(2\cdot 0 + 1) &amp;{}\mod 3 \\<br /> \text{Step 3: } 0&amp;{}=(2\cdot 1 + 1) &amp;{}\mod 3 \\<br /> \end{align}&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;\ldots&lt;/math&gt;&lt;br /&gt;<br /> <br /> This example generates a sequence with a repeating cycle of two integers.&lt;br /&gt;<br /> <br /> (If we choose the numbers properly, we could get a sequence of &quot;random&quot; numbers. How do we find the value of &lt;math&gt;a,b,&lt;/math&gt; and &lt;math&gt;m&lt;/math&gt;? At the very least &lt;math&gt;m&lt;/math&gt; should be a very '''large''', preferably prime number. The larger &lt;math&gt;m&lt;/math&gt; is, the higher the possibility to get a sequence of &quot;random&quot; numbers. This is easier to solve in Matlab. In Matlab, the command rand() generates random numbers which are uniformly distributed on the interval (0,1)). Matlab uses &lt;math&gt;a=7^5, b=0, m=2^{31}-1&lt;/math&gt; – recommended in a 1988 paper, &quot;Random Number Generators: Good Ones Are Hard To Find&quot; by Stephen K. Park and Keith W. Miller (Important part is that &lt;math&gt;m&lt;/math&gt; should be '''large and prime''')&lt;br /&gt; <br /> <br /> Note: &lt;math&gt;\frac {x_{n+1}}{m-1}&lt;/math&gt; is an approximation to the value of a U(0,1) random variable.&lt;br /&gt; <br /> <br /> <br /> <br /> '''MatLab Instruction for Multiplicative Congruential Algorithm:'''&lt;br /&gt;<br /> Before you start, you need to clear all existing defined variables and operations:&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;clear all<br /> &gt;&gt;close all<br /> &lt;/pre&gt;<br /> <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;a=17<br /> &gt;&gt;b=3<br /> &gt;&gt;m=31<br /> &gt;&gt;x=5<br /> &gt;&gt;mod(a*x+b,m)<br /> ans=26<br /> &gt;&gt;x=mod(a*x+b,m)<br /> &lt;/pre&gt;<br /> <br /> ''(Note: &lt;br /&gt;<br /> 1. Keep repeating this command over and over again and you will get random numbers – this is how the command rand works in a computer. &lt;br /&gt;<br /> 2. There is a function in MATLAB called '''RAND''' to generate a random number between 0 and 1. &lt;br /&gt;<br /> For example, in MATLAB, we can use '''rand(1,1000)''' to generate 1000's numbers between 0 and 1. This is essentially a vector with 1 row, 1000 columns, with each entry a random number between 0 and 1.&lt;br /&gt;<br /> 3. If we would like to generate 1000 or more numbers, we could use a '''for''' loop&lt;br /&gt;&lt;br /&gt;<br /> <br /> ''(Note on MATLAB commands: &lt;br /&gt;<br /> 1. clear all: clears all variables.&lt;br /&gt;<br /> 2. close all: closes all figures.&lt;br /&gt;<br /> 3. who: displays all defined variables.&lt;br /&gt;<br /> 4. clc: clears screen.&lt;br /&gt;<br /> 5. ; : prevents the results from printing.&lt;br /&gt;<br /> 6. disstool: displays a graphing tool.&lt;br /&gt;&lt;br /&gt;<br /> <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;a=13<br /> &gt;&gt;b=0<br /> &gt;&gt;m=31<br /> &gt;&gt;x(1)=1<br /> &gt;&gt;for ii=2:1000<br /> x(ii)=mod(a*x(ii-1)+b,m);<br /> end<br /> &gt;&gt;size(x)<br /> ans=1 1000<br /> &gt;&gt;hist(x)<br /> &lt;/pre&gt;<br /> [[File:MCA_Example.jpg|300px]]<br /> <br /> ''(Note: The semicolon after the x(ii)=mod(a*x(ii-1)+b,m) ensures that Matlab will not print the entire vector of x. It will instead calculate it internally and you will be able to work with it. Adding the semicolon to the end of this line reduces the run time significantly.) ''<br /> <br /> <br /> This algorithm involves three integer parameters &lt;math&gt;a, b,&lt;/math&gt; and &lt;math&gt;m&lt;/math&gt; and an initial value, &lt;math&gt;x_0&lt;/math&gt; called the '''seed'''. A sequence of numbers is defined by &lt;math&gt;x_{k+1} = ax_k+ b \mod m&lt;/math&gt;. &lt;br /&gt;<br /> <br /> Note: For some bad &lt;math&gt;a&lt;/math&gt; and &lt;math&gt;b&lt;/math&gt;, the histogram may not look uniformly distributed.&lt;br /&gt;<br /> <br /> Note: In MATLAB, hist(x) will generate a graph representing the distribution. Use this function after you run the code to check the real sample distribution.<br /> <br /> '''Example''': &lt;math&gt;a=13, b=0, m=31&lt;/math&gt;&lt;br /&gt;<br /> The first 30 numbers in the sequence are a permutation of integers from 1 to 30, and then the sequence repeats itself so '''it is important to choose &lt;math&gt;m&lt;/math&gt; large''' to decrease the probability of each number repeating itself too early. Values are between &lt;math&gt;0&lt;/math&gt; and &lt;math&gt;m-1&lt;/math&gt;. If the values are normalized by dividing by &lt;math&gt;m-1&lt;/math&gt;, then the results are '''approximately''' numbers uniformly distributed in the interval [0,1]. There is only a finite number of values (30 possible values in this case). In MATLAB, you can use function &quot;hist(x)&quot; to see if it looks uniformly distributed. We saw that the values between 0-30 had the same frequency in the histogram, so we can conclude that they are uniformly distributed. &lt;br /&gt;<br /> <br /> If &lt;math&gt;x_0=1&lt;/math&gt;, then &lt;br /&gt;<br /> :&lt;math&gt;x_{k+1} = 13x_{k}\mod{31}&lt;/math&gt;<br /> <br /> So,<br /> <br /> :&lt;math&gt;\begin{align} x_{0} &amp;{}= 1 \\<br /> <br /> x_{1} &amp;{}= 13 \times 1 + 0 &amp;{}\mod{31} = 13 \\<br /> <br /> x_{2} &amp;{}= 13 \times 13 + 0 &amp;{}\mod{31} = 14 \\<br /> <br /> x_{3} &amp;{}= 13 \times 14 + 0 &amp;{}\mod{31} =27 \\<br /> \end{align}&lt;/math&gt;<br /> <br /> etc.<br /> <br /> For example, with &lt;math&gt;a = 3, b = 2, m = 4, x_0 = 1&lt;/math&gt;, we have:<br /> <br /> :&lt;math&gt;x_{k+1} = (3x_{k} + 2)\mod{4}&lt;/math&gt;<br /> <br /> So,<br /> <br /> :&lt;math&gt;\begin{align}<br /> x_{0} &amp;{}= 1 \\<br /> x_{1} &amp;{}= 3 \times 1 + 2 \mod{4} = 1 \\<br /> x_{2} &amp;{}= 3 \times 1 + 2 \mod{4} = 1 \\<br /> \end{align}&lt;/math&gt;&lt;br /&gt;<br /> Another Example, a =3, b =2, m = 5, x_0=1<br /> etc.<br /> &lt;hr/&gt;<br /> &lt;p style=&quot;color:red;font-size:16px;&quot;&gt;FAQ:&lt;/P&gt;<br /> 1.Why is it 1 to 30 instead of 0 to 30 in the example above?&lt;br&gt;<br /> ''&lt;math&gt;b = 0&lt;/math&gt; so in order to have &lt;math&gt;x_k&lt;/math&gt; equal to 0, &lt;math&gt;x_{k-1}&lt;/math&gt; must be 0 (since &lt;math&gt;a=13&lt;/math&gt; is relatively prime to 31). However, the seed is 1. Hence, we will never observe 0 in the sequence.''&lt;br&gt;<br /> Alternatively, {0} and {1,2,...,30} are two orbits of the left multiplication by 13 in the group &lt;math&gt;\Z_{31}&lt;/math&gt;.&lt;br&gt;<br /> 2.Will the number 31 ever appear?Is there a probability that a number never appears? &lt;br&gt;<br /> ''The number 31 will never appear. When you perform the operation &lt;math&gt;\mod m&lt;/math&gt;, the largest possible answer that you could receive is &lt;math&gt;m-1&lt;/math&gt;. Whether or not a particular number in the range from 0 to &lt;math&gt;m - 1&lt;/math&gt; appears in the above algorithm will be dependent on the values chosen for &lt;math&gt;a, b&lt;/math&gt; and &lt;math&gt;m&lt;/math&gt;. ''<br /> &lt;hr/&gt;<br /> <br /> '''Examples:[From Textbook]'''&lt;br /&gt;<br /> &lt;math&gt;\text{If }x_0=3 \text{ and } x_n=(5x_{n-1}+7)\mod 200&lt;/math&gt;, &lt;math&gt;\text{find }x_1,\cdots,x_{10}&lt;/math&gt;.&lt;br /&gt;<br /> '''Solution:'''&lt;br /&gt;<br /> &lt;math&gt;\begin{align}<br /> x_1 &amp;{}= (5 \times 3+7) &amp;{}\mod{200} &amp;{}= 22 \\<br /> x_2 &amp;{}= 117 &amp;{}\mod{200} &amp;{}= 117 \\<br /> x_3 &amp;{}= 592 &amp;{}\mod{200} &amp;{}= 192 \\<br /> x_4 &amp;{}= 2967 &amp;{}\mod{200} &amp;{}= 167 \\<br /> x_5 &amp;{}= 14842 &amp;{}\mod{200} &amp;{}= 42 \\<br /> x_6 &amp;{}= 74217 &amp;{}\mod{200} &amp;{}= 17 \\<br /> x_7 &amp;{}= 371092 &amp;{}\mod{200} &amp;{}= 92 \\<br /> x_8 &amp;{}= 1855467 &amp;{}\mod{200} &amp;{}= 67 \\<br /> x_9 &amp;{}= 9277342 &amp;{}\mod{200} &amp;{}= 142 \\<br /> x_{10} &amp;{}= 46386717 &amp;{}\mod{200} &amp;{}= 117 \\<br /> \end{align}&lt;/math&gt;&lt;br /&gt;<br /> <br /> '''Comments:'''&lt;br /&gt;<br /> <br /> Matlab code:<br /> a=5;<br /> b=7;<br /> m=200;<br /> x(1)=3;<br /> for ii=2:1000<br /> x(ii)=mod(a*x(ii-1)+b,m);<br /> end<br /> size(x);<br /> hist(x)<br /> <br /> <br /> <br /> Typically, it is good to choose &lt;math&gt;m&lt;/math&gt; such that &lt;math&gt;m&lt;/math&gt; is large, and &lt;math&gt;m&lt;/math&gt; is prime. Careful selection of parameters '&lt;math&gt;a&lt;/math&gt;' and '&lt;math&gt;b&lt;/math&gt;' also helps generate relatively &quot;random&quot; output values, where it is harder to identify patterns. For example, when we used a composite (non prime) number such as 40 for &lt;math&gt;m&lt;/math&gt;, our results were not satisfactory in producing an output resembling a uniform distribution.&lt;br /&gt;<br /> <br /> The computed values are between 0 and &lt;math&gt;m-1&lt;/math&gt;. If the values are normalized by dividing by '''&lt;math&gt;m-1&lt;/math&gt;''', their result is numbers uniformly distributed on the interval &lt;math&gt;\left[0,1\right]&lt;/math&gt; (similar to computing from uniform distribution).&lt;br /&gt;<br /> <br /> From the example shown above, if we want to create a large group of random numbers, it is better to have large, prime &lt;math&gt;m&lt;/math&gt; so that the generated random values will not repeat after several iterations. Note: the period for this example is 8: from '&lt;math&gt;x_2&lt;/math&gt;' to '&lt;math&gt;x_9&lt;/math&gt;'.&lt;br /&gt;<br /> <br /> There has been a research on how to choose uniform sequence. Many programs give you the options to choose the seed. Sometimes the seed is chosen by CPU.&lt;br /&gt;<br /> <br /> &lt;span style=&quot;background:#F5F5DC&quot;&gt;Theorem (extra knowledge)&lt;/span&gt;&lt;br /&gt;<br /> Let c be a non-zero constant. Then for any seed x0, and LCG will have largest max. period if and only if&lt;br /&gt;<br /> (i) m and c are coprime;&lt;br /&gt;<br /> (ii) (a-1) is divisible by all prime factor of m;&lt;br /&gt;<br /> (iii) if and only if m is divisible by 4, then a-1 is also divisible by 4.&lt;br /&gt;<br /> <br /> We want our LCG to have a large cycle.<br /> We call a cycle with m element the maximal period.<br /> We can make it bigger by making m big and prime.<br /> Recall:any number you can think of can be broken into a factor of prime<br /> Define coprime:Two numbers X and Y, are coprime if they do not share any prime factors.<br /> <br /> Example:&lt;br /&gt;<br /> &lt;font size=&quot;3&quot;&gt;Xn=(15Xn-1 + 4) mod 7&lt;/font&gt;&lt;br /&gt;<br /> (i) m=7 c=4 -&gt; coprime;&lt;br /&gt;<br /> (ii) a-1=14 and a-1 is divisible by 7;&lt;br /&gt;<br /> (iii) dose not apply.&lt;br /&gt;<br /> (The extra knowledge stops here)<br /> <br /> <br /> <br /> In this part, I learned how to use R code to figure out the relationship between two integers<br /> division, and their remainder. And when we use R to calculate R with random variables for a range such as(1:1000),the graph of distribution is like uniform distribution.<br /> &lt;div style=&quot;border:1px solid #cccccc;border-radius:10px;box-shadow: 0 5px 15px 1px rgba(0, 0, 0, 0.6), 0 0 200px 1px rgba(255, 255, 255, 0.5);padding:20px;margin:20px;background:#FFFFAD;&quot;&gt;<br /> &lt;h4 style=&quot;text-align:center;&quot;&gt;Summary of Multiplicative Congruential Algorithm&lt;/h4&gt;<br /> &lt;p&gt;&lt;b&gt;Problem:&lt;/b&gt; generate Pseudo Random Numbers.&lt;/p&gt;<br /> &lt;b&gt;Plan:&lt;/b&gt; <br /> &lt;ol&gt;<br /> &lt;li&gt;find integer: &lt;i&gt;a b m&lt;/i&gt;(large prime) &lt;i&gt;x&lt;sub&gt;0&lt;/sub&gt;&lt;/i&gt;(the seed) .&lt;/li&gt;<br /> &lt;li&gt;&lt;math&gt;x_{k+1}=(ax_{k}+b)&lt;/math&gt;mod m&lt;/li&gt;<br /> &lt;/ol&gt;<br /> &lt;b&gt;Matlab Instruction:&lt;/b&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;&amp;gt;&amp;gt;clear all<br /> &amp;gt;&amp;gt;close all<br /> &amp;gt;&amp;gt;a=17<br /> &amp;gt;&amp;gt;b=3<br /> &amp;gt;&amp;gt;m=31<br /> &amp;gt;&amp;gt;x=5<br /> &amp;gt;&amp;gt;mod(a*x+b,m)<br /> ans=26<br /> &amp;gt;&amp;gt;x=mod(a*x+b,m)<br /> &lt;/pre&gt;<br /> &lt;/div&gt;<br /> Another algorithm for generating pseudo random numbers is the multiply with carry method. Its simplest form is similar to the linear congruential generator. They differs in that the parameter b changes in the MWC algorithm. It is as follows: &lt;br&gt;<br /> <br /> 1.) x&lt;sub&gt;k+1&lt;/sub&gt; = ax&lt;sub&gt;k&lt;/sub&gt; + b&lt;sub&gt;k&lt;/sub&gt; mod m &lt;br&gt;<br /> 2.) b&lt;sub&gt;k+1&lt;/sub&gt; = floor((ax&lt;sub&gt;k&lt;/sub&gt; + b&lt;sub&gt;k&lt;/sub&gt;)/m) &lt;br&gt;<br /> 3.) set k to k + 1 and go to step 1<br /> [http://www.javamex.com/tutorials/random_numbers/multiply_with_carry.shtml Source]<br /> <br /> === Inverse Transform Method ===<br /> Now that we know how to generate random numbers, we use these values to sample form distributions such as exponential. However, to easily use this method, the probability distribution consumed must have a cumulative distribution function (cdf) &lt;math&gt;F&lt;/math&gt; with a tractable (that is, easily found) inverse &lt;math&gt;F^{-1}&lt;/math&gt;.&lt;br /&gt;<br /> <br /> '''Theorem''': &lt;br /&gt;<br /> If we want to generate the value of a discrete random variable X, we must generate a random number U, uniformly distributed over (0,1).<br /> Let &lt;math&gt;F:\R \rightarrow \left[0,1\right]&lt;/math&gt; be a cdf. If &lt;math&gt;U \sim U\left[0,1\right]&lt;/math&gt;, then the random variable given by &lt;math&gt;X:=F^{-1}\left(U\right)&lt;/math&gt;<br /> follows the distribution function &lt;math&gt;F\left(\cdot\right)&lt;/math&gt;,<br /> where &lt;math&gt;F^{-1}\left(u\right):=\inf F^{-1}\big(\left[u,+\infty\right)\big) = \inf\{x\in\R | F\left(x\right) \geq u\}&lt;/math&gt; is the generalized inverse.&lt;br /&gt;<br /> '''Note''': &lt;math&gt;F&lt;/math&gt; need not be invertible everywhere on the real line, but if it is, then the generalized inverse is the same as the inverse in the usual case. We only need it to be invertible on the range of F(x), [0,1]. <br /> <br /> '''Proof of the theorem:'''&lt;br /&gt;<br /> The generalized inverse satisfies the following: &lt;br /&gt;<br /> <br /> :&lt;math&gt;P(X\leq x)&lt;/math&gt; &lt;br /&gt;<br /> &lt;math&gt;= P(F^{-1}(U)\leq x)&lt;/math&gt; (since &lt;math&gt;X= F^{-1}(U)&lt;/math&gt; by the inverse method)&lt;br /&gt;<br /> &lt;math&gt;= P((F(F^{-1}(U))\leq F(x))&lt;/math&gt; (since &lt;math&gt;F &lt;/math&gt; is monotonically increasing) &lt;br /&gt;<br /> &lt;math&gt;= P(U\leq F(x)) &lt;/math&gt; (since &lt;math&gt; P(U\leq a)= a&lt;/math&gt; for &lt;math&gt;U \sim U(0,1), a \in [0,1]&lt;/math&gt;,&lt;br /&gt;<br /> &lt;math&gt;= F(x) , \text{ where } 0 \leq F(x) \leq 1 &lt;/math&gt; &lt;br /&gt;<br /> <br /> This is the c.d.f. of X. &lt;br /&gt;<br /> &lt;br /&gt;<br /> <br /> That is &lt;math&gt;F^{-1}\left(u\right) \leq x \Leftrightarrow u \leq F\left(x\right)&lt;/math&gt;&lt;br /&gt;<br /> <br /> Finally, &lt;math&gt;P(X \leq x) = P(F^{-1}(U) \leq x) = P(U \leq F(x)) = F(x)&lt;/math&gt;, since &lt;math&gt;U&lt;/math&gt; is uniform on the unit interval.&lt;br /&gt;<br /> <br /> This completes the proof.&lt;br /&gt;<br /> <br /> Therefore, in order to generate a random variable X~F, it can generate U according to U(0,1) and then make the transformation x=&lt;math&gt; F^{-1}(U) &lt;/math&gt; &lt;br /&gt;<br /> <br /> Note that we can apply the inverse on both sides in the proof of the inverse transform only if the pdf of X is monotonic. A monotonic function is one that is either increasing for all x, or decreasing for all x. Of course, this holds true for all CDFs, since they are monotonic by definition. &lt;br /&gt;<br /> <br /> In short, what the theorem tells us is that we can use a random number &lt;math&gt; U from U(0,1) &lt;/math&gt; to randomly sample a point on the CDF of X, then apply the inverse of the CDF to map the given probability to its domain, which gives us the random variable X.&lt;br/&gt;<br /> <br /> <br /> '''Example 1 - Exponential''': &lt;math&gt; f(x) = \lambda e^{-\lambda x}&lt;/math&gt;&lt;br/&gt;<br /> Calculate the CDF:&lt;br /&gt;<br /> &lt;math&gt; F(x)= \int_0^x f(t) dt = \int_0^x \lambda e ^{-\lambda t}\ dt&lt;/math&gt;<br /> &lt;math&gt; = \frac{\lambda}{-\lambda}\, e^{-\lambda t}\, | \underset{0}{x} &lt;/math&gt;<br /> &lt;math&gt; = -e^{-\lambda x} + e^0 =1 - e^{- \lambda x} &lt;/math&gt;&lt;br /&gt;<br /> Solve the inverse:&lt;br /&gt;<br /> &lt;math&gt; y=1-e^{- \lambda x} \Rightarrow 1-y=e^{- \lambda x} \Rightarrow x=-\frac {ln(1-y)}{\lambda}&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt; y=-\frac {ln(1-x)}{\lambda} \Rightarrow F^{-1}(x)=-\frac {ln(1-x)}{\lambda}&lt;/math&gt;&lt;br /&gt;<br /> Note that 1 − U is also uniform on (0, 1) and thus −log(1 − U) has the same distribution as −logU. &lt;br /&gt;<br /> Steps: &lt;br /&gt;<br /> Step 1: Draw U ~U[0,1];&lt;br /&gt;<br /> Step 2: &lt;math&gt; x=\frac{-ln(U)}{\lambda} &lt;/math&gt; &lt;br /&gt;&lt;br /&gt;<br /> <br /> <br /> EXAMPLE 2 Normal distribution <br /> G(y)=P[Y&lt;=y)<br /> =P[-sqr (y) &lt; z &lt; sqr (y))<br /> =integrate from -sqr(z) to Sqr(z) 1/sqr(2pi) e ^(-z^2/2) dz<br /> = 2 integrate from 0 to sqr(y) 1/sqr(2pi) e ^(-z^2/2) dz<br /> its the cdf of Y=z^2 <br /> <br /> pdf g(y)= G'(y)<br /> pdf pf x^2 (1) <br /> <br /> '''MatLab Code''':&lt;br /&gt;<br /> <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;u=rand(1,1000);<br /> &gt;&gt;hist(u) # this will generate a fairly uniform diagram<br /> &lt;/pre&gt;<br /> [[File:ITM_example_hist(u).jpg|300px]]<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> #let λ=2 in this example; however, you can make another value for λ<br /> &gt;&gt;x=(-log(1-u))/2;<br /> &gt;&gt;size(x) #1000 in size <br /> &gt;&gt;figure<br /> &gt;&gt;hist(x) #exponential <br /> &lt;/pre&gt;<br /> [[File:ITM_example_hist(x).jpg|300px]]<br /> <br /> '''Example 2 - Continuous Distribution''':&lt;br /&gt;<br /> <br /> &lt;math&gt; f(x) = \dfrac {\lambda } {2}e^{-\lambda \left| x-\theta \right| } for -\infty &lt; X &lt; \infty , \lambda &gt;0 &lt;/math&gt;&lt;br/&gt;<br /> <br /> Calculate the CDF:&lt;br /&gt;<br /> <br /> &lt;math&gt; F(x)= \frac{1}{2} e^{-\lambda (\theta - x)} , for \ x \le \theta &lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt; F(x) = 1 - \frac{1}{2} e^{-\lambda (x - \theta)}, for \ x &gt; \theta &lt;/math&gt;&lt;br/&gt;<br /> <br /> Solve for the inverse:&lt;br /&gt;<br /> <br /> &lt;math&gt;F^{-1}(x)= \theta + ln(2y)/\lambda, for \ 0 \le y \le 0.5&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;F^{-1}(x)= \theta - ln(2(1-y))/\lambda, for \ 0.5 &lt; y \le 1&lt;/math&gt;&lt;br/&gt;<br /> <br /> Algorithm:&lt;br /&gt;<br /> Steps: &lt;br /&gt;<br /> Step 1: Draw U ~ U[0, 1];&lt;br /&gt;<br /> Step 2: Compute &lt;math&gt;X = F^-1(U)&lt;/math&gt; i.e. &lt;math&gt;X = \theta + \frac {1}{\lambda} ln(2U)&lt;/math&gt; for U &lt; 0.5 else &lt;math&gt;X = \theta -\frac {1}{\lambda} ln(2(1-U))&lt;/math&gt;<br /> <br /> <br /> '''Example 3 - &lt;math&gt;F(x) = x^5&lt;/math&gt;''':&lt;br/&gt;<br /> Given a CDF of X: &lt;math&gt;F(x) = x^5&lt;/math&gt;, transform U~U[0,1]. &lt;br /&gt;<br /> Sol: <br /> Let &lt;math&gt;y=x^5&lt;/math&gt;, solve for x: &lt;math&gt;x=y^\frac {1}{5}&lt;/math&gt;. Therefore, &lt;math&gt;F^{-1} (x) = x^\frac {1}{5}&lt;/math&gt;&lt;br /&gt;<br /> Hence, to obtain a value of x from F(x), we first set 'u' as an uniform distribution, then obtain the inverse function of F(x), and set<br /> &lt;math&gt;x= u^\frac{1}{5}&lt;/math&gt;&lt;br /&gt;&lt;br /&gt;<br /> <br /> Algorithm:&lt;br /&gt;<br /> Steps: &lt;br /&gt;<br /> Step 1: Draw U ~ rand[0, 1];&lt;br /&gt;<br /> Step 2: X=U^(1/5);&lt;br /&gt;<br /> <br /> '''Example 4 - BETA(1,β)''':&lt;br/&gt;<br /> Given u~U[0,1], generate x from BETA(1,β)&lt;br /&gt;<br /> Solution:<br /> &lt;math&gt;F(x)= 1-(1-x)^\beta&lt;/math&gt;, <br /> &lt;math&gt;u= 1-(1-x)^\beta&lt;/math&gt;&lt;br /&gt;<br /> Solve for x: <br /> &lt;math&gt;(1-x)^\beta = 1-u&lt;/math&gt;, <br /> &lt;math&gt;1-x = (1-u)^\frac {1}{\beta}&lt;/math&gt;, <br /> &lt;math&gt;x = 1-(1-u)^\frac {1}{\beta}&lt;/math&gt;&lt;br /&gt;<br /> let β=3, use Matlab to construct N=1000 observations from Beta(1,3)&lt;br /&gt;<br /> '''MatLab Code''':&lt;br /&gt;<br /> <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt; u = rand(1,1000);<br /> x = 1-(1-u)^(1/3);<br /> &gt;&gt; hist(x,50)<br /> &gt;&gt; mean(x)<br /> &lt;/pre&gt;<br /> <br /> '''Example 5 - Estimating &lt;math&gt;\pi&lt;/math&gt;''':&lt;br/&gt;<br /> Let's use rand() and Monte Carlo Method to estimate &lt;math&gt;\pi&lt;/math&gt; &lt;br /&gt;<br /> N= total number of points &lt;br /&gt;<br /> N&lt;sub&gt;c&lt;/sub&gt; = total number of points inside the circle&lt;br /&gt;<br /> Prob[(x,y) lies in the circle=&lt;math&gt;\frac {Area(circle)}{Area(square)}&lt;/math&gt;&lt;br /&gt;<br /> If we take square of size 2, circle will have area =&lt;math&gt;\pi (\frac {2}{2})^2 =\pi&lt;/math&gt;.&lt;br /&gt;<br /> Thus &lt;math&gt;\pi= 4(\frac {N_c}{N})&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;font size=&quot;3&quot;&gt;For example, '''UNIF(a,b)'''&lt;br /&gt;<br /> &lt;math&gt;y = F(x) = (x - a)/ (b - a) &lt;/math&gt;<br /> &lt;math&gt;x = (b - a ) * y + a&lt;/math&gt;<br /> &lt;math&gt;X = a + ( b - a) * U&lt;/math&gt;&lt;br /&gt;<br /> where U is UNIF(0,1)&lt;/font&gt;<br /> <br /> '''Limitations:'''&lt;br /&gt;<br /> 1. This method is flawed since not all functions are invertible or monotonic: generalized inverse is hard to work on.&lt;br /&gt;<br /> 2. It may be impractical since some CDF's and/or integrals are not easy to compute such as Gaussian distribution.&lt;br /&gt;<br /> <br /> We learned how to prove the transformation from cdf to inverse cdf,and use the uniform distribution to obtain a value of x from F(x).<br /> We can also use uniform distribution in inverse method to determine other distributions.<br /> The probability of getting a point for a circle over the triangle is a closed uniform distribution, each point in the circle and over the triangle is almost the same. Then, we can look at the graph to determine what kind of distribution the graph resembles.<br /> <br /> ==== Probability Distribution Function Tool in MATLAB ====<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> disttool #shows different distributions<br /> &lt;/pre&gt; <br /> <br /> This command allows users to explore different types of distribution and see how the changes affect the parameters on the plot of either a CDF or PDF.<br /> <br /> <br /> [[File:Disttool.jpg|450px]]<br /> change the value of mu and sigma can change the graph skew side.<br /> <br /> == Class 3 - Tuesday, May 14 ==<br /> === Recall the Inverse Transform Method ===<br /> Let U~Unif(0,1),then the random variable X = F&lt;sup&gt;-1&lt;/sup&gt;(u) has distribution F. &lt;br /&gt;<br /> To sample X with CDF F(x), &lt;br /&gt;<br /> <br /> &lt;math&gt;1) U~ \sim~ Unif [0,1] &lt;/math&gt;<br /> '''2) X = F&lt;sup&gt;-1&lt;/sup&gt;(u) '''&lt;br /&gt;<br /> <br /> <br /> <br /> <br /> <br /> &lt;br /&gt;<br /> <br /> '''Note''': CDF of a U(a,b) random variable is:<br /> :&lt;math&gt;<br /> F(x)= \begin{cases}<br /> 0 &amp; \text{for }x &lt; a \\[8pt]<br /> \frac{x-a}{b-a} &amp; \text{for }a \le x &lt; b \\[8pt]<br /> 1 &amp; \text{for }x \ge b<br /> \end{cases}<br /> &lt;/math&gt; <br /> <br /> Thus, for &lt;math&gt; U &lt;/math&gt; ~ &lt;math&gt;U(0,1) &lt;/math&gt;, we have &lt;math&gt;P(U\leq 1) = 1&lt;/math&gt; and &lt;math&gt;P(U\leq 1/2) = 1/2&lt;/math&gt;.&lt;br /&gt;<br /> More generally, we see that &lt;math&gt;P(U\leq a) = a&lt;/math&gt;.&lt;br /&gt;<br /> For this reason, we had &lt;math&gt;P(U\leq F(x)) = F(x)&lt;/math&gt;.&lt;br /&gt;<br /> <br /> '''Reminder: ''' &lt;br /&gt; <br /> '''This is only for uniform distribution &lt;math&gt; U~ \sim~ Unif [0,1] &lt;/math&gt; '''&lt;br /&gt;<br /> &lt;math&gt; P (U \le 1) = 1 &lt;/math&gt; &lt;br /&gt;<br /> &lt;math&gt; P (U \le 0.5) = 0.5 &lt;/math&gt; &lt;br /&gt;<br /> &lt;math&gt; P (U \le a) = a &lt;/math&gt; &lt;br /&gt;<br /> <br /> [[File:2.jpg]] &lt;math&gt;P(U\leq a)=a&lt;/math&gt;<br /> <br /> Note that on a single point there is no mass probability (i.e. &lt;math&gt;u&lt;/math&gt; &lt;= 0.5, is the same as &lt;math&gt; u &lt;/math&gt; &lt; 0.5) <br /> More formally, this is saying that &lt;math&gt; P(X = x) = F(x)- \lim_{s \to x^-}F(x)&lt;/math&gt; , which equals zero for any continuous random variable<br /> <br /> ====Limitations of the Inverse Transform Method====<br /> <br /> Though this method is very easy to use and apply, it does have a major disadvantage/limitation:<br /> <br /> * We need to find the inverse cdf &lt;math&gt; F^{-1}(\cdot) &lt;/math&gt;. In some cases the inverse function does not exist, or is difficult to find because it requires a closed form expression for F(x).<br /> <br /> For example, it is too difficult to find the inverse cdf of the Gaussian distribution, so we must find another method to sample from the Gaussian distribution.<br /> <br /> In conclusion, we need to find another way of sampling from more complicated distributions<br /> <br /> === Discrete Case ===<br /> The same technique can be used for discrete case. We want to generate a discrete random variable x, that has probability mass function: &lt;br/&gt;<br /> <br /> :&lt;math&gt;\begin{align}P(X = x_i) &amp;{}= p_i \end{align}&lt;/math&gt;<br /> :&lt;math&gt;x_0 \leq x_1 \leq x_2 \dots \leq x_n&lt;/math&gt;<br /> :&lt;math&gt;\sum p_i = 1&lt;/math&gt;<br /> <br /> Algorithm for applying Inverse Transformation Method in Discrete Case (Procedure):&lt;br&gt;<br /> 1. Define a probability mass function for &lt;math&gt;x_{i}&lt;/math&gt; where i = 1,....,k. Note: k could grow infinitely. &lt;br&gt;<br /> 2. Generate a uniform random number U, &lt;math&gt; U~ \sim~ Unif [0,1] &lt;/math&gt;&lt;br&gt;<br /> 3. If &lt;math&gt;U\leq p_{o}&lt;/math&gt;, deliver &lt;math&gt;X = x_{o}&lt;/math&gt;&lt;br&gt;<br /> 4. Else, if &lt;math&gt;U\leq p_{o} + p_{1} &lt;/math&gt;, deliver &lt;math&gt;X = x_{1}&lt;/math&gt;&lt;br&gt;<br /> 5. Repeat the process again till we reached to &lt;math&gt;U\leq p_{o} + p_{1} + ......+ p_{k}&lt;/math&gt;, deliver &lt;math&gt;X = x_{k}&lt;/math&gt;&lt;br&gt;<br /> <br /> Note that after generating a random U, the value of X can be determined by finding the interval &lt;math&gt;[F(x_{j-1}),F(x_{j})]&lt;/math&gt; in which U lies. &lt;br /&gt;<br /> <br /> <br /> <br /> '''Example 3.0:''' &lt;br /&gt;<br /> Generate a random variable from the following probability function:&lt;br /&gt;<br /> {| class=&quot;wikitable&quot;<br /> |-<br /> |-<br /> | x<br /> | -2<br /> | -1<br /> | 0<br /> | 1<br /> | 2<br /> |-<br /> | f(x)<br /> | 0.1<br /> | 0.5<br /> | 0.07<br /> | 0.03<br /> | 0.3<br /> |}<br /> <br /> Answer:&lt;br /&gt;<br /> 1. Gen U~U(0,1)&lt;br /&gt;<br /> 2. If U &lt; 0.5 then output -1&lt;br /&gt;<br /> else if U &lt; 0.8 then output 2&lt;br /&gt;<br /> else if U &lt; 0.9 then output -2&lt;br /&gt;<br /> else if U &lt; 0.97 then output 0 else output 1&lt;br /&gt;<br /> <br /> '''Example 3.1 (from class):''' (Coin Flipping Example)&lt;br /&gt;<br /> We want to simulate a coin flip. We have U~U(0,1) and X = 0 or X = 1. <br /> <br /> We can define the U function so that: <br /> <br /> If &lt;math&gt;U\leq 0.5&lt;/math&gt;, then X = 0<br /> <br /> and if &lt;math&gt;0.5 &lt; U\leq 1&lt;/math&gt;, then X =1. <br /> <br /> This allows the probability of Heads occurring to be 0.5 and is a good generator of a random coin flip.<br /> <br /> &lt;math&gt; U~ \sim~ Unif [0,1] &lt;/math&gt; <br /> :&lt;math&gt;\begin{align}<br /> P(X = 0) &amp;{}= 0.5\\<br /> P(X = 1) &amp;{}= 0.5\\<br /> \end{align}&lt;/math&gt;<br /> The answer is:<br /> :&lt;math&gt;<br /> x = \begin{cases}<br /> 0, &amp; \text{if } U\leq 0.5 \\<br /> 1, &amp; \text{if } 0.5 &lt; U \leq 1<br /> \end{cases}&lt;/math&gt;<br /> <br /> <br /> * '''Code'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;for ii=1:1000<br /> u=rand;<br /> if u&lt;0.5<br /> x(ii)=0;<br /> else<br /> x(ii)=1;<br /> end<br /> end<br /> &gt;&gt;hist(x)<br /> &lt;/pre&gt;<br /> [[File:Coin_example.jpg|300px]]<br /> <br /> Note: The role of semi-colon in Matlab: Matlab will not print out the results if the line ends in a semi-colon and vice versa.<br /> <br /> '''Example 3.2 (From class):'''<br /> <br /> Suppose we have the following discrete distribution:<br /> <br /> :&lt;math&gt;\begin{align}<br /> P(X = 0) &amp;{}= 0.3 \\<br /> P(X = 1) &amp;{}= 0.2 \\<br /> P(X = 2) &amp;{}= 0.5<br /> \end{align}&lt;/math&gt;<br /> [[File:33.jpg]]<br /> <br /> The cumulative distribution function (cdf) for this distribution is then:<br /> <br /> :&lt;math&gt;<br /> F(x) = \begin{cases}<br /> 0, &amp; \text{if } x &lt; 0 \\<br /> 0.3, &amp; \text{if } x &lt; 1 \\<br /> 0.5, &amp; \text{if } x &lt; 2 \\<br /> 1, &amp; \text{if } x \ge 2<br /> \end{cases}&lt;/math&gt;<br /> <br /> Then we can generate numbers from this distribution like this, given &lt;math&gt;U \sim~ Unif[0, 1]&lt;/math&gt;:<br /> <br /> :&lt;math&gt;<br /> x = \begin{cases}<br /> 0, &amp; \text{if } U\leq 0.3 \\<br /> 1, &amp; \text{if } 0.3 &lt; U \leq 0.5 \\<br /> 2, &amp; \text{if } 0.5 &lt;U\leq 1<br /> \end{cases}&lt;/math&gt;<br /> <br /> &quot;Procedure&quot;&lt;br /&gt;<br /> 1. Draw U~u (0,1)&lt;br /&gt;<br /> 2. if U&lt;=0.3 deliver x=0&lt;br /&gt;<br /> 3. else if 0.3&lt;U&lt;=0.5 deliver x=1&lt;br /&gt;<br /> 4. else 0.5&lt;U&lt;=1 deliver x=2<br /> <br /> Can you find a faster way to run this algorithm? Consider:<br /> <br /> :&lt;math&gt;<br /> x = \begin{cases}<br /> 2, &amp; \text{if } U\leq 0.5 \\<br /> 1, &amp; \text{if } 0.5 &lt; U \leq 0.7 \\<br /> 0, &amp; \text{if } 0.7 &lt;U\leq 1<br /> \end{cases}&lt;/math&gt;<br /> <br /> The logic for this is that U is most likely to fall into the largest range. Thus by putting the largest range (in this case x &gt;= 0.5) we can improve the run time of this algorithm. Could this algorithm be improved further using the same logic?<br /> <br /> * '''Code''' (as shown in class)&lt;br /&gt;<br /> Use Editor window to edit the code &lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;close all<br /> &gt;&gt;clear all<br /> &gt;&gt;for ii=1:1000<br /> u=rand;<br /> if u&lt;=0.3<br /> x(ii)=0;<br /> elseif u&lt;=0.5<br /> x(ii)=1;<br /> else<br /> x(ii)=2;<br /> end<br /> end<br /> &gt;&gt;size(x)<br /> &gt;&gt;hist(x)<br /> &lt;/pre&gt;<br /> [[File:Discrete_example.jpg|300px]]<br /> <br /> The algorithm above generates a vector (1,1000) containing 0's ,1's and 2's in differing proportions. Due to the criteria for accepting 0, 1 or 2 into the vector we get proportions of 0,1 &amp;2 that correspond to their respective probabilities. So plotting the histogram (frequency of 0,1&amp;2) doesn't give us the pmf but a frequency histogram that shows the proportions of each, which looks identical to the pmf.<br /> <br /> '''Example 3.3''': Generating a random variable from pdf &lt;br&gt;<br /> :&lt;math&gt;<br /> f_{x}(x) = \begin{cases}<br /> 2x, &amp; \text{if } 0\leq x \leq 1 \\<br /> 0, &amp; \text{if } otherwise<br /> \end{cases}&lt;/math&gt;<br /> <br /> :&lt;math&gt;<br /> F_{x}(x) = \begin{cases}<br /> 0, &amp; \text{if } x &lt; 0 \\<br /> \int_{0}^{x}2sds = x^{2}, &amp; \text{if } 0\leq x \leq 1 \\<br /> 1, &amp; \text{if } x &gt; 1 <br /> \end{cases}&lt;/math&gt;<br /> <br /> :&lt;math&gt;\begin{align} U = x^{2}, X = F^{-1}x(U)= U^{\frac{1}{2}}\end{align}&lt;/math&gt;<br /> <br /> '''Example 3.4''': Generating a Bernoulli random variable &lt;br&gt;<br /> :&lt;math&gt;\begin{align} P(X = 1) = p, P(X = 0) = 1 - p\end{align}&lt;/math&gt;<br /> :&lt;math&gt;<br /> F(x) = \begin{cases}<br /> 1-p, &amp; \text{if } x &lt; 1 \\<br /> 1, &amp; \text{if } x \ge 1<br /> \end{cases}&lt;/math&gt;<br /> 1. Draw &lt;math&gt; U~ \sim~ Unif [0,1] &lt;/math&gt;&lt;br&gt;<br /> 2. &lt;math&gt;<br /> X = \begin{cases}<br /> 0, &amp; \text{if } 0 &lt; U &lt; 1-p \\<br /> 1, &amp; \text{if } 1-p \le U &lt; 1<br /> \end{cases}&lt;/math&gt;<br /> <br /> <br /> '''Example 3.5''': Generating Binomial(n,p) Random Variable&lt;br&gt;<br /> &lt;math&gt; use p\left( x=i+1\right) =\dfrac {n-i} {i+1}\dfrac {p} {1-p}p\left( x=i\right) &lt;/math&gt;<br /> <br /> Step 1: Generate a random number &lt;math&gt;U&lt;/math&gt;.&lt;br&gt;<br /> Step 2: &lt;math&gt;c = \frac {p}{(1-p)}&lt;/math&gt;, &lt;math&gt;i = 0&lt;/math&gt;, &lt;math&gt;pr = (1-p)^n&lt;/math&gt;, &lt;math&gt;F = pr&lt;/math&gt;&lt;br&gt;<br /> Step 3: If U&lt;F, set X = i and stop,&lt;br&gt;<br /> Step 4: &lt;math&gt; pr = \, {\frac {c(n-i)}{(i+1)}} {pr}, F = F +pr, i = i+1&lt;/math&gt;&lt;br&gt;<br /> Step 5: Go to step 3&lt;br&gt;<br /> *Note: These steps can be found in Simulation 5th Ed. by Sheldon Ross.<br /> *Note: Another method by seeing the Binomial as a sum of n independent Bernoulli random variables, U1, ..., Un. Then set X equal to the number of Ui that are less than or equal to p. To use this method, n random numbers are needed and n comparisons need to be done. On the other hand, the inverse transformation method is simpler because only one random variable needs to be generated and it makes 1 + np comparisons.&lt;br&gt;<br /> Step 1: Generate n uniform numbers U1 ... Un.&lt;br&gt;<br /> Step 2: X = &lt;math&gt;\sum U_i &lt; = p&lt;/math&gt; where P is the probability of success.<br /> <br /> '''Example 3.6''': Generating a Poisson random variable &lt;br&gt;<br /> <br /> &quot;Let X ~ Poi(u). Write an algorithm to generate X.<br /> The PDF of a poisson is:<br /> :&lt;math&gt;\begin{align} f(x) = \frac {\, e^{-u} u^x}{x!} \end{align}&lt;/math&gt;<br /> We know that<br /> :&lt;math&gt;\begin{align} P_{x+1} = \frac {\, e^{-u} u^{x+1}}{(x+1)!} \end{align}&lt;/math&gt;<br /> The ratio is &lt;math&gt;\begin{align} \frac {P_{x+1}}{P_x} = ... = \frac {u}{{x+1}} \end{align}&lt;/math&gt;<br /> Therefore, &lt;math&gt;\begin{align} P_{x+1} = \, {\frac {u}{x+1}} P_x\end{align}&lt;/math&gt;<br /> <br /> Algorithm: &lt;br&gt;<br /> 1) Generate U ~ U(0,1) &lt;br&gt;<br /> 2) &lt;math&gt;\begin{align} X = 0 \end{align}&lt;/math&gt;<br /> &lt;math&gt;\begin{align} F = P(X = 0) = e^{-u}*u^0/{0!} = e^{-u} = p \end{align}&lt;/math&gt;<br /> 3) If U&lt;F, output x &lt;br&gt;<br /> &lt;font size=&quot;3&quot;&gt;Else,&lt;/font&gt; &lt;math&gt;\begin{align} p = (u/(x+1))^p \end{align}&lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt;\begin{align} F = F + p \end{align}&lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt;\begin{align} x = x + 1 \end{align}&lt;/math&gt; &lt;br&gt;<br /> 4) Go to 1&quot; &lt;br&gt;<br /> <br /> Acknowledgements: This is an example from Stat 340 Winter 2013<br /> <br /> <br /> '''Example 3.7''': Generating Geometric Distribution:<br /> <br /> Consider Geo(p) where p is the probability of success, and define random variable X such that X is the total number of trials required to achieve the first success. x=1,2,3..... We have pmf:<br /> &lt;math&gt;P(X=x_i) = \, p (1-p)^{x_{i}-1}&lt;/math&gt;<br /> We have CDF:<br /> &lt;math&gt;F(x)=P(X \leq x)=1-P(X&gt;x) = 1-(1-p)^x&lt;/math&gt;, P(X&gt;x) means we get at least x failures before we observe the first success.<br /> Now consider the inverse transform:<br /> :&lt;math&gt;<br /> x = \begin{cases}<br /> 1, &amp; \text{if } U\leq p \\<br /> 2, &amp; \text{if } p &lt; U \leq 1-(1-p)^2 \\<br /> 3, &amp; \text{if } 1-(1-p)^2 &lt;U\leq 1-(1-p)^3 \\<br /> ....<br /> k, &amp; \text{if } 1-(1-p)^{k-1} &lt;U\leq 1-(1-p)^k <br /> ....<br /> \end{cases}&lt;/math&gt;<br /> <br /> <br /> '''Note''': Unlike the continuous case, the discrete inverse-transform method can always be used for any discrete distribution (but it may not be the most efficient approach) &lt;br&gt;<br /> <br /> <br /> <br /> '''General Procedure'''&lt;br /&gt;<br /> 1. Draw U ~ U(0,1)&lt;br /&gt;<br /> 2. If &lt;math&gt;U \leq P_{0}&lt;/math&gt; deliver &lt;math&gt;x = x_{0}&lt;/math&gt;&lt;br /&gt;<br /> 3. Else if &lt;math&gt;U \leq P_{0} + P_{1}&lt;/math&gt; deliver &lt;math&gt;x = x_{1}&lt;/math&gt;&lt;br /&gt;<br /> 4. Else if &lt;math&gt;U \leq P_{0} + P_{1} + P_{2} &lt;/math&gt; deliver &lt;math&gt;x = x_{2}&lt;/math&gt;&lt;br /&gt;<br /> ... <br /> &lt;font size=&quot;3&quot;&gt;Else if&lt;/font&gt; &lt;math&gt;U \leq P_{0} + ... + P_{k} &lt;/math&gt; &lt;font size=&quot;3&quot;&gt;deliver&lt;/font&gt; &lt;math&gt;x = x_{k}&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;br /'''&gt;===Inverse Transform Algorithm for Generating a Binomial(n,p) Random Variable(from textbook)==='''<br /> &lt;br /&gt;step 1: Generate a random number U<br /> &lt;br /&gt;step 2: c=p/(1-p),i=0, pr=(1-p)&lt;sup&gt;n&lt;/sup&gt;, F=pr.<br /> &lt;br /&gt;step 3: If U&lt;F, set X=i and stop.<br /> &lt;br /&gt;step 4: pr =[c(n-i)/(i+1)]pr, F=F+pr, i=i+1.<br /> &lt;br /&gt;step 5: Go to step 3.<br /> <br /> <br /> '''Problems'''&lt;br /&gt;<br /> Though this method is very easy to use and apply, it does have a major disadvantage/limitation:<br /> We need to find the inverse cdf F^{-1}(\cdot) . In some cases the inverse function does not exist, or is difficult to find because it requires a closed form expression for F(x).<br /> For example, it is too difficult to find the inverse cdf of the Gaussian distribution, so we must find another method to sample from the Gaussian distribution.<br /> In conclusion, we need to find another way of sampling from more complicated distributions<br /> Flipping a coin is a discrete case of uniform distribution, and the code below shows an example of flipping a coin 1000 times; the result is close to the expected value 0.5.&lt;br&gt;<br /> Example 2, as another discrete distribution, shows that we can sample from parts like 0,1 and 2, and the probability of each part or each trial is the same.&lt;br&gt;<br /> Example 3 uses inverse method to figure out the probability range of each random varible.<br /> &lt;div style=&quot;border:1px solid #cccccc;border-radius:10px;box-shadow: 0 5px 15px 1px rgba(0, 0, 0, 0.6), 0 0 200px 1px rgba(255, 255, 255, 0.5);padding:20px;margin:20px;background:#FFFFAD;&quot;&gt;<br /> &lt;h2 style=&quot;text-align:center;&quot;&gt;Summary of Inverse Transform Method&lt;/h2&gt;<br /> &lt;p&gt;&lt;b&gt;Problem:&lt;/b&gt;generate types of distribution.&lt;/p&gt;<br /> &lt;p&gt;&lt;b&gt;Plan:&lt;/b&gt;&lt;/p&gt;<br /> &lt;b style='color:lightblue;'&gt;Continuous case:&lt;/b&gt;<br /> &lt;ol&gt;<br /> &lt;li&gt;find CDF F&lt;/li&gt;<br /> &lt;li&gt;find the inverse F&lt;sup&gt;-1&lt;/sup&gt;&lt;/li&gt;<br /> &lt;li&gt;Generate a list of uniformly distributed number {x}&lt;/li&gt;<br /> &lt;li&gt;{F&lt;sup&gt;-1&lt;/sup&gt;(x)} is what we want&lt;/li&gt;<br /> &lt;/ol&gt;<br /> &lt;b&gt;Matlab Instruction&lt;/b&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;&amp;gt;&amp;gt;u=rand(1,1000);<br /> &amp;gt;&amp;gt;hist(u)<br /> &amp;gt;&amp;gt;x=(-log(1-u))/2;<br /> &amp;gt;&amp;gt;size(x) <br /> &amp;gt;&amp;gt;figure<br /> &amp;gt;&amp;gt;hist(x)<br /> &lt;/pre&gt;<br /> &lt;br&gt;<br /> &lt;b style='color:lightblue'&gt;Discrete case:&lt;/b&gt;<br /> &lt;ol&gt;<br /> &lt;li&gt;generate a list of uniformly distributed number {u}&lt;/li&gt;<br /> &lt;li&gt;d&lt;sub&gt;i&lt;/sub&gt;=x&lt;sub&gt;i&lt;/sub&gt; if&lt;math&gt; X=x_i, &lt;/math&gt; if &lt;math&gt; F(x_{i-1})&lt;U\leq F(x_i) &lt;/math&gt;&lt;/li&gt;<br /> &lt;li&gt;{d&lt;sub&gt;i&lt;/sub&gt;=x&lt;sub&gt;i&lt;/sub&gt;} is what we want&lt;/li&gt;<br /> &lt;/ol&gt;<br /> &lt;b&gt;Matlab Instruction&lt;/b&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;&amp;gt;&amp;gt;for ii=1:1000<br /> u=rand;<br /> if u&amp;lt;0.5<br /> x(ii)=0;<br /> else<br /> x(ii)=1;<br /> end<br /> end<br /> &amp;gt;&amp;gt;hist(x)<br /> &lt;/pre&gt;<br /> &lt;/div&gt;<br /> <br /> === Generalized Inverse-Transform Method ===<br /> <br /> Valid for any CDF F(x): return X=min{x:F(x)&lt;math&gt;\leq&lt;/math&gt; U}, where U~U(0,1)<br /> <br /> 1. Continues, possibly with flat spots (i.e. not strictly increasing) <br /> <br /> 2. Discrete<br /> <br /> 3. Mixed continues discrete<br /> <br /> <br /> '''Advantages of Inverse-Transform Method'''<br /> <br /> Inverse transform method preserves monotonicity and correlation<br /> <br /> which helps in<br /> <br /> 1. Variance reduction methods ...<br /> <br /> 2. Generating truncated distributions ...<br /> <br /> 3. Order statistics ...<br /> <br /> ===Acceptance-Rejection Method===<br /> <br /> Although the inverse transformation method does allow us to change our uniform distribution, it has two limits;<br /> # Not all functions have inverse functions (ie, the range of x and y have limit and do not fix the inverse functions)<br /> # For some distributions, such as Gaussian, it is too difficult to find the inverse<br /> <br /> To generate random samples for these functions, we will use different methods, such as the '''Acceptance-Rejection Method'''. This method is more efficient than the inverse transform method. The basic idea is to find an alternative probability distribution with density function f(x);<br /> <br /> Suppose we want to draw random sample from a target density function ''f(x)'', ''x∈S&lt;sub&gt;x&lt;/sub&gt;'', where ''S&lt;sub&gt;x&lt;/sub&gt;'' is the support of ''f(x)''. If we can find some constant ''c''(≥1) (In practice, we prefer c as close to 1 as possible) and a density function ''g(x)'' having the same support ''S&lt;sub&gt;x&lt;/sub&gt;'' so that ''f(x)≤cg(x), ∀x∈S&lt;sub&gt;x&lt;/sub&gt;'', then we can apply the procedure for Acceptance-Rejection Method. Typically we choose a density function that we already know how to sample from for ''g(x)''.<br /> <br /> [[File:AR_Method.png]]<br /> <br /> <br /> The main logic behind the Acceptance-Rejection Method is that:&lt;br&gt;<br /> 1. We want to generate sample points from an unknown distribution, say f(x).&lt;br&gt;<br /> 2. We use &lt;math&gt;\,cg(x)&lt;/math&gt; to generate points so that we have more points than f(x) could ever generate for all x. (where c is a constant, and g(x) is a known distribution)&lt;br&gt;<br /> 3. For each value of x, we accept and reject some points based on a probability, which will be discussed below.&lt;br&gt;<br /> <br /> Note: If the red line was only g(x) as opposed to &lt;math&gt;\,c g(x)&lt;/math&gt; (i.e. c=1), then &lt;math&gt;g(x) \geq f(x)&lt;/math&gt; for all values of x if and only if g and f are the same functions. This is because the sum of pdf of g(x)=1 and the sum of pdf of f(x)=1, hence, &lt;math&gt;g(x) \ngeqq f(x)&lt;/math&gt; \,&amp;forall;x. &lt;br&gt;<br /> <br /> Also remember that &lt;math&gt;\,c g(x)&lt;/math&gt; always generates higher probability than what we need. Thus we need an approach of getting the proper probabilities.&lt;br&gt;&lt;br&gt;<br /> <br /> c must be chosen so that &lt;math&gt;f(x)\leqslant c g(x)&lt;/math&gt; for all value of x. c can only equal 1 when f and g have the same distribution. Otherwise:&lt;br&gt;<br /> Either use a software package to test if &lt;math&gt;f(x)\leqslant c g(x)&lt;/math&gt; for an arbitrarily chosen c &gt; 0, or:&lt;br&gt;<br /> 1. Find first and second derivatives of f(x) and g(x).&lt;br&gt;<br /> 2. Identify and classify all local and absolute maximums and minimums, using the First and Second Derivative Tests, as well as all inflection points.&lt;br&gt;<br /> 3. Verify that &lt;math&gt;f(x)\leqslant c g(x)&lt;/math&gt; at all the local maximums as well as the absolute maximums.&lt;br&gt;<br /> 4. Verify that &lt;math&gt;f(x)\leqslant c g(x)&lt;/math&gt; at the tail ends by calculating &lt;math&gt;\lim_{x \to +\infty} \frac{f(x)}{\, c g(x)}&lt;/math&gt; and &lt;math&gt;\lim_{x \to -\infty} \frac{f(x)}{\, c g(x)}&lt;/math&gt; and seeing that they are both &lt; 1. Use of L'Hopital's Rule should make this easy, since both f and g are p.d.f's, resulting in both of them approaching 0.&lt;br&gt;<br /> 5.Efficiency: the number of times N that steps 1 and 2 need to be called(also the number of iterations needed to successfully generate X) is a random variable and has a geometric distribution with success probability &lt;math&gt;p=P(U \leq f(Y)/(cg(Y)))&lt;/math&gt; , &lt;math&gt;P(N=n)=(1-p(n-1))p ,n \geq 1&lt;/math&gt;.Thus on average the number of iterations required is given by &lt;math&gt; E(N)=\frac{1} p&lt;/math&gt;<br /> <br /> c should be close to the maximum of f(x)/g(x), not just some arbitrarily picked large number. Otherwise, the Acceptance-Rejection method will have more rejections (since our probability &lt;math&gt;f(x)\leqslant c g(x)&lt;/math&gt; will be close to zero). This will render our algorithm inefficient. <br /> <br /> The expected number of iterations of the algorithm required with an X is c.<br /> &lt;br&gt;<br /> '''Note:''' &lt;br&gt;<br /> 1. Value around x&lt;sub&gt;1&lt;/sub&gt; will be sampled more often under cg(x) than under f(x).There will be more samples than we actually need, if &lt;math&gt;\frac{f(y)}{\, c g(y)}&lt;/math&gt; is small, the acceptance-rejection technique will need to be done to these points to get the accurate amount.In the region above x&lt;sub&gt;1&lt;/sub&gt;, we should accept less and reject more. &lt;br&gt;<br /> 2. Value around x&lt;sub&gt;2&lt;/sub&gt;: number of sample that are drawn and the number we need are much closer. So in the region above x&lt;sub&gt;2&lt;/sub&gt;, we accept more. As a result, g(x) and f(x) are comparable.&lt;br&gt;<br /> 3. The constant c is needed because we need to adjust the height of g(x) to ensure that it is above f(x). Besides that, it is best to keep the number of rejected varieties small for maximum efficiency. &lt;br&gt; <br /> <br /> Another way to understand why the the acceptance probability is &lt;math&gt;\frac{f(y)}{\, c g(y)}&lt;/math&gt;, is by thinking of areas. From the graph above, we see that the target function in under the proposed function c g(y). Therefore, &lt;math&gt;\frac{f(y)}{\, c g(y)}&lt;/math&gt; is the proportion or the area under c g(y) that also contains f(y). Therefore we say we accept sample points for which u is less then &lt;math&gt;\frac{f(y)}{\, c g(y)}&lt;/math&gt; because then the sample points are guaranteed to fall under the area of c g(y) that contains f(y). &lt;br&gt;<br /> &lt;br&gt;<br /> <br /> '''There are 2 cases that are possible:''' &lt;br&gt;<br /> -Sample of points is more than enough, &lt;math&gt;c g(x) \geq f(x) &lt;/math&gt; &lt;br&gt;<br /> -Similar or the same amount of points, &lt;math&gt;c g(x) \geq f(x) &lt;/math&gt; &lt;br&gt;<br /> '''There is 1 case that is not possible:''' &lt;br&gt;<br /> -Less than enough points, such that &lt;math&gt; g(x) &lt;/math&gt; is greater than &lt;math&gt; f &lt;/math&gt;, &lt;math&gt;g(x) \geq f(x)&lt;/math&gt; &lt;br&gt;<br /> &lt;br&gt;<br /> <br /> '''Procedure'''<br /> <br /> #Draw Y~g(.)<br /> #Draw U~u(0,1) (Note: U and Y are independent)<br /> #If &lt;math&gt;u\leq \frac{f(y)}{cg(y)}&lt;/math&gt; (which is &lt;math&gt;P(accepted|y)&lt;/math&gt;) then x=y, else return to Step 1&lt;br&gt;<br /> <br /> <br /> Note: Recall &lt;math&gt;P(U\leq a)=a&lt;/math&gt;. Thus by comparing u and &lt;math&gt;\frac{f(y)}{\, c g(y)}&lt;/math&gt;, we can get a probability of accepting y at these points. For instance, at some points that cg(x) is much larger than f(x), the probability of accepting x=y is quite small.&lt;br&gt;<br /> ie. At X&lt;sub&gt;1&lt;/sub&gt;, low probability to accept the point since f(x) is much smaller than cg(x).&lt;br&gt;<br /> At X&lt;sub&gt;2&lt;/sub&gt;, high probability to accept the point. &lt;math&gt;P(U\leq a)=a&lt;/math&gt; in Uniform Distribution.<br /> <br /> Note: Since U is the variable for uniform distribution between 0 and 1. It equals to 1 for all. The condition depends on the constant c. so the condition changes to &lt;math&gt;c\leq \frac{f(y)}{g(y)}&lt;/math&gt;<br /> <br /> <br /> introduce the relationship of cg(x)and f(x),and prove why they have that relationship and where we can use this rule to reject some cases.<br /> and learn how to see the graph to find the accurate point to reject or accept the ragion above the random variable x.<br /> for the example, x1 is bad point and x2 is good point to estimate the rejection and acceptance<br /> <br /> '''Some notes on the constant C'''&lt;br&gt;<br /> 1. C is chosen such that &lt;math&gt; c g(y)\geq f(y)&lt;/math&gt;, that is,&lt;math&gt; c g(y)&lt;/math&gt; will always dominate &lt;math&gt;f(y)&lt;/math&gt;. Because of this, <br /> C will always be greater than or equal to one and will only equal to one if and only if the proposal distribution and the target distribution are the same. It is normally best to choose C such that the absolute maxima of both &lt;math&gt; c g(y)&lt;/math&gt; and &lt;math&gt; f(y)&lt;/math&gt; are the same.&lt;br&gt;<br /> <br /> 2. &lt;math&gt; \frac {1}{C} &lt;/math&gt; is the area of &lt;math&gt; F(y)&lt;/math&gt; over the area of &lt;math&gt; c G(y)&lt;/math&gt; and is the acceptance rate of the points generated. For example, if &lt;math&gt; \frac {1}{C} = 0.7&lt;/math&gt; then on average, 70 percent of all points generated are accepted.&lt;br&gt;<br /> <br /> 3. C is the average number of times Y is generated from g .<br /> <br /> === Theorem ===<br /> <br /> Let &lt;math&gt;f: \R \rightarrow [0,+\infty]&lt;/math&gt; be a well-defined pdf, and &lt;math&gt;\displaystyle Y&lt;/math&gt; be a random variable with pdf &lt;math&gt;g: \R \rightarrow [0,+\infty]&lt;/math&gt; such that &lt;math&gt;\exists c \in \R^+&lt;/math&gt; with &lt;math&gt;f \leq c \cdot g&lt;/math&gt;. If &lt;math&gt;\displaystyle U \sim~ U(0,1)&lt;/math&gt; is independent of &lt;math&gt;\displaystyle Y&lt;/math&gt;, then the random variable defined as &lt;math&gt;X := Y \vert U \leq \frac{f(Y)}{c \cdot g(Y)}&lt;/math&gt; has pdf &lt;math&gt;\displaystyle f&lt;/math&gt;, and the condition &lt;math&gt;U \leq \frac{f(Y)}{c \cdot g(Y)}&lt;/math&gt; is denoted by &quot;Accepted&quot;.<br /> <br /> === Proof ===<br /> Recall the conditional probability formulas:&lt;br /&gt;<br /> &lt;math&gt;\begin{align}<br /> P(A|B)=\frac{P(A \cap B)}{P(B)}, \text{ or }P(A|B)=\frac{P(B|A)P(A)}{P(B)} \text{ for pmf}<br /> \end{align}&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;P(y|accepted)=f(y)=\frac{P(accepted|y)P(y)}{P(accepted)}&lt;/math&gt;&lt;br /&gt; <br /> &lt;br /&gt;based on the concept from '''procedure-step1''':&lt;br /&gt;<br /> &lt;math&gt;P(y)=g(y)&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;P(accepted|y)=\frac{f(y)}{cg(y)}&lt;/math&gt; &lt;br /&gt;<br /> (the larger the value is, the larger the chance it will be selected) &lt;br /&gt;&lt;br /&gt;<br /> <br /> <br /> &lt;math&gt;<br /> \begin{align}<br /> P(accepted)&amp;=\int_y\ P(accepted|y)P(y)\\<br /> &amp;=\int_y\ \frac{f(s)}{cg(s)}g(s)ds\\<br /> &amp;=\frac{1}{c} \int_y\ f(s) ds\\<br /> &amp;=\frac{1}{c}<br /> \end{align}&lt;/math&gt;&lt;br /&gt;<br /> <br /> Therefore:&lt;br /&gt;<br /> &lt;math&gt;\begin{align}<br /> P(x)&amp;=P(y|accepted)\\<br /> &amp;=\frac{\frac{f(y)}{cg(y)}g(y)}{1/c}\\<br /> &amp;=\frac{\frac{f(y)}{c}}{1/c}\\<br /> &amp;=f(y)\end{align}&lt;/math&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;<br /> <br /> '''''Here is an alternative introduction of Acceptance-Rejection Method'''''<br /> <br /> '''Comments:'''<br /> <br /> -Acceptance-Rejection Method is not good for all cases. The limitation with this method is that sometimes many points will be rejected. One obvious disadvantage is that it could be very hard to pick the &lt;math&gt;g(y)&lt;/math&gt; and the constant &lt;math&gt;c&lt;/math&gt; in some cases. We have to pick the SMALLEST C such that &lt;math&gt;cg(x) \leq f(x)&lt;/math&gt; else the the algorithm will not be efficient. This is because &lt;math&gt;f(x)/cg(x)&lt;/math&gt; will become smaller and probability &lt;math&gt;u \leq f(x)/cg(x)&lt;/math&gt; will go down and many points will be rejected making the algorithm inefficient. <br /> <br /> -'''Note:''' When &lt;math&gt;f(y)&lt;/math&gt; is very different than &lt;math&gt;g(y)&lt;/math&gt;, it is less likely that the point will be accepted as the ratio above would be very small and it will be difficult for &lt;math&gt;U&lt;/math&gt; to be less than this small value. &lt;br/&gt;An example would be when the target function (&lt;math&gt;f&lt;/math&gt;) has a spike or several spikes in its domain - this would force the known distribution (&lt;math&gt;g&lt;/math&gt;) to have density at least as large as the spikes, making the value of &lt;math&gt;c&lt;/math&gt; larger than desired. As a result, the algorithm would be highly inefficient.<br /> <br /> '''Acceptance-Rejection Method'''&lt;br/&gt;<br /> '''Example 1''' (discrete case)&lt;br/&gt;<br /> We wish to generate X~Bi(2,0.5), assuming that we cannot generate this directly.&lt;br/&gt;<br /> We use a discrete distribution DU[0,2] to approximate this.&lt;br/&gt;<br /> &lt;math&gt;f(x)=Pr(X=x)=2Cx×(0.5)^2\,&lt;/math&gt;&lt;br/&gt;<br /> <br /> {| class=wikitable align=left<br /> |&lt;math&gt;x&lt;/math&gt;||0||1||2 <br /> |-<br /> |&lt;math&gt;f(x)&lt;/math&gt;||1/4||1/2||1/4 <br /> |-<br /> |&lt;math&gt;g(x)&lt;/math&gt;||1/3||1/3||1/3 <br /> |-<br /> |&lt;math&gt;c=f(x)/g(x)&lt;/math&gt;||3/4||3/2||3/4<br /> |-<br /> |&lt;math&gt;f(x)/(cg(x))&lt;/math&gt;||1/2||1||1/2<br /> |}<br /> <br /> <br /> Since we need &lt;math&gt;c \geq f(x)/g(x)&lt;/math&gt;&lt;br/&gt;<br /> We need &lt;math&gt;c=3/2&lt;/math&gt;&lt;br/&gt;<br /> <br /> Therefore, the algorithm is:&lt;br/&gt;<br /> 1. Generate &lt;math&gt;u,v~U(0,1)&lt;/math&gt;&lt;br/&gt;<br /> 2. Set &lt;math&gt;y= \lfloor 3*u \rfloor&lt;/math&gt; (This is using uniform distribution to generate DU[0,2]&lt;br/&gt;<br /> 3. If &lt;math&gt;(y=0)&lt;/math&gt; and &lt;math&gt;(v&lt;\tfrac{1}{2}), output=0&lt;/math&gt; &lt;br/&gt;<br /> If &lt;math&gt;(y=2) &lt;/math&gt; and &lt;math&gt;(v&lt;\tfrac{1}{2}), output=2 &lt;/math&gt;&lt;br/&gt;<br /> Else if &lt;math&gt;y=1, output=1&lt;/math&gt;&lt;br/&gt;<br /> <br /> <br /> An elaboration of “c”&lt;br/&gt;<br /> c is the expected number of times the code runs to output 1 random variable. Remember that when &lt;math&gt;u &lt; \tfrac{f(x)}{cg(x)}&lt;/math&gt; is not satisfied, we need to go over the code again.&lt;br/&gt;<br /> <br /> Proof&lt;br/&gt;<br /> <br /> Let &lt;math&gt;f(x)&lt;/math&gt; be the function we wish to generate from, but we cannot use inverse transform method to generate directly.&lt;br/&gt;<br /> Let &lt;math&gt;g(x)&lt;/math&gt; be the helper function &lt;br/&gt;<br /> Let &lt;math&gt;kg(x)&gt;=f(x)&lt;/math&gt;&lt;br/&gt;<br /> Since we need to generate y from &lt;math&gt;g(x)&lt;/math&gt;,&lt;br/&gt;<br /> &lt;math&gt;Pr(select y)=g(y)&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;Pr(output y|selected y)=Pr(u&lt;f(y)/(cg(y)))= f(y)/(cg(y))&lt;/math&gt; (Since u~Unif(0,1))&lt;br/&gt;<br /> &lt;math&gt;Pr(output y)=Pr(output y1|selected y1)Pr(select y1)+ Pr(output y2|selected y2)Pr(select y2)+…+ Pr(output yn|selected yn)Pr(select yn)=1/c&lt;/math&gt; &lt;br/&gt;<br /> Consider that we are asking for expected time for the first success, it is a geometric distribution with probability of success=1/c&lt;br/&gt;<br /> Therefore, &lt;math&gt;E(X)=1/(1/c))=c&lt;/math&gt; &lt;br/&gt;<br /> <br /> Acknowledgements: Some materials have been borrowed from notes from Stat340 in Winter 2013.<br /> <br /> Use the conditional probability to proof if the probability is accepted, then the result is closed pdf of the original one.<br /> the example shows how to choose the c for the two function &lt;math&gt;g(x)&lt;/math&gt; and &lt;math&gt;f(x)&lt;/math&gt;.<br /> <br /> === Example of Acceptance-Rejection Method===<br /> <br /> Generating a random variable having p.d.f. &lt;br /&gt;<br /> &lt;math&gt;\displaystyle f(x) = 20x(1 - x)^3, 0&lt; x &lt;1 &lt;/math&gt;&lt;br /&gt;<br /> Since this random variable (which is beta with parameters (2,4)) is concentrated in the interval (0, 1), let us consider the acceptance-rejection method with&lt;br /&gt;<br /> &lt;math&gt;\displaystyle g(x) = 1,0&lt;x&lt;1&lt;/math&gt;&lt;br /&gt;<br /> To determine the constant c such that f(x)/g(x) &lt;= c, we use calculus to determine the maximum value of&lt;br /&gt;<br /> &lt;math&gt;\displaystyle f(x)/g(x) = 20x(1 - x)^3 &lt;/math&gt;&lt;br /&gt;<br /> Differentiation of this quantity yields &lt;br /&gt; <br /> &lt;math&gt;\displaystyle d/dx[f(x)/g(x)]=20*[(1-x)^3-3x(1-x)^2]&lt;/math&gt;&lt;br /&gt;<br /> Setting this equal to 0 shows that the maximal value is attained when x = 1/4, <br /> and thus, &lt;br /&gt;<br /> &lt;math&gt;\displaystyle f(x)/g(x)&lt;= 20*(1/4)*(3/4)^3=135/64=c &lt;/math&gt;&lt;br /&gt;<br /> Hence,&lt;br /&gt;<br /> &lt;math&gt;\displaystyle f(x)/cg(x)=(256/27)*(x*(1-x)^3)&lt;/math&gt;&lt;br /&gt;<br /> and thus the simulation procedure is as follows:<br /> <br /> 1) Generate two random numbers U1 and U2 .<br /> <br /> 2) If U&lt;sub&gt;2&lt;/sub&gt;&lt;(256/27)*U&lt;sub&gt;1&lt;/sub&gt;*(1-U&lt;sub&gt;1&lt;/sub&gt;)&lt;sup&gt;3&lt;/sup&gt;, set X=U&lt;sub&gt;1&lt;/sub&gt;, and stop<br /> Otherwise return to step 1). <br /> The average number of times that step 1) will be performed is c = 135/64.<br /> <br /> (The above example is from http://www.cs.bgu.ac.il/~mps042/acceptance.htm, example 2.)<br /> <br /> use the derivative to proof the accepetance-rejection method,<br /> find the local maximum of f(x)/g(x).<br /> and we can calculate the best constant c.<br /> <br /> ===Another Example of Acceptance-Rejection Method===<br /> Generate a random variable from:&lt;br /&gt; <br /> &lt;math&gt;\displaystyle f(x)=3*x^2, 0&lt;x&lt;1 &lt;/math&gt;&lt;br /&gt;<br /> Assume g(x) to be uniform over interval (0,1), where 0&lt; x &lt;1&lt;br /&gt;<br /> Therefore:&lt;br /&gt;<br /> &lt;math&gt;\displaystyle c = max(f(x)/(g(x)))= 3&lt;/math&gt;&lt;br /&gt; <br /> <br /> the best constant c is the max(f(x)/(cg(x))) and the c make the area above the f(x) and below the g(x) to be small.<br /> because g(.) is uniform so the g(x) is 1. max(g(x)) is 1&lt;br /&gt;<br /> &lt;math&gt;\displaystyle f(x)/(cg(x))= x^2&lt;/math&gt;&lt;br /&gt;<br /> Acknowledgement: this is example 1 from http://www.cs.bgu.ac.il/~mps042/acceptance.htm<br /> <br /> == Class 4 - Thursday, May 16 == <br /> <br /> '''Goals'''&lt;br&gt; <br /> *When we want to find target distribution &lt;math&gt;f(x)&lt;/math&gt;, we need to first find a proposal distribution &lt;math&gt;g(x)&lt;/math&gt; that is easy to sample from. &lt;br&gt; <br /> *Relationship between the proposal distribution and target distribution is: &lt;math&gt; c \cdot g(x) \geq f(x) &lt;/math&gt;, where c is constant. This means that the area of f(x) is under the area of &lt;math&gt; c \cdot g(x)&lt;/math&gt;. &lt;br&gt;<br /> *Chance of acceptance is less if the distance between &lt;math&gt;f(x)&lt;/math&gt; and &lt;math&gt; c \cdot g(x)&lt;/math&gt; is big, and vice-versa, we use &lt;math&gt; c &lt;/math&gt; to keep &lt;math&gt; \frac {f(x)}{c \cdot g(x)} &lt;/math&gt; below 1 (so &lt;math&gt;f(x) \leq c \cdot g(x)&lt;/math&gt;). Therefore, we must find the constant &lt;math&gt; C &lt;/math&gt; to achieve this.&lt;br /&gt;<br /> *In other words, &lt;math&gt;C&lt;/math&gt; is chosen to make sure &lt;math&gt; c \cdot g(x) \geq f(x) &lt;/math&gt;. However, it will not make sense if &lt;math&gt;C&lt;/math&gt; is simply chosen to be arbitrarily large. We need to choose &lt;math&gt;C&lt;/math&gt; such that &lt;math&gt;c \cdot g(x)&lt;/math&gt; fits &lt;math&gt;f(x)&lt;/math&gt; as tightly as possible. This means that we must find the minimum c such that the area of f(x) is under the area of c*g(x). &lt;br /&gt;<br /> *The constant c cannot be a negative number.&lt;br /&gt;<br /> <br /> <br /> '''How to find C''':&lt;br /&gt;<br /> <br /> &lt;math&gt;\begin{align}<br /> &amp;c \cdot g(x) \geq f(x)\\<br /> &amp;c\geq \frac{f(x)}{g(x)} \\<br /> &amp;c= \max \left(\frac{f(x)}{g(x)}\right) <br /> \end{align}&lt;/math&gt;&lt;br&gt;<br /> <br /> If &lt;math&gt;f&lt;/math&gt; and &lt;math&gt; g &lt;/math&gt; are continuous, we can find the extremum by taking the derivative and solve for &lt;math&gt;x_0&lt;/math&gt; such that:&lt;br/&gt;<br /> &lt;math&gt; 0=\frac{d}{dx}\frac{f(x)}{g(x)}|_{x=x_0}&lt;/math&gt; &lt;br/&gt;<br /> <br /> Thus &lt;math&gt; c = \frac{f(x_0)}{g(x_0)} &lt;/math&gt;&lt;br/&gt;<br /> <br /> Note: This procedure is called the Acceptance-Rejection Method.&lt;br&gt; <br /> <br /> '''The Acceptance-Rejection method''' involves finding a distribution that we know how to sample from, g(x), and multiplying g(x) by a constant c so that &lt;math&gt;c \cdot g(x)&lt;/math&gt; is always greater than or equal to f(x). Mathematically, we want &lt;math&gt; c \cdot g(x) \geq f(x) &lt;/math&gt;.<br /> And it means, c has to be greater or equal to &lt;math&gt;\frac{f(x)}{g(x)}&lt;/math&gt;. So the smallest possible c that satisfies the condition is the maximum value of &lt;math&gt;\frac{f(x)}{g(x)}&lt;/math&gt;&lt;br/&gt;. <br /> But in case of c being too large, the chance of acceptance of generated values will be small, thereby losing efficiency of the algorithm. Therefore, it is best to get the smallest possible c such that &lt;math&gt; c g(x) \geq f(x)&lt;/math&gt;. &lt;br&gt;<br /> <br /> '''Important points:'''&lt;br&gt; <br /> <br /> *For this method to be efficient, the constant c must be selected so that the rejection rate is low. (The efficiency for this method is &lt;math&gt;\left ( \frac{1}{c} \right )&lt;/math&gt;)&lt;br&gt;<br /> *It is easy to show that the expected number of trials for an acceptance is &lt;math&gt; \frac{Total Number of Trials} {C} &lt;/math&gt;. &lt;br&gt;<br /> *recall the '''acceptance rate is 1/c'''. (Not rejection rate) <br /> :Let &lt;math&gt;X&lt;/math&gt; be the number of trials for an acceptance, &lt;math&gt; X \sim~ Geo(\frac{1}{c})&lt;/math&gt;&lt;br&gt;<br /> :&lt;math&gt;\mathbb{E}[X] = \frac{1}{\frac{1}{c}} = c &lt;/math&gt;<br /> *The number of trials needed to generate a sample size of &lt;math&gt;N&lt;/math&gt; follows a negative binomial distribution. The expected number of trials needed is then &lt;math&gt;cN&lt;/math&gt;.&lt;br&gt;<br /> *So far, the only distribution we know how to sample from is the '''UNIFORM''' distribution. &lt;br&gt;<br /> <br /> <br /> '''Procedure''': &lt;br&gt;<br /> <br /> 1. Choose &lt;math&gt;g(x)&lt;/math&gt; (simple density function that we know how to sample, i.e. Uniform so far) &lt;br&gt;<br /> The easiest case is &lt;math&gt;U~ \sim~ Unif [0,1] &lt;/math&gt;. However, in other cases we need to generate UNIF(a,b). We may need to perform a linear transformation on the &lt;math&gt;U~ \sim~ Unif [0,1] &lt;/math&gt; variable. &lt;br&gt;<br /> 2. Find a constant c such that :&lt;math&gt; c \cdot g(x) \geq f(x) &lt;/math&gt;, otherwise return to step 1.<br /> <br /> '''Recall the general procedure of Acceptance-Rejection Method'''<br /> #Let &lt;math&gt;Y \sim~ g(y)&lt;/math&gt; <br /> #Let &lt;math&gt;U \sim~ Unif [0,1] &lt;/math&gt;<br /> #If &lt;math&gt;U \leq \frac{f(Y)}{c \cdot g(Y)}&lt;/math&gt; then X=Y; else return to step 1 (This is not the way to find C. This is the general procedure.)<br /> <br /> &lt;hr&gt;&lt;b&gt;Example: &lt;br&gt; <br /> <br /> Generate a random variable from the pdf&lt;/b&gt;&lt;br&gt;<br /> &lt;math&gt; f(x) = <br /> \begin{cases} <br /> 2x, &amp; \mbox{if }0 \leqslant x \leqslant 1 \\<br /> 0, &amp; \mbox{otherwise}<br /> \end{cases} &lt;/math&gt;<br /> <br /> We can note that this is a special case of Beta(2,1), where, <br /> &lt;math&gt;beta(a,b)=\frac{\Gamma(a+b)}{\Gamma(a)\Gamma(b)}x^{(a-1)}(1-x)^{(b-1)}&lt;/math&gt;&lt;br&gt;<br /> <br /> Where &amp;Gamma; (n) = (n - 1)! if n is positive integer<br /> <br /> &lt;math&gt;Gamma(z)=\int _{0}^{\infty }t^{z-1}e^{-t}dt&lt;/math&gt;<br /> <br /> Aside: Beta function<br /> <br /> In mathematics, the beta function, also called the Euler integral of the first kind, is a special function defined by<br /> &lt;math&gt;B(x,y)=\int_0^1 \! {t^{(x-1)}}{(1-t)^{(y-1)}}\,dt&lt;/math&gt;&lt;br&gt;<br /> <br /> <br /> &lt;math&gt;beta(2,1)= \frac{\Gamma(3)}{(\Gamma(2)\Gamma(1))}x^1 (1-x)^0 = 2x&lt;/math&gt;&lt;br&gt;<br /> <br /> &lt;hr&gt;<br /> &lt;math&gt;g=u(0,1)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;y=g&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;f(x)\leq c\cdot g(x)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;c\geq \frac{f(x)}{g(x)}&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;c = \max \frac{f(x)}{g(x)} &lt;/math&gt;&lt;br&gt;<br /> &lt;br&gt;&lt;math&gt;c = \max \frac{2x}{1}, 0 \leq x \leq 1&lt;/math&gt;&lt;br&gt;<br /> Taking x = 1 gives the highest possible c, which is c=2<br /> &lt;br /&gt;Note that c is a scalar greater than 1.<br /> &lt;br /&gt;cg(x) is proposal dist, and f(x) is target dist.<br /> <br /> [[File:Beta(2,1)_example.jpg|750x750px]]<br /> <br /> '''Note:''' g follows uniform distribution, it only covers half of the graph which runs from 0 to 1 on y-axis. Thus we need to multiply by c to ensure that &lt;math&gt;c\cdot g&lt;/math&gt; can cover entire f(x) area. In this case, c=2, so that makes g run from 0 to 2 on y-axis which covers f(x).<br /> <br /> '''Comment:'''&lt;br&gt; <br /> From the picture above, we could observe that the area under f(x)=2x is a half of the area under the pdf of UNIF(0,1). This is why in order to sample 1000 points of f(x), we need to sample approximately 2000 points in UNIF(0,1).<br /> And in general, if we want to sample n points from a distritubion with pdf f(x), we need to scan approximately &lt;math&gt;n\cdot c&lt;/math&gt; points from the proposal distribution (g(x)) in total. &lt;br&gt;<br /> &lt;b&gt;Step&lt;/b&gt;<br /> &lt;ol&gt;<br /> &lt;li&gt;Draw y~U(0,1)&lt;/li&gt;<br /> &lt;li&gt;Draw u~U(0,1)&lt;/li&gt;<br /> &lt;li&gt;if &lt;math&gt;u \leq \frac{(2\cdot y)}{(2\cdot 1)}, u \leq y,&lt;/math&gt; then &lt;math&gt; x=y&lt;/math&gt;&lt;br&gt;<br /> &lt;li&gt;Else go to Step 1&lt;/li&gt;<br /> &lt;/ol&gt;<br /> <br /> '''Note:''' In the above example, we sample 2 numbers. If second number (u) is less than or equal to first number (y), then accept x=y, if not then start all over.<br /> <br /> &lt;span style=&quot;font-weight:bold;color:green;&quot;&gt;Matlab Code&lt;/span&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;close all<br /> &gt;&gt;clear all<br /> &gt;&gt;ii=1; # ii:numbers that are accepted<br /> &gt;&gt;jj=1; # jj:numbers that are generated<br /> &gt;&gt;while ii&lt;1000<br /> y=rand;<br /> u=rand;<br /> jj=jj+1;<br /> if u&lt;=y<br /> x(ii)=y;<br /> ii=ii+1;<br /> end<br /> end<br /> &gt;&gt;hist(x) # It is a histogram<br /> &gt;&gt;jj<br /> jj = 2024 # should be around 2000<br /> &lt;/pre&gt;<br /> [[File:ARM_Example.jpg|300px]]<br /> <br /> :'''*Note:''' The reason that a for loop is not used is that we need to continue the looping until we get 1000 successful samples. We will reject some samples during the process and therefore do not know the number of y we are going to generate. <br /> <br /> :'''*Note2:''' In this example, we used c=2, which means we accept half of the points we generate on average. Generally speaking, 1/c would be the probability of acceptance, and an indicator of the efficiency of your chosen proposal distribution and algorithm. <br /> <br /> :'''*Note3:''' We use '''while''' instead of '''for''' when looping because we do not know how many iterations are required to generate 1000 successful samples. We can view this as a negative binomial distribution so while the expected number of iterations required is n * c, it will likely deviate from this amount. We expect 2000 in this case.<br /> <br /> :'''*Note4:''' If c=1, we will accept all points, which is the ideal situation. However, this is essentially impossible because if c = 1 then our distributions f(x) and g(x) must be identical, so we will have to be satisfied with as close to 1 as possible.<br /> <br /> '''Use Inverse Method for this Example'''&lt;br&gt;<br /> :&lt;math&gt;F(x)=\int_0^x \! 2s\,ds={x^2}-0={x^2}&lt;/math&gt;&lt;br&gt;<br /> :&lt;math&gt;y=x^2&lt;/math&gt;&lt;br&gt;<br /> :&lt;math&gt;x=\sqrt y&lt;/math&gt;<br /> :&lt;math&gt; F^{-1}\left (\, x \, \right) =\sqrt x&lt;/math&gt;<br /> <br /> :*'''Procedure'''<br /> :1: Draw &lt;math&gt; U~ \sim~ Unif [0,1] &lt;/math&gt;&lt;br&gt;<br /> :2: &lt;math&gt; x=F^{-1}\left (\, u\, \right) =\sqrt u&lt;/math&gt;<br /> <br /> &lt;span style=&quot;font-weight:bold;color:green;&quot;&gt;Matlab Code&lt;/span&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;u=rand(1,1000);<br /> &gt;&gt;x=u.^0.5;<br /> &gt;&gt;hist(x)<br /> &lt;/pre&gt;<br /> [[File:ARM(IFM)_Example.jpg|300px]]<br /> <br /> &lt;span style=&quot;font-weight:bold;colour:green;&quot;&gt;Matlab Tip:&lt;/span&gt;<br /> Periods, &quot;.&quot;,meaning &quot;element-wise&quot;, are used to describe the operation you want performed on each element of a vector. In the above example, to take the square root of every element in U, the notation U.^0.5 is used. However if you want to take the square root of the entire matrix U the period, &quot;.&quot; would be excluded. i.e. Let matrix B=U^0.5, then &lt;math&gt;B^T*B=U&lt;/math&gt;. For example if we have a two 1 X 3 matrices and we want to find out their product; using &quot;.&quot; in the code will give us their product. However, if we don't use &quot;.&quot;, it will just give us an error. For example, a =[1 2 3] b=[2 3 4] are vectors, a.*b=[2 6 12], but a*b does not work since the matrix dimensions must agree.<br /> <br /> '''<br /> '''Example for A-R method:'''<br /> <br /> Given &lt;math&gt; f(x)= \frac{3}{4} (1-x^2), -1 \leq x \leq 1 &lt;/math&gt;, use A-R method to generate random number<br /> <br /> <br /> [[Solution:]]<br /> <br /> Let g=U(-1,1) and g(x)=1/2<br /> <br /> let y ~ f, <br /> &lt;math&gt; cg(x)\geq f(x),<br /> c\frac{1}{2} \geq \frac{3}{4} (1-x^2) /1, <br /> c=max 2\cdot\frac{3}{4} (1-x^2) = 3/2 &lt;/math&gt;<br /> <br /> The process:<br /> <br /> :1: Draw U1 ~ U(0,1) &lt;br&gt;<br /> :2: Draw U2 ~ U(0,1) &lt;br&gt;<br /> :3: let &lt;math&gt; y = U1*2 - 1 &lt;/math&gt;<br /> :4: if &lt;math&gt;U2 \leq \frac { \frac{3}{4} * (1-y^2)} { \frac{3}{4}} = {1-y^2}&lt;/math&gt;, then x=y, '''note that''' (3/4(1-y^2)/(3/4) is getting from f(y) / (cg(y)) )<br /> :5: else: return to '''step 1''' <br /> <br /> ----<br /> <br /> <br /> =====Example of Acceptance-Rejection Method=====<br /> <br /> &lt;math&gt;\begin{align} <br /> &amp; f(x) = 3x^2, 0&lt;x&lt;1 \\<br /> \end{align}&lt;/math&gt;&lt;br\&gt;<br /> <br /> &lt;math&gt;\begin{align}<br /> &amp; g(x)=1, 0&lt;x&lt;1 \\<br /> \end{align}&lt;/math&gt;&lt;br\&gt;<br /> <br /> &lt;math&gt;c = \max \frac{f(x)}{g(x)} = \max \frac{3x^2}{1} = 3 &lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\frac{f(x)}{c \cdot g(x)} = x^2&lt;/math&gt;<br /> <br /> 1. Generate two uniform numbers in the unit interval &lt;math&gt;U_1, U_2 \sim~ U(0,1)&lt;/math&gt;&lt;br&gt;<br /> 2. If &lt;math&gt;U_2 \leqslant {U_1}^2&lt;/math&gt;, accept &lt;math&gt;\begin{align}U_1\end{align}&lt;/math&gt; as the random variable with pdf &lt;math&gt;\begin{align}f\end{align}&lt;/math&gt;, if not return to Step 1<br /> <br /> We can also use &lt;math&gt;\begin{align}g(x)=2x\end{align}&lt;/math&gt; for a more efficient algorithm<br /> <br /> &lt;math&gt;c = \max \frac{f(x)}{g(x)} = \max \frac {3x^2}{2x} = \frac {3x}{2} &lt;/math&gt;.<br /> Use the inverse method to sample from &lt;math&gt;\begin{align}g(x)\end{align}&lt;/math&gt;<br /> &lt;math&gt;\begin{align}G(x)=x^2\end{align}&lt;/math&gt;.<br /> Generate &lt;math&gt;\begin{align}U\end{align}&lt;/math&gt; from &lt;math&gt;\begin{align}U(0,1)\end{align}&lt;/math&gt; and set &lt;math&gt;\begin{align}x=sqrt(u)\end{align}&lt;/math&gt;<br /> <br /> 1. Generate two uniform numbers in the unit interval &lt;math&gt;U_1, U_2 \sim~ U(0,1)&lt;/math&gt;&lt;br&gt;<br /> 2. If &lt;math&gt;U_2 \leq \frac{3\sqrt{U_1}}{2}&lt;/math&gt;, accept &lt;math&gt;U_1&lt;/math&gt; as the random variable with pdf &lt;math&gt;f&lt;/math&gt;, if not return to Step 1<br /> <br /> *Note :the function &lt;math&gt;\begin{align}q(x) = c * g(x)\end{align}&lt;/math&gt; is called an envelop or majoring function.&lt;br&gt;<br /> To obtain a better proposing function &lt;math&gt;\begin{align}g(x)\end{align}&lt;/math&gt;, we can first assume a new &lt;math&gt;\begin{align}q(x)\end{align}&lt;/math&gt; and then solve for the normalizing constant by integrating.&lt;br&gt;<br /> In the previous example, we first assume &lt;math&gt;\begin{align}q(x) = 3x\end{align}&lt;/math&gt;. To find the normalizing constant, we need to solve &lt;math&gt;k *\sum 3x = 1&lt;/math&gt; which gives us k = 2/3. So,&lt;math&gt;\begin{align}g(x) = k*q(x) = 2x\end{align}&lt;/math&gt;.<br /> <br /> <br /> '''Possible Limitations'''<br /> <br /> -This method could be computationally inefficient depending on the rejection rate. We may have to sample many points before&lt;br&gt; <br /> we get the 1000 accepted points. In the example we did in class relating the &lt;math&gt;f(x)=2x&lt;/math&gt;, &lt;br&gt;<br /> we had to sample around 2070 points before we finally accepted 1000 sample points.&lt;br&gt;<br /> -If the form of the proposal distribution, g, is very different from target distribution, f, then c is very large and the algorithm is not computationally efficient.&lt;br&gt;<br /> <br /> '''Acceptance - Rejection Method Application on Normal Distribution''' &lt;br&gt;<br /> <br /> &lt;math&gt;X \sim∼ N(\mu,\sigma^2), \text{ or } X = \sigma Z + \mu, Z \sim~ N(0,1) &lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\vert Z \vert&lt;/math&gt; has probability density function of &lt;br&gt;<br /> <br /> f(x) = (2/&lt;math&gt;\sqrt{2\pi}&lt;/math&gt;) e&lt;sup&gt;-x&lt;sup&gt;2&lt;/sup&gt;/2&lt;/sup&gt;<br /> <br /> g(x) = e&lt;sup&gt;-x&lt;/sup&gt;<br /> <br /> Take h(x) = f(x)/g(x) and solve for h'(x) = 0 to find x so that h(x) is maximum. <br /> <br /> Hence x=1 maximizes h(x) =&gt; c = &lt;math&gt;\sqrt{2e/\pi}&lt;/math&gt;<br /> <br /> Thus f(y)/cg(y) = e&lt;sup&gt;-(y-1)&lt;sup&gt;2&lt;/sup&gt;/2&lt;/sup&gt;<br /> <br /> <br /> learn how to use code to calculate the c between f(x) and g(x).<br /> <br /> &lt;p style=&quot;font-weight:bold;text-size:20px;&quot;&gt;How to transform &lt;math&gt;U(0,1)&lt;/math&gt; to &lt;math&gt;U(a, b)&lt;/math&gt;&lt;/p&gt;<br /> <br /> 1. Draw U from &lt;math&gt;U(0,1)&lt;/math&gt;<br /> <br /> 2. Take &lt;math&gt;Y=(b-a)U+a&lt;/math&gt;<br /> <br /> 3. Now Y follows &lt;math&gt;U(a,b)&lt;/math&gt;<br /> <br /> '''Example''': Generate a random variable z from the Semicircular density &lt;math&gt;f(x)= \frac{2}{\pi R^2} \sqrt{R^2-x^2}, -R\leq x\leq R&lt;/math&gt;.<br /> <br /> -&gt; Proposal distribution: UNIF(-R, R)<br /> <br /> -&gt; We know how to generate using &lt;math&gt; U \sim UNIF (0,1) &lt;/math&gt; Let &lt;math&gt; Y= 2RU-R=R(2U-1)&lt;/math&gt;, therefore Y follows &lt;math&gt;U(-R,R)&lt;/math&gt;<br /> <br /> -&gt; In order to maximize the function we must maximize the top and minimize the bottom.<br /> <br /> Now, we need to find c:<br /> Since c=max[f(x)/g(x)], where &lt;br /&gt;<br /> &lt;math&gt;f(x)= \frac{2}{\pi R^2} \sqrt{R^2-x^2}&lt;/math&gt;, &lt;math&gt;g(x)=\frac{1}{2R}&lt;/math&gt;, &lt;math&gt;-R\leq x\leq R&lt;/math&gt;&lt;br /&gt;<br /> Thus, we have to maximize R^2-x^2.<br /> =&gt; When x=0, it will be maximized.<br /> Therefore, c=4/pi. * Note: This also means that the probability of accepting a point is &lt;math&gt;\pi/4&lt;/math&gt;.<br /> <br /> We will accept the points with limit f(x)/[cg(x)].<br /> Since &lt;math&gt;\frac{f(y)}{cg(y)}=\frac{\frac{2}{\pi R^{2}} \sqrt{R^{2}-y^{2}}}{\frac{4}{\pi} \frac{1}{2R}}=\frac{\frac{2}{\pi R^{2}} \sqrt{R^{2}-R^{2}(2U-1)^{2}}}{\frac{2}{\pi R}}&lt;/math&gt; <br /> <br /> * Note: Y= R(2U-1)<br /> We can also get Y= R(2U-1) by using the formula y = a+(b-a)*u, to transform U~(0,1) to U~(a,b). Letting a=-R and b=R, and substituting it in the formula y = a+(b-a)*u, we get Y= R(2U-1).<br /> <br /> Thus, &lt;math&gt;\frac{f(y)}{cg(y)}=\sqrt{1-(2U-1)^{2}}&lt;/math&gt; * this also means the probability we can accept points<br /> <br /> The algorithm to generate random variable x is then:<br /> <br /> 1. Draw &lt;Math&gt;\ U&lt;/math&gt; from &lt;math&gt;\ U(0,1)&lt;/math&gt;<br /> <br /> 2. Draw &lt;Math&gt;\ U_{1}&lt;/math&gt; from &lt;math&gt;\ U(0,1)&lt;/math&gt;<br /> <br /> 3. If &lt;math&gt;U_{1} \leq \sqrt{1-(2U-1)^2}, set x = U_{1}&lt;/math&gt;<br /> else return to step 1.<br /> <br /> <br /> <br /> The condition is &lt;br /&gt;<br /> &lt;Math&gt; U_{1} \leq \sqrt{(1-(2U-1)^2)}&lt;/Math&gt;&lt;br&gt;<br /> &lt;Math&gt;\ U_{1}^2 \leq 1 - (2U -1)^2&lt;/Math&gt;&lt;br&gt;<br /> &lt;Math&gt;\ U_{1}^2 - 1 \leq -(2U - 1)^2&lt;/Math&gt;&lt;br&gt;<br /> &lt;Math&gt;\ 1 - U_{1}^2 \geq (2U - 1)^2&lt;/Math&gt;<br /> <br /> <br /> <br /> <br /> '''One more example about AR method''' &lt;br/&gt;<br /> (In this example, we will see how to determine the value of c when c is a function with unknown parameters instead of a value)<br /> Let &lt;math&gt;f(x)=x×e^{-x}, x &gt; 0 &lt;/math&gt; &lt;br/&gt;<br /> Use &lt;math&gt;g(x)=a×e^{-a×x}&lt;/math&gt; to generate random variable &lt;br/&gt;<br /> &lt;br/&gt;<br /> Solution: First of all, we need to find c&lt;br/&gt;<br /> &lt;math&gt;cg(x)&gt;=f(x)&lt;/math&gt; &lt;br/&gt;<br /> &lt;math&gt;c&gt;=\frac{f(x)}{g(x)}&lt;/math&gt; &lt;br/&gt;<br /> &lt;math&gt;\frac{f(x)}{g(x)}=\frac{x}{a} * e^{-(1-a)x}&lt;/math&gt; &lt;br/&gt;<br /> take derivative with respect to x, and set it to 0 to get the maximum, &lt;br/&gt;<br /> &lt;math&gt;\frac{1}{a} * e^{-(1-a)x} - \frac{x}{a} * e^{-(1-a)x} * (1-a) = 0 &lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;x=\frac {1}{1-a}&lt;/math&gt; &lt;br/&gt;<br /> <br /> &lt;math&gt;\frac {f(x)}{g(x)} = \frac {e^{-1}}{a*(1-a)} &lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;\frac {f(0)}{g(0)} = 0&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;\frac {f(\infty)}{g(\infty)} = 0&lt;/math&gt;&lt;br/&gt;<br /> &lt;br/&gt;<br /> therefore, &lt;b&gt;&lt;math&gt;c= \frac {e^{-1}}{a*(1-a)}&lt;/math&gt;&lt;/b&gt;&lt;br/&gt;<br /> &lt;br/&gt;<br /> &lt;b&gt;In order to minimize c, we need to find the appropriate a&lt;/b&gt; &lt;br/&gt;<br /> Take derivative with respect to a and set it to be zero, &lt;br/&gt;<br /> We could get &lt;math&gt;a= \frac {1}{2}&lt;/math&gt; &lt;br/&gt;<br /> &lt;b&gt;&lt;math&gt;c=\frac{4}{e}&lt;/math&gt;&lt;/b&gt;<br /> &lt;br/&gt;<br /> Procedure: &lt;br/&gt;<br /> 1. Generate u v ~unif(0,1) &lt;br/&gt;<br /> 2. Generate y from g, since g is exponential with rate 2, let y=-0.5*ln(u) &lt;br/&gt;<br /> 3. If &lt;math&gt;v&lt;\frac{f(y)}{c\cdot g(y)}&lt;/math&gt;, output y&lt;br/&gt;<br /> Else, go to 1&lt;br/&gt;<br /> <br /> Acknowledgements: The example above is from Stat 340 Winter 2013 notes.<br /> <br /> '''Summary of how to find the value of c''' &lt;br/&gt;<br /> Let &lt;math&gt;h(x) = \frac {f(x)}{g(x)}&lt;/math&gt;, and then we have the following:&lt;br /&gt;<br /> 1. First, take derivative of h(x) with respect to x, get x&lt;sub&gt;1&lt;/sub&gt;;&lt;br /&gt;<br /> 2. Plug x&lt;sub&gt;1&lt;/sub&gt; into h(x) and get the value(or a function) of c, denote as c&lt;sub&gt;1&lt;/sub&gt;;&lt;br /&gt;<br /> 3. Check the endpoints of x and sub the endpoints into h(x);&lt;br /&gt;<br /> 4. (if c&lt;sub&gt;1&lt;/sub&gt; is a value, then we can ignore this step) Since we want the smallest value of c such that &lt;math&gt;f(x) \leq c\cdot g(x)&lt;/math&gt; for all x, we want the unknown parameter that minimizes c. &lt;br /&gt;So we take derivative of c&lt;sub&gt;1&lt;/sub&gt; with respect to the unknown parameter (ie k=unknown parameter) to get the value of k. &lt;br /&gt;Then we submit k to get the value of c&lt;sub&gt;1&lt;/sub&gt;. (Double check that &lt;math&gt;c_1 \geq 1&lt;/math&gt;&lt;br /&gt;<br /> 5. Pick the maximum value of h(x) to be the value of c.&lt;br /&gt;<br /> <br /> For the two examples above, we need to generate the probability function to uniform distribution,<br /> and figure out &lt;math&gt;c=max\frac {f(y)}{g(y)} &lt;/math&gt;.<br /> If &lt;math&gt;v&lt;\frac {f(y)}{c\cdot g(y)}&lt;/math&gt;, output y.<br /> <br /> <br /> '''Summary of when to use the Accept Rejection Method''' &lt;br/&gt;<br /> 1) When the calculation of inverse cdf cannot to be computed or is too difficult to compute. &lt;br/&gt;<br /> 2) When f(x) can be evaluated to at least one of the normalizing constant. &lt;br/&gt;<br /> 3) A constant c where &lt;math&gt;f(x)\leq c\cdot g(x)&lt;/math&gt;&lt;br/&gt;<br /> 4) A uniform draw&lt;br/&gt;<br /> <br /> ==== Interpretation of 'C' ====<br /> We can use the value of c to calculate the acceptance rate by &lt;math&gt;\tfrac{1}{c}&lt;/math&gt;.<br /> <br /> For instance, assume c=1.5, then we can tell that 66.7% of the points will be accepted (&lt;math&gt;\tfrac{1}{1.5} = 0.667&lt;/math&gt;). We can also call the efficiency of the method is 66.7%.<br /> <br /> Likewise, if the minimum value of possible values for C is &lt;math&gt;\tfrac{4}{3}&lt;/math&gt;, &lt;math&gt;1/ \tfrac{4}{3}&lt;/math&gt; of the generated random variables will be accepted. Thus the efficient of the algorithm is 75%.<br /> <br /> In order to ensure the algorithm is as efficient as possible, the 'C' value should be as close to one as possible, such that &lt;math&gt;\tfrac{1}{c}&lt;/math&gt; approaches 1 =&gt; 100% acceptance rate.<br /> <br /> <br /> &gt;&gt; close All<br /> &gt;&gt; clear All<br /> &gt;&gt; i=1<br /> &gt;&gt; j=0;<br /> &gt;&gt; while ii&lt;1000<br /> y=rand<br /> u=rand<br /> if u&lt;=y;<br /> x(ii)=y<br /> ii=ii+1<br /> end<br /> end<br /> <br /> == Class 5 - Tuesday, May 21 ==<br /> Recall the example in the last lecture. The following code will generate a random variable required by the question.<br /> <br /> * '''Code'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;close all<br /> &gt;&gt;clear all<br /> &gt;&gt;ii=1;<br /> &gt;&gt;R=1; #Note: that R is a constant in which we can change <br /> i.e. if we changed R=4 then we would have a density between -4 and 4<br /> &gt;&gt;while ii&lt;1000<br /> u1 = rand;<br /> u2 = rand;<br /> y = R*(2*u2-1);<br /> if (1-u1^2)&gt;=(2*u2-1)^2<br /> x(ii) = y;<br /> ii = ii + 1; #Note: for beginner programmers that this step increases <br /> the ii value for next time through the while loop<br /> end<br /> end<br /> &gt;&gt;hist(x,20) # 20 is the number of bars<br /> <br /> &gt;&gt;hist(x,30) #30 is the number of bars<br /> &lt;/pre&gt;<br /> <br /> calculate process:<br /> &lt;math&gt;u_{1} &lt;= \sqrt (1-(2u-1)^2) &lt;/math&gt; &lt;br&gt; <br /> &lt;math&gt;(u_{1})^2 &lt;=(1-(2u-1)^2) &lt;/math&gt; &lt;br&gt; <br /> &lt;math&gt;(u_{1})^2 -1 &lt;=(-(2u-1)^2) &lt;/math&gt; &lt;br&gt; <br /> &lt;math&gt;1-(u_{1})^2 &gt;=((2u-1)^2-1) &lt;/math&gt; &lt;br&gt; <br /> <br /> <br /> MATLAB tips: hist(x,y) plots a histogram of variable x, where y is the number of bars in the graph.<br /> <br /> [[File:ARM_cont_example.jpg|300px]]<br /> <br /> === Discrete Examples ===<br /> * '''Example 1''' &lt;br&gt;<br /> Generate random variable &lt;math&gt;X&lt;/math&gt; according to p.m.f&lt;br/&gt;<br /> &lt;math&gt;\begin{align}<br /> P(x &amp;=1) &amp;&amp;=0.15 \\<br /> P(x &amp;=2) &amp;&amp;=0.25 \\<br /> P(x &amp;=3) &amp;&amp;=0.3 \\<br /> P(x &amp;=4) &amp;&amp;=0.1 \\<br /> P(x &amp;=5) &amp;&amp;=0.2 \\<br /> \end{align}&lt;/math&gt;&lt;br/&gt;<br /> <br /> The discrete case is analogous to the continuous case. Suppose we want to generate an X that is a discrete random variable with pmf f(x)=P(X=x). Suppose also that we use the discrete uniform distribution as our target distribution, then &lt;math&gt; g(x)= P(X=x) =0.2 &lt;/math&gt; for all X.<br /> <br /> The following algorithm then yields our X:<br /> <br /> Step 1 Draw discrete uniform distribution of 1, 2, 3, 4 and 5, &lt;math&gt;Y \sim~ g&lt;/math&gt;.&lt;br/&gt;<br /> Step 2 Draw &lt;math&gt;U \sim~ U(0,1)&lt;/math&gt;.&lt;br/&gt;<br /> Step 3 If &lt;math&gt;U \leq \frac{f(Y)}{c \cdot g(Y)}&lt;/math&gt;, then &lt;b&gt; X = Y &lt;/b&gt;;&lt;br/&gt;<br /> Else return to Step 1.&lt;br/&gt;<br /> <br /> C can be found by maximizing the ratio :&lt;math&gt; \frac{f(x)}{g(x)} &lt;/math&gt;. To do this, we want to maximize &lt;math&gt; f(x) &lt;/math&gt; and minimize &lt;math&gt; g(x) &lt;/math&gt;. &lt;br&gt;<br /> :&lt;math&gt;c = max \frac{f(x)}{g(x)} = \frac {0.3}{0.2} = 1.5 &lt;/math&gt; &lt;br/&gt;<br /> Note: In this case &lt;math&gt;f(x)=P(X=x)=0.3&lt;/math&gt; (highest probability from the discrete probabilities in the question)<br /> :&lt;math&gt;\frac{p(x)}{cg(x)} = \frac{p(x)}{1.5*0.2} = \frac{p(x)}{0.3} &lt;/math&gt;&lt;br&gt;<br /> Note: The U is independent from y in Step 2 and 3 above.<br /> ~The constant c is a indicator of rejection rate or efficiency of the algorithm. It can represent the average number of trials of the algorithm. Thus, a higher c would mean that the algorithm is comparatively inefficient.<br /> <br /> the acceptance-rejection method of pmf, the uniform probability is the same for all variables, and there are 5 parameters(1,2,3,4,5), so g(x) is 0.2<br /> <br /> Remember that we always want to choose &lt;math&gt; cg &lt;/math&gt; to be equal to or greater than &lt;math&gt; f &lt;/math&gt;, but as close as possible.<br /> &lt;br /&gt;limitations: If the form of the proposal dist g is very different from target dist f, then c is very large and the algorithm is not computatively efficient.<br /> <br /> * '''Code for example 1'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;close all<br /> &gt;&gt;clear all<br /> &gt;&gt;p=[.15 .25 .3 .1 .2]; %This a vector holding the values<br /> &gt;&gt;ii=1;<br /> &gt;&gt;while ii &lt; 1000<br /> y=unidrnd(5); %generates random numbers for the discrete uniform <br /> u=rand; distribution with maximum 5.<br /> if u&lt;= p(y)/0.3<br /> x(ii)=y;<br /> ii=ii+1;<br /> end<br /> end<br /> &gt;&gt;hist(x)<br /> &lt;/pre&gt;<br /> [[File:ARM_disc_example.jpg|300px]]<br /> <br /> unidrnd(k) draws from the discrete uniform distribution of integers &lt;math&gt;1,2,3,...,k&lt;/math&gt; If this function is not built in to your MATLAB then we can do simple transformation on the rand(k) function to make it work like the unidrnd(k) function. <br /> <br /> The acceptance rate is &lt;math&gt;\frac {1}{c}&lt;/math&gt;, so the lower the c, the more efficient the algorithm. Theoretically, c equals 1 is the best case because all samples would be accepted; however it would only be true when the proposal and target distributions are exactly the same, which would never happen in practice. <br /> <br /> For example, if c = 1.5, the acceptance rate would be &lt;math&gt;\frac {1}{1.5}=\frac {2}{3}&lt;/math&gt;. Thus, in order to generate 1000 random values, on average, a total of 1500 iterations would be required. <br /> <br /> A histogram to show 1000 random values of f(x), more random value make the probability close to the express probability value.<br /> <br /> <br /> * '''Example 2'''&lt;br&gt;<br /> p(x=1)=0.1&lt;br /&gt;p(x=2)=0.3&lt;br /&gt;p(x=3)=0.6&lt;br /&gt;<br /> Let g be the uniform distribution of 1, 2, or 3&lt;br /&gt;<br /> g(x)= 1/3&lt;br /&gt;<br /> &lt;math&gt;c=max(\tfrac{p_{x}}{g(x)})=0.6/(\tfrac{1}{3})=1.8&lt;/math&gt;&lt;br /&gt;<br /> Hence &lt;math&gt;\tfrac{p(x)}{cg(x)} = p(x)/(1.8 (\tfrac{1}{3}))= \tfrac{p(x)}{0.6}&lt;/math&gt;<br /> <br /> 1,y~g&lt;br /&gt;<br /> 2,u~U(0,1)&lt;br /&gt;<br /> 3, If &lt;math&gt;U \leq \frac{f(y)}{cg(y)}&lt;/math&gt;, set x = y. Else go to 1.<br /> <br /> * '''Code for example 2'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;close all<br /> &gt;&gt;clear all<br /> &gt;&gt;p=[.1 .3 .6]; %This a vector holding the values <br /> &gt;&gt;ii=1;<br /> &gt;&gt;while ii &lt; 1000<br /> y=unidrnd(3); %generates random numbers for the discrete uniform distribution with maximum 3<br /> u=rand; <br /> if u&lt;= p(y)/0.6<br /> x(ii)=y; <br /> ii=ii+1; %else ii=ii+1<br /> end<br /> end<br /> &gt;&gt;hist(x)<br /> &lt;/pre&gt;<br /> <br /> <br /> * '''Example 3'''&lt;br&gt;<br /> <br /> Suppose &lt;math&gt;\begin{align}p_{x} = e^{-3}3^{x}/x! , x\geq 0\end{align}&lt;/math&gt; (Poisson distribution)<br /> <br /> '''First:''' Try the first few &lt;math&gt;\begin{align}p_{x}'s\end{align}&lt;/math&gt;: 0.0498, 0.149, 0.224, 0.224, 0.168, 0.101, 0.0504, 0.0216, 0.0081, 0.0027 for &lt;math&gt;\begin{align} x = 0,1,2,3,4,5,6,7,8,9 \end{align}&lt;/math&gt;&lt;br&gt;<br /> <br /> '''Proposed distribution:''' Use the geometric distribution for &lt;math&gt;\begin{align}g(x)\end{align}&lt;/math&gt;;&lt;br&gt;<br /> <br /> &lt;math&gt;\begin{align}g(x)=p(1-p)^{x}\end{align}&lt;/math&gt;, choose &lt;math&gt;\begin{align}p=0.25\end{align}&lt;/math&gt;&lt;br&gt;<br /> <br /> Look at &lt;math&gt;\begin{align}p_{x}/g(x)\end{align}&lt;/math&gt; for the first few numbers: 0.199 0.797 1.59 2.12 2.12 1.70 1.13 0.647 0.324 0.144 for &lt;math&gt;\begin{align} x = 0,1,2,3,4,5,6,7,8,9 \end{align}&lt;/math&gt;&lt;br&gt;<br /> <br /> We want &lt;math&gt;\begin{align}c=max(p_{x}/g(x))\end{align}&lt;/math&gt; which is approximately 2.12&lt;br&gt;<br /> <br /> '''The general procedures to generate &lt;math&gt;\begin{align}p(x)\end{align}&lt;/math&gt; is as follows:'''<br /> <br /> 1. Generate &lt;math&gt;\begin{align}U_{1} \sim~ U(0,1); U_{2} \sim~ U(0,1)\end{align}&lt;/math&gt;&lt;br&gt;<br /> <br /> 2. &lt;math&gt;\begin{align}j = \lfloor \frac{ln(U_{1})}{ln(.75)} \rfloor+1;\end{align}&lt;/math&gt;&lt;br&gt;<br /> <br /> 3. if &lt;math&gt;U_{2} &lt; \frac{p_{j}}{cg(j)}&lt;/math&gt;, set &lt;math&gt;\begin{align}X = x_{j}\end{align}&lt;/math&gt;, else go to step 1.<br /> <br /> Note: In this case, &lt;math&gt;\begin{align}f(x)/g(x)\end{align}&lt;/math&gt; is extremely difficult to differentiate so we were required to test points. If the function is very easy to differentiate, we can calculate the max as if it were a continuous function then check the two surrounding points for which is the highest discrete value.<br /> <br /> *'''Example 4''' (Hypergeometric &amp; Binomial)&lt;br&gt; <br /> <br /> Suppose we are given f(x) such that it is hypergeometically distributed, given 10 white balls, 5 red balls, and select 3 balls, let X be the number of red ball selected, without replacement. <br /> <br /> Choose g(x) such that it is binomial distribution, Bin(3, 1/3). Find the rejection constant, c<br /> <br /> Solution:<br /> For hypergeometric: &lt;math&gt;P(X=0) =\binom{10}{3}/\binom{15}{3} =0.2637, P(x=1)=\binom{10}{2} * \binom{5}{1} /\binom{15}{3}=0.4945, P(X=2)=\binom{10}{1} * \binom{5}{2} /\binom{15}{3}=0.2198,&lt;/math&gt;&lt;br&gt;&lt;br&gt;<br /> &lt;math&gt;P(X=3)=\binom{5}{3}/\binom{15}{3}= 0.02198&lt;/math&gt;<br /> <br /> <br /> For Binomial g(x): P(X=0) = (2/3)^3=0.2963; P(X=1)= 3*(1/3)*(2/3)^2 = 0.4444, P(X=2)=3*(1/3)^2*(2/3)=0.2222, P(X=3)=(1/3)^3=0.03704<br /> <br /> Find the value of f/g for each X<br /> <br /> X=0: 0.8898; <br /> X=1: 1.1127; <br /> X=2: 0.9891; <br /> X=3: 0.5934<br /> <br /> Choose the maximum which is [[c=1.1127]]<br /> <br /> Looking for the max f(x) is 0.4945 and the max g(x) is 0.4444, so we can calculate the max c is 1.1127.<br /> But for the graph, this c is not the best because it does not cover all the point of f(x), so we need to move the c*g(x) graph to cover all f(x), and decreasing the rejection ratio.<br /> <br /> Limitation: If the shape of the proposed distribution g is very different from the target distribution f, then the rejection rate will be high (High c value). Computationally, the algorithm is always right; however it is inefficient and requires many iterations. &lt;br&gt;<br /> Here is an example: <br /> [[File:ARM_Fail.jpg]]<br /> <br /> In the above example, we need to move c*g(x) to the peak of f to cover the whole f. Thus c will be very large and 1/c will be small.<br /> The higher the rejection rate, more points will be rejected.&lt;br&gt; <br /> More on rejection/acceptance rate: 1/c is the acceptance rate. As c decreases (note: the minimum value of c is 1), the acceptance rate increases. In our last example, 1/c=1/1.5≈66.67%. Around 67% of points generated will be accepted.&lt;br&gt;<br /> &lt;div style=&quot;margin-bottom:10px;border:10px solid red;background: yellow&quot;&gt; the example below provides a better understanding about the pros and cons of the AR method. The AR method is useless when dealing with sampling distribution with a higher peak since c will be large, hence making our algorithm inefficient&lt;br&gt;<br /> which brings the acceptance rate low which leads to very time consuming sampling &lt;/div&gt;<br /> &lt;div style=&quot;border:1px solid #cccccc;border-radius:10px;box-shadow: 0 5px 15px 1px rgba(0, 0, 0, 0.6), 0 0 200px 1px rgba(255, 255, 255, 0.5);padding:20px;margin:20px;background:#FFFFAD;&quot;&gt;<br /> &lt;h2 style=&quot;text-align:center;&quot;&gt;Acceptance-Rejection Method&lt;/h2&gt;<br /> &lt;p&gt;&lt;b&gt;Problem:&lt;/b&gt; The CDF is not invertible or it is difficult to find the inverse.&lt;/p&gt;<br /> &lt;p&gt;&lt;b&gt;Plan:&lt;/b&gt;&lt;/p&gt;<br /> &lt;ol&gt;<br /> &lt;li&gt;Draw y~g(.)&lt;/li&gt;<br /> &lt;li&gt;Draw u~Unif(0,1)&lt;/li&gt;<br /> &lt;li&gt;If &lt;math&gt;u\leq \frac{f(y)}{cg(y)}&lt;/math&gt;then set x=y. Else return to Step 1&lt;/li&gt;<br /> &lt;/ol&gt;<br /> &lt;p&gt;x will have the desired distribution.&lt;/p&gt;<br /> &lt;b&gt;Matlab Example&lt;/b&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;close all<br /> clear all<br /> ii=1;<br /> R=1;<br /> while ii&amp;lt;1000<br /> u1 = rand;<br /> u2 = rand;<br /> y = R*(2*u2-1);<br /> if (1-u1^2)&amp;gt;=(2*u2-1)^2<br /> x(ii) = y;<br /> ii = ii + 1;<br /> end<br /> end<br /> hist(x,20)<br /> &lt;/pre&gt;<br /> &lt;/div&gt;<br /> <br /> <br /> Recall that,<br /> Suppose we have an efficient method for simulating a random variable having probability mass function {q(j),j&gt;=0}. We can use this as the basis for simulating from the distribution having mass function {p(j),j&gt;=0} by first simulating a random variable Y having mass function {q(j)} and then accepting this simulated value with a probability proportional to p(Y)/q(Y).<br /> Specifically, let c be a constant such that <br /> p(j)/q(j)&lt;=c for all j such that p(j)&gt;0<br /> We now have the following technique, called the acceptance-rejection method, for simulating a random variable X having mass function p(j)=P{X=j}.<br /> <br /> === Sampling from commonly used distributions ===<br /> <br /> Please note that this is not a general technique as is that of acceptance-rejection sampling. Later, we will generalize the distributions for multidimensional purposes.<br /> <br /> * '''Gamma'''&lt;br /&gt;<br /> <br /> The CDF of the Gamma distribution &lt;math&gt;Gamma(t,\lambda)&lt;/math&gt; is(t denotes the shape, &lt;math&gt;\lambda&lt;/math&gt; denotes the scale: &lt;br&gt;<br /> &lt;math&gt; F(x) = \int_0^{x} \frac{e^{-y}y^{t-1}}{(t-1)!} \mathrm{d}y, \; \forall x \in (0,+\infty)&lt;/math&gt;, where &lt;math&gt;t \in \N^+ \text{ and } \lambda \in (0,+\infty)&lt;/math&gt;.&lt;br&gt;<br /> <br /> The gamma distribution is often used to model waiting times between a certain number of events. It can also be expressed as the sum of infinitely many independent and identically distributed exponential distributions. This distribution has two parameters: the number of exponential terms n, and the rate parameter &lt;math&gt;\lambda&lt;/math&gt;. In this distribution there is the Gamma function, &lt;math&gt;\Gamma &lt;/math&gt; which has some very useful properties. &quot;Source: STAT 340 Spring 2010 Course Notes&quot; &lt;br/&gt;<br /> <br /> Neither Inverse Transformation nor Acceptance-Rejection Method can be easily applied to Gamma distribution.<br /> However, we can use additive property of Gamma distribution to generate random variables.<br /> <br /> * '''Additive Property'''&lt;br /&gt;<br /> If &lt;math&gt;X_1, \dots, X_t&lt;/math&gt; are independent exponential distributions with hazard rate &lt;math&gt; \lambda &lt;/math&gt; (in other words, &lt;math&gt; X_i\sim~ Exp (\lambda) &lt;/math&gt;&lt;math&gt;, Exp (\lambda)= Gamma (1, \lambda)), then \Sigma_{i=1}^t X_i \sim~ Gamma (t, \lambda) &lt;/math&gt;<br /> <br /> <br /> Side notes: if &lt;math&gt; X_i\sim~ Gamma(a,\lambda)&lt;/math&gt; and &lt;math&gt; Y_i\sim~ Gamma(B,\lambda)&lt;/math&gt; are independent gamma distributions, then &lt;math&gt;\frac{X}{X+Y}&lt;/math&gt; has a distribution of &lt;math&gt; Beta(a,B). &lt;/math&gt;<br /> <br /> <br /> If we want to sample from the Gamma distribution, we can consider sampling from &lt;math&gt;t&lt;/math&gt; independent exponential distributions using the Inverse Method for each &lt;math&gt; X_i&lt;/math&gt; and add them up. Note that this only works the specific set of gamma distributions where t is a positive integer.<br /> <br /> According to this property, a random variable that follows Gamma distribution is the sum of i.i.d (independent and identically distributed) exponential random variables. Now we want to generate 1000 values of &lt;math&gt;Gamma(20,10)&lt;/math&gt; random variables, so we need to obtain the value of each one by adding 20 values of &lt;math&gt;X_i \sim~ Exp(10)&lt;/math&gt;. To achieve this, we generate a 20-by-1000 matrix whose entries follow &lt;math&gt;Exp(10)&lt;/math&gt; and add the rows together.&lt;br /&gt;<br /> &lt;math&gt; x_1 \sim~Exp(\lambda)&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;x_2 \sim~Exp(\lambda)&lt;/math&gt;&lt;br /&gt;<br /> ...&lt;br /&gt;<br /> &lt;math&gt;x_t \sim~Exp(\lambda)&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;x_1+x_2+...+x_t~&lt;/math&gt;<br /> <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;l=1<br /> &gt;&gt;u-rand(1,1000);<br /> &gt;&gt;x=-(1/l)*log(u); <br /> &gt;&gt;hist(x)<br /> &gt;&gt;rand<br /> &lt;/pre&gt;<br /> <br /> <br /> * '''Procedure '''<br /> <br /> :#Sample independently from a uniform distribution &lt;math&gt;t&lt;/math&gt; times, giving &lt;math&gt; U_1,\dots,U_t \sim~ U(0,1)&lt;/math&gt; <br /> :#Use the Inverse Transform Method, &lt;math&gt; X_i = -\frac {1}{\lambda}\log(1-U_i)&lt;/math&gt;, giving &lt;math&gt; X_1,\dots,X_t \sim~Exp(\lambda)&lt;/math&gt;<br /> :#Use the additive property,&lt;math&gt; X = \Sigma_{i=1}^t X_i \sim~ Gamma (t, \lambda) &lt;/math&gt;&lt;br&gt;<br /> <br /> <br /> * '''Note for Procedure '''<br /> :#If &lt;math&gt;U\sim~U(0,1)&lt;/math&gt;, then &lt;math&gt;U&lt;/math&gt; and &lt;math&gt;1-U&lt;/math&gt; will have the same distribution (both follows &lt;math&gt;U(0,1)&lt;/math&gt;)<br /> :#This is because the range for &lt;math&gt;1-U&lt;/math&gt; is still &lt;math&gt;(0,1)&lt;/math&gt;, and their densities are identical over this range.<br /> :#Let &lt;math&gt;Y=1-U&lt;/math&gt;, &lt;math&gt;Pr(Y&lt;=y)=Pr(1-U&lt;=y)=Pr(U&gt;=1-y)=1-Pr(U&lt;=1-y)=1-(1-y)=y&lt;/math&gt;, thus &lt;math&gt;1-U\sim~U(0,1)&lt;/math&gt;<br /> <br /> <br /> <br /> * '''Code'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;close all<br /> &gt;&gt;clear all<br /> &gt;&gt;lambda = 1;<br /> &gt;&gt;u = rand(20, 1000); Note: this command generate a 20x1000 matrix <br /> (which means we generate 1000 number for each X_i with t=20); <br /> all the elements are generated by rand<br /> &gt;&gt;x = (-1/lambda)*log(1-u); Note: log(1-u) is essentially the same as log(u) only if u~U(0,1) <br /> &gt;&gt;xx = sum(x) Note: sum(x) will sum all elements in the same column. <br /> size(xx) can help you to verify<br /> &gt;&gt;size(sum(x)) Note: see the size of x if we forget it<br /> (the answer is 20 1000)<br /> &gt;&gt;hist(x(1:)) Note: the graph of the first exponential distribution <br /> &gt;&gt;hist(xx)<br /> &lt;/pre&gt;<br /> [[File:Gamma_example.jpg|300px]]<br /> <br /> <br /> <br /> size(x) and size(u) are both 20*1000 matrix.<br /> Since if u~unif(0, 1), u and 1 - u have the same distribution, we can substitute 1-u with u to simply the equation.<br /> Alternatively, the following command will do the same thing with the previous commands.<br /> <br /> * '''Code'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;close all<br /> &gt;&gt;clear all<br /> &gt;&gt;lambda = 1;<br /> &gt;&gt;xx = sum((-1/lambda)*log(rand(20, 1000))); ''This is simple way to put the code in one line. <br /> Here we can use either log(u) or log(1-u) since U~U(0,1);<br /> &gt;&gt;hist(xx)<br /> &lt;/pre&gt;<br /> <br /> In the matrix rand(20,1000) means 20 row with 1000 numbers for each.<br /> use the code to show the generalize the distributions for multidimensional purposes in different cases, such as sum xi (each xi not equal xj), and they are independent, or matrix. Finally, we can see the conclusion is shown by the histogram.<br /> <br /> === Other Sampling Method: Box Muller ===<br /> [[File:Unnamed_QQ_Screenshot20130521203625.png‎]]<br /> * From cartesian to polar coordinates &lt;br /&gt;<br /> &lt;math&gt; R=\sqrt{x_{1}^2+x_{2}^2}= x_{2}/sin(\theta)= x_{1}/cos(\theta)&lt;/math&gt; &lt;br /&gt;<br /> &lt;math&gt; tan(\theta)=x_{2}/x_{1} \rightarrow \theta=tan^{-1}(x_{2}/x_{1})&lt;/math&gt; &lt;br /&gt;<br /> <br /> *Box-Muller Transformation:&lt;br&gt;<br /> It is a transformation that consumes two continuous uniform random variables &lt;math&gt; X \sim U(0,1), Y \sim U(0,1) &lt;/math&gt; and outputs a bivariate normal random variable with &lt;math&gt; Z_1\sim N(0,1), Z_2\sim N(0,1). &lt;/math&gt;<br /> <br /> === '''Matlab''' ===<br /> <br /> If X is a matrix,<br /> * ''X(1,:)'' returns the first row<br /> * ''X(:,1)'' returns the first column<br /> * ''X(i,j)'' returns the (i,j)th entry<br /> * ''sum(X,'''1''')'' or ''sum(X)'' is a summation of the '''rows''' of X. The output is a row vector of the sums of each column.<br /> * ''sum(X,'''2''')'' is a summation of the '''columns''' of X, returning a vector.<br /> * ''rand(r,c)'' will generate uniformly distributed random numbers in r rows and c columns.<br /> * The dot operator (.), when placed before a function, such as +,-,^, *, and many others specifies to apply that function to every element of a vector or a matrix. For example, to add a constant c to elements of a matrix A, do A.+c as opposed to simply A+c. The dot operator is not required for functions that can only take a number as their input (such as log).<br /> * Matlab processes loops very slow, while it is fast with matrices and vectors, so it is preferable to use the dot operator to and matrices of random numbers than loops if it is possible.<br /> <br /> == Class 6 - Thursday, May 23 ==<br /> <br /> === Announcement ===<br /> 1. On the day of each lecture, students from the morning section can only contribute the first half of the lecture (i.e. 8:30 - 9:10 am), so that the second half can be saved for the ones from the afternoon section. After the day of lecture, students are free to contribute anything.<br /> <br /> === Standard Normal distribution ===<br /> If X ~ N(0,1) i.e. Standard Normal Distribution - then its p.d.f. is of the form<br /> :&lt;math&gt;f(x) = \frac{1}{\sqrt{2\pi}}\, e^{- \frac{\scriptscriptstyle 1}{\scriptscriptstyle 2} x^2}&lt;/math&gt;<br /> <br /> *Warning : the General Normal distribution is:<br /> &lt;table&gt;<br /> &lt;tr&gt;<br /> &lt;td&gt;&lt;div onmouseover=&quot;document.getElementById('woyun').style.visibility='visible'&quot;<br /> onmouseout=&quot;document.getElementById('woyun').style.visibility='hidden'&quot;&gt;<br /> &lt;math&gt;<br /> f(x) = \frac{1}{\sigma\sqrt{2\pi}} e^{ -\frac{(x-\mu)^2}{2\sigma^2} }<br /> &lt;/math&gt;<br /> &lt;/div&gt;<br /> &lt;/td&gt;<br /> &lt;td&gt;<br /> &lt;div id=&quot;woyun&quot; style=&quot;<br /> <br /> visibility:hidden;<br /> width:100px;<br /> height:100px;<br /> background:#FFFFAD;<br /> position:relative;<br /> animation:movement infinite;<br /> animation-duration:2s;<br /> animation-direction:alternate;<br /> <br /> <br /> /* Safari and Chrome */<br /> -webkit-animation:movement infinite;<br /> -webkit-animation-duration:2s;<br /> -webkit-animation-direction:alternate; <br /> <br /> <br /> @keyframes movement<br /> {<br /> from {left:0px;}<br /> to {left:200px;}<br /> }<br /> <br /> @-webkit-keyframes movement /* Safari and Chrome */<br /> {<br /> from {left:0px;}<br /> to {left:200px;}<br /> }&quot;<br /> &gt;which is almost useless in this course&lt;/div&gt;<br /> &lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;/table&gt;<br /> where &lt;math&gt; \mu &lt;/math&gt; is the mean or expectation of the distribution and &lt;math&gt; \sigma &lt;/math&gt; is standard deviation &lt;br /&gt;<br /> &lt;br /&gt;<br /> *N(0,1) is standard normal. &lt;math&gt; \mu &lt;/math&gt; =0 and &lt;math&gt; \sigma &lt;/math&gt;=1 &lt;br /&gt;<br /> &lt;br /&gt;<br /> <br /> Let X and Y be independent standard normal.<br /> <br /> Let &lt;math&gt; \theta &lt;/math&gt; and R denote the Polar coordinate of the vector (X, Y) <br /> where &lt;math&gt; X = R \cdot \sin\theta &lt;/math&gt; and &lt;math&gt; Y = R \cdot \cos \theta &lt;/math&gt;<br /> <br /> [[File:rtheta.jpg]]<br /> <br /> Note: R must satisfy two properties:<br /> <br /> :1. Be a positive number (as it is a length)<br /> <br /> :2. It must be from a distribution that has more data points closer to the origin so that as we go further from the origin, less points are generated (the two options are Chi-squared and Exponential distribution) <br /> <br /> The form of the joint distribution of R and &lt;math&gt;\theta&lt;/math&gt; will show that the best choice for distribution of R&lt;sup&gt;2&lt;/sup&gt; is exponential.<br /> <br /> <br /> We cannot use the Inverse Transformation Method since F(x) does not have a closed form solution. So we will use joint probability function of two independent standard normal random variables and polar coordinates to simulate the distribution:<br /> <br /> We know that <br /> <br /> &lt;math&gt;R^{2}= X^{2}+Y^{2}&lt;/math&gt; and &lt;math&gt; \tan(\theta) = \frac{y}{x} &lt;/math&gt; where X and Y are two independent standard normal<br /> :&lt;math&gt;f(x) = \frac{1}{\sqrt{2\pi}}\, e^{- \frac{\scriptscriptstyle 1}{\scriptscriptstyle 2} x^2}&lt;/math&gt;<br /> :&lt;math&gt;f(y) = \frac{1}{\sqrt{2\pi}}\, e^{- \frac{\scriptscriptstyle 1}{\scriptscriptstyle 2} y^2}&lt;/math&gt;<br /> :&lt;math&gt;f(x,y) = \frac{1}{\sqrt{2\pi}}\, e^{- \frac{\scriptscriptstyle 1}{\scriptscriptstyle 2} x^2} * \frac{1}{\sqrt{2\pi}}\, e^{- \frac{\scriptscriptstyle 1}{\scriptscriptstyle 2} y^2}=\frac{1}{2\pi}\, e^{- \frac{\scriptscriptstyle 1}{\scriptscriptstyle 2} (x^2+y^2)} &lt;/math&gt;&lt;br /&gt; - Since for independent distributions, their joint probability function is the multiplication of two independent probability functions. It can also be shown using 1-1 transformation that the joint distribution of R and θ is given by, 1-1 transformation:&lt;br /&gt;<br /> <br /> <br /> '''Let &lt;math&gt;d=R^2&lt;/math&gt;'''&lt;br /&gt;<br /> <br /> &lt;math&gt;x= \sqrt {d}\cos \theta &lt;/math&gt;<br /> &lt;math&gt;y= \sqrt {d}\sin \theta &lt;/math&gt;<br /> then <br /> &lt;math&gt;\left| J\right| = \left| \dfrac {1} {2}d^{-\frac {1} {2}}\cos \theta d^{\frac{1}{2}}\cos \theta +\sqrt {d}\sin \theta \dfrac {1} {2}d^{-\frac{1}{2}}\sin \theta \right| = \dfrac {1} {2}&lt;/math&gt;<br /> It can be shown that the joint density of &lt;math&gt; d /R^2&lt;/math&gt; and &lt;math&gt; \theta &lt;/math&gt; is:<br /> :&lt;math&gt;\begin{matrix} f(d,\theta) = \frac{1}{2}e^{-\frac{d}{2}}*\frac{1}{2\pi},\quad d = R^2 \end{matrix},\quad for\quad 0\leq d&lt;\infty\ and\quad 0\leq \theta\leq 2\pi &lt;/math&gt;<br /> <br /> <br /> <br /> Note that &lt;math&gt; \begin{matrix}f(r,\theta)\end{matrix}&lt;/math&gt; consists of two density functions, Exponential and Uniform, so assuming that r and &lt;math&gt;\theta&lt;/math&gt; are independent<br /> &lt;math&gt; \begin{matrix} \Rightarrow d \sim~ Exp(1/2), \theta \sim~ Unif[0,2\pi] \end{matrix} &lt;/math&gt;<br /> ::* &lt;math&gt; \begin{align} R^2 = d = x^2 + y^2 \end{align} &lt;/math&gt;<br /> ::* &lt;math&gt; \tan(\theta) = \frac{y}{x} &lt;/math&gt;<br /> &lt;math&gt;\begin{align} f(d) = Exp(1/2)=\frac{1}{2}e^{-\frac{d}{2}}\ \end{align}&lt;/math&gt; <br /> &lt;br&gt;<br /> &lt;math&gt;\begin{align} f(\theta) =\frac{1}{2\pi}\ \end{align}&lt;/math&gt;<br /> &lt;br&gt;<br /> <br /> To sample from the normal distribution, we can generate a pair of independent standard normal X and Y by:&lt;br /&gt;<br /> <br /> 1) Generating their polar coordinates&lt;br /&gt;<br /> 2) Transforming back to rectangular (Cartesian) coordinates.&lt;br /&gt;<br /> <br /> <br /> '''Alternative Method of Generating Standard Normal Random Variables'''&lt;br /&gt; <br /> <br /> Step 1: Generate &lt;math&gt;u_{1}&lt;/math&gt; ~&lt;math&gt;Unif(0,1)&lt;/math&gt;&lt;br /&gt;<br /> Step 2: Generate &lt;math&gt;Y_{1}&lt;/math&gt; ~&lt;math&gt;Exp(1)&lt;/math&gt;,&lt;math&gt;Y_{2}&lt;/math&gt;~&lt;math&gt;Exp(2)&lt;/math&gt;&lt;br /&gt;<br /> Step 3: If &lt;math&gt;Y_{2} \geq(Y_{1}-1)^2/2&lt;/math&gt;,set &lt;math&gt;V=Y1&lt;/math&gt;,otherwise,go to step 1&lt;br /&gt;<br /> Step 4: If &lt;math&gt;u_{1} \leq 1/2&lt;/math&gt;,then &lt;math&gt;X=-V&lt;/math&gt;&lt;br /&gt;<br /> <br /> ===Expectation of a Standard Normal distribution===&lt;br /&gt;<br /> <br /> The expectation of a standard normal distribution is 0&lt;br /&gt;<br /> <br /> '''Proof:''' &lt;br /&gt;<br /> <br /> :&lt;math&gt;\operatorname{E}[X]= \;\int_{-\infty}^{\infty} x \frac{1}{\sqrt{2\pi}} e^{-x^2/2} \, dx.&lt;/math&gt;<br /> :&lt;math&gt;\phi(x) = \frac{1}{\sqrt{2\pi}}\, e^{- \frac{\scriptscriptstyle 1}{\scriptscriptstyle 2} x^2}.&lt;/math&gt;<br /> :&lt;math&gt;=\;\int_{-\infty}^{\infty} x \phi(x), dx.&lt;/math&gt;<br /> :Since the first derivative ''ϕ''′(''x'') is −''xϕ''(''x'')<br /> :&lt;math&gt;=\;\ - \int_{-\infty}^{\infty} \phi'(x), dx.&lt;/math&gt;<br /> :&lt;math&gt;= - \left[\phi(x)\right]_{-\infty}^{\infty}&lt;/math&gt;<br /> :&lt;math&gt;= 0&lt;/math&gt;&lt;br /&gt;<br /> <br /> '''Note,''' more intuitively, because x is an odd function (f(x)+f(-x)=0). Taking integral of x will give &lt;math&gt;x^2/2 &lt;/math&gt; which is an even function (f(x)=f(-x)). This is in relation to the symmetrical properties of the standard normal distribution. If support is from negative infinity to infinity, then the integral will return 0.&lt;br /&gt;<br /> <br /> <br /> '''Procedure (Box-Muller Transformation Method):''' &lt;br /&gt;<br /> <br /> Pseudorandom approaches to generating normal random variables used to be limited. Inefficient methods such as inverse Gaussian function, sum of uniform random variables, and acceptance-rejection were used. In 1958, a new method was proposed by George Box and Mervin Muller of Princeton University. This new technique was easy to use and also had the accuracy to the inverse transform sampling method that it grew more valuable as computers became more computationally astute. &lt;br&gt;<br /> The Box-Muller method takes a sample from a bivariate independent standard normal distribution, each component of which is thus a univariate standard normal. The algorithm is based on the following two properties of the bivariate independent standard normal distribution: &lt;br&gt;<br /> if &lt;math&gt;Z = (Z_{1}, Z_{2}&lt;/math&gt;) has this distribution, then &lt;br&gt;<br /> <br /> 1.&lt;math&gt;R^2=Z_{1}^2+Z_{2}^2&lt;/math&gt; is exponentially distributed with mean 2, i.e. &lt;br&gt;<br /> &lt;math&gt;P(R^2 \leq x) = 1-e^{-x/2}&lt;/math&gt;. &lt;br&gt;<br /> 2.Given &lt;math&gt;R^2&lt;/math&gt;, the point &lt;math&gt;(Z_{1},Z_{2}&lt;/math&gt;) is uniformly distributed on the circle of radius R centered at the origin. &lt;br&gt;<br /> We can use these properties to build the algorithm: &lt;br&gt;<br /> <br /> <br /> 1) Generate random number &lt;math&gt; \begin{align} U_1,U_2 \sim~ \mathrm{Unif}(0, 1) \end{align} &lt;/math&gt; &lt;br /&gt;<br /> 2) Generate polar coordinates using the exponential distribution of d and uniform distribution of θ,<br /> <br /> <br /> <br /> &lt;math&gt; \begin{align} R^2 = d = -2\log(U_1), &amp; \quad r = \sqrt{d} \\ &amp; \quad \theta = 2\pi U_2 \end{align} &lt;/math&gt;<br /> <br /> <br /> &lt;math&gt; \begin{matrix} \ R^2 \sim~ Exp(1/2), \theta \sim~ Unif[0,2\pi] \end{matrix} &lt;/math&gt; &lt;br /&gt;<br /> <br /> Note: If U~unif(0,1), then ln(1-U)=ln(U)<br /> <br /> 3) Transform polar coordinates (i.e. R and θ) back to Cartesian coordinates (i.e. X and Y), &lt;br&gt; &lt;math&gt; \begin{align} x = R\cos(\theta) \\ y = R\sin(\theta) \end{align} &lt;/math&gt; &lt;br /&gt;.<br /> <br /> Alternatively,&lt;br&gt; &lt;math&gt; x =\cos(2\pi U_2)\sqrt{-2\ln U_1}\, &lt;/math&gt; and&lt;br&gt; &lt;math&gt; y =\sin(2\pi U_2)\sqrt{-2\ln U_1}\, &lt;/math&gt;&lt;br /&gt;<br /> <br /> <br /> '''Note:''' In steps 2 and 3, we are using a similar technique as that used in the inverse transform method. &lt;br /&gt;<br /> The Box-Muller Transformation Method generates a pair of independent Standard Normal distributions, X and Y (Using the transformation of polar coordinates). &lt;br /&gt;<br /> <br /> If you want to generate a number of independent standard normal distributed numbers (more than two), you can run the Box-Muller method several times.&lt;br/&gt;<br /> For example: &lt;br /&gt;<br /> If you want 8 independent standard normal distributed numbers, then run the Box-Muller methods 4 times (8/2 times). &lt;br /&gt; <br /> If you want 9 independent standard normal distributed numbers, then run the Box-Muller methods 5 times (10/2 times), and then delete one. &lt;br /&gt;<br /> <br /> <br /> '''Matlab Code'''&lt;br /&gt;<br /> <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;close all<br /> &gt;&gt;clear all<br /> &gt;&gt;u1=rand(1,1000);<br /> &gt;&gt;u2=rand(1,1000);<br /> &gt;&gt;d=-2*log(u1);<br /> &gt;&gt;tet=2*pi*u2;<br /> &gt;&gt;x=d.^0.5.*cos(tet);<br /> &gt;&gt;y=d.^0.5.*sin(tet);<br /> &gt;&gt;hist(tet) <br /> &gt;&gt;hist(d)<br /> &gt;&gt;hist(x)<br /> &gt;&gt;hist(y)<br /> &lt;/pre&gt;<br /> &lt;br&gt;<br /> '''Remember''': For the above code to work the &quot;.&quot; needs to be after the d to ensure that each element of d is raised to the power of 0.5.&lt;br /&gt; Otherwise matlab will raise the entire matrix to the power of 0.5.&quot;&lt;br&gt;<br /> <br /> '''Note:'''&lt;br&gt;the first graph is hist(tet) and it is a uniform distribution.&lt;br&gt;The second one is hist(d) and it is a exponential distribution.&lt;br&gt;The third one is hist(x) and it is a normal distribution.&lt;br&gt;The last one is hist(y) and it is also a normal distribution.<br /> <br /> Attention:There is a &quot;dot&quot; between sqrt(d) and &quot;*&quot;. It is because d and tet are vectors. &lt;br&gt;<br /> <br /> <br /> [[File:Normal_theta.jpg|300px]][[File:Normal_d.jpg|300px]]<br /> [[File:normal_x.jpg|300x300px]][[File:normal_y.jpg|300x300px]]<br /> <br /> As seen in the histograms above, X and Y generated from this procedure have a standard normal distribution.<br /> <br /> * '''Code'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;close all<br /> &gt;&gt;clear all<br /> &gt;&gt;x=randn(1,1000);<br /> &gt;&gt;hist(x)<br /> &gt;&gt;hist(x+2)<br /> &gt;&gt;hist(x*2+2)&lt;br&gt;<br /> &lt;/pre&gt;<br /> &lt;br&gt;<br /> '''Note:'''&lt;br&gt; <br /> 1. randn is random sample from a standard normal distribution.&lt;br /&gt;<br /> 2. hist(x+2) will be centered at 2 instead of at 0. &lt;br /&gt;<br /> 3. hist(x*3+2) is also centered at 2. The mean doesn't change, but the variance of x*3+2 becomes nine times (3^2) the variance of x.&lt;br /&gt;<br /> [[File:Normal_x.jpg|300x300px]][[File:Normal_x+2.jpg|300x300px]][[File:Normal(2x+2).jpg|300px]]<br /> &lt;br /&gt;<br /> <br /> &lt;b&gt;Comment&lt;/b&gt;:&lt;br /&gt;<br /> Box-Muller transformations are not computationally efficient. The reason for this is the need to compute sine and cosine functions. A way to get around this time-consuming difficulty is by an indirect computation of the sine and cosine of a random angle (as opposed to a direct computation which generates U and then computes the sine and cosine of 2πU. &lt;br /&gt;<br /> <br /> <br /> <br /> '''Alternative Methods of generating normal distribution'''&lt;br /&gt;<br /> <br /> 1. Even though we cannot use inverse transform method, we can approximate this inverse using different functions.One method would be '''rational approximation'''.&lt;br /&gt;<br /> 2.'''Central limit theorem''' : If we sum 12 independent U(0,1) distribution and subtract 6 (which is E(ui)*12)we will approximately get a standard normal distribution.&lt;br /&gt;<br /> 3. '''Ziggurat algorithm''' which is known to be faster than Box-Muller transformation and a version of this algorithm is used for the randn function in matlab.&lt;br /&gt;<br /> <br /> If Z~N(0,1) and X= μ +Zσ then X~&lt;math&gt; N(\mu, \sigma^2)&lt;/math&gt;<br /> <br /> If Z&lt;sub&gt;1&lt;/sub&gt;, Z&lt;sub&gt;2&lt;/sub&gt;... Z&lt;sub&gt;d&lt;/sub&gt; are independent identically distributed N(0,1),<br /> then Z=(Z&lt;sub&gt;1&lt;/sub&gt;,Z&lt;sub&gt;2&lt;/sub&gt;...Z&lt;sub&gt;d&lt;/sub&gt;)&lt;sup&gt;T&lt;/sup&gt; ~N(0, I&lt;sub&gt;d&lt;/sub&gt;), where 0 is the zero vector and I&lt;sub&gt;d&lt;/sub&gt; is the identity matrix.<br /> <br /> For the histogram, the constant is the parameter that affect the center of the graph.<br /> <br /> === Proof of Box Muller Transformation ===<br /> <br /> '''Definition:'''&lt;br /&gt;<br /> A transformation which transforms from a '''two-dimensional continuous uniform''' distribution to a '''two-dimensional bivariate normal''' distribution (or complex normal distribution).<br /> <br /> Let U&lt;sub&gt;1&lt;/sub&gt; and U&lt;sub&gt;2&lt;/sub&gt; be independent uniform (0,1) random variables. Then<br /> &lt;math&gt;X_{1} = ((-2lnU_{1})^.5)*cos(2\pi U_{2})&lt;/math&gt;<br /> <br /> &lt;math&gt;X_{2} = (-2lnU_{1})^0.5*sin(2\pi U_{2})&lt;/math&gt;<br /> are '''independent''' N(0,1) random variables.<br /> <br /> This is a standard transformation problem. The joint distribution is given by <br /> f(x1 ,x2) = f&lt;sub&gt;u1&lt;/sub&gt;, &lt;sub&gt;u2&lt;/sub&gt;(g1^− 1(x1,x2),g2^− 1(x1,x2)) * | J |<br /> <br /> where J is the Jacobian of the transformation,<br /> <br /> J = |∂u&lt;sub&gt;1&lt;/sub&gt;/∂x&lt;sub&gt;1&lt;/sub&gt;,∂u&lt;sub&gt;1&lt;/sub&gt;/∂x&lt;sub&gt;2&lt;/sub&gt;|<br /> |∂u&lt;sub&gt;2&lt;/sub&gt;/∂x&lt;sub&gt;1&lt;/sub&gt;,∂u&lt;sub&gt;2&lt;/sub&gt;/∂x&lt;sub&gt;2&lt;/sub&gt;|<br /> where <br /> u&lt;sub&gt;1&lt;/sub&gt; = g&lt;sub&gt;1&lt;/sub&gt; ^-1(x1,x2)<br /> u&lt;sub&gt;2&lt;/sub&gt; = g&lt;sub&gt;2&lt;/sub&gt; ^-1(x1,x2)<br /> <br /> Inverting the above transformation, we have<br /> u1 = exp^{-(x&lt;sub&gt;1&lt;/sub&gt; ^2+ x&lt;sub&gt;2&lt;/sub&gt; ^2)/2}<br /> u2 = (1/2pi)*tan^-1 (x&lt;sub&gt;2&lt;/sub&gt;/x&lt;sub&gt;1&lt;/sub&gt;)<br /> <br /> Finally we get<br /> f(x1,x2) = {exp^(-(x1^2+x2^2)/2)}/2pi<br /> which factors into two standard normal pdfs.<br /> <br /> <br /> (The quote is from http://mathworld.wolfram.com/Box-MullerTransformation.html)<br /> (The proof is from http://www.math.nyu.edu/faculty/goodman/teaching/MonteCarlo2005/notes/GaussianSampling.pdf)<br /> <br /> === General Normal distributions ===<br /> General normal distribution is a special version of the standard normal distribution. The domain of the general normal distribution is affected by the standard deviation and translated by the mean value. <br /> *The pdf of the general normal distribution is <br /> :<br /> &lt;table&gt;<br /> &lt;tr&gt;<br /> &lt;td&gt;&lt;div onmouseover=&quot;document.getElementById('woyun').style.visibility='visible'&quot;<br /> onmouseout=&quot;document.getElementById('woyun').style.visibility='hidden'&quot;&gt;<br /> &lt;math&gt;<br /> f(x) = \frac{1}{\sigma\sqrt{2\pi}} e^{ -\frac{(x-\mu)^2}{2\sigma^2} }<br /> &lt;/math&gt;<br /> &lt;/div&gt;<br /> &lt;/td&gt;<br /> &lt;td&gt;<br /> &lt;div id=&quot;woyun&quot; style=&quot;visibility:hidden&quot;&gt;which is almost useless in this course&lt;/div&gt;<br /> &lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;/table&gt;<br /> where &lt;math&gt; \mu &lt;/math&gt; is the mean or expectation of the distribution and &lt;math&gt; \sigma &lt;/math&gt; is standard deviation &lt;br /&gt;<br /> <br /> The probability density must be scaled by 1/sigma so that the integral is still 1.(Acknowledge: https://en.wikipedia.org/wiki/Normal_distribution)<br /> The special case of the normal distribution is standard normal distribution, which the variance is 1 and the mean is zero. If X is a general normal deviate, then &lt;math&gt; Z=\dfrac{X - (\mu)}{\sigma} &lt;/math&gt; will have a standard normal distribution.<br /> <br /> If Z ~ N(0,1), and we want &lt;math&gt;X &lt;/math&gt;~&lt;math&gt; N(\mu, \sigma^2)&lt;/math&gt;, then &lt;math&gt;X = \mu + \sigma * Z&lt;/math&gt; Since &lt;math&gt;E(x) = \mu +\sigma*0 = \mu &lt;/math&gt; and &lt;math&gt;Var(x) = 0 +\sigma^2*1&lt;/math&gt;<br /> <br /> If &lt;math&gt;Z_1,...Z_d&lt;/math&gt; ~ N(0,1) and are independent then &lt;math&gt;Z = (Z_1,..Z_d)^{T} &lt;/math&gt;~ &lt;math&gt;N(0,I_d)&lt;/math&gt;<br /> ie.<br /> * '''Code'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;close all<br /> &gt;&gt;clear all<br /> &gt;&gt;z1=randn(1,1000); &lt;-generate variable from standard normal distribution<br /> &gt;&gt;z2=randn(1,1000);<br /> &gt;&gt;z=[z1;z2]; &lt;-produce a vector<br /> &gt;&gt;plot(z(1,:),z(2,:),'.')<br /> &lt;/pre&gt;<br /> [[File:Nonstdnormal_example.jpg|300px]]<br /> <br /> If Z~N(0,Id) and X= &lt;math&gt;\underline{\mu} + \Sigma^{\frac{1}{2}} \,Z &lt;/math&gt; then &lt;math&gt;\underline{X}&lt;/math&gt; ~&lt;math&gt;N(\underline{\mu},\Sigma)&lt;/math&gt;<br /> <br /> =====Non-Standard Normal Distributions=====<br /> <br /> '''Example 1: Single-variate Normal'''<br /> <br /> If X ~ Norm(0, 1) then (a + bX) has a normal distribution with a mean of &lt;math&gt;\displaystyle a&lt;/math&gt; and a standard deviation of &lt;math&gt;\displaystyle b&lt;/math&gt; (which is equivalent to a variance of &lt;math&gt;\displaystyle b^2&lt;/math&gt;). Using this information with the Box-Muller transform, we can generate values sampled from some random variable &lt;math&gt;\displaystyle Y\sim N(a,b^2) &lt;/math&gt; for arbitrary values of &lt;math&gt;\displaystyle a,b&lt;/math&gt;.<br /> <br /> # Generate a sample u from Norm(0, 1) using the Box-Muller transform.<br /> # Set v = a + bu.<br /> <br /> The values for v generated in this way will be equivalent to sample from a &lt;math&gt;\displaystyle N(a, b^2)&lt;/math&gt;distribution. We can modify the MatLab code used in the last section to demonstrate this. We just need to add one line before we generate the histogram:<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> v = a + b * x;<br /> &lt;/pre&gt;<br /> <br /> For instance, this is the histogram generated when b = 15, a = 125:<br /> <br /> [[File:Hist normal.jpg|center|500]]<br /> <br /> '''Example 2: Multi-variate Normal'''<br /> <br /> The Box-Muller method can be extended to higher dimensions to generate multivariate normals. The objects generated will be nx1 vectors, and their variance will be described by nxn covariance matrices.<br /> <br /> &lt;math&gt;\mathbf{z} = N(\mathbf{u}, \Sigma)&lt;/math&gt; defines the n by 1 vector &lt;math&gt;\mathbf{z}&lt;/math&gt; such that:<br /> <br /> * &lt;math&gt;\displaystyle u_i&lt;/math&gt; is the average of &lt;math&gt;\displaystyle z_i&lt;/math&gt;<br /> * &lt;math&gt;\!\Sigma_{ii}&lt;/math&gt; is the variance of &lt;math&gt;\displaystyle z_i&lt;/math&gt;<br /> * &lt;math&gt;\!\Sigma_{ij}&lt;/math&gt; is the co-variance of &lt;math&gt;\displaystyle z_i&lt;/math&gt; and &lt;math&gt;\displaystyle z_j&lt;/math&gt;<br /> <br /> If &lt;math&gt;\displaystyle z_1, z_2, ..., z_d&lt;/math&gt; are normal variables with mean 0 and variance 1, then the vector &lt;math&gt;\displaystyle (z_1, z_2,..., z_d) &lt;/math&gt; has mean 0 and variance &lt;math&gt;\!I&lt;/math&gt;, where 0 is the zero vector and &lt;math&gt;\!I&lt;/math&gt; is the identity matrix. This fact suggests that the method for generating a multivariate normal is to generate each component individually as single normal variables.<br /> <br /> The mean and the covariance matrix of a multivariate normal distribution can be adjusted in ways analogous to the single variable case. If &lt;math&gt;\mathbf{z} \sim N(0,I)&lt;/math&gt;, then &lt;math&gt;\Sigma^{1/2}\mathbf{z}+\mu \sim N(\mu,\Sigma)&lt;/math&gt;. Note here that the covariance matrix is symmetric and nonnegative, so its square root should always exist.<br /> <br /> We can compute &lt;math&gt;\mathbf{z}&lt;/math&gt; in the following way:<br /> <br /> # Generate an n by 1 vector &lt;math&gt;\mathbf{x} = \begin{bmatrix}x_{1} &amp; x_{2} &amp; ... &amp; x_{n}\end{bmatrix}&lt;/math&gt; where &lt;math&gt;x_{i}&lt;/math&gt; ~ Norm(0, 1) using the Box-Muller transform.<br /> # Calculate &lt;math&gt;\!\Sigma^{1/2}&lt;/math&gt; using singular value decomposition.<br /> # Set &lt;math&gt;\mathbf{z} = \Sigma^{1/2} \mathbf{x} + \mathbf{u}&lt;/math&gt;.<br /> <br /> The following MatLab code provides an example, where a scatter plot of 10000 random points is generated. In this case x and y have a co-variance of 0.9 - a very strong positive correlation.<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> x = zeros(10000, 1);<br /> y = zeros(10000, 1);<br /> for ii = 1:10000<br /> u1 = rand;<br /> u2 = rand;<br /> R2 = -2 * log(u1);<br /> theta = 2 * pi * u2;<br /> x(ii) = sqrt(R2) * cos(theta);<br /> y(ii) = sqrt(R2) * sin(theta);<br /> end<br /> <br /> E = [1, 0.9; 0.9, 1];<br /> [u s v] = svd(E);<br /> root_E = u * (s ^ (1 / 2)) * u';<br /> <br /> z = (root_E * [x y]');<br /> z(1,:) = z(1,:) + 0;<br /> z(2,:) = z(2,:) + -3;<br /> <br /> scatter(z(1,:), z(2,:))<br /> &lt;/pre&gt;<br /> <br /> Note: The svd command computes the matrix singular value decomposition.<br /> <br /> [u,s,v] = svd(E) produces a diagonal matrix s of the same dimension as E, with nonnegative diagonal elements in decreasing order, and unitary matrices u and v so that E = u*s*v'.<br /> <br /> This code generated the following scatter plot:<br /> <br /> [[File:scatter covar.jpg|center|500px]]<br /> <br /> In Matlab, we can also use the function &quot;sqrtm()&quot; or &quot;chol()&quot; (Cholesky Decomposition) to calculate square root of a matrix directly. Note that the resulting root matrices may be different but this does materially affect the simulation.<br /> Here is an example:<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> E = [1, 0.9; 0.9, 1];<br /> r1 = sqrtm(E);<br /> r2 = chol(E);<br /> &lt;/pre&gt;<br /> <br /> R code for a multivariate normal distribution:<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> n=10000;<br /> r2&lt;--2*log(runif(n));<br /> theta&lt;-2*pi*(runif(n));<br /> x&lt;-sqrt(r2)*cos(theta);<br /> <br /> y&lt;-sqrt(r2)*sin(theta);<br /> a&lt;-matrix(c(x,y),nrow=n,byrow=F);<br /> e&lt;-matrix(c(1,.9,09,1),nrow=2,byrow=T);<br /> svde&lt;-svd(e);<br /> root_e&lt;-svde$u %*% diag(svde$d)^1/2;<br /> z&lt;-t(root_e %*%t(a));<br /> z[,1]=z[,1]+5;<br /> z[,2]=z[,2]+ -8;<br /> par(pch=19);<br /> plot(z,col=rgb(1,0,0,alpha=0.06))<br /> &lt;/pre&gt;<br /> <br /> [[File:m_normal.png|center|500px]]<br /> <br /> === Bernoulli Distribution ===<br /> The Bernoulli distribution is a discrete probability distribution, which usually describes an event that only has two possible results, i.e. success or failure (x=0 or 1). If the event succeed, we usually take value 1 with success probability p, and take value 0 with failure probability q = 1 - p. <br /> <br /> P ( x = 0) = q = 1 - p &lt;br /&gt;<br /> P ( x = 1) = p &lt;br /&gt;<br /> P ( x = 0) + P (x = 1) = p + q = 1 &lt;br /&gt;<br /> <br /> If X~Ber(p), its pdf is of the form &lt;math&gt;f(x)= p^{x}(1-p)^{(1-x)}&lt;/math&gt;, x=0,1<br /> &lt;br&gt; P is the success probability.<br /> <br /> The Bernoulli distribution is a special case of binomial distribution, where the variate x only has two outcomes; so that the Bernoulli also can use the probability density function of the binomial distribution with the variate x taking values 0 and 1.<br /> <br /> The most famous example for the Bernoulli Distribution would be the &quot;Flip Coin&quot; question, which has only two possible outcomes(Success or Failure) with the same probabilities of 0.5 <br /> <br /> Let x1,x2 denote the lifetime of 2 independent particles, x1~exp(&lt;math&gt;\lambda&lt;/math&gt;), x2~exp(&lt;math&gt;\lambda&lt;/math&gt;)<br /> we are interested in y=min(x1,x2)<br /> <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> <br /> Procedure:<br /> <br /> To simulate the event of flipping a coin, let P be the probability of flipping head and X = 1 and 0 represent<br /> flipping head and tail respectively:<br /> <br /> 1. Draw U ~ Uniform(0,1)<br /> <br /> 2. If U &lt;= P<br /> <br /> X = 1<br /> <br /> Else<br /> <br /> X = 0<br /> <br /> 3. Repeat as necessary<br /> <br /> &lt;/pre&gt;<br /> <br /> An intuitive way to think of this is in the coin flip example we discussed in a previous lecture. In this example we set p = 1/2 and this allows for 50% of points to be heads or tails.<br /> <br /> * '''Code to Generate Bernoulli(p = 0.3)'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> i = 1;<br /> <br /> while (i &lt;=1000)<br /> u =rand();<br /> p = 0.3;<br /> if (u &lt;= p)<br /> x(i) = 1;<br /> else<br /> x(i) = 0;<br /> end<br /> i = i + 1;<br /> end<br /> <br /> hist(x)<br /> &lt;/pre&gt;<br /> <br /> However, we know that if &lt;math&gt;\begin{align} X_i \sim Bernoulli(p) \end{align}&lt;/math&gt; where each &lt;math&gt;\begin{align} X_i \end{align}&lt;/math&gt; is independent,&lt;br /&gt;<br /> &lt;math&gt;U = \sum_{i=1}^{n} X_i \sim Binomial(n,p)&lt;/math&gt;&lt;br /&gt;<br /> So we can sample from binomial distribution using this property.<br /> Note: We can consider Binomial distribution as the sum of n, ''independent'', Bernoulli distributions<br /> &lt;div style=&quot;background:#CCFF33;border-radius:5px;box-shadow: 10px 10px 5px #888888;padding:30px;&quot;&gt;<br /> * '''Code to Generate Binomial(n = 20,p = 0.7)'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> p = 0.7;<br /> n = 20;<br /> <br /> for k=1:5000<br /> i = 1;<br /> for i=1:n<br /> u=rand();<br /> if (u &lt;= p)<br /> y(i) = 1;<br /> else<br /> y(i) = 0;<br /> end<br /> end<br /> <br /> x(k) = sum(y==1);<br /> end<br /> <br /> hist(x)<br /> <br /> &lt;/pre&gt;<br /> <br /> <br /> <br /> <br /> &lt;/div&gt;<br /> Note: We can also regard the Bernoulli Distribution as either a conditional distribution or &lt;math&gt;f(x)= p^{x}(1-p)^{(1-x)}&lt;/math&gt;, x=0,1.<br /> <br /> Comments on Matlab:<br /> When doing operations on vectors, always put a dot before the operator if you want the operation to be done to every element in the vector. <br /> example: Let V be a vector with dimension 2*4 and you want each element multiply by 3. <br /> The Matlab code is 3.*V<br /> <br /> some examples for using code to generate distribution.<br /> <br /> == Class 7 - Tuesday, May 28 ==<br /> <br /> <br /> [[Note that the material in this lecture will not be on the exam; it was only to supplement what we have learned.]]<br /> ===Universality of the Uniform Distribution/Inverse Method===<br /> <br /> The inverse method is universal in the sense that we can potentially sample from any distribution where we can find the inverse of the cumulative distribution function.<br /> <br /> Procedure:<br /> <br /> 1) Generate U~Unif (0, 1)&lt;br&gt;<br /> 2) Set &lt;math&gt;x=F^{-1}(u)&lt;/math&gt;&lt;br&gt;<br /> 3) X~f(x)&lt;br&gt;<br /> <br /> '''Remark'''&lt;br&gt;<br /> 1) The preceding can be written algorithmically for discrete random variables as &lt;br&gt;<br /> Generate a random number U ~ U(0,1] &lt;br&gt;<br /> If U &lt; p&lt;sub&gt;0&lt;/sub&gt; set X = x&lt;sub&gt;0&lt;/sub&gt; and stop &lt;br&gt;<br /> If U &lt; p&lt;sub&gt;0&lt;/sub&gt; + p&lt;sub&gt;1&lt;/sub&gt; set X = x&lt;sub&gt;1&lt;/sub&gt; and stop &lt;br&gt;<br /> ... &lt;br&gt;<br /> 2) If the x&lt;sub&gt;i&lt;/sub&gt;, i&gt;=0, are ordered so that x&lt;sub&gt;0&lt;/sub&gt; &lt; x&lt;sub&gt;1&lt;/sub&gt; &lt; x&lt;sub&gt;2&lt;/sub&gt; &lt;... and if we let F denote the distribution function of X, then X will equal x&lt;sub&gt;j&lt;/sub&gt; if F(x&lt;sub&gt;j-1&lt;/sub&gt;) &lt;= U &lt; F(x&lt;sub&gt;j&lt;/sub&gt;)<br /> <br /> '''Example 1'''&lt;br&gt;<br /> <br /> Let &lt;math&gt;X&lt;/math&gt;&lt;sub&gt;1&lt;/sub&gt;,&lt;math&gt;X&lt;/math&gt;&lt;sub&gt;2&lt;/sub&gt; denote the lifetime of two independent particles:&lt;br&gt;<br /> &lt;math&gt;X&lt;/math&gt;&lt;sub&gt;1&lt;/sub&gt;~exp(&lt;math&gt;\lambda&lt;/math&gt;&lt;sub&gt;1&lt;/sub&gt;)&lt;br&gt;<br /> &lt;math&gt;X&lt;/math&gt;&lt;sub&gt;2&lt;/sub&gt;~exp(&lt;math&gt;\lambda&lt;/math&gt;&lt;sub&gt;2&lt;/sub&gt;)&lt;br&gt;<br /> <br /> We are interested in &lt;math&gt;y=min(X&lt;/math&gt;&lt;sub&gt;1&lt;/sub&gt;&lt;math&gt;,X&lt;/math&gt;&lt;sub&gt;2&lt;/sub&gt;&lt;math&gt;)&lt;/math&gt;&lt;br&gt;<br /> Design an algorithm based on the Inverse-Transform Method to generate samples according to &lt;math&gt;f&lt;/math&gt;&lt;sub&gt;y&lt;/sub&gt;&lt;math&gt;(y)&lt;/math&gt;&lt;br&gt;<br /> <br /> '''Solution:'''&lt;br&gt;<br /> <br /> x&lt;sub&gt;1&lt;/sub&gt;~exp(&lt;math&gt;\lambda_1&lt;/math&gt;)&lt;br&gt;<br /> x&lt;sub&gt;2&lt;/sub&gt;~exp(&lt;math&gt;\lambda_2&lt;/math&gt;)&lt;br&gt;<br /> &lt;math&gt;f_{x(x)}=\lambda e^{-\lambda x},x\geq0 &lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt;F_X(x)=1-e^{-\lambda x}, x\geq 0&lt;/math&gt;&lt;br&gt;<br /> <br /> &lt;math&gt;1-F_Y(y) = P(Y&gt;y)&lt;/math&gt; = P(min(X&lt;sub&gt;1&lt;/sub&gt;,X&lt;sub&gt;2&lt;/sub&gt;) &gt; y) = &lt;math&gt;\, P((X_1)&gt;y) P((X_2)&gt;y) = e^{\, -(\lambda_1 + \lambda_2) y}&lt;/math&gt;&lt;br&gt;<br /> <br /> &lt;math&gt;F_Y(y)=1-e^{\, -(\lambda_1 + \lambda_2) y}, y\geq 0&lt;/math&gt;&lt;br&gt;<br /> <br /> &lt;math&gt;U=1-e^{\, -(\lambda_1 + \lambda_2) y}&lt;/math&gt; =&gt; &lt;math&gt;y=\, {-\frac {1}{{\lambda_1 +\lambda_2}}} ln(1-u)&lt;/math&gt;&lt;br&gt;<br /> <br /> '''Procedure:'''<br /> <br /> Step1: Generate U~ U(0, 1)&lt;br&gt;<br /> <br /> Step2: set &lt;math&gt;y=\, {-\frac {1}{{\lambda_1 +\lambda_2}}} ln(1-u)&lt;/math&gt;&lt;br&gt;<br /> <br /> or set &lt;math&gt;y=\, {-\frac {1} {{\lambda_1 +\lambda_2}}} ln(u)&lt;/math&gt;&lt;br&gt;<br /> Since it is a uniform distribution, therefore after generate a lot of times 1-u and u are the same.<br /> <br /> <br /> * '''Matlab Code'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt; lambda1 = 1;<br /> &gt;&gt; lambda2 = 2;<br /> &gt;&gt; u = rand;<br /> &gt;&gt; y = -log(u)/(lambda1 + lambda2) <br /> &lt;/pre&gt;<br /> <br /> If we generalize this example from two independent particles to n independent particles we will have:&lt;br&gt;<br /> <br /> &lt;math&gt;X&lt;/math&gt;&lt;sub&gt;1&lt;/sub&gt;~exp(&lt;math&gt;\lambda&lt;/math&gt;&lt;sub&gt;1&lt;/sub&gt;)&lt;br&gt;&lt;math&gt;X&lt;/math&gt;&lt;sub&gt;2&lt;/sub&gt;~exp(&lt;math&gt;\lambda&lt;/math&gt;&lt;sub&gt;2&lt;/sub&gt;)&lt;br&gt; ...&lt;br&gt; &lt;math&gt;X&lt;/math&gt;&lt;sub&gt;n&lt;/sub&gt;~exp(&lt;math&gt;\lambda&lt;/math&gt;&lt;sub&gt;n&lt;/sub&gt;)&lt;br&gt;.<br /> <br /> And the algorithm using the inverse-transform method as follows:<br /> <br /> step1: Generate U~U(0,1)<br /> <br /> Step2: &lt;math&gt;y=\, {-\frac {1}{{ \sum\lambda_i}}} ln(1-u)&lt;/math&gt;&lt;br&gt;<br /> <br /> <br /> '''Example 2'''&lt;br&gt;<br /> Consider U~Unif[0,1)&lt;br&gt;<br /> &lt;math&gt;X=\, a (1-\sqrt{1-u})&lt;/math&gt;, <br /> &lt;br&gt;where a&gt;0 and a is a real number<br /> What is the distribution of X?&lt;br&gt;<br /> <br /> '''Solution:'''&lt;br&gt;<br /> <br /> We can find a form for the cumulative distribution function of X by isolating U as U~Unif[0,1) will take values from the range of F(X)uniformly. It then remains to differentiate the resulting form by X to obtain the probability density function.<br /> <br /> &lt;math&gt;X=\, a (1-\sqrt{1-u})&lt;/math&gt;&lt;br&gt;<br /> =&gt;&lt;math&gt;1-\frac {x}{a}=\sqrt{1-u}&lt;/math&gt;&lt;br&gt;<br /> =&gt;&lt;math&gt;u=1-(1-\frac {x}{a})^2&lt;/math&gt;&lt;br&gt;<br /> =&gt;&lt;math&gt;u=\, {\frac {x}{a}} (2-\frac {x}{a})&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;f(x)=\frac {dF(x)}{dx}=\frac {2}{a}-\frac {2x}{a^2}=\, \frac {2}{a} (1-\frac {x}{a})&lt;/math&gt;&lt;br&gt;<br /> [[File:Example_2_diagram.jpg]]<br /> <br /> '''Example 3'''&lt;br&gt;<br /> <br /> Suppose F&lt;sub&gt;X&lt;/sub&gt;(x) = x&lt;sup&gt;n&lt;/sup&gt;, 0 ≤ x ≤ 1, n ∈ N &gt; 0. Generate values from X.&lt;br&gt;<br /> <br /> '''Solution:'''&lt;br&gt;<br /> &lt;br&gt;<br /> 1. Generate &lt;math&gt;U ~\sim~ Unif[0, 1)&lt;/math&gt;&lt;br&gt;<br /> 2. Set &lt;math&gt;X = U^{1/n}&lt;/math&gt;&lt;br&gt;<br /> &lt;br&gt;<br /> For example, when &lt;math&gt;n = 20&lt;/math&gt;,&lt;br&gt;<br /> &lt;math&gt;U = 0.6&lt;/math&gt; =&gt; &lt;math&gt;X = U^{1/20} = 0.974&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;U = 0.5 =&gt;&lt;/math&gt; &lt;math&gt;X = U^{1/20} = 0.966&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;U = 0.2&lt;/math&gt; =&gt; &lt;math&gt;X = U^{1/20} = 0.923&lt;/math&gt;&lt;br&gt;<br /> &lt;br&gt;<br /> Observe from above that the values of X for n = 20 are close to 1, this is because we can view &lt;math&gt;X^n&lt;/math&gt; as the maximum of n independent random variables &lt;math&gt;X,&lt;/math&gt; &lt;math&gt;X~\sim~Unif(0,1)&lt;/math&gt; and is much likely to be close to 1 as n increases. This is because when n is large the exponent tends towards 0. This observation is the motivation for method 2 below.&lt;br&gt;<br /> <br /> Recall that<br /> If Y = max (X&lt;sub&gt;1&lt;/sub&gt;, X&lt;sub&gt;2&lt;/sub&gt;, ... , X&lt;sub&gt;n&lt;/sub&gt;), where X&lt;sub&gt;1&lt;/sub&gt;, X&lt;sub&gt;2&lt;/sub&gt;, ... , X&lt;sub&gt;n&lt;/sub&gt; are independent, &lt;br&gt;<br /> F&lt;sub&gt;Y&lt;/sub&gt;(y) = P(Y ≤ y) = P(max (X&lt;sub&gt;1&lt;/sub&gt;, X&lt;sub&gt;2&lt;/sub&gt;, ... , X&lt;sub&gt;n&lt;/sub&gt;) ≤ y) = P(X&lt;sub&gt;1&lt;/sub&gt; ≤ y, X&lt;sub&gt;2&lt;/sub&gt; ≤ y, ... , X&lt;sub&gt;n&lt;/sub&gt; ≤ y) = F&lt;sub&gt;x&lt;sub&gt;1&lt;/sub&gt;&lt;/sub&gt;(y) F&lt;sub&gt;x&lt;sub&gt;2&lt;/sub&gt;&lt;/sub&gt;(y) ... F&lt;sub&gt;x&lt;sub&gt;n&lt;/sub&gt;&lt;/sub&gt;(y)&lt;br&gt;<br /> Similarly if &lt;math&gt; Y = min(X_1,\ldots,X_n)&lt;/math&gt; then the cdf of &lt;math&gt;Y&lt;/math&gt; is &lt;math&gt;F_Y = 1- &lt;/math&gt;&lt;math&gt;\prod&lt;/math&gt;&lt;math&gt;(1- F_{X_i})&lt;/math&gt;&lt;br&gt; <br /> &lt;br&gt;<br /> '''Method 1:''' Following the above result we can see that in this example, F&lt;sub&gt;X&lt;/sub&gt; = x&lt;sup&gt;n&lt;/sup&gt; is the cumulative distribution function of the max of n uniform random variables between 0 and 1 (since for U~Unif(0, 1), F&lt;sub&gt;U&lt;/sub&gt;(x) = &lt;br&gt;<br /> '''Method 2:''' generate X by having a sample of n independent U~Unif(0, 1) and take the max of the n samples to be x. However, the solution given above using inverse-transform method only requires generating one uniform random number instead of n of them, so it is a more efficient method.<br /> &lt;br&gt;<br /> <br /> Generate the Y = max (X1, X2, ... , Xn), Y = min (X1, X2, ... , Xn), pdf and cdf, but (xi and xj are independent) i,j=1,2,3,4,5.....<br /> <br /> '''Example 4 (New)'''&lt;br&gt;<br /> Now, we are having an similar example as example 1 just doing the maximum way.<br /> <br /> Let X&lt;sub&gt;1&lt;/sub&gt;,X&lt;sub&gt;2&lt;/sub&gt; denote the lifetime of two independent particles:&lt;br&gt;<br /> &lt;math&gt;\, X_1, X_2 \sim exp(\lambda)&lt;/math&gt;&lt;br&gt;<br /> <br /> We are interested in Z=max(X&lt;sub&gt;1&lt;/sub&gt;,X&lt;sub&gt;2&lt;/sub&gt;)&lt;br&gt;<br /> Design an algorithm based on the Inverse-Transform Method to generate samples according to f&lt;sub&gt;Z&lt;/sub&gt;(z)&lt;br&gt;<br /> <br /> &lt;math&gt;\, F_Z(z)=P[Z&lt;=z] = F_{X_1}(z) \cdot F_{X_2}(z) = (1-e^{-\lambda z})^2&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt; \text{thus } F^{-1}(z) = -\frac{1}{\lambda}\log(1-\sqrt z)&lt;/math&gt;&lt;br&gt;<br /> <br /> To sample Z: &lt;br&gt;<br /> &lt;math&gt;\, \text{Step 1: Generate } U \sim U[0,1)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\, \text{Step 2: Let } Z = -\frac{1}{\lambda}\log(1-\sqrt U)&lt;/math&gt;, therefore we can generate random variable of Z.&lt;br&gt;&lt;br&gt;<br /> <br /> '''Discrete Case:'''<br /> &lt;font size=&quot;3&quot;&gt;<br /> u~unif(0,1)&lt;br&gt;<br /> x &lt;- 0, S &lt;- P&lt;sub&gt;0&lt;/sub&gt;&lt;br&gt;<br /> while u &lt; S&lt;br&gt;<br /> x &lt;- x + 1&lt;br&gt;<br /> S &lt;- S + P&lt;sub&gt;0&lt;/sub&gt;&lt;br&gt;<br /> Return x&lt;br&gt;&lt;/font&gt;<br /> <br /> ===Decomposition Method===<br /> The CDF, F, is a composition if &lt;math&gt;F_{X}(x)&lt;/math&gt; can be written as:<br /> <br /> &lt;math&gt;F_{X}(x) = \sum_{i=1}^{n}p_{i}F_{X_{i}}(x)&lt;/math&gt; where<br /> <br /> 1) p&lt;sub&gt;i&lt;/sub&gt; &gt; 0<br /> <br /> 2) &lt;math&gt;\sum_{i=1}^{n}&lt;/math&gt;p&lt;sub&gt;i&lt;/sub&gt; = 1.<br /> <br /> 3) &lt;math&gt;F_{X_{i}}(x)&lt;/math&gt; is a CDF<br /> <br /> The general algorithm to generate random variables from a composition CDF is:<br /> <br /> 1) Generate U,V ~ &lt;math&gt; Unif(0,1)&lt;/math&gt;<br /> <br /> 2) If U &lt; p&lt;sub&gt;1&lt;/sub&gt;, V = &lt;math&gt;F_{X_{1}}(x)&lt;/math&gt;&lt;sup&gt;-1&lt;/sup&gt;<br /> <br /> 3) Else if U &lt; p&lt;sub&gt;1&lt;/sub&gt; + p&lt;sub&gt;2&lt;/sub&gt;, V = &lt;math&gt;F_{X_{2}}(x)&lt;/math&gt;&lt;sup&gt;-1&lt;/sup&gt;<br /> <br /> 4) Repeat from Step 1 (if N randomly generated variables needed, repeat N times)<br /> <br /> &lt;b&gt;Explanation&lt;/b&gt;&lt;br&gt;<br /> Each random variable that is a part of X contributes &lt;math&gt;p_{i} F_{X_{i}}(x)&lt;/math&gt; to &lt;math&gt;F_{X}(x)&lt;/math&gt; every time.<br /> From a sampling point of view, that is equivalent to contributing &lt;math&gt;F_{X_{i}}(x)&lt;/math&gt; &lt;math&gt;p_{i}&lt;/math&gt; of the time. The logic of this is similar to that of the Accept-Reject Method, but instead of rejecting a value depending on the value u takes, we instead decide which distribution to sample it from.<br /> <br /> <br /> &lt;b&gt; Simplified Version &lt;/b&gt;&lt;br&gt;<br /> 1) Generate &lt;math&gt;u \sim Unif(0,1)&lt;/math&gt; &lt;br&gt;<br /> 2) Set &lt;math&gt; X=0, s=P_0&lt;/math&gt;&lt;br&gt;<br /> 3) While &lt;math&gt; u &gt; s, &lt;/math&gt;&lt;br&gt;<br /> set &lt;math&gt; X = X+1&lt;/math&gt; and &lt;math&gt; s=s+P_x &lt;/math&gt; &lt;br&gt;<br /> 4) Return &lt;math&gt; X &lt;/math&gt;<br /> <br /> === Examples of Decomposition Method ===<br /> &lt;b&gt;Example 1&lt;/b&gt; &lt;br&gt;<br /> &lt;math&gt;f(x) = \frac{5}{12}(1+(x-1)^4) 0\leq x\leq 2&lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt;f(x) = \frac{5}{12}+\frac{5}{12}(x-1)^4 = \frac{5}{6} (\frac{1}{2})+\frac {1}{6}(\frac{5}{2})(x-1))^4&lt;/math&gt; &lt;br&gt;<br /> Let&lt;math&gt;f_{x_1}= \frac{1}{2}&lt;/math&gt; and &lt;math&gt;f_{x_2} = \frac {5}{2}(x-1)^4&lt;/math&gt; &lt;br&gt;<br /> <br /> Algorithm: <br /> Generate U~Unif(0,1) &lt;br&gt;<br /> If &lt;math&gt;0&lt;u&lt;\frac {5}{6}&lt;/math&gt;, then we sample from f&lt;sub&gt;x1&lt;/sub&gt; &lt;br&gt;<br /> Else if &lt;math&gt;\frac{5}{6}&lt;u&lt;1&lt;/math&gt;, we sample from f&lt;sub&gt;x2&lt;/sub&gt; &lt;br&gt;<br /> We can find the inverse CDF of f&lt;sub&gt;x2&lt;/sub&gt; and utilize the Inverse Transform Method in order to sample from f&lt;sub&gt;x2&lt;/sub&gt; &lt;br&gt;<br /> Sampling from f&lt;sub&gt;x1&lt;/sub&gt; is more straightforward since it is uniform over the interval (0,2) &lt;br&gt;<br /> <br /> divided f(x) to two pdf of x1 and x2, with uniform distribution, of two range of uniform.<br /> <br /> &lt;b&gt;Example 2&lt;/b&gt; &lt;br&gt;<br /> &lt;math&gt;f(x)=\frac{1}{4}e^{-x}+2x+\frac{1}{12}, \quad 0\leq x \leq 3 &lt;/math&gt; &lt;br&gt;<br /> We can rewrite f(x) as &lt;math&gt;f(x)=(\frac{1}{4}) e^{-x}+(\frac{2}{4}) 4x+(\frac{1}{4}) \frac{1}{3}&lt;/math&gt; &lt;br&gt;<br /> Let f&lt;sub&gt;x1&lt;/sub&gt; = &lt;math&gt;e^{-x}&lt;/math&gt;, f&lt;sub&gt;x2&lt;/sub&gt; = 4x, and f&lt;sub&gt;x3&lt;/sub&gt; = &lt;math&gt;\frac{1}{3}&lt;/math&gt; &lt;br&gt;<br /> Generate U~Unif(0,1)&lt;br&gt;<br /> If &lt;math&gt;0&lt;u&lt;\frac{1}{4}&lt;/math&gt;, we sample from f&lt;sub&gt;x1&lt;/sub&gt; &lt;br&gt;&lt;br&gt;<br /> If &lt;math&gt;\frac{1}{4}\leq u &lt; \frac{3}{4}&lt;/math&gt;, we sample from f&lt;sub&gt;x2&lt;/sub&gt; &lt;br&gt;&lt;br&gt;<br /> Else if &lt;math&gt;\frac{3}{4} \leq u &lt; 1&lt;/math&gt;, we sample from f&lt;sub&gt;x3&lt;/sub&gt; &lt;br&gt;<br /> We can find the inverse CDFs of f&lt;sub&gt;x1&lt;/sub&gt; and f&lt;sub&gt;x2&lt;/sub&gt; and utilize the Inverse Transform Method in order to sample from f&lt;sub&gt;x1&lt;/sub&gt; and f&lt;sub&gt;x2&lt;/sub&gt; &lt;br&gt;&lt;br&gt;<br /> We find F&lt;sub&gt;x1&lt;/sub&gt; = &lt;math&gt; 1-e^{-x}&lt;/math&gt; and F&lt;sub&gt;x2&lt;/sub&gt; = &lt;math&gt;2x^{2}&lt;/math&gt; &lt;br&gt;<br /> We find the inverses are &lt;math&gt; X = -ln(1-u)&lt;/math&gt; for F&lt;sub&gt;x1&lt;/sub&gt; and &lt;math&gt; X = \sqrt{\frac{U}{2}}&lt;/math&gt; for F&lt;sub&gt;x2&lt;/sub&gt; &lt;br&gt;<br /> Sampling from f&lt;sub&gt;x3&lt;/sub&gt; is more straightforward since it is uniform over the interval (0,3) &lt;br&gt;<br /> <br /> In general, to write an &lt;b&gt;efficient &lt;/b&gt; algorithm for: &lt;br&gt;<br /> &lt;math&gt;F_{X}(x) = p_{1}F_{X_{1}}(x) + p_{2}F_{X_{2}}(x) + ... + p_{n}F_{X_{n}}(x)&lt;/math&gt; &lt;br&gt;<br /> We would first calculate &lt;math&gt; {q_i} = \sum_{j=1}^i p_j, \forall i = 1,\dots, n&lt;/math&gt;<br /> Then Generate &lt;math&gt; U \sim~ Unif(0,1) &lt;/math&gt; &lt;br&gt;<br /> If &lt;math&gt; U &lt; q_1 &lt;/math&gt; sample from &lt;math&gt; f_1 &lt;/math&gt; &lt;br&gt;<br /> else if &lt;math&gt; u&lt;q_i &lt;/math&gt; sample from &lt;math&gt; f_i &lt;/math&gt; for &lt;math&gt; 1 &lt; i &lt; n &lt;/math&gt;&lt;br&gt;<br /> else sample from &lt;math&gt; f_n &lt;/math&gt; &lt;br&gt;<br /> <br /> when we divided the pdf of different range of f(x1) f(x2) and f(x3), and generate all of them and inverse, U~U(0,1)<br /> &lt;div style=&quot;background:#66CCFF;padding:20px;border-radius:5px;&quot;&gt;<br /> <br /> === Example of Decomposition Method ===<br /> <br /> &lt;math&gt;F_x(x) = \frac {1}{3} x+\frac {1}{3} x^2+\frac {1}{3} x^3, 0\leq x\leq 1&lt;/math&gt;<br /> <br /> Let &lt;math&gt;U =F_x(x) = \frac {1}{3} x+\frac {1}{3} x^2+\frac {1}{3} x^3&lt;/math&gt;, solve for x.<br /> <br /> &lt;math&gt;P_1=\frac{1}{3}, F_{x1} (x)= x, P_2=\frac{1}{3},F_{x2} (x)= x^2, <br /> P_3=\frac{1}{3},F_{x3} (x)= x^3&lt;/math&gt;<br /> <br /> '''Algorithm:'''<br /> <br /> Generate &lt;math&gt;\,U \sim Unif [0,1)&lt;/math&gt;<br /> <br /> Generate &lt;math&gt;\,V \sim Unif [0,1)&lt;/math&gt;<br /> <br /> if &lt;math&gt;0\leq u \leq \frac{1}{3}, x = v&lt;/math&gt;<br /> <br /> else if &lt;math&gt;u \leq \frac{2}{3}, x = v^{\frac{1}{2}}&lt;/math&gt;<br /> <br /> else &lt;math&gt;x=v^{\frac{1}{3}}&lt;/math&gt; &lt;br&gt;<br /> <br /> <br /> '''Matlab Code:''' <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> u=rand # U is <br /> v=rand<br /> if u&lt;1/3<br /> x=v<br /> elseif u&lt;2/3<br /> x=sqrt(v)<br /> else<br /> x=v^(1/3)<br /> end<br /> &lt;/pre&gt;<br /> <br /> <br /> &lt;span style=&quot;margin:0 auto;&quot;&gt;=== Example of Decomposition Method(new) ===&lt;/span&gt;<br /> <br /> F&lt;sub&gt;x&lt;/sub&gt;(x) = 1/2*x+1/2*x&lt;sup&gt;2&lt;/sup&gt;, 0&lt;= x&lt;=1<br /> <br /> let U =F&lt;sub&gt;x&lt;/sub&gt;(x) = 1/2*x+1/2*x&lt;sup&gt;2&lt;/sup&gt;, solve for x.<br /> <br /> P&lt;sub&gt;1&lt;/sub&gt;=1/2, F&lt;sub&gt;x1&lt;/sub&gt;(x)= x, P&lt;sub&gt;2&lt;/sub&gt;=1/2,F&lt;sub&gt;x2&lt;/sub&gt;(x)= x&lt;sup&gt;2&lt;/sup&gt;, <br /> <br /> '''Algorithm:'''<br /> <br /> Generate U ~ Unif [0,1)<br /> <br /> Generate V~ Unif [0,1)<br /> <br /> if 0&lt;u&lt;1/2, x = v<br /> <br /> else x = v&lt;sup&gt;1/2&lt;/sup&gt;<br /> <br /> <br /> '''Matlab Code:''' <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> u=rand<br /> v=rand<br /> if u&lt;1/2<br /> x=v<br /> else<br /> x=sqrt(v)<br /> end<br /> &lt;/pre&gt;<br /> &lt;/div&gt;<br /> <br /> '''Extra Knowledge about Decomposition Method'''<br /> <br /> There are different types and applications of Decomposition Method<br /> <br /> 1. Primal decomposition<br /> <br /> 2. Dual decomposition<br /> <br /> 3. Decomposition with constraints<br /> <br /> 4. More general decomposition structures<br /> <br /> 5. Rate control<br /> <br /> 6. Single commodity network ﬂow<br /> <br /> For More Details, please refer to http://www.stanford.edu/class/ee364b/notes/decomposition_notes.pdf<br /> <br /> ===Fundamental Theorem of Simulation===<br /> Consider two shapes, A and B, where B is a sub-shape (subset) of A. <br /> We want to sample uniformly from inside the shape B.<br /> Then we can sample uniformly inside of A, and throw away all samples outside of B, and this will leave us with a uniform sample from within B. <br /> (Basis of the Accept-Reject algorithm)<br /> <br /> The advantage of this method is that we can sample a unknown distribution from a easy distribution. The disadvantage of this method is that it may need to reject many points, which is inefficient.&lt;br /&gt;<br /> Inverse each part of partial CDF, the partial CDF is divided by the original CDF, partial range is uniform distribution.&lt;br /&gt;<br /> More specific definition of the theorem can be found here.&lt;ref&gt;http://www.bus.emory.edu/breno/teaching/MCMC_GibbsHandouts.pdf&lt;/ref&gt;<br /> <br /> Matlab code:<br /> <br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> close all <br /> clear all<br /> ii=1;<br /> while ii&lt;1000<br /> u=rand<br /> y=R*(2*U-1)<br /> if (1-U^2)&gt;=(2*u-1)^2<br /> x(ii)=y;<br /> ii=ii+1<br /> end<br /> &lt;/pre&gt;<br /> <br /> ===Question 2===<br /> <br /> Use Acceptance and Rejection Method to sample from &lt;math&gt;f_X(x)=b*x^n*(1-x)^n&lt;/math&gt; , &lt;math&gt;n&gt;0&lt;/math&gt;, &lt;math&gt;0&lt;x&lt;1&lt;/math&gt;<br /> <br /> Solution:<br /> This is a beta distribution, Beta ~&lt;math&gt;\int _{0}^{1}b*x^{n}*(1-x)^{n}dx = 1&lt;/math&gt;<br /> <br /> U&lt;sub&gt;1~Unif[0,1)<br /> <br /> <br /> U&lt;sub&gt;2~Unif[0,1)<br /> <br /> fx=&lt;math&gt; bx^{1/2}(1-x)^{1/2} &lt;= bx^{-1/2}\sqrt2 ,0&lt;=x&lt;=1/2 &lt;/math&gt;<br /> <br /> <br /> <br /> The beta distribution maximized at 0.5 with value &lt;math&gt;(1/4)^n&lt;/math&gt;.<br /> So, &lt;math&gt;c=b*(1/4)^n&lt;/math&gt;&lt;br /&gt;<br /> Algorithm: &lt;br /&gt;<br /> 1.Draw &lt;math&gt;U_1&lt;/math&gt; from &lt;math&gt;U(0, 1)&lt;/math&gt;. &lt;math&gt; U_2&lt;/math&gt; from &lt;math&gt;U(0, 1)&lt;/math&gt; &lt;br /&gt;<br /> 2.If &lt;math&gt;U_2&lt;=b*(U_1)^n*(1-(U_1))^n/b*(1/4)^n=(4*(U_1)*(1-(U_1)))^n&lt;/math&gt;&lt;br /&gt;<br /> then X=U_1<br /> Else return to step 1.<br /> <br /> Discrete Case:<br /> Most discrete random variables do not have a closed form inverse CDF. Also, its CDF &lt;math&gt;F:X \rightarrow [0,1]&lt;/math&gt; is not necessarily onto. This means that not every point in the interval &lt;math&gt; [0,1] &lt;/math&gt; has a preimage in the support set of X through the CDF function.&lt;br /&gt;<br /> <br /> Let &lt;math&gt;X&lt;/math&gt; be a discrete random variable where &lt;math&gt;a \leq X \leq b&lt;/math&gt; and &lt;math&gt;a,b \in \mathbb{Z}&lt;/math&gt; . &lt;br&gt;<br /> To sample from &lt;math&gt;X&lt;/math&gt;, we use the partition method below: &lt;br&gt;<br /> <br /> &lt;math&gt;\, \text{Step 1: Generate u from } U \sim Unif[0,1]&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\, \text{Step 2: Set } x=a, s=P(X=a)&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;\, \text{Step 3: While } u&gt;s, x=x+1, s=s+P(X=x)&lt;/math&gt; &lt;br /&gt;<br /> &lt;math&gt;\, \text{Step 4: Return } x&lt;/math&gt;&lt;br /&gt;<br /> <br /> ==Class 8 - Thursday, May 30, 2013==<br /> <br /> In this lecture, we will discuss algorithms to generate 3 well-known distributions: Binomial, Geometric and Poisson. For each of these distributions, we will first state its general understanding, probability mass function, expectation and variance. Then, we will derive one or more algorithms to sample from each of these distributions, and implement the algorithms on Matlab. &lt;br \&gt;<br /> <br /> ===The Bernoulli distribution===<br /> <br /> The Bernoulli distribution is a special case of the binomial distribution, where n = 1. X ~ Bin(1, p) has the same meaning as X ~ Ber(p), where p is the probability of success and 1-p is the probability of failure (we usually define a variate q, q= 1-p). The mean of Bernoulli is p and the variance is p(1-p). Bin(n, p), is the distribution of the sum of n independent Bernoulli trials, Bernoulli(p), each with the same probability p, where 0&lt;p&lt;1. &lt;br&gt;<br /> For example, let X be the event that a coin toss results in a &quot;head&quot; with probability ''p'', then ''X~Bernoulli(p)''. &lt;br&gt;<br /> P(X=1)= p<br /> P(X=0)= q = 1-p<br /> Therefore, P(X=0) + P(X=1) = p + q = 1<br /> <br /> '''Algorithm: '''<br /> <br /> 1) Generate &lt;math&gt;u\sim~Unif(0,1)&lt;/math&gt; &lt;br&gt;<br /> 2) If &lt;math&gt;u \leq p&lt;/math&gt;, then &lt;math&gt;x = 1 &lt;/math&gt;&lt;br&gt;<br /> else &lt;math&gt;x = 0&lt;/math&gt; &lt;br&gt;<br /> The answer is: &lt;br&gt;<br /> when &lt;math&gt; U \leq p, x=1&lt;/math&gt; &lt;br&gt;<br /> when &lt;math&gt;U \geq p, x=0&lt;/math&gt;&lt;br&gt;<br /> 3) Repeat as necessary<br /> <br /> * '''Matlab Code'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt; p = 0.8 % an arbitrary probability for example<br /> &gt;&gt; for i = 1: 100<br /> &gt;&gt; u = rand;<br /> &gt;&gt; if u &lt; p<br /> &gt;&gt; x(ii) = 1;<br /> &gt;&gt; else<br /> &gt;&gt; x(ii) = 0;<br /> &gt;&gt; end<br /> &gt;&gt; end<br /> &gt;&gt; hist(x)<br /> &lt;/pre&gt;<br /> <br /> ===The Binomial Distribution===<br /> <br /> In general, if the random variable X follows the binomial distribution with parameters n and p, we write X ~ Bin(n, p). <br /> (Acknowledge: https://en.wikipedia.org/wiki/Binomial_distribution)<br /> If X ~ B(n, p), then its pmf is of form:<br /> <br /> f(x)=(nCx) p&lt;sup&gt;x&lt;/sup&gt;(1-p)&lt;sup&gt;(n-x)&lt;/sup&gt;, x=0,1,...n&lt;br /&gt;<br /> Or f(x) = &lt;math&gt;(n!/x!(n-x)!)&lt;/math&gt; p&lt;sup&gt;x&lt;/sup&gt;(1-p)&lt;sup&gt;(n-x)&lt;/sup&gt;, x=0,1,...n &lt;br /&gt;<br /> <br /> Mean (x) = E(x) = &lt;math&gt; np &lt;/math&gt;<br /> Variance = &lt;math&gt; np(1-p) &lt;/math&gt;&lt;br/&gt;<br /> <br /> Generate n uniform random number &lt;math&gt;U_1,...,U_n&lt;/math&gt; and let X be the number of &lt;math&gt;U_i&lt;/math&gt; that are less than or equal to p.<br /> The logic behind this algorithm is that the Binomial Distribution is simply a Bernoulli Trial, with a probability of success of p, repeated n times. Thus, we can sample from the distribution by sampling from n Bernoulli. The sum of these n bernoulli trials will represent one binomial sampling. Thus, in the below example, we are sampling 1000 realizations from 20 Bernoulli random variables. By summing up the rows of the 20 by 1000 matrix that is produced, we are summing up the 20 bernoulli outcomes to produce one binomial sampling. We have 1000 rows, which means we have realizations from 1000 binomial random variables when this sum is done (the output of the sum is a 1 by 1000 sized vector).&lt;br /&gt;<br /> To continue with the previous example, let X be the number of heads in a series of ''n'' independent coin tosses - where for each toss, the probability of coming up with a head is ''p'' - then ''X~Bin(n, p)''. &lt;br /&gt;<br /> MATLAB tips: to get a pdf f(x), we can use code binornd(N,P). N means number of trials and p is the probability of success. a=[2 3 4],if set a&lt;3, will produce a=[1 0 0]. If you set &quot;a == 3&quot;, it will produce [0 1 0]. If a=[2 6 9 10], if set a&lt;4, will produce a=[1 0 0 0], because only the first element (2) is less than 4, meanwhile the rest are greater. So we can use this to get the number which is less than p.&lt;br /&gt;<br /> <br /> Algorithm for Bernoulli is given as above<br /> <br /> '''Code'''&lt;br&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;a=[3 5 8];<br /> &gt;&gt;a&lt;5<br /> ans= 1 0 0<br /> <br /> &gt;&gt;rand(20,1000)<br /> &gt;&gt;rand(20,1000)&lt;0.4<br /> &gt;&gt;A = sum(rand(20,1000)&lt;0.4) #sum of raws ~ Bin(20 , 0.3)<br /> &gt;&gt;hist(A)<br /> &gt;&gt;mean(A)<br /> Note: 1 in the above code means sum the matrix by column<br /> <br /> &gt;&gt;sum(sum(rand(20,1000)&lt;0.4)&gt;8)/1000<br /> This is an estimate of Pr[A&gt;8].<br /> <br /> &lt;/pre&gt;<br /> <br /> [[File:Binomial_example.jpg|300px]]<br /> <br /> remark: a=[2 3 4],if set a&lt;3, will produce a=[1 0 0]. If you set &quot;a == 3&quot;, it will produce [0 1 0].<br /> using code to find some value what i want to get from the matrix. Its useful to define some matrixs.<br /> <br /> Relation between Bernoulli Distribution and Binomial Distribution:<br /> For instance, we want to find numbers ≤0.3. Uniform collects which is ≤0.3, and Binomial calculates how many numbers are there ≤0.3.<br /> <br /> ===The Geometric Distribution===<br /> Geometric distribution is a discrete distribution. There are two types geometric distributions, the first one is the probability distribution of the number of X Bernoulli fail trials, with probability 1-p, needed until the first success situation happened, X come from the set { 1, 2, 3, ...}; the other one is the probability distribution of the number Y = X − 1 of failures, with probability 1-p, before the first success, Y comes from the set { 0, 1, 2, 3, ... }.<br /> <br /> For example,&lt;br /&gt;<br /> If the success event showed at the first time, which x=1, then f(x)=p.&lt;br /&gt;<br /> If the success event showed at the second time and failed at the first time, which x = 2, then f(x)= p(1-p).&lt;br /&gt;<br /> If the success event showed at the third time and failed at the first and second time, which x = 3, then f(x)= p(1-p)&lt;sup&gt;2 &lt;/sup&gt;. etc.&lt;br /&gt;<br /> If the success event showed at the k time and all failed before time k, which implies x = k, then f(k)= p(1-p)&lt;sup&gt;(k-1)&lt;/sup&gt;&lt;br /&gt;<br /> which is,&lt;br /&gt;<br /> x Pr&lt;br /&gt;<br /> 1 P&lt;br /&gt;<br /> 2 P(1-P)&lt;br /&gt;<br /> 3 P(1-P)&lt;sup&gt;2&lt;/sup&gt;&lt;br /&gt;<br /> . .&lt;br /&gt;<br /> . .&lt;br /&gt;<br /> . .&lt;br /&gt;<br /> n P(1-P)&lt;sup&gt;(n-1)&lt;/sup&gt;&lt;br /&gt;<br /> Also, the sequence of the outputs of the probability is a geometric sequence.<br /> <br /> For example, suppose a die is thrown repeatedly until the first time a &quot;6&quot; appears. This is a question of geometric distribution of the number of times on the set { 1, 2, 3, ... } with p = 1/6.<br /> <br /> Generally speaking, if X~G(p) then its pdf is of the form f(x)=(1-p)&lt;sup&gt;(x-1)&lt;/sup&gt;*p, x=1,2,...&lt;br /&gt;<br /> The random variable X is the number of trials required until the first success in a series of independent''' Bernoulli trials'''.&lt;br /&gt;<br /> <br /> <br /> <br /> Other properties<br /> <br /> <br /> Probability mass function : P(X=k) = p(1-p)&lt;sup&gt;(k-1)&lt;/sup&gt;<br /> <br /> Tail probability : P(X&gt;n) = &lt;math&gt;(1-p)^n&lt;/math&gt;<br /> <br /> The CDF : P(X&lt;n) = 1 - &lt;math&gt;(1-p)^n&lt;/math&gt;<br /> <br /> <br /> &lt;span style=&quot;background:#F5F5DC&quot;&gt;<br /> <br /> Mean of x = 1/p<br /> Var(x) = (1-p)/p^2<br /> <br /> There are two ways to look at a geometric distribution.<br /> <br /> &lt;b&gt;1st Method&lt;/b&gt;<br /> <br /> We look at the number of trials before the first success. This includes the last trial in which you succeeded. This will be used in our course. <br /> <br /> pdf is of form f(x)=&gt;(1-p)&lt;sup&gt;(x-1)&lt;/sup&gt;*(p), x = 1, 2, 3, ...<br /> <br /> &lt;b&gt;2nd Method&lt;/b&gt;<br /> <br /> This involves modeling the failure before the first success. This does not include the last trial in which we succeeded. <br /> <br /> pdf is of form f(x)=&gt; ((1-p)^x)*p , x = 0, 1, 2, ....<br /> <br /> &lt;/span&gt;<br /> <br /> <br /> If Y~Exp(&lt;math&gt;\lambda&lt;/math&gt;) then &lt;math&gt;X=\left \lfloor Y \right \rfloor+1&lt;/math&gt; is geometric.&lt;br /&gt;<br /> Choose e^(-&lt;math&gt;\lambda&lt;/math&gt;)=1-p. Then X ~ geo (p) &lt;br /&gt;<br /> <br /> P (X &gt; x) = (1-p)&lt;sup&gt;x&lt;/sup&gt;(because first x trials are not successful) &lt;br/&gt;<br /> <br /> NB: An advantage of using this method is that nothing is rejected. We accept all the points, and the method is more efficient. Also, this method is closer to the inverse transform method as nothing is being rejected. &lt;br /&gt;<br /> <br /> '''Proof''' &lt;br/&gt;<br /> <br /> &lt;math&gt;P(X&gt;x) = P( \left \lfloor Y \right \rfloor + 1 &gt; X) = P(\left \lfloor Y \right \rfloor &gt; x- 1) = P(Y&gt;= x) = e^{-\lambda × x} &lt;/math&gt; &lt;br&gt;<br /> <br /> SInce p = 1- e&lt;sup&gt;-&lt;math&gt;\lambda&lt;/math&gt;&lt;/sup&gt; or &lt;math&gt;\lambda&lt;/math&gt;= &lt;math&gt;-log(1-p)&lt;/math&gt;(compare the pdf of exponential distribution and Geometric distribution,we can look at e&lt;sup&gt;-&lt;math&gt;\lambda&lt;/math&gt;&lt;/sup&gt; the probability of the fail trial), then &lt;br&gt;<br /> <br /> P(X&gt;x) = e&lt;sup&gt;(-&lt;math&gt;\lambda&lt;/math&gt; * x)&lt;/sup&gt; = e&lt;sup&gt;log(1-p)*x&lt;/sup&gt; = (1-p)&lt;sup&gt;x&lt;/sup&gt; &lt;br/&gt;<br /> <br /> Note that floor(Y)&gt;X -&gt; Y &gt;= X+1 (X is an integer) &lt;br/&gt;<br /> <br /> proof how to use EXP distribution to find P(X&gt;x)=(1-p)^x<br /> <br /> &lt;br&gt;<br /> Suppose X has the exponential distribution with rate parameter &lt;math&gt; \lambda &gt; 0 &lt;/math&gt; &lt;br&gt;<br /> the &lt;math&gt;\left \lfloor X \right \rfloor &lt;/math&gt; and &lt;math&gt;\left \lceil X \right \rceil &lt;/math&gt; have geometric distribution on &lt;math&gt; \mathcal{N} &lt;/math&gt; and &lt;math&gt; \mathcal{N}_{+} &lt;/math&gt; respectively each with success probability &lt;math&gt; 1-e^ {- \lambda} &lt;/math&gt; &lt;br&gt;<br /> <br /> Proof: &lt;br&gt;<br /> &lt;math&gt;\text{For } n \in \mathcal{N} &lt;/math&gt;&lt;br//&gt;<br /> <br /> &lt;math&gt;\begin{align}<br /> P(\left \lfloor X \right \rfloor = n)&amp;{}= P( n \leq X &lt; n+1) \\<br /> &amp;{}= F( n+1) - F(n) \\<br /> \text{By algebra and simplification:} \\<br /> P(\left \lfloor X \right \rfloor = n)&amp;{}= (e^ {-\lambda})^n \cdot (1 - e^ {-\lambda}) \\<br /> &amp;{}= Geo (1 - e^ {-\lambda}) \\<br /> <br /> \text{Proof of ceiling part follows immediately.} \\<br /> \end{align}&lt;/math&gt; &lt;br//&gt;<br /> <br /> <br /> <br /> <br /> <br /> '''Algorithm:''' &lt;br /&gt;<br /> 1) Let &lt;math&gt;\lambda = -\log (1-p) &lt;/math&gt;&lt;br /&gt;<br /> 2) Generate a &lt;math&gt;Y \sim Exp(\lambda )&lt;/math&gt; &lt;br /&gt;<br /> 3) We can then let &lt;math&gt;X = \left \lfloor Y \right \rfloor + 1, where X\sim Geo(p)&lt;/math&gt; &lt;br /&gt;<br /> note: &lt;math&gt;\left \lfloor Y \right \rfloor &gt;2 -&gt; Y&gt;=3&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt; \left \lfloor Y \right \rfloor &gt;5 -&gt; Y&gt;=6&lt;/math&gt;&lt;br /&gt;<br /> &lt;br /&gt;<br /> <br /> &lt;math&gt;\left \lfloor Y \right \rfloor&gt;x &lt;/math&gt; -&gt; Y&gt;= X+1 &lt;br /&gt;<br /> <br /> &lt;math&gt;P(Y&gt;=X)&lt;/math&gt;&lt;br /&gt;<br /> Y ~ Exp (&lt;math&gt;\lambda&lt;/math&gt;)&lt;br /&gt;<br /> pdf of Y : &lt;math&gt;\lambda e^{-\lambda}&lt;/math&gt;&lt;br /&gt;<br /> cdf of Y : &lt;math&gt;1- e^{-\lambda}&lt;/math&gt;&lt;br /&gt;<br /> cdf &lt;math&gt;P(Y&lt;x)=1-e^{-\lambda x}&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;P(Y&gt;=x)=1-(1- e^{-\lambda x})=e^{-\lambda x}&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt; e^{-\lambda}=1-p -&gt; -log(1-p)=\lambda&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;P(Y&gt;=x)=e^{-\lambda x}=e^{log(1-p)x}=(1-p)^x&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;E[x]=1/P &lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;Var= (1-P)/(P^2)&lt;/math&gt;&lt;br /&gt;<br /> P(X&gt;x)&lt;br /&gt;<br /> =P(floor(y)+1&gt;x)&lt;br /&gt;<br /> =P(floor(y)&gt;x-1)&lt;br /&gt;<br /> =P(y&gt;=x)<br /> <br /> use &lt;math&gt;e^{-\lambda}=1-p&lt;/math&gt; to figure out the mean and variance.<br /> '''Code'''&lt;br&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt;p=0.4;<br /> &gt;&gt;l=-log(1-p);<br /> &gt;&gt;u=rand(1,1000);<br /> &gt;&gt;y=(-1/l)*log(u);<br /> &gt;&gt;x=floor(y)+1;<br /> &gt;&gt;hist(x)<br /> <br /> ===Note:===<br /> mean(x)~E[X]=&gt; 1/p<br /> Var(x)~V[X]=&gt; (1-p)/p^2<br /> <br /> A specific Example:<br /> Consider x=5<br /> &gt;&gt; sum(x==5)/1000 -&gt; chance that will succeed at fifth trial;<br /> &gt;&gt; ans = <br /> 0.0780<br /> &gt;&gt; sum(x&gt;10)/1000 -&gt; chance that will succeed after 10 trials<br /> &gt;&gt; ans = <br /> 0.0320<br /> <br /> &lt;/pre&gt;<br /> <br /> Note that the above mean is the average amount of times you should try until you get a successful case.&lt;br/&gt;<br /> <br /> [[File:Geometric_example.jpg|300px]]<br /> <br /> &lt;span style=&quot;background:#F5F5DC&quot;&gt;<br /> EXAMPLE for geometric distribution: Consider the case of rolling a die: &lt;/span&gt;<br /> <br /> X=the number of rolls that it takes for the number 5 to appear. <br /> <br /> We have X ~Geo(1/6), &lt;math&gt;f(x)=(1/6)*(5/6)^{x-1}&lt;/math&gt;, x=1,2,3.... <br /> <br /> Now, let &lt;math&gt;\left \lfloor Y \right \rfloor=e^{\lambda}&lt;/math&gt; =&gt; x=floor(Y) +1 <br /> <br /> Let &lt;math&gt;e^{-\lambda}=5/6&lt;/math&gt; <br /> <br /> &lt;math&gt;P(X&gt;x) = P(Y&gt;=x)&lt;/math&gt; (from the class notes) <br /> <br /> We have &lt;math&gt;e^{-\lambda *x} = (5/6)^x&lt;/math&gt; <br /> <br /> Algorithm: let &lt;math&gt;\lambda = -\log(5/6)&lt;/math&gt; <br /> <br /> 1) Let Y be &lt;math&gt;e^{\lambda}&lt;/math&gt;, exponentially distributed <br /> <br /> 2) Set &lt;math&gt;X= \left \lfloor Y \right \rfloor +1 &lt;/math&gt;, to generate X <br /> <br /> &lt;math&gt; E[x]=6, Var[X]=5/6 /(1/6^2) = 30 &lt;/math&gt;<br /> <br /> <br /> &lt;span style=&quot;background:#F5F5DC&quot;&gt;GENERATING NEGATIVE BINOMIAL RV USING GEOMETRIC RV'S&lt;/span&gt;<br /> <br /> Property of negative binomial Random Variable: &lt;br/&gt;<br /> <br /> The negative binomial random variable is a sum of r independent geometric random variables.&lt;br/&gt;<br /> <br /> Using this property we can formulate the following algorithm:&lt;br/&gt;<br /> <br /> Step 1: Generate r geometric rv's each with probability p using the procedure presented above.&lt;br/&gt;<br /> Step 2: Take the sum of these r geometric rv's. This RV follows NB(r,p)&lt;br/&gt;<br /> <br /> remark the step 1 and step 2. Looking for the floor Y, and e^(-mu)=1-p=5/6, and then generate x.<br /> <br /> ===Poisson Distribution===<br /> If &lt;math&gt;\displaystyle X \sim \text{Poi}(\lambda)&lt;/math&gt;, its pdf is of the form &lt;math&gt;\displaystyle \, f(x) = \frac{e^{-\lambda}\lambda^x}{x!}&lt;/math&gt; , where &lt;math&gt;\displaystyle \lambda &lt;/math&gt; is the rate parameter.&lt;br /&gt;<br /> <br /> definition:In probability theory and statistics, the Poisson distribution (pronounced [pwasɔ̃]) is a discrete probability distribution that expresses the probability of a given number of events occurring in a fixed interval of time and/or space if these events occur with a known average rate and independently of the time since the last event. The Poisson distribution can also be used for the number of events in other specified intervals such as distance, area or volume.<br /> For instance, suppose someone typically gets 4 pieces of mail per day on average. There will be, however, a certain spread: sometimes a little more, sometimes a little less, once in a while nothing at all. Given only the average rate, for a certain period of observation (pieces of mail per day, phonecalls per hour, etc.), and assuming that the process, or mix of processes, that produces the event flow is essentially random, the Poisson distribution specifies how likely it is that the count will be 3, or 5, or 10, or any other number, during one period of observation. That is, it predicts the degree of spread around a known average rate of occurrence.<br /> The Derivation of the Poisson distribution section shows the relation with a formal definition.(from Wikipedia)<br /> <br /> Understanding of Poisson distribution:<br /> <br /> If customers '''independently''' come to bank over time, all following exponential distributions with rate &lt;math&gt;\lambda&lt;/math&gt; per unit of time, then <br /> X(t) = # of customer in [0,t] ~ Poi&lt;math&gt;(\lambda t)&lt;/math&gt;<br /> <br /> Its mean and variance are&lt;br /&gt;<br /> &lt;math&gt;\displaystyle E[X]=\lambda&lt;/math&gt;&lt;br /&gt;<br /> &lt;math&gt;\displaystyle Var[X]=\lambda&lt;/math&gt;&lt;br /&gt;<br /> An useful property: If &lt;math&gt;X_i \sim \mathrm{Pois}(\lambda_i)\, i=1,\dots,n&lt;/math&gt; are independent and &lt;math&gt;\lambda=\sum_{i=1}^n \lambda_i&lt;/math&gt;, then &lt;math&gt;Y = \left( \sum_{i=1}^n X_i \right) \sim \mathrm{Pois}(\lambda)&lt;/math&gt;<br /> <br /> A Poisson random variable X can be interpreted as the maximal number of i.i.d. (Independent and Identically Distributed) exponential variables(with parameter &lt;math&gt;\lambda&lt;/math&gt;) whose sum does not exceed 1.&lt;br /&gt;<br /> The traditional understanding of the Poisson distribution as the total number of events in a specific interval can be understood here since the above definition simply describes the Poisson as the sum of waiting times for n events in an interval of length 1.<br /> &lt;br /&gt;<br /> &lt;br /&gt;<br /> &lt;math&gt;\displaystyle\text{Let } Y_j \sim \text{Exp}(\lambda), U_j \sim \text{Unif}(0,1)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;Y_j = -\frac{1}{\lambda}\log(U_j) \text{ from Inverse Transform Method}&lt;/math&gt;&lt;br&gt;&lt;br&gt;<br /> <br /> &lt;math&gt;\begin{align} <br /> X &amp;= \max \{ n: \sum_{j=1}^{n} Y_j \leq 1 \} \\<br /> &amp;= \max \{ n: \sum_{j=1}^{n} - \frac{1}{\lambda}\log(U_j) \leq 1 \} \\<br /> &amp;= \max \{ n: \sum_{j=1}^{n} \log(U_j) &gt;= -\lambda \} \\<br /> &amp;= \max \{ n: \log(\prod_{j=1}^{n} U_j) &gt;= -\lambda \} \\<br /> &amp;= \max \{ n: \prod_{j=1}^{n} U_j &gt;= e^{-\lambda} \} \\<br /> &amp;= \min \{ n: \prod_{j=1}^{n} U_j &gt;= e^{-\lambda} \} - 1 \\<br /> \end{align}&lt;/math&gt;&lt;br&gt;&lt;br /&gt;<br /> <br /> Note: From above, we can use Logarithm Rules &lt;math&gt;\log(a)+\log(b)=\log(ab)&lt;/math&gt; to generate the result.&lt;br&gt;&lt;br /&gt;<br /> '''Algorithm:''' &lt;br /&gt;<br /> 1) Set n=1, a=1 &lt;br /&gt;<br /> 2) Generate &lt;math&gt;U_n \sim U(0,1), a=aU_n &lt;/math&gt; &lt;br /&gt;<br /> 3) If &lt;math&gt;a &gt;= e^{-\lambda}&lt;/math&gt; , then n=n+1, and go to Step 2. Else, x=n-1 &lt;br /&gt;<br /> <br /> using inverse-method to proof mean and variance of poisson distribution.<br /> <br /> ===MATLAB Code for generating Poisson Distribution===<br /> &lt;pre style='font-size:16px'&gt;<br /> &gt;&gt;l=2; N=1000 <br /> &gt;&gt;for ii=1:N<br /> n=1;<br /> a=1;<br /> u=rand;<br /> a=a*u;<br /> while a&gt;exp(-l)<br /> n=n+1;<br /> u=rand;<br /> a=a*u;<br /> end<br /> x(ii)=n-1;<br /> end<br /> &gt;&gt;hist(x)<br /> &gt;&gt;Sum(x==1)/N # Probability of x=1<br /> &gt;&gt;Sum(x&gt;3)/N # Probability of x &gt; 3<br /> &lt;/pre&gt;<br /> <br /> [[File:Poisson_example.jpg|300px]]<br /> <br /> === Another way to generate random variable from poisson distribution ===<br /> &lt;br/&gt;<br /> Note: &lt;math&gt;P(X=x)=\frac {e^{-\lambda}\lambda^x}{x!}, \forall x \in \N&lt;/math&gt;&lt;br/&gt;<br /> Let &lt;math&gt;\displaystyle p(x) = P(X=x)&lt;/math&gt; denote the pmf of &lt;math&gt;\displaystyle X&lt;/math&gt;.&lt;br/&gt;<br /> Then ratio is &lt;math&gt;\frac{p(x+1)}{p(x)}=\frac{\lambda}{x+1}, \forall x \in \N&lt;/math&gt; &lt;br/&gt;<br /> Therefore, &lt;math&gt;p(x+1)=\frac{\lambda}{x+1}p(x)&lt;/math&gt; &lt;br/&gt;<br /> Algorithm: &lt;br/&gt;<br /> 1. Set &lt;math&gt;\displaystyle x=0&lt;/math&gt;&lt;br/&gt;<br /> 2. Set &lt;math&gt;\displaystyle F=p=e^{-\lambda}&lt;/math&gt; &lt;br/&gt;<br /> 3. Generate &lt;math&gt;\displaystyle U \sim~ \text{Unif}(0,1)&lt;/math&gt; &lt;br/&gt;<br /> 4. If &lt;math&gt;\displaystyle U&lt;F&lt;/math&gt;, output &lt;math&gt;\displaystyle x&lt;/math&gt;&lt;br/&gt;<br /> Else&lt;br/&gt;<br /> &lt;math&gt;\displaystyle p=\frac{\lambda}{x+1} p&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;\displaystyle F=F+p&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;\displaystyle x = x+1&lt;/math&gt;&lt;br/&gt;<br /> Go to 4.&lt;br/&gt;<br /> <br /> This is indeed the inverse-transform method, with a clever way to calculate the CDF on the fly.<br /> <br /> u=rand(0.1000)<br /> hist(x)<br /> <br /> == Class 9 - Tuesday, June 4, 2013 ==<br /> <br /> === Beta Distribution ===<br /> The beta distribution is a continuous probability distribution. &lt;br&gt;<br /> PDF:&lt;math&gt;\displaystyle \text{ } f(x) = \frac{\Gamma(\alpha+\beta)}{\Gamma(\alpha)\Gamma(\beta)}x^{\alpha-1}(1-x)^{\beta-1} &lt;/math&gt;&lt;br&gt; where &lt;math&gt;0 \leq x \leq 1&lt;/math&gt; and &lt;math&gt;\alpha&lt;/math&gt;&gt;0, &lt;math&gt;\beta&lt;/math&gt;&gt;0&lt;br/&gt;<br /> &lt;div style = &quot;align:left; background:#F5F5DC; font-size: 120%&quot;&gt;<br /> Definition:<br /> In probability theory and statistics, the beta distribution is a family of continuous probability distributions defined on the interval [0, 1] parametrized by two positive shape parameters, denoted by α and β, that appear as exponents of the random variable and control the shape of the distribution.&lt;br/.&gt;<br /> More can be find in the link: &lt;ref&gt;http://en.wikipedia.org/wiki/Beta_distribution&lt;/ref&gt;<br /> &lt;/div&gt;<br /> <br /> There are two positive shape parameters in this distribution defined as alpha and beta: &lt;br&gt;<br /> -Both parameters are greater than 0, and X is within the interval [0,1]. &lt;br&gt;<br /> -Alpha is used as exponents of the random variable. &lt;br&gt;<br /> -Beta is used to control the shape of the this distribution. We use the beta distribution to build the model of the behavior of random variables, which are limited to intervals of finite length. &lt;br&gt;<br /> -For example, we can use the beta distribution to analyze the time allocation of sunshine data and variability of soil properties. &lt;br&gt;<br /> <br /> If X~Beta(&lt;math&gt;\alpha, \beta&lt;/math&gt;) then its p.d.f. is of the form<br /> <br /> :&lt;math&gt;\displaystyle \text{ } f(x) = \frac{\Gamma(\alpha+\beta)}{\Gamma(\alpha)\Gamma(\beta)}x^{\alpha-1}(1-x)^{\beta-1} &lt;/math&gt; where &lt;math&gt;0 \leq x \leq 1&lt;/math&gt; and &lt;math&gt;\alpha&lt;/math&gt;&gt;0, &lt;math&gt;\beta&lt;/math&gt;&gt;0&lt;br&gt;<br /> and<br /> &lt;math&gt;f(x;\alpha,\beta)= 0 &lt;/math&gt; otherwise<br /> Note: &lt;math&gt;\Gamma(\alpha)=(\alpha-1)! &lt;/math&gt; if &lt;math&gt;\alpha&lt;/math&gt; is a positive integer.<br /> <br /> Note: Gamma Function Properties<br /> <br /> If &lt;math&gt;\alpha=\frac{1}{2} , <br /> <br /> \Gamma(\frac {1}{2})=\sqrt\pi &lt;/math&gt; <br /> <br /> The mean of the beta distribution is &lt;math&gt;\frac{\alpha}{\alpha + \beta}&lt;/math&gt;. The variance is &lt;math&gt;\frac{\alpha\beta}{(\alpha+\beta)^2 (\alpha + \beta + 1)}&lt;/math&gt;<br /> The variance of the beta distribution decreases monotonically if &lt;math&gt; \alpha = \beta &lt;/math&gt; and as &lt;math&gt; \alpha = \beta &lt;/math&gt; increases, the variance decreases. <br /> <br /> The formula for the cumulative distribution function of the beta distribution is also called the incomplete beta function ratio (commonly denoted by Ix) and is defined as F(x) = I(x)(p,q) <br /> <br /> To generate random variables of a Beta distribution, there are multiple cases depending on the value of &lt;math&gt;\alpha &lt;/math&gt; and &lt;math&gt; \beta &lt;/math&gt;:<br /> <br /> '''Case 1:''' If &lt;math&gt;\alpha=1&lt;/math&gt; and &lt;math&gt;\beta=1&lt;/math&gt;<br /> <br /> :&lt;math&gt;\displaystyle \text{Beta}(1,1) = \frac{\Gamma(1+1)}{\Gamma(1)\Gamma(1)}x^{1-1}(1-x)^{1-1}&lt;/math&gt;&lt;br&gt;<br /> :&lt;math&gt; = \frac{1!}{0!0!}x^{0}(1-x)^{0}&lt;/math&gt;&lt;br&gt;<br /> :&lt;math&gt; = 1 &lt;/math&gt;&lt;br&gt;<br /> <br /> Note: 0! = 1. &lt;br&gt;<br /> Hence, the distribution is:&lt;br&gt;<br /> :&lt;math&gt;\displaystyle \text{Beta}(1,1) = U (0, 1) &lt;/math&gt;&lt;br&gt;<br /> If the Question asks for sampling Beta Distribution, we can sample from Uniform Distribution which we already know how to sample from&lt;br&gt;<br /> Algorithm:&lt;br&gt;<br /> Generate U~Unif(0,1)&lt;br&gt;<br /> <br /> '''Case 2:''' Either &lt;math&gt;\alpha=1&lt;/math&gt; or &lt;math&gt;\beta=1&lt;/math&gt;<br /> <br /> <br /> e.g. &lt;math&gt;\alpha=1&lt;/math&gt;<br /> We don't make any assumption about &lt;math&gt;\beta&lt;/math&gt; except that it is a positive integer. &lt;br\&gt;<br /> <br /> :&lt;math&gt;\displaystyle \text{f}(x) = \frac{\Gamma(1+\beta)}{\Gamma(1)\Gamma(\beta)}x^{1-1}(1-x)^{\beta-1}=\beta(1-x)^{\beta-1}&lt;/math&gt;&lt;br&gt;<br /> <br /> :&lt;math&gt;\beta=1 &lt;/math&gt;<br /> <br /> :&lt;math&gt;\displaystyle \text{f}(x) = \frac{\Gamma(\alpha+1)}{\Gamma(\alpha)\Gamma(1)}x^{\alpha-1}(1-x)^{1-1}=\alpha x^{\alpha-1}&lt;/math&gt;&lt;br&gt;<br /> <br /> By integrating &lt;math&gt;f(x)&lt;/math&gt;, we find the CDF of X is &lt;math&gt;F(x) = x^{\alpha}&lt;/math&gt;.<br /> As &lt;math&gt;F(x)^{-1} = x^\frac {1}{\alpha}&lt;/math&gt;, using the inverse transform method, &lt;math&gt; X = U^\frac {1}{\alpha} &lt;/math&gt; with U ~ U[0,1].<br /> <br /> '''Algorithm'''<br /> <br /> :1. Generate U~Unif(0,1)&lt;br\&gt;<br /> :2. Assign &lt;math&gt;x = u^\frac {1}{\alpha}&lt;/math&gt;<br /> <br /> After we have simplified this example, we can use other distribution methods to solve the problem.<br /> <br /> '''MATLAB Code to generate random n variables using the above algorithm'''<br /> &lt;pre style='font-size:16px'&gt;<br /> <br /> x = rand(1,n).^(1/alpha) <br /> &lt;/pre&gt;<br /> <br /> '''Case 3:'''&lt;br\&gt; To sample from beta in general, we use the property that &lt;br\&gt;<br /> <br /> :if &lt;math&gt;Y_1&lt;/math&gt; follows gamma &lt;math&gt;(\alpha,1)&lt;/math&gt;&lt;br\&gt;<br /> : &lt;math&gt;Y_2&lt;/math&gt; follows gamma &lt;math&gt;(\beta,1)&lt;/math&gt;&lt;br\&gt;<br /> <br /> '''Note:<br /> 1. &lt;math&gt;\alpha&lt;/math&gt; and &lt;math&gt;\beta&lt;/math&gt; are shape parameters here and 1 is the scale parameter.&lt;br\&gt;'''<br /> :then &lt;math&gt;Y=\frac {Y_1}{Y_1+Y_2}&lt;/math&gt; follows Beta &lt;math&gt;(\alpha,\beta)&lt;/math&gt;&lt;br\&gt;<br /> 2.Exponential: &lt;math&gt;-\frac{1}{\lambda} \log(u)&lt;/math&gt; &lt;br\&gt;<br /> 3.Gamma: &lt;math&gt;-\frac{1}{\lambda} \log(u_1 * \cdots * u_t)&lt;/math&gt;&lt;br\&gt;<br /> <br /> '''Algorithm'''&lt;br\&gt;<br /> *1. Sample from Y1 ~ Gamma (&lt;math&gt;\alpha&lt;/math&gt;,1) &lt;math&gt;\alpha&lt;/math&gt; is the shape, and 1 is the scale. &lt;br\&gt;<br /> *2. Sample from Y2 ~ Gamma (&lt;math&gt;\beta&lt;/math&gt;,1) &lt;br\&gt;<br /> *3. Set <br /> :&lt;math&gt; Y = \frac{Y_1}{Y_1+Y_2}&lt;/math&gt;&lt;br&gt;<br /> <br /> Please see the following example for Matlab code. &lt;br&gt; <br /> <br /> <br /> '''Case 4:'''&lt;br\&gt; Use The Acceptance-Rejection Method &lt;br\&gt;<br /> The beta density is&lt;br /&gt;<br /> &lt;math&gt;\displaystyle \text{Beta}(\alpha,\beta) = \frac{\Gamma(\alpha+\beta)}{\Gamma(\alpha)\Gamma(\beta)}x^{\alpha-1}(1-x)^{\beta-1} &lt;/math&gt; where &lt;math&gt;0 \leq x \leq 1&lt;/math&gt;&lt;br&gt;<br /> Assume &lt;math&gt;\alpha,\beta \geq 1&lt;/math&gt;. Then &lt;math&gt;\displaystyle f(x)&lt;/math&gt; has the maximum at &lt;math&gt;\frac{\alpha-1}{\alpha+\beta-2}&lt;/math&gt;.&lt;br /&gt;<br /> (Please note that we could find the maximum by taking the derivative of f(x), let f'(x)=0 and then use maximum likelihood estimate to find what the maximum value is)&lt;br&gt;<br /> Define&lt;br /&gt;<br /> &lt;math&gt; c=f(\frac{\alpha-1}{\alpha+\beta-2}&lt;/math&gt;) and choose &lt;math&gt;\displaystyle g(x)=1&lt;/math&gt;.&lt;br /&gt;<br /> The A-R method becomes&lt;br /&gt;<br /> 1.Generate independent &lt;math&gt;\displaystyle U_1&lt;/math&gt; and &lt;math&gt;\displaystyle U_2&lt;/math&gt; from &lt;math&gt;\displaystyle UNIF[0,1]&lt;/math&gt; until &lt;math&gt;\displaystyle cU_2 \leq f(U_1)&lt;/math&gt;;&lt;br /&gt;<br /> 2.Return &lt;math&gt;\displaystyle U_1&lt;/math&gt;.&lt;br /&gt;<br /> '''MATLAB Code for generating Beta Distribution'''<br /> &lt;pre style='font-size:16px'&gt;<br /> &gt;&gt;Y1 = sum(-log(rand(10,1000))) #Gamma(10,1), sum 10 exponentials for each of the 1000 samples<br /> <br /> &gt;&gt;Y2 = sum(-log(rand(5,1000))) #Gamma(5,1), sum 5 exponentials for each of the 1000 samples<br /> <br /> %NOTE: here, lamda is 1, since the scale parameter for Y1 &amp; Y2 are both 1<br /> <br /> &gt;&gt;Y=Y1./(Y1+Y2) #Don't forget to divide elements using &quot;.&quot; Where Y follows Beta(10,5)<br /> <br /> &gt;&gt;figure <br /> <br /> &gt;&gt;hist(Y1) #Gamma curve<br /> <br /> &gt;&gt;figure<br /> <br /> &gt;&gt;hist(Y2) #Gamma curve<br /> <br /> &gt;&gt;figure<br /> <br /> &gt;&gt;hist(Y) #Do this to check that the shape fits beta. ~Beta(10,5).<br /> <br /> &gt;&gt;disttool #Check the beta plot.<br /> <br /> &lt;/pre&gt;<br /> This is the histogram of Y, precisely simulated version of Beta (10,5)<br /> <br /> <br /> [[File:Beta(10,5)_Simulated.jpg|300px]]<br /> <br /> This is the pdf of various beta distributions<br /> <br /> [[File:325px-Beta_distribution_pdf.png|300px]]<br /> <br /> [[File:untitled.jpg|300px]]&lt;br /&gt;<br /> MATLAB tips: rand(10,1000) produces one 10*1000 matrix and sum(rand(10,1000)) produces a 10*1000 matrix<br /> and each element in the matrix follows CDF of uniform distribution.<br /> <br /> Example for the code to explain the beta distribution.<br /> <br /> <br /> '''Another MATLAB Code for generating Beta Distribution using AR method'''<br /> &lt;pre style='font-size:16px'&gt;<br /> &gt;&gt;alpha = 3<br /> &gt;&gt;beta = 2<br /> &gt;&gt; a = sum (-log(rand(alpha,1000)))<br /> &gt;&gt; b = sum (-log(rand(beta,1000)))<br /> &gt;&gt; aandb=sum(-log(rand(alpha+beta,1000)))<br /> &gt;&gt; t = (alpha - 1)/(alpha + beta -2)<br /> &gt;&gt; c = (andb/(a*b))*t^(alpha-1)*(1-t)^(beta-1)<br /> &gt;&gt; u1 = rand<br /> &gt;&gt; u2 = rand<br /> &gt;&gt; x = (andb/(a*b))*u1^(alpha-1)*(1-u1)^(beta-1)<br /> &gt;&gt; while c*u2&gt;x<br /> &gt;&gt; u1 = rand<br /> &gt;&gt; u2 = rand<br /> &gt;&gt; x = (andb/(a*b))*u1^(alpha-1)*(1-u1)^(beta-1)<br /> &gt;&gt; end<br /> &gt;&gt; u1<br /> &lt;/pre&gt;<br /> <br /> === Random Vector Generation ===<br /> We want to sample from &lt;math&gt;X = (X_1, X_2, &lt;/math&gt;…,&lt;math&gt; X_d)&lt;/math&gt;, a d-dimensional vector from a known pdf &lt;math&gt;f(x)&lt;/math&gt; and cdf &lt;math&gt;F(x)&lt;/math&gt;.<br /> We need to take into account the following two cases: <br /> <br /> ====Case 1====<br /> if the &lt;math&gt;x_1, x_2 \cdots, x_d&lt;/math&gt;'s are independent, then&lt;br/&gt;<br /> &lt;math&gt;f(x) = f(x_1,\cdots, x_d) = f(x_1)\cdots f(x_d)&lt;/math&gt;&lt;br/&gt;<br /> we can sample from each component &lt;math&gt;x_1, x_2,\cdots, x_d&lt;/math&gt; individually, and then form a vector.&lt;br/&gt;<br /> <br /> based on the property of independence, we can derive the pdf or pmf of &lt;math&gt;x=x_1,x_2,x_3,x_4,x_5,\cdots&lt;/math&gt;<br /> <br /> ====Case 2====<br /> If &lt;math&gt;X_1, X_2, \cdots , X_d&lt;/math&gt; are not independent&lt;br/&gt;<br /> &lt;math&gt;f(x) = f(x_1, \cdots , x_d) = f(x_1) f(x_2|x_1) \cdots f(x_d|x_{d-1},\cdots ,x_1)&lt;/math&gt;&lt;br/&gt;<br /> we need to know the conditional distributions of &lt;math&gt;f(x_2|x_1), f(x_3|x_2, x_1),\cdots, f(x_d|x_{d-1}, \cdots, x_1)&lt;/math&gt;&lt;br/&gt;<br /> This is generally a hard problem. Conditional probabilities are not easy to compute, then sampling from these would be based on your statistics knowledge.<br /> In each case, we have to consider the previous cases.<br /> &lt;math&gt;f(x_1)&lt;/math&gt; is one-dimensional, some as &lt;math&gt;f(x_2|x_1)&lt;/math&gt; and all others.<br /> In general, one could consider the covariance matrix &lt;math&gt; C &lt;/math&gt; of random variables &lt;math&gt; X_1&lt;/math&gt;,…,&lt;math&gt;X_d &lt;/math&gt;. &lt;br&gt;<br /> Suppose we now have the Cholesky factor &lt;math&gt; G&lt;/math&gt; of &lt;math&gt; C &lt;/math&gt; (i.e. &lt;math&gt; C = GG^T &lt;/math&gt;). In matlab, we use Chol(C) &lt;br&gt; <br /> For any d-tuple &lt;math&gt; X := (X_1 ,\ldots , X_d) &lt;/math&gt; (i.e random variable generated by &lt;math&gt; X_1,\ldots , X_d &lt;/math&gt; respectively)<br /> &lt;math&gt; GX &lt;/math&gt; would yield the desired distribution. &lt;br/&gt;<br /> <br /> '''Note''' (Product Rule)&lt;br/&gt;<br /> 1.) All cases can use this (independent or dependent): &lt;math&gt;f(x) = f(x_1, x_2)= f(x_1) f(x_2|x_1)&lt;/math&gt; &lt;br/&gt;<br /> 2.) If we determine that &lt;math&gt;x_1&lt;/math&gt; and &lt;math&gt; x_2&lt;/math&gt; are ''independent'', then we can use &lt;math&gt;f(x) = f(x_1, x_2)= f(x_1)f(x_2)&lt;/math&gt; &lt;br/&gt;<br /> *ie. If late for class=&lt;math&gt;x_1&lt;/math&gt; and sick=&lt;math&gt;x_2&lt;/math&gt;, then these are dependent variables so can only use equation 1 (&lt;math&gt;f(x) = f(x_1, x_2)= f(x_1) f(x_2|x_1)&lt;/math&gt;)&lt;br/&gt;<br /> *ie. If late for class=&lt;math&gt;x_1&lt;/math&gt; and milk is white=&lt;math&gt;x_2&lt;/math&gt;, then these are independent variables so can use both equations 1 and 2. &lt;br/&gt;<br /> <br /> the case show the formula of the X = (X1,X2,…,Xd), a d-dimensional vector, when they are not independent of each x. we use conditional function to define the probability function of x with d-dimensional.<br /> <br /> ====Example====<br /> Generate uniform random vectors<br /> <br /> 1) x = (x&lt;sub&gt;1&lt;/sub&gt;, …, x&lt;sub&gt;d&lt;/sub&gt;) from the d-dimensional rectangle &lt;br/&gt;<br /> 2) D = { (x&lt;sub&gt;1&lt;/sub&gt;, …, x&lt;sub&gt;d&lt;/sub&gt;) : a&lt;sub&gt;i&lt;/sub&gt; &lt;= x&lt;sub&gt;i&lt;/sub&gt; &lt;= b&lt;sub&gt;i&lt;/sub&gt; , i = 1, …, d} &lt;br/&gt;<br /> <br /> Algorithm: &lt;br/&gt;<br /> 1) For i = 1 to d &lt;br/&gt;<br /> 2) U&lt;sub&gt;i&lt;/sub&gt; ~ U(0,1) &lt;br/&gt;<br /> 3) x&lt;sub&gt;i&lt;/sub&gt; = a&lt;sub&gt;i&lt;/sub&gt; + U(b&lt;sub&gt;i&lt;/sub&gt;-a&lt;sub&gt;i&lt;/sub&gt;) &lt;br/&gt;<br /> 4) End &lt;br/&gt;<br /> <br /> *Note: x&lt;sub&gt;i&lt;/sub&gt; = a&lt;sub&gt;i&lt;/sub&gt; + U(b&lt;sub&gt;i&lt;/sub&gt;-a&lt;sub&gt;i&lt;/sub&gt;) denotes X&lt;sub&gt;i&lt;/sub&gt; ~U(a&lt;sub&gt;i&lt;/sub&gt;,b&lt;sub&gt;i&lt;/sub&gt;) &lt;br/&gt;<br /> <br /> An example of the 2-D case is given below:<br /> <br /> &lt;pre style='font-size:14px'&gt;<br /> &gt;&gt;a=[1 2]; <br /> &gt;&gt;b=[4 6]; <br /> &gt;&gt;for i=1:2<br /> u(i) = rand(); <br /> x(i) = a(i) + (b(i) - a(i))*u(i);<br /> end<br /> <br /> &gt;&gt;hold on =&gt; this is to retain current graph when adding new graphs<br /> &gt;&gt;rectangle('Position',[1 2 3 4]) =&gt; draw the boundary of the rectangle<br /> &gt;&gt;axis([0 10 0 10]) =&gt; change the size of axes<br /> &gt;&gt;plot(x(1),x(2),'.')<br /> <br /> &lt;/pre&gt;<br /> [[File:2d_ex.jpg|300px]]<br /> <br /> ==== Matlab Code: ====<br /> <br /> &lt;pre style='font-size:14px'&gt;<br /> function x = urectangle (d,n,a,b)<br /> for ii = 1:d;<br /> u(ii,:) = rand(1,n);<br /> x(ii,:) = a+ u(ii,:)*(b-a);<br /> %keyboard #makes the function stop at this step so you can evaluate the variables<br /> end<br /> <br /> &gt;&gt;x=urectangle(2, 100, 2, 5);<br /> &gt;&gt;scatter(x(1,:),x(2,:))<br /> <br /> &gt;&gt;x=urectangle(2, 10000, 2, 5); #generate 10000 numbers (instead of 100)<br /> &gt;&gt;x=urectangle(3, 10000, 2, 5); #changed to 3-dimensional<br /> &gt;&gt;scatter3(x(1,:), x(2,:), x(3,:))<br /> &gt;&gt;axis square<br /> &lt;/pre&gt;<br /> [[File:Urectangle_2d.jpg|300px]][[File:Urectangle_3d.jpg|300px]]<br /> <br /> === Vector Acceptance-Rejection Method ===<br /> <br /> The acceptance-rejection method can be extended to n-dimensional cases, with the same concept:<br /> <br /> If a random vector is to be generated uniformly from G, an irregular shape in the nth dimension, and W is a regular shape arbitrarily close to G in the nth dimension, then acceptance-rejection method can be applied as follows:<br /> <br /> 1. Sample from the regular shape W<br /> <br /> 2. Accept sample points if they are inside G<br /> <br /> &lt;br&gt;'''Example:''' &lt;br&gt; <br /> Generate a random vector Z that is uniformly distributed over region G<br /> <br /> G: d-dimensional unit ball, &lt;math&gt;G = \big\{{x: \sum_{i}{x_i}^2 \leq 1}\big\}&lt;/math&gt;<br /> <br /> w: d-dimensional hypercube, &lt;math&gt;W = \big\{{-1 \leq x_i \leq 1}\big\}_{i=1}^d&lt;/math&gt;<br /> <br /> '''Procedure:'''&lt;br /&gt;<br /> Step 1: &lt;math&gt;U_1 \sim~ U(0,1),\cdots, U_d \sim~ U(0,1)&lt;/math&gt;&lt;br /&gt;<br /> Step 2: &lt;math&gt;X_1 = 1 - 2U_1, \cdots, X_d = 1 - 2U_d, R = \sum_i X_i^2&lt;/math&gt;&lt;br /&gt;<br /> Step 3: If &lt;math&gt;R \leq 1, Z=(X_1, ..... , X_d)&lt;/math&gt;&lt;br /&gt;<br /> Else go to step 1<br /> <br /> it is an example of the vector A/R, regular shape is W likes the proposal distribution g(x), G is the target distribution g(x) &lt;br\&gt;<br /> <br /> Suppose we sampled from the target area W uniformly, let Aw, Ag indicate the area of W and G, g(x)=1/Aw and f(x)=1/Ag<br /> <br /> <br /> The following is a picture relating to the example<br /> <br /> [[File:Untitled.jpg]]<br /> <br /> Matlab code:<br /> &lt;pre style='font-size:16px'&gt;<br /> u = rand(d,n);<br /> z = 1- 2 *u;<br /> R = sum(z.^2);<br /> jj=1;<br /> <br /> for ii=1:n<br /> <br /> if R(ii)&lt;=1<br /> <br /> x(:,jj)=z(:,ii);<br /> jj=jj+1;<br /> <br /> end<br /> <br /> end<br /> <br /> output = x;<br /> <br /> end<br /> &lt;/pre&gt;<br /> <br /> ==Class 10 - Thursday June 6th 2013 == <br /> MATLAB code for using Acceptance/Rejection Method to sample from a d-dimensional unit ball.<br /> G: d-dimensional unit ball G<br /> W: d-dimensional Hypercube<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> 1) U1~UNIF(0,1)<br /> U2~UNIF(0,1)<br /> ...<br /> Ud~UNIF(0,1)<br /> 2) X1 = 1-2U1<br /> X2 = 1-2U2<br /> ...<br /> Xd = 1-2Ud<br /> R = sum(Xi^2)<br /> 3) If R&lt;=1<br /> X = (X1,X2,...,Xd),<br /> else go to step 1<br /> &lt;/pre&gt;<br /> <br /> ==== Code: ====<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> function output = Unitball(d,n) <br /> <br /> u = rand(d,n);<br /> z = 1- 2 *u;<br /> R = sum(z.^2);<br /> jj=1;<br /> <br /> for ii=1:n<br /> <br /> if R(ii)&lt;=1<br /> <br /> x(:,jj)=z(:,ii);<br /> jj=jj+1;<br /> <br /> end<br /> <br /> end<br /> <br /> output = x;<br /> <br /> end<br /> <br /> &gt;&gt; data = Unitball(d, n)<br /> &gt;&gt; scatter(data(1,:), data(2,:)) %plot 2d graph<br /> <br /> R(ii) computes the sum of the square of each element of a vector, so if it is less than 1,<br /> then the vector is in the unit ball.<br /> <br /> x(:,jj) means all the numbers in the jj column.<br /> <br /> z(:,ii) means all the numbers in the ii column starting from 1st column until the nth<br /> column, which is the last one.<br /> <br /> higher dimension, less efficient and we need more data points<br /> <br /> Save it with the name of the pattern.<br /> <br /> &lt;/pre&gt;<br /> <br /> <br /> [[File:Unitball.png|400px]]<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> Execution: <br /> <br /> &gt;&gt;[x]=Unitball(2,10000);<br /> &gt;&gt;scatter(x(1,:),x(2,:)); %plot 2D circle<br /> &gt;&gt;axis square; %make the x-y axis has same size <br /> &gt;&gt;size(x)<br /> <br /> ans =<br /> <br /> 2 7839<br /> <br /> &gt;&gt;scatter(x(1,:),x(2,:))<br /> &lt;/pre&gt;<br /> <br /> scatter(x(1,:),x(2,:)) the (x(1,:) means all the numbers in the first row are parameter.<br /> <br /> '''Calculate the efficiency:'''<br /> &lt;pre style='font-size:16px'&gt;<br /> <br /> &gt;&gt;c=7839/10000 %Efficiency = points accepted / total points <br /> <br /> c =<br /> <br /> 0.7839<br /> <br /> &lt;/pre&gt;<br /> <br /> We can use the above program to calculate how many points in the circle condition are in the square.<br /> <br /> '''Estimate &lt;math&gt;\displaystyle \pi&lt;/math&gt;'''<br /> :*We know the radius is 1<br /> :*Then the area of the square is &lt;math&gt;(1-(-1))^2=4&lt;/math&gt;&lt;br\&gt;<br /> :*Then the area of the circle is &lt;math&gt;\pi&lt;/math&gt;&lt;br\&gt;<br /> :*&lt;math&gt;\pi&lt;/math&gt; is approximated to be &lt;math&gt;4\times c=4 \times 0.7839=3.1356&lt;/math&gt; in the above example &lt;br\&gt;<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> &gt;&gt; 4*size(x,2)/10000<br /> <br /> ans =<br /> <br /> 3.1356<br /> <br /> &gt;&gt; [x]=Unitball(3,10000);<br /> &gt;&gt; scatter3(x(1,:),x(2,:),x(3,:)) %plot 3d ball<br /> &gt;&gt; axis square<br /> &gt;&gt; size(x,2)/10000 %returns the size of the dimension of X specified by scalar 2<br /> <br /> ans =<br /> <br /> 0.5231<br /> <br /> &gt;&gt; [x]=Unitball(5,10000);<br /> &gt;&gt; size(x,2)/10000<br /> <br /> ans =<br /> <br /> 0.1648<br /> <br /> &lt;/pre&gt;<br /> 3d unit ball<br /> <br /> [[File:3-dimensional unitball.jpg|400px]]<br /> <br /> Note that c increases exponentially as d increases, which will result in a lower acceptance rate and more points being rejected. So this method is not efficient for large values of d.<br /> <br /> In practice, when we need to vectorlize a high quality image or genes then d would have to be very large. So AR method is not an efficient way to solve the problem.<br /> <br /> === Efficiency ===<br /> <br /> In the above example, the efficiency of the vector A/R is equal to the ratio<br /> <br /> &lt;math&gt;\frac{1}{C}=\frac{\text{volume of hyperball}}{\text{volume of hybercube}}= \max \frac{g(x)}{f(x)} &lt;/math&gt;<br /> <br /> In general, the efficiency can be thought of as the total number of points accepted divided by the total number of points generated.<br /> <br /> As the dimension '''increase''', the efficiency of the algorithm will '''decrease''' exponentially.<br /> <br /> For example, for approximating value of &lt;math&gt;\pi&lt;/math&gt;, when &lt;math&gt;d \text{(dimension)} =2&lt;/math&gt;, the efficiency is around 0.7869; when &lt;math&gt;d=3&lt;/math&gt;, the efficiency is around 0.5244; when &lt;math&gt;d=10&lt;/math&gt;, the efficiency is around 0.0026: it is getting close to 0.<br /> <br /> A 'C' value of 1 implies an acceptance rate of 100% (most efficient scenario) but as we sample from higher dimensions, 'C' usually gets larger. Thus, when we want to generate high dimension vectors, Acceptance-Rejection Method is not efficient to be used.<br /> <br /> &lt;span style=&quot;color:red;padding:0 auto;&quot;&gt;&lt;br&gt;The end of midterm coverage&lt;/span&gt;<br /> &lt;div style=&quot;border:1px solid #cccccc;border-radius:10px;box-shadow: 0 5px 15px 1px rgba(0, 0, 0, 0.6), 0 0 200px 1px rgba(255, 255, 255, 0.5);padding:20px;margin:20px;background:#FFFFAD;&quot;&gt;<br /> &lt;h2 style=&quot;text-align:center;&quot;&gt;Summary of vector acceptance-rejection sampling&lt;/h2&gt;<br /> &lt;p&gt;&lt;b&gt;Problem:&lt;/b&gt; &lt;math&gt; f(x_1, x_2, ...x_n)&lt;/math&gt; is difficult to sample from&lt;/p&gt;<br /> &lt;p&gt;&lt;b&gt;Plan:&lt;/b&gt;&lt;/p&gt;<br /> Let W represent the sample space covered by &lt;math&gt; f(x_1, x_2, ...x_n)&lt;/math&gt;<br /> &lt;ol&gt;<br /> &lt;li&gt;1.Draw &lt;math&gt;\vec{y}=y_1,y_2...y_n\sim~g()&lt;/math&gt; where g has sample space G which is greater than W. g is a distribution that is easy to sample from (i.e. uniform)&lt;/li&gt;<br /> &lt;li&gt;2.if &lt;math&gt;\vec{y} \subseteq W &lt;/math&gt; then &lt;math&gt;\vec{x}=\vec{y} &lt;/math&gt;&lt;br /&gt; else go 1) &lt;/li&gt;<br /> &lt;/ol&gt;<br /> &lt;p&gt;x will have the desired distribution.&lt;/p&gt;<br /> <br /> &lt;/div&gt;<br /> <br /> ==== Stochastic Process ====<br /> The basic idea of Stochastic Process (also called random process) is a collection of some random variables, <br /> &lt;math&gt;\big\{X_t:t\in T\big\}&lt;/math&gt;, where the set X is called the state space that each variable is in it and T is called the index set. <br /> <br /> '''Definition:''' In probability theory, a stochastic process /stoʊˈkæstɪk/, or sometimes random process (widely used) is a collection of random variables; this is often used to represent the evolution of some random value, or system, over time. This is the probabilistic counterpart to a deterministic process (or deterministic system). Instead of describing a process which can only evolve in one way (as in the case, for example, of solutions of an ordinary differential equation), in a stochastic or random process there is some indeterminacy: even if the initial condition (or starting point) is known, there are several (often infinitely many) directions in which the process may evolve. (from Wikipedia)<br /> <br /> A stochastic process is non-deterministic. This means that even if we know the initial condition(state), and we know some possibilities of the states to follow, the exact value of the final state remains to be uncertain. <br /> <br /> We can illustrate this with an example of speech: if &quot;I&quot; is the first word in a sentence, the set of words that could follow would be limited (eg. like, want, am), and the same happens for the third word and so on. The words then have some probabilities among them such that each of them is a random variable, and the sentence would be a collection of random variables. &lt;br&gt;<br /> Also, Different Stochastic Process has different properties.<br /> <br /> In the course, we study two Stochastic Process models.<br /> <br /> The two stochastic Process models we will study are:<br /> <br /> 1. Poisson Process-This is continuous time counting process that satisfies a couple of properties that are listed in the next section. The Poisson process is understood to be a good model for events such as incoming phone calls, number of traffic accidents, and goals during a game of hockey or soccer. It is also an example of a birth-death process.&lt;br&gt;<br /> 2. Markov Process- This is a stochastic process that satisfies the Markov property which can be understood as the memory-less property. The property states that the jump to a future state only depends on the current state of the process, and not of the process's history. This model is used to model random walks exhibited by particles, the health state of a life insurance policyholder, decision making by a memory-less mouse in a maze, etc. &lt;br&gt;<br /> <br /> <br /> =====Example=====<br /> The state space is the set of English words, and &lt;math&gt;x_t&lt;/math&gt; are words that are said. Another example involves the stock market: the set of all non-negative numbers is the state space, and &lt;math&gt;x_t&lt;/math&gt; are stock prices.<br /> <br /> stochastic process always has state space and the index set to limit the range.<br /> <br /> The state space is the set of cars, while &lt;math&gt;x_t&lt;/math&gt; are sport cars.<br /> <br /> Births in a hospital occur randomly at an average rate<br /> <br /> The number of cases of a disease in different towns<br /> <br /> ==== Poisson Process ====<br /> [[File:Possionprocessidiagram.png‎]]<br /> <br /> The Poisson process is a discrete counting process which counts the number of&lt;br\&gt;<br /> of events and the time that these occur in a given time interval.&lt;br\&gt;<br /> <br /> e.g traffic accidents , arrival of emails. Emails arrive at random time &lt;math&gt;T_1, T_2 ... T_n&lt;/math&gt; for example (2, 7, 3) is the number of emails received on day 1, day 2, day 3. This is a stochastic process and Poisson process with condition.<br /> <br /> The probability of observing x events in a given interval is given by<br /> &lt;math&gt; P(X = x) = e^{-\lambda}* \lambda^x/ x! &lt;/math&gt;<br /> where x = 0; 1; 2; 3; 4; ....<br /> <br /> -Let &lt;math&gt;N_t&lt;/math&gt; denote the number of arrivals within the time interval &lt;math&gt;(0,t]&lt;/math&gt;&lt;br\&gt;<br /> -Let &lt;math&gt;N(a,b]&lt;/math&gt; denote the number of arrivals in the time interval (a,b]&lt;br\&gt;<br /> -By definition, &lt;math&gt;N(a,b]=N_b-N_a&lt;/math&gt;&lt;br\&gt;<br /> -The two random variables &lt;math&gt;N(a,b]&lt;/math&gt; and &lt;math&gt;N(c,d]&lt;/math&gt; are independent if &lt;math&gt;(a,b]&lt;/math&gt; and &lt;math&gt;(c,d]&lt;/math&gt; do not intersect.&lt;br\&gt;<br /> <br /> <br /> '''Definition:''' An arrival counting process &lt;math&gt;N=(N_t)&lt;/math&gt; is called (Homogeneous) Poisson Process (PP) with rate &lt;math&gt;\lambda &gt; 0&lt;/math&gt; if <br /> <br /> A. The numbers of points in non-overlapping intervals are independent. &lt;br \&gt;<br /> B. The number of points in interval &lt;math&gt;I(a,b]&lt;/math&gt; has a poisson distribution with mean &lt;math&gt;\lambda (b-a)&lt;/math&gt; ,where (b-a) represents the length of I.<br /> <br /> In particular, observe that if &lt;math&gt;N=(N_t)&lt;/math&gt; is a Poisson process of rate &lt;math&gt;\lambda&gt;0&lt;/math&gt;, then the moments are<br /> E[N&lt;sub&gt;t&lt;/sub&gt;] = &lt;math&gt;\lambda t&lt;/math&gt; and Var[N&lt;sub&gt;t&lt;/sub&gt;] = &lt;math&gt;\lambda t&lt;/math&gt;<br /> <br /> the rate parameter may change over time; such a process is called a non-homogeneous Poisson process<br /> <br /> ==== Examples ====<br /> &lt;br /&gt;<br /> '''How to generate a multivariate normal with the built-in function &quot;randn&quot;: (example)'''&lt;br /&gt;<br /> (please check the general idea at the end of lecture 6 course note.)<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> z=randn(length(mu),n); %Here length(mu)=2 since s is a 2*2 matrix;<br /> s=[1 0.7;0.7 1]<br /> A=chol(s)% This only works for a positive definite A, otherwise we need to consider some other form of decomposition<br /> x=mu'*ones(1,n) + A*z; %ones(1,n) is a function expand the size of mu from 1*1<br /> %matrix to 1*n matrix;<br /> &lt;/pre&gt;<br /> For example, if we use mu = [2 5], we would get &lt;br/&gt;<br /> &lt;math&gt; = \left[ \begin{array}{ccc}<br /> 3.8214 &amp; 0.3447 \\<br /> 6.3097 &amp; 5.6157 \end{array} \right]&lt;/math&gt;<br /> <br /> <br /> If we want to use box-muller to generate a multivariate normal, we could use the code in lecture 6:<br /> &lt;pre style='font-size:16px'&gt;<br /> d = length(mu);<br /> R = chol(Sigma);<br /> <br /> U1=rand(n,d)<br /> U2=rand(d,d)<br /> D=-2*log(U1)<br /> tet=2*pi*U2<br /> Z=(D.^0.5)*cos(tet)' %since in lecture 6, we set up x and y are independent normal<br /> %distribution, so in here we can choose either cos and sin<br /> <br /> X = Z*R + ones(n,1)*mu';<br /> &lt;/pre&gt;<br /> <br /> ==== '''Central Limit Theorem''' ====<br /> <br /> Theorem: &quot;Given a distribution with mean μ and variance σ², the sampling distribution of the mean approaches a normal distribution with a mean (μ) and a variance σ²/N as N, the sample size, increases&quot;. Furthermore, the original distribution can be of any arbitrary shape, the sampling distribution of the mean will approach a normal distribution with a large enough N.&lt;ref&gt;<br /> http://davidmlane.com/hyperstat/A14043.html<br /> &lt;/ref&gt; <br /> <br /> Applying the central limit theorem to simulations, we may revise the definition to be the following: Given certain conditions, the mean of a sufficiently large number of independent random variables, each with a well defined mean and variance, will be approximately normal distributed.(i.e. if we simulate sufficiently many independent observations based on well defined mean and variance, the mean of these observations will follow an approximately normal distribution.) <br /> <br /> We illustrate with an example using 1000 observations each of 20 independent exponential random variables.<br /> <br /> &lt;pre style='font-size:16px'&gt;<br /> &gt;&gt; X = exprnd (20,20,1000); % 1000 instances of 20 exponential random numbers with mean 20<br /> &gt;&gt; hist(X(1,:))<br /> &gt;&gt; hist(X(1:2,:))<br /> ...<br /> &gt;&gt;hist(X(1:20,:)) -&gt; approaches to normal<br /> &lt;/pre&gt;<br /> <br /> (The definition of CLT is from http://en.wikipedia.org/wiki/Central_limit_theorem)<br /> <br /> &lt;math&gt; \lim_{n \to \infty} P*[{\frac{X_1 + ... + X_n -n*\mu}{\sigma*\surd n}} &lt; x] = \Phi (x)&lt;/math&gt;<br /> <br /> ==Class 11 - Tuesday，June 11, 2013==<br /> === Announcement ===<br /> Midterm covers up to the middle of last lecture, which means stochastic process will not be on midterm. There won't be any Matlab syntax questions. And Students can contribute to any previous classes. We might however be asked to write down algorithms.<br /> <br /> ===Poisson Process===<br /> A discrete stochastic variable ''X'' is said to have a Poisson distribution with parameter ''λ'' &gt; 0 if<br /> :&lt;math&gt;\!f(n)= \frac{\lambda^n e^{-\lambda}}{n!} \qquad n= 0,1,2,3,4,5,\ldots,&lt;/math&gt;.<br /> <br /> &lt;math&gt;\{X_t:t\in T\}&lt;/math&gt; where &lt;math&gt;\ X_t &lt;/math&gt; is state space and T is index set.<br /> <br /> <br /> '''Properties of Homogeneous Poisson Process'''&lt;br&gt;<br /> (a) '''Independence:''' The numbers of arrivals in non-overlapping intervals are independent &lt;br&gt;<br /> (b) '''Homogeneity or Uniformity:''' The number of arrivals in each interval I(a,b] is Poisson distribution with rate &lt;math&gt;\lambda (b-a)&lt;/math&gt;&lt;br/&gt;<br /> (c) '''Individuality:''' for a sufficiently short time period of length h, the probability of 2 or more events occurring in the interval is close to 0, or formally &lt;math&gt;\mathcal{O}(h)&lt;/math&gt;&lt;br&gt;<br /> <br /> NOTE: it is very important to note that the time between the occurrence of consecutive events (in a Poisson Process) is exponentially distributed with the same parameter as that in the Poisson distribution. This characteristic is used when trying to simulate a Poisson Process.<br /> <br /> For a small interval (t,t+h], where h is small&lt;br&gt;<br /> 1. The number of arrivals up to time t(N&lt;sub&gt;t&lt;/sub&gt;) is independent of the number of arrival in the interval&lt;br&gt;<br /> 2. &lt;math&gt; P (N(t,t+h)=1|N_{t} ) = P (N(t,t+h)=1) =\frac{e^{-\lambda h} (\lambda h)^1}{1!} =e^{-\lambda h} {\lambda h} \approx \lambda h &lt;/math&gt; since &lt;math&gt;e^{-\lambda h} \approx 1&lt;/math&gt; when h is small.&lt;br&gt;<br /> <br /> &lt;math&gt;\lambda h&lt;/math&gt; can be thought of as the probability of observing an arrival in the interval t to t+h.&lt;br&gt;<br /> <br /> Similarly, the probability of not observing an arrival in this interval is 1-&lt;math&gt;\lambda &lt;/math&gt; h.&lt;br&gt;<br /> <br /> <br /> '''Generate a Poisson Process'''&lt;br /&gt;<br /> <br /> &lt;math&gt;U_{n} \sim~ U(0,1)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;T_n-T_{n-1}=-\frac {1}{\lambda} log(U_n)&lt;/math&gt;&lt;br&gt;<br /> <br /> 1. set &lt;math&gt;T_{0}=0&lt;/math&gt; and n=1&lt;br/&gt;<br /> <br /> 2. &lt;math&gt;U_{n} \sim~ U(0,1)&lt;/math&gt;&lt;br /&gt;<br /> <br /> 3. &lt;math&gt;T_{n} = T_{n-1}-\frac {1}{\lambda} log (U_{n}) &lt;/math&gt; (declare an arrival)&lt;br /&gt;<br /> <br /> 4. if &lt;math&gt;T_{n} \gneq T&lt;/math&gt; stop&lt;br /&gt;<br /> &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&lt;br /&gt;<br /> &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;n=n+1 go to step 2&lt;br /&gt;<br /> <br /> Since &lt;math&gt;P(N(t,t+h)=1) = e^{-{\lambda} h}\lambda h&lt;/math&gt;, we can regard &lt;math&gt;\lambda &lt;/math&gt;h as a exponential distribution, and according to what we learnt, &lt;math&gt;T_n-T_{n-1} = h = -\frac {1}{\lambda} log(U_n)&lt;/math&gt;.&lt;br&gt;<br /> *Note : Recall that exponential random variable is the waiting time until one event of interested occurs.<br /> <br /> '''Review of Poisson - Example'''<br /> <br /> Let X be the r.v of the number of accidents in an hour, following the Poisson distribution with mean 1.8.<br /> <br /> &lt;math&gt;P(X=0)=e^{-1.8} &lt;/math&gt;<br /> <br /> &lt;math&gt;P(X=4)=\frac {e^{-1.8}(1.8)^4}{4!} &lt;/math&gt;<br /> <br /> &lt;math&gt;P(X\geq1) = 1 - P(x=0) = 1- e^{-1.8}&lt;/math&gt;<br /> <br /> &lt;span style=&quot;background:#F5F5DC&quot;&gt;<br /> &lt;math&gt;P(N_3&gt; 3 | N_2)=P(N_1 &gt; 2)&lt;/math&gt;<br /> &lt;/span&gt;<br /> <br /> When we use the inverse-transform method, we can assume the poisson process to be an exponential distribution, and get the h function from the inverse method. Sometimes we assume that h is very small.<br /> <br /> '''Multi-dimensional Poisson Process'''&lt;br&gt;<br /> <br /> The poisson distribution arises as the distribution of counts of occurrences of events in (multidimensional) intervals in multidimensional poisson process in a directly equivalent way to the result for unidimensional processes. This,is ''D'' is any region the multidimensional space for which |D|, the area or volume of the region, is finite, and if {{nowrap|''N''(''D'')}} is count of the number of events in ''D'', then<br /> <br /> &lt;math&gt; P(N(D)=k)=\frac{(\lambda|D|)^k e^{-\lambda|D|}}{k!} .&lt;/math&gt;<br /> <br /> === Generating a Homogeneous Poisson Process === <br /> Suppose we want to generate the first n events of a Poisson process with rate &lt;math&gt;{\lambda}&lt;/math&gt;. We showed that the times between successive events are independent exponential random variables, each with rate &lt;math&gt;{\lambda}&lt;/math&gt;. Therefore, we can first generate n random numbers U&lt;sub&gt;1&lt;/sub&gt;, U&lt;sub&gt;2&lt;/sub&gt;,...,U&lt;sub&gt;n&lt;/sub&gt; and use the inverse transform method to find the corresponding exponential variable X&lt;sub&gt;1&lt;/sub&gt;, X&lt;sub&gt;2&lt;/sub&gt;,...,X&lt;sub&gt;n&lt;/sub&gt;. Then X&lt;sub&gt;i&lt;/sub&gt; can be interpreted as the time between the (&lt;i&gt;i&lt;/i&gt;-1)st and the &lt;i&gt;i&lt;/i&gt;th event of the process. &lt;br&gt;<br /> The actual time of the &lt;i&gt;j&lt;/i&gt;th event is the sum of the first j interarrival times. Therefore, the generated values of the first n event times are &lt;math&gt;\sum_{i=1}^{j} X_i&lt;/math&gt;, j = 1...n. <br /> &lt;br&gt;<br /> <br /> Homogeneous poisson process refers to the rate of occurrences remaining constant for all periods of time. &lt;br&gt;<br /> &lt;br&gt;U&lt;sub&gt;n&lt;/sub&gt;~U(0,1)&lt;br&gt;<br /> &lt;math&gt;T_n-T_{n-1}=-\frac {1}{\lambda} log(U_n)&lt;/math&gt; &lt;br&gt;<br /> <br /> The waiting time between each occurrence follows the exponential distribution with parameter &lt;math&gt;\lambda &lt;/math&gt;.<br /> &lt;math&gt;T_n&lt;/math&gt; represents the time elapsed at the n&lt;sup&gt;th&lt;/sup&gt; occurence.<br /> <br /> 1) Set T&lt;sub&gt;0&lt;/sub&gt; = 0 ,and n = 1 &lt;br&gt;<br /> 2) U&lt;sub&gt;n&lt;/sub&gt; follow U(0,1) &lt;br&gt;<br /> 3) T&lt;sub&gt;n&lt;/sub&gt; - T&lt;sub&gt;n-1&lt;/sub&gt; =&lt;math&gt; -\frac {1}{\lambda} &lt;/math&gt; log (U&lt;sub&gt;n&lt;/sub&gt;) (Declare an arrival)&lt;br&gt;<br /> 4) if T&lt;sub&gt;n&lt;/sub&gt; &gt;T stop;<br /> else n = n + 1, go to step 2 &lt;br&gt;<br /> <br /> h is the a range and we assume the probability of every point in this rang is the same by uniform ditribution.(cause h is small)<br /> and we test the rang is Tn smaller than T. And &lt;math&gt; -\frac {1}{\lambda} &lt;/math&gt; log (U&lt;sub&gt;n&lt;/sub&gt;)represents that chance that one arrival arrives.<br /> <br /> &lt;b&gt;Higher Dimensions:&lt;/b&gt;&lt;br&gt;<br /> To sample from higher dimensional Poisson process:&lt;br&gt;<br /> 1. Generate a random number N that is Poisson distributed with parameter &lt;math&gt;{\lambda}&lt;/math&gt;*A&lt;sub&gt;d&lt;/sub&gt;, where A&lt;sub&gt;d&lt;/sub&gt; is the area under the bounded region. (ie A&lt;sub&gt;2&lt;/sub&gt; is area of the region, A&lt;sub&gt;3&lt;/sub&gt; is the volume of the 3-d space.&lt;br&gt;<br /> 2. Given N=n, generate n random (uniform) points in the region.&lt;br&gt;<br /> <br /> '''[[At the end, it generates n (cumulative) arrival times, up to time TT.]]'''<br /> <br /> ==== MatLab Code ====<br /> &lt;pre style='font-size:16px'&gt;<br /> <br /> T(1)=0; % Matlab does not allow 0 as an index, so we start with T(1).<br /> ii=1;<br /> l=2;<br /> TT=5;<br /> <br /> while T(ii)&lt;=TT<br /> u=rand;<br /> ii=ii+1;<br /> T(ii)=T(ii-1) - (1/l)*log(u); <br /> end<br /> <br /> plot(T, '.')<br /> <br /> &lt;/pre&gt;<br /> &lt;br&gt;<br /> <br /> The following plot is using TT = 50.&lt;br&gt;<br /> The number of points generated every time on average should be &lt;math&gt;\lambda&lt;/math&gt; * TT. &lt;br&gt;<br /> The maximum value of the points should be TT. &lt;br&gt;<br /> [[File:Poisson.jpg]]&lt;br&gt;<br /> when TT be big, the plot of the graph will be linear, when we set the TT be 5 or small number, the plot graph looks like discrete distribution.<br /> <br /> ===Markov chain===<br /> &quot;A Markov Chain is a stochastic process where: &lt;br/&gt;<br /> <br /> 1) Each stage has a fixed number of states, &lt;br/&gt;<br /> 2) the (conditional) probabilities at each stage only depend on the previous state.&quot; &lt;br/&gt;<br /> <br /> Source: &quot;http://math.scu.edu/~cirving/m6_chapter8_notes.pdf&quot; &lt;br/&gt;<br /> <br /> A Markov Chain is said to be irreducible if for each pair of states i and j there is a positive probability, starting in state i, that the process will ever enter state j.(source:&quot;https://en.wikipedia.org/wiki/Markov_chain&quot;)<br /> <br /> Markov Chain is the simplification of assumption, for instance, the result of GPA in university depend on the GPA's in high school, middle school, elementary school, etc., but during a job interview after university graduation, the interviewer would most likely to ask about the GPA in university of the interviewee but not the GPA from early years because they assume what happened before are summarized and adequately represented by the information of the GPA earned during university. Therefore, it's not necessary to look back to elementary school. A Markov Chain works the same way, we assume that everything that has occurred earlier in the process is only important for finding out where we are now, the future only depends on the present of where we are now, not the past of how we got here. So the n&lt;sub&gt;th&lt;/sub&gt;random variable would only depend on the n-1&lt;sub&gt;th&lt;/sub&gt;term but not all the previous ones. A Markov process exhibits the memoryless property.&lt;br/&gt;<br /> <br /> Examples of Markov Chain applications in various fields:&lt;br /&gt;<br /> *Physics: The movement of a particle (memory-less random walk)&lt;br /&gt;<br /> *Finance: The volatility of prices of financial securities or commodities&lt;br /&gt;<br /> *Actuarial Science: The pricing and valuation of multiple-state and multiple-lives insurance and annuity contracts&lt;br /&gt;<br /> *Technology: The Google link analysis algorithm &quot;PageRank&quot;&lt;br /&gt;<br /> <br /> <br /> '''Definition''' An irreducible Markov Chain is said to be aperiodic if for some n &lt;math&gt;\ge 0 &lt;/math&gt; and some state j.&lt;br /&gt;<br /> &lt;math&gt; P*(X_n=j | X_0 =j) &gt; 0 &lt;/math&gt; and &lt;math&gt; P*(X_{n+1} | X_0=j) &gt; 0 &lt;/math&gt; &lt;br /&gt;<br /> <br /> It can be shown that if the Markov Chain is irreducible and aperiodic then, &lt;br /&gt;<br /> &lt;math&gt; \pi_j = \lim_{n -&gt; \infty} P*(X_n = j) for j=1...N &lt;/math&gt; &lt;br /&gt;<br /> Source: From Simulation textbook &lt;br /&gt;<br /> <br /> Product Rule (Stochastic Process):&lt;br /&gt;<br /> &lt;math&gt;f(x_1,x_2,...,x_n)=f(x_1)f(x_2\mid x_1)f(x_3\mid x_2,x_1)...f(x_n\mid x_{n-1},x_{n-2},....)&lt;/math&gt;<br /> <br /> In Markov Chain&lt;br /&gt;<br /> &lt;math&gt; f(x_1,x_2,...,x_n)=f(x_1)f(x_2\mid x_1)f(x_3\mid x_2)...f(x_n\mid x_{n-1}) &lt;/math&gt;<br /> <br /> Concept: The current status of a subject must be relative to the past.However, it will only depend on the previous result only. In other words, if an event occurring tomorrow follows a Markov process it will depend on today and yesterday (the past) is negligible. The past (not including the current state of course) is negligible since its information is believed to have been captured and reflected in the current state. <br /> <br /> A Markov Chain is a Stochastic Process for which the distribution of &lt;math&gt;x_t&lt;/math&gt; depends only on &lt;math&gt;x_{t-1}&lt;/math&gt;.<br /> <br /> Given &lt;math&gt; x_t&lt;/math&gt;, &lt;math&gt;x_{t-1}&lt;/math&gt; and &lt;math&gt;x_{t+1} &lt;/math&gt; are independent. The process of getting &lt;math&gt; x_n &lt;/math&gt; is drawn as follows. The distribution of &lt;math&gt;x_n&lt;/math&gt; only depends on the value of &lt;math&gt;x_{n-1}&lt;/math&gt;.<br /> <br /> &lt;math&gt; x_1 \rightarrow x_2\rightarrow...\rightarrow x_n&lt;/math&gt; <br /> <br /> Formal Definition:<br /> The process &lt;math&gt; \{x_n: n \in T\} &lt;/math&gt; is a markov chain if:&lt;br /&gt;<br /> &lt;math&gt; Pr(x_n|x_{n-1},...,x_1) = Pr(x_n|x_{n-1}) \ \ \forall n\in T &lt;/math&gt; and &lt;math&gt; \forall x\in X&lt;/math&gt;<br /> <br /> &lt;span style=&quot;background:#F5F5DC&quot;&gt;CONTINUOUS TIME MARKOV PROCESS&lt;/span&gt;<br /> <br /> A continuous time markov process would be one where the time spent in each state is not discrete, but can take <br /> on positive real values. In other words, the index set is the positive real numbers. If the process is homogeneous, then the time spent will have an exponential distribution. In this case we will have a transition rate matrix that captures the rate at which we move between two states. An example will be the homogeneous Poisson process.&lt;br /&gt;<br /> <br /> <br /> ====Transition Matrix====<br /> <br /> Transition Probability: &lt;math&gt; P_{ij} = P(X_{t+1} =j | X_t =i) &lt;/math&gt; is the one-step transition probability from state i to state j.<br /> <br /> Transition Probability: &lt;math&gt; P_{ij}(k) = P(X_{t+1}(k) =j | X_t(k) =i) &lt;/math&gt; is the k-step transition probability from state i to state j.<br /> <br /> The matrix P whose elements are transition Probabilities &lt;math&gt; P_{ij} &lt;/math&gt; is a one-step transition matrix. <br /> <br /> Example:&lt;br /&gt;<br /> <br /> [[File:Transition_Map.png]]<br /> <br /> &lt;math&gt;\begin{align}<br /> P_{ab} &amp;=P(X_{t+1} &amp;=b &amp;\mid X_{t} &amp;=a) &amp;= 0.3 \\<br /> P_{aa} &amp;=P(X_{t+1} &amp;=a &amp;\mid X_{t} &amp;=a) &amp;= 0.7 \\<br /> P_{ba} &amp;=P(X_{t+1} &amp;=a &amp;\mid X_{t} &amp;=b) &amp;= 0.2 \\<br /> P_{bb} &amp;=P(X_{t+1} &amp;=b &amp;\mid X_{t} &amp;=b) &amp;= 0.8 \\<br /> \end{align}&lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt; P= \left [ \begin{matrix}<br /> 0.7 &amp; 0.3 \\<br /> 0.2 &amp; 0.8<br /> \end{matrix} \right] &lt;/math&gt;<br /> <br /> Note: Column 1 corresponds to the state at time t and Column 2 corresponds to the state at time t+1.<br /> <br /> The above matrix can be drawn into a state transition diagram<br /> <br /> Properties of Transition Matrix: &lt;br /&gt;<br /> <br /> 1. &lt;math&gt; 1 \geq P_{ij} \geq 0 &lt;/math&gt;&lt;br /&gt;<br /> <br /> 2. &lt;math&gt;\sum_{j}^{}{P_{ij}=1}&lt;/math&gt; which means the rows of P should sum to 1.&lt;br /&gt;<br /> <br /> Remark: &lt;math&gt;\sum_{i}^{}{P_{ij}\neq1}&lt;/math&gt; in general. If equality holds, the matrix is called a doubly stochastic matrix.<br /> <br /> In general, one would consider a (finite) set of elements &lt;math&gt; \Omega &lt;/math&gt; such that: &lt;br&gt;<br /> <br /> &lt;math&gt; \forall x \in \Omega &lt;/math&gt;, the probability of the next state is given according to the distribution &lt;math&gt; P(x,\cdot) &lt;/math&gt; &lt;br&gt;<br /> <br /> This means our model can be simulated as a sequence of random variables &lt;math&gt; (X_0, X_1, X_2, \ldots ) &lt;/math&gt; with state space &lt;math&gt; \Omega &lt;/math&gt; and transition matrix &lt;math&gt; P = [P_{ij}] &lt;/math&gt; where &lt;math&gt; \forall t \in \N, 0 \leq s \leq t+1, x_s \in \Omega, &lt;/math&gt; &lt;br/&gt;<br /> <br /> we have to following property (Markov property): &lt;br/&gt;<br /> &lt;math&gt; P(X_{t+1}= x_{t+1} \vert \cap^{t}_{s=0} X_s = x_s) = P(X_{t+1} =x_{t+1} \vert X_t =x_t) = P(x_t,x_{t+1}) &lt;/math&gt; &lt;br&gt;<br /> <br /> And &lt;math&gt; \forall x \in \Omega, \sum_{y\in\Omega} P(x,y) =1; \; \forall x,y\in\Omega, P_{xy} = P(x,y) \geq 0 &lt;/math&gt;&lt;br&gt;<br /> <br /> Moreover if &lt;math&gt; \forall x,y \in \Omega, \exists k, P^k (x,y) &gt; 0 &lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt; |\Omega| &lt; \infty &lt;/math&gt; (i.e Any two states can be translated somehow) &lt;br&gt;<br /> <br /> Then one might consider the periodicity of the chain and derive a notion of cyclic behavior. &lt;br&gt;<br /> <br /> === Examples of Transition Matrix ===<br /> <br /> [[File:Mark13.png]]&lt;br&gt;<br /> The picture is from http://www.google.ca/imgres?imgurl=http://academic.uprm.edu/wrolke/esma6789/graphs/mark13.png&amp;imgrefurl=http://academic.uprm.edu/wrolke/esma6789/mark1.htm&amp;h=274&amp;w=406&amp;sz=5&amp;tbnid=6A8GGaxoPux9kM:&amp;tbnh=83&amp;tbnw=123&amp;prev=/search%3Fq%3Dtransition%2Bmatrix%26tbm%3Disch%26tbo%3Du&amp;zoom=1&amp;q=transition+matrix&amp;usg=__hZR-1Cp6PbZ5PfnSjs2zU6LnCiI=&amp;docid=PaQvi1F97P2urM&amp;sa=X&amp;ei=foTxUY3DB-rMyQGvq4D4Cg&amp;sqi=2&amp;ved=0CDYQ9QEwAQ&amp;dur=5515)<br /> <br /> 1.There are four states: 0,1,2, and 3.<br /> <br /> Each row adds up to 1, and all the entries are between 0 and 1(since the transition probability is always between 0 and 1).&lt;br /&gt;<br /> This matrix means that &lt;br /&gt;<br /> - at state 0, can only go to state 1, since probability is 1.&lt;br /&gt;<br /> - at state 1, can go to state 0 with a probability of 1/3 and state 2 with a probability of 2/3&lt;br /&gt;<br /> - at state 2, can go to state 1 with a probability of 2/3 and state 3 with a probability of 1/3&lt;br /&gt;<br /> - at state 3, can only go to state 2, since probability is 1.&lt;br /&gt;<br /> <br /> 2. Consider a Markov chain with state space {0, 1} and transition matrix <br /> &lt;math&gt; P= \left [ \begin{matrix}<br /> 1/3 &amp; 2/3 \\<br /> 3/4 &amp; 1/4<br /> \end{matrix} \right] &lt;/math&gt;. <br /> Assuming that the chain starts in state 0 at time n = 0, what is the probability that it is in state 1 at time n = 2?<br /> <br /> <br /> &lt;math&gt;\begin{align}<br /> P(X_{2} &amp;=1 &amp;\mid X_{0} &amp;=0) &amp; =P(X_{1} &amp;=0,X_{2} &amp;=1 &amp;\mid X_{0} &amp;=0)+P(X_{1} &amp;=1,X_{2} &amp;=1 &amp;\mid X_{0} &amp;=0)\end{align} &lt;/math&gt;<br /> &lt;math&gt; \begin{align} P(X_{1} &amp;=0 &amp;\mid X_{0} &amp;=0) * P(X_{2} &amp;=1 &amp;\mid X_{1} &amp;=0)+P(X_{1} &amp;=1 &amp;\mid X_{0} &amp;=0) * P(X_{2} &amp;=1 &amp;\mid X_{1}&amp;=1) &amp;=1/3*2/3+ 2/3*1/4 &amp;=7/18 \\<br /> \end{align}&lt;/math&gt;&lt;br /&gt;<br /> <br /> == Class 12 - Thursday，June 13, 2013 ==<br /> &lt;b&gt;Time&lt;/b&gt;<br /> Jun 17, 2013 2:30 PM - 3:30 PM<br /> ===Midterm Review===<br /> <br /> === Multiplicative Congruential Algorithm ===<br /> &lt;div style=&quot;border:1px solid red&quot;&gt;<br /> A Linear Congruential Generator (LCG) yields a sequence of randomized numbers calculated with a linear equation. The method represents one of the oldest and best-known pseudorandom number generator algorithms. The theory behind them is easy to understand, and they are easily implemented and fast, especially on computer hardware which can provide modulo arithmetic by storage-bit truncation.&lt;br&gt;<br /> from wikipedia<br /> &lt;/div&gt;<br /> <br /> &lt;math&gt;\begin{align}x_k+1= (ax_k+c) \mod m\end{align}&lt;/math&gt;&lt;br /&gt;<br /> <br /> Where a, c, m and x&lt;sub&gt;1&lt;/sub&gt; (the seed) are values we must chose before running the algorithm. While there is no set value for each, it is best for m to be large and prime. For example, Matlab uses a = 75,b = 0,m = 231 − 1.<br /> <br /> '''Examples:'''&lt;br&gt; <br /> 1. &lt;math&gt;\begin{align}X_{0} = 10 ,a = 2 , c = 1 , m = 13 \end{align}&lt;/math&gt;&lt;br&gt; <br /> <br /> &lt;math&gt;\begin{align}X_{1} = 2 * 10 + 1\mod 13 = 8\end{align}&lt;/math&gt;&lt;br&gt;<br /> <br /> &lt;math&gt;\begin{align}X_{2} = 2 * 8 + 1\mod 13 = 4\end{align}&lt;/math&gt; ... and so on&lt;br&gt;<br /> <br /> <br /> 2. &lt;math&gt;\begin{align}X_{0} = 44 ,a = 13 , c = 17 , m = 211\end{align}&lt;/math&gt;&lt;br&gt; <br /> <br /> &lt;math&gt;\begin{align}X_{1} = 13 * 44 + 17\mod 211 = 167\end{align}&lt;/math&gt;&lt;br&gt; <br /> <br /> &lt;math&gt;\begin{align}X_{2} = 13 * 167 + 17\mod 211 = 78\end{align}&lt;/math&gt;&lt;br&gt; <br /> <br /> &lt;math&gt;\begin{align}X_{3} = 13 * 78 + 17\mod 211 = 187\end{align}&lt;/math&gt; ... and so on&lt;br&gt;<br /> <br /> === Inverse Transformation Method ===<br /> '''For continuous cases:''' &lt;br/ &gt;<br /> 1. U~U(0,1)&lt;br/ &gt;<br /> 2. X=F&lt;sup&gt;-1&lt;/sup&gt;(u)&lt;br/ &gt;<br /> <br /> '''Note:''' &lt;br/&gt;<br /> In Uniform Distribution &lt;math&gt;P(X&lt;=a)=a &lt;/math&gt;&lt;br/ &gt;<br /> proof:&lt;math&gt;P(X&lt;=x) = P(F^{-1}(u)&lt;=x)=P(u&lt;=F(x)) = F(x)&lt;/math&gt;&lt;br/ &gt;<br /> <br /> '''For discrete cases:''' &lt;br/ &gt;<br /> 1. U~U(0,1)&lt;br/ &gt;<br /> 2. x=x&lt;sub&gt;i&lt;/sub&gt; if F(x&lt;sub&gt;i-1&lt;/sub&gt;)&lt;math&gt;&lt;&lt;/math&gt;u&lt;math&gt;\leq&lt;/math&gt;F(x&lt;sub&gt;i&lt;/sub&gt;)&lt;br/ &gt;<br /> <br /> ===Acceptance-Rejection Method===<br /> cg(x)&gt;=f(x)<br /> &lt;math&gt;c=max\frac{f(x)}{g(x)}&lt;/math&gt;<br /> &lt;br&gt;&lt;math&gt;\frac{1}{c}&lt;/math&gt; is the efficiency of the method/probability of acceptance<br /> <br /> 1. Y~g(x)&lt;br /&gt;<br /> 2. U~U(0,1)&lt;br /&gt;<br /> 3. If &lt;math&gt;U&lt;=\frac{f(y)}{c*g(y)}&lt;/math&gt; then X=Y<br /> else go to step 1<br /> <br /> Proof that this method generates the desired distribution:<br /> &lt;math&gt;P(Y|accepted)=\frac{P(accepted|Y)P(Y)}{P(accepted)}=\frac{\frac{f(y)}{cg(y)} g(y)}{\int_{y}^{ } \frac{f(y)}{cg(y)} g(y) dy}=\frac{\frac{f(y)}{c}}{\frac{1}{c}\cdot 1}=f(y)&lt;/math&gt;<br /> <br /> ===Multivariate===<br /> <br /> f(x&lt;sub&gt;1&lt;/sub&gt;,....,x&lt;sub&gt;n&lt;/sub&gt;)=f(x&lt;sub&gt;1&lt;/sub&gt;)f(x&lt;sub&gt;2&lt;/sub&gt;|x&lt;sub&gt;1&lt;/sub&gt;)...f(x&lt;sub&gt;n&lt;/sub&gt;|x&lt;sub&gt;n-1&lt;/sub&gt;,...x&lt;sub&gt;1&lt;/sub&gt;)<br /> in general we need knowledge of conditional distribution if x&lt;sub&gt;1&lt;/sub&gt;....x&lt;sub&gt;n&lt;/sub&gt; are independent<br /> i.e. f(x&lt;sub&gt;1&lt;/sub&gt;...x&lt;sub&gt;n&lt;/sub&gt;)=f(x&lt;sub&gt;1&lt;/sub&gt;)f(x&lt;sub&gt;2&lt;/sub&gt;)...f(x&lt;sub&gt;n&lt;/sub&gt;)<br /> <br /> === Vector A/R Method===<br /> <br /> This method is not efficient for high dimensional vectors&lt;br/&gt;<br /> <br /> - Sample uniformly from a space W that contains the sample space G of interest&lt;br/&gt;<br /> - Accept if the point is inside G &lt;br/&gt;<br /> <br /> Steps:<br /> 1. Sample uniformly from W &lt;br/&gt;<br /> g(x)=&lt;math&gt;\frac{1}{A_W}&lt;/math&gt;, where A&lt;sub&gt;W&lt;/sub&gt; is the area of W.&lt;br/&gt;<br /> f(x)=&lt;math&gt;\frac{1}{A_G}&lt;/math&gt;, where A&lt;sub&gt;G&lt;/sub&gt; is the area of G.&lt;br/&gt;<br /> 2. If the point is inside G, accept the point. Else, reject and repeat step 1.<br /> <br /> <br /> -&lt;math&gt;\frac{A_G}{A_W}&lt;/math&gt; = &lt;math&gt;\frac{1}{c}&lt;/math&gt; is the efficiency of the algorithm. That is the amount of points accepted over the amount of points rejected. This acceptance rate drops greatly for each dimension added.<br /> <br /> Note that &lt;math&gt;\frac{A_G}{A_W}&lt;/math&gt; decreases exponentially as the dimensional goes up, thus its not efficient for for high dimensional.<br /> <br /> === Common distribution ===<br /> <br /> ===Exponential===<br /> <br /> Models the waiting time until the first success.&lt;br&gt;<br /> &lt;math&gt;X\sim~Exp(\lambda)&lt;/math&gt; &lt;br /&gt;<br /> <br /> &lt;math&gt;f(x) = \lambda e^{-\lambda x} \, , x&gt;0 &lt;/math&gt;&lt;br/&gt;<br /> <br /> &lt;math&gt;1.\, U\sim~U(0,1)&lt;/math&gt;<br /> &lt;br /&gt;<br /> &lt;math&gt;2.\, x = \frac{-1}{\lambda} log(U)&lt;/math&gt;<br /> <br /> ===Normal===<br /> Box-Muller method<br /> <br /> 1.&lt;math&gt;U_{1},U_{2}\sim~ U(0,1)&lt;/math&gt; &lt;br/&gt;<br /> 2.&lt;math&gt;R^{2}=-2log(U_{1}), R^{2}\sim~ Exp(1/2)&lt;/math&gt;<br /> <br /> &lt;math&gt;\theta = 2\pi U_{2},\theta\sim~ U(0,2\pi) &lt;/math&gt;&lt;br/&gt;<br /> 3.&lt;math&gt;X=Rcos(\theta), Y=Rsin(\theta), X,Y\sim~ N(0,1)&lt;/math&gt;&lt;br/ &gt;<br /> &lt;br/ &gt;<br /> <br /> To obtain any normal distribution X once a set of standard normal samples Z is generated, apply the following transformations: &lt;br/ &gt;<br /> &lt;math&gt;Z\sim N(0,1)\rightarrow X \sim N(\mu,\sigma ^{2})&lt;/math&gt;&lt;br/ &gt;<br /> &lt;math&gt;X=\mu+\sigma~Z&lt;/math&gt;&lt;br/ &gt;<br /> <br /> In the multivariate case,&lt;br/ &gt;<br /> &lt;math&gt;\underline{Z}\sim N(\underline{0},I)\rightarrow \underline{X} \sim N(\underline{\mu},\Sigma)&lt;/math&gt; &lt;br/ &gt;<br /> &lt;math&gt;\underline{X} = \underline{\mu} +\Sigma ^{1/2} \underline{Z}&lt;/math&gt;&lt;br/&gt;<br /> Note: &lt;math&gt;\Sigma^{1/2}&lt;/math&gt; can be obtained from Cholesky decomposition (chol(A) in MATLAB), which is guaranteed to exist, as &lt;math&gt;\Sigma&lt;/math&gt; is positive semi-definite.<br /> <br /> === Gamma ===<br /> Gamma(t,λ) &lt;br&gt;<br /> t: The number of exponentials and the shape parameter&lt;br&gt;<br /> λ: The mean of the exponentials and the scale parameter&lt;br&gt; <br /> <br /> Also, Gamma(t,λ) can be expressed into a summation of t exp(λ).&lt;br&gt;<br /> &lt;math&gt;x=\frac {-1}{\lambda}\log(u_1)-\frac {1}{\lambda}\log(u_2)-.......-\frac {1}{\lambda}\log(u_t)&lt;/math&gt;<br /> <br /> &lt;math&gt;=\frac {-1}{\lambda}[\log(u_1)+\log(u_2)+.....+\log(u_t)]&lt;/math&gt;<br /> <br /> &lt;math&gt;=\frac {-1}{\lambda}\log(\prod_{j=1}^{t} U_j)&lt;/math&gt;<br /> <br /> This is a special property of gamma distribution.<br /> <br /> === Bernoulli ===<br /> <br /> A Bernoulli random variable can only take two possible values: 0 and 1. 1 represents &quot;success&quot; and 0 represents &quot;failure.&quot; If p is the probability of success, we have pdf<br /> <br /> &lt;math&gt; f(x)= p^x (1-p)^{1-x},\, x=0,1 &lt;/math&gt;&lt;br&gt;<br /> <br /> To generate a Bernoulli random variable we use the following procedure:<br /> <br /> &lt;math&gt; 1. U\sim~U(0,1)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt; 2. if\, u &lt;= p, then\, x=1\,&lt;/math&gt;&lt;br /&gt; <br /> &lt;math&gt; else\, x=0&lt;/math&gt;&lt;br/&gt;<br /> where 1 stands for success and 0 stands for failure.&lt;br&gt;<br /> <br /> ===Binomial===<br /> <br /> The sum of n independent Bernoulli trials<br /> &lt;br\&gt;<br /> &lt;math&gt; X\sim~ Bin(n,p)&lt;/math&gt;&lt;br/&gt;<br /> 1.&lt;math&gt; U1, U2, ... Un \sim~U(0,1)&lt;/math&gt;&lt;br/&gt;<br /> 2. &lt;math&gt; X= \sum^{n}_{1} I(U_i \leq p) &lt;/math&gt; ,where &lt;math&gt;I(U_i \leq p)&lt;/math&gt; is an indicator for a successful trial.&lt;br/&gt;<br /> Return to 1&lt;br/&gt;<br /> <br /> I is an indicator variable if for &lt;math&gt;U \leq P,\, then\, I(U\leq P)=1;\, else I(U&gt;P)=0.&lt;/math&gt;<br /> <br /> Repeat this N times if you need N samples.<br /> <br /> The theory behind the algorithm is the fact that the sum of n independent and identically distributed Bernoulli trials, Ber(p), follows a binomial Bin(n,p) distribution. <br /> <br /> Example:<br /> <br /> Suppose rolling a die, success= lands on 5, fail ow<br /> <br /> p=1/6, 1-p=5/6, rolling for 10 times, n=10<br /> <br /> simulate this binomial distribution.<br /> <br /> 1) Generate &lt;math&gt;U_1....U_{10} \sim~ U(0,1) &lt;/math&gt; &lt;br&gt;<br /> 2) &lt;math&gt; X= \sum^{10}_{1} I(U_i \leq \frac{1}{6}) &lt;/math&gt; &lt;br&gt;<br /> 3)Return to 1)<br /> <br /> === Beta Distribution ===<br /> &lt;math&gt;\displaystyle \text{Beta}(\alpha,\beta) = \frac{\Gamma(\alpha+\beta)}{\Gamma(\alpha)\Gamma(\beta)}x^{\alpha-1}(1-x)^{\beta-1} &lt;/math&gt; where &lt;math&gt;0 \leq x \leq 1&lt;/math&gt; and &lt;math&gt;\alpha&lt;/math&gt;&gt;0, &lt;math&gt;\beta&lt;/math&gt;&gt;0&lt;br&gt;<br /> and<br /> &lt;math&gt;f(x;\alpha,\beta)= 0 &lt;/math&gt; otherwise<br /> <br /> :&lt;math&gt;\displaystyle \text{Beta}(1,1) = U (0, 1) &lt;/math&gt;&lt;br&gt;<br /> <br /> <br /> :&lt;math&gt;\displaystyle \text{Beta}(\alpha,1)={f}(x) = \frac{\Gamma(\alpha+1)}{\Gamma(\alpha)\Gamma(1)}x^{\alpha-1}(1-x)^{1-1}=\alpha x^{\alpha-1}&lt;/math&gt;&lt;br&gt;<br /> <br /> <br /> '''Gamma Distribution'''<br /> <br /> '''Algorithm'''&lt;br\&gt;<br /> *1. Sample from Y1 ~ Gamma (&lt;math&gt;\alpha&lt;/math&gt;,1) where &lt;math&gt;\alpha&lt;/math&gt; is the shape, and 1 is the scale.&lt;br\&gt;<br /> *2. Sample from Y2 ~ Gamma (&lt;math&gt;\beta&lt;/math&gt;,1) where &lt;math&gt;\alpha&lt;/math&gt; is the shape, and 1 is the scale.&lt;br\&gt;<br /> *3. Set &lt;math&gt; Y = \frac{Y_1}{Y_1+Y_2}, &lt;/math&gt; Then Y ~ &lt;math&gt;\beta ( \alpha , \beta )&lt;/math&gt;, where we suppose &lt;math&gt;\alpha , \beta&lt;/math&gt; are integers.<br /> <br /> === Geometric ===<br /> <br /> This distribution models the number of failures before the first success.<br /> <br /> X~Geo(p)<br /> <br /> &lt;math&gt;f(x)=p*(1-p)^{x-1}, x=1, 2, ........&lt;/math&gt;&lt;br&gt;<br /> If Y~Exp&lt;math&gt;(\lambda)&lt;/math&gt; where &lt;math&gt;\lambda=-log(1-p)&lt;/math&gt;&lt;br&gt;<br /> then &lt;math&gt;X=floor[Y]+1&lt;/math&gt; is Geo(p)&lt;br&gt;<br /> <br /> '''Proof:'''<br /> &lt;math&gt;F(x)=P(X&lt;=x)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt; =1-P(X&gt;x)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt; =1-P(floor [Y]+1&gt;x)&lt;/math&gt; as &lt;math&gt; floor [Y]+1&gt;x&lt;/math&gt; implies &lt;math&gt; Y\leq x &lt;/math&gt; for all real-valued Y&lt;br&gt; <br /> &lt;math&gt; =1-P(Y&gt;=x)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt; =1-(1-P(Y&lt;x))&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt; =1-e^{-\lambda*x}&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt; =1-(1-p)^x&lt;/math&gt;, which is the CDF of Geo(p)&lt;br&gt;<br /> <br /> The above method can also be viewed with the inverse method since it is not rejecting any points. The following uses inverse method to generate Geo(p).&lt;br&gt;<br /> &lt;math&gt;F(x)=P(X \leq x)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;F(x)=1- P(X&gt;x)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;P(X \leq x)=1-(1-p)^x&lt;/math&gt; since &lt;math&gt;P(X&gt;x)=(1-p)^x&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;y=1-(1-p)^x&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;y-1=-(1-p)^x&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;1-y=(1-p)^x&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;1-y=(e^{-\lambda})^x=e^{-\lambda*x}&lt;/math&gt; since &lt;math&gt;1-p=e^{-\lambda}&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;log(1-y)=-\lambda*x&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;x=-1/(\lambda)*log(1-y)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;F^-1(x)=-1/(\lambda)*log(1-x)&lt;/math&gt;&lt;br&gt;<br /> &lt;br&gt;<br /> '''Algorithm:'''&lt;br /&gt;<br /> 1. Generate U ~ U(0,1)&lt;br&gt;<br /> 2. Generate &lt;math&gt;Y=-1/(\lambda)*log(1-u)&lt;/math&gt; where &lt;math&gt;Y ~ \sim Exp(\lambda) &lt;/math&gt;&lt;br&gt;<br /> 3. Set &lt;math&gt;X=[Y]+1&lt;/math&gt;&lt;br&gt;<br /> <br /> Note: &lt;br /&gt;<br /> If X~Unif (0,1), Y= floor(3X) = [3X]-&gt; Y ~ DU[0,2] (DU means discrete uniform)&lt;br /&gt;<br /> <br /> If X~Unif (0,1), Y= floor(5U)-2 = [5U]-2 -&gt; Y~ DU[-2,2] <br /> &lt;br&gt;<br /> <br /> There is also another intuitive method:&lt;br&gt;<br /> 1. Draw U ~ U(0,1)&lt;br&gt;<br /> 2. i = 1, Pi = 1 - (1 - P)^i. &lt;br&gt;<br /> 3. If u &lt;= Pi = 1 - (1 - P)^i, set X = i.<br /> Else, i = i + 1. &lt;br&gt;<br /> <br /> ===Poisson===<br /> <br /> This distribution models the number of times and event occurs in a given time period<br /> <br /> X~Poi&lt;math&gt;(\lambda)&lt;/math&gt; &lt;br&gt;<br /> X is the maximum number of iid Exp(&lt;math&gt;\lambda&lt;/math&gt;) whose sum is less than or equal to 1.&lt;br&gt;<br /> &lt;math&gt;X = \max\{n: \sum\limits_{i=1}^n Y_i \leq 1, Y_i \sim Exp(\lambda)\}&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt; = \max\{n: \sum\limits_{i=1}^n \frac{-1}{\lambda} log(U_i)&lt;=1 , U_i \sim U[0,1]\}&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt; = \max\{n: \prod\limits_{i=1}^n U_i &gt;= e^{-\lambda}, U_i \sim U[0,1]\}&lt;/math&gt;&lt;br&gt;<br /> <br /> '''Algorithm'''&lt;br\&gt;<br /> *1. Set n=1, a=1&lt;br\&gt;<br /> *2. &lt;math&gt;U_n&lt;/math&gt; ~ &lt;math&gt;U[0,1]&lt;/math&gt; and set &lt;math&gt;a=aU_n&lt;/math&gt;&lt;br\&gt;<br /> *3. If &lt;math&gt;a \geq e^{-\lambda}&lt;/math&gt; then: n=n+1 and go to Step 2. Else set X=n-1.<br /> <br /> An alternate way to write an algorithm for Poisson is as followings:<br /> <br /> 1) x = 0, F = P(X=0) = e^-λ = p<br /> <br /> <br /> 2) Generate U ~ U(0,1)<br /> <br /> <br /> 3) If U &lt; F, then output x<br /> <br /> <br /> 4) Else p = (λ/(x+1)) * p<br /> <br /> F = F + p<br /> <br /> x = x+1<br /> <br /> 5) else go to step 2<br /> <br /> Acknowledgments: from Spring 2012 stat 340 coursenotes<br /> <br /> == Class 13 - Tuesday June 18th 2013 ==<br /> '''Markov Chain'''<br /> &lt;br&gt;N-Step Transition Matrix: a matrix &lt;math&gt; P_n &lt;/math&gt; whose elements are the probability of moving from state i to state j in n steps. &lt;br/&gt;<br /> &lt;math&gt;P_n (i,j)=Pr⁡(X_{m+n}=j|X_m=i)&lt;/math&gt; &lt;br/&gt;<br /> <br /> Explanation: (with an example) Suppose there 10 states { 1, 2, ..., 10}, and suppose you are on state 2, then P&lt;sub&gt;8&lt;/sub&gt;(2, 5) represent the probability of moving from state 2 to state 5 in 8 steps.<br /> <br /> One-step transition probability:&lt;br/&gt;<br /> The probability of X&lt;sub&gt;n+1&lt;/sub&gt; being in state j given that X&lt;sub&gt;n&lt;/sub&gt; is in state i is called the <br /> one-step transition probability and is denoted by P&lt;sub&gt;i,j&lt;/sub&gt;&lt;sup&gt;n,n+1&lt;/sup&gt;. That is &lt;br/&gt;<br /> P&lt;sub&gt;i,j&lt;/sub&gt;&lt;sup&gt;n,n+1&lt;/sup&gt; = Pr(X&lt;sub&gt;n+1&lt;/sub&gt; =j/X&lt;sub&gt;n&lt;/sub&gt; =i)<br /> <br /> Example from previous class: &lt;br/&gt;<br /> <br /> &lt;math&gt; P= \left [ \begin{matrix}<br /> 0.7 &amp; 0.3 \\<br /> 0.2 &amp; 0.8<br /> \end{matrix} \right] &lt;/math&gt;<br /> <br /> The two step transition probability matrix is:<br /> <br /> &lt;math&gt; P P= \left [ \begin{matrix}<br /> 0.7 &amp; 0.3 \\<br /> 0.2 &amp; 0.8<br /> \end{matrix} \right] \left [ \begin{matrix}<br /> 0.7 &amp; 0.3 \\<br /> 0.2 &amp; 0.8<br /> \end{matrix} \right] &lt;/math&gt;=&lt;math&gt;\left [ \begin{matrix}<br /> 0.7(0.7)+0.3(0.2) &amp; 0.7(0.3)+0.3(0.8) \\<br /> 0.2(0.7)+0.8(0.2) &amp; 0.2(0.3)+0.8(0.8)<br /> \end{matrix} \right] &lt;/math&gt;=&lt;math&gt;\left [ \begin{matrix}<br /> 0.55 &amp; 0.45 \\<br /> 0.3 &amp; 0.7<br /> \end{matrix} \right] &lt;/math&gt;&lt;br\&gt;<br /> <br /> Interpretation:&lt;br\&gt;<br /> - If at time 0 we are in state 1, then the probability of us being in state 1 at time 2 is 0.55 and 0.45 for state 2.&lt;br\&gt;<br /> - If at time 0 we are in state 2, then the probability of us being in state 1 at time 2 is 0.3 and 0.7 for state 2.&lt;br\&gt;<br /> <br /> &lt;math&gt;P_2 = P_1 P_1 &lt;/math&gt;&lt;br\&gt;<br /> <br /> &lt;math&gt;P_3 = P_1 P_2 &lt;/math&gt;&lt;br\&gt;<br /> <br /> &lt;math&gt;P_n = P_1 P_(n-1) &lt;/math&gt;&lt;br\&gt;<br /> <br /> &lt;math&gt;P_n = P_1^n &lt;/math&gt;&lt;br\&gt;<br /> <br /> <br /> <br /> The two-step transition probability of moving from state a to state a: <br /> &lt;br/&gt;<br /> &lt;math&gt;P_2 (a,a)=Pr⁡ (X_{m+2}=a| X_m=a)=Pr⁡(X_{m+1}=a| X_m=a)Pr⁡(X_{m+2}=a|X_{m+1}=a)+ Pr⁡(X_{m+1}=b|X_m=a)Pr⁡(X_{m+2}=a|X_{m+1}=b)&lt;/math&gt; &lt;br/&gt;<br /> <br /> &lt;math&gt; =0.7(0.7)+0.3(0.2)=0.55 &lt;/math&gt;&lt;br/&gt;<br /> <br /> Another Example: &lt;br/&gt;<br /> <br /> &lt;math&gt; P= \left [ \begin{matrix}<br /> 1 &amp; 0 \\<br /> 0.7 &amp; 0.3<br /> \end{matrix} \right] &lt;/math&gt;<br /> <br /> The two step transition probability matrix is:<br /> <br /> &lt;math&gt; P P= \left [ \begin{matrix}<br /> 1 &amp; 0 \\<br /> 0.7 &amp; 0.3<br /> \end{matrix} \right] \left [ \begin{matrix}<br /> 1 &amp; 0 \\<br /> 0.7 &amp; 0.3<br /> \end{matrix} \right] &lt;/math&gt;=&lt;math&gt;\left [ \begin{matrix}<br /> 1(1)+ 0(0.7) &amp; 1(0) + 0(0.3) \\<br /> 1(0.7)+0.7(0.3) &amp; 0(0.7)+0.3(0.3)<br /> \end{matrix} \right] &lt;/math&gt;=&lt;math&gt;\left [ \begin{matrix}<br /> 1 &amp; 0 \\<br /> 0.91 &amp; 0.09<br /> \end{matrix} \right] &lt;/math&gt;&lt;br\&gt;<br /> <br /> This is the two-step transition matrix.<br /> <br /> === n-step transition matrix ===<br /> The elements of matrix P&lt;sub&gt;n&lt;/sub&gt; （i.e. the ij&lt;sub&gt;th&lt;/sub&gt; entry P&lt;sub&gt;ij&lt;/sub&gt;) is the probability of moving to state j from state i in n steps<br /> <br /> In general &lt;math&gt;P_n = P^n&lt;/math&gt; with &lt;math&gt;P_n(i,j) \geq 0&lt;/math&gt; and &lt;math&gt;\sum_{j} P_n(i,j) = 1&lt;/math&gt;&lt;br /&gt;<br /> Note: &lt;math&gt;P_2 = P_1\times P_1; P_n = P^n&lt;/math&gt;&lt;br /&gt;<br /> The equation above is a special case of the Chapman-Kolmogorov equations.&lt;br /&gt;<br /> It is true because of the Markov property or the memoryless property of Markov chains, where the probabilities of going forward to the next state &lt;br /&gt;<br /> only depends on your current state, not your previous states. By intuition, we can multiply the 1-step transition &lt;br /&gt;<br /> matrix n-times to get a n-step transition matrix.&lt;br /&gt;<br /> <br /> Example: We can see how &lt;math&gt;P_n = P^n&lt;/math&gt; from the following:<br /> &lt;br/&gt;<br /> &lt;math&gt;\vec{\mu_1}=\vec{\mu_0}\cdot P&lt;/math&gt; &lt;br/&gt;<br /> &lt;math&gt;\vec{\mu_2}=\vec{\mu_1}\cdot P&lt;/math&gt; &lt;br/&gt;<br /> &lt;math&gt;\vec{\mu_3}=\vec{\mu_2}\cdot P&lt;/math&gt; &lt;br/&gt;<br /> Therefore, <br /> &lt;br/&gt;<br /> &lt;math&gt;\vec{\mu_3}=\vec{\mu_0}\cdot P^3<br /> &lt;/math&gt; &lt;br/&gt;<br /> <br /> &lt;math&gt;P_n(i,j)&lt;/math&gt; is called n-steps transition probability. &lt;br&gt;<br /> &lt;math&gt;\vec{\mu_0} &lt;/math&gt; is called the '''initial distribution'''. &lt;br&gt;<br /> &lt;math&gt;\vec{\mu_n} = \vec{\mu_0}* P^n &lt;/math&gt; &lt;br /&gt;<br /> <br /> Example with Markov Chain:<br /> Consider a two-state Markov chain {&lt;math&gt;X_t; t = 0, 1, 2,...&lt;/math&gt;} with states {1,2} and transition probability matrix<br /> <br /> &lt;math&gt; P= \left [ \begin{matrix}<br /> 1/2 &amp; 1/2 \\<br /> 1/3 &amp; 2/3<br /> \end{matrix} \right] &lt;/math&gt;<br /> <br /> Given &lt;math&gt; X_0 = 1 &lt;/math&gt;. Compute the following:<br /> <br /> a)&lt;math&gt; P(X_1=1 | X_0=1) = P(1,1) = 1/2 &lt;/math&gt;<br /> <br /> b)&lt;math&gt; P(X_2=1, X_1=1 |X_0=1) = P(X_2=1|X_1=1)*P(X_1=1|X_0=1)= 1/2 * 1/2 = 1/4 &lt;/math&gt;<br /> <br /> c)&lt;math&gt; P(X_2=1|X_0=1)= P_2(1,1) = 5/12 &lt;/math&gt;<br /> <br /> d)&lt;math&gt; P^2=P*P= \left [ \begin{matrix}<br /> 5/12 &amp; 7/12 \\<br /> 7/18 &amp; 11/18<br /> \end{matrix} \right] &lt;/math&gt;<br /> <br /> === Marginal Distribution of Markov Chain ===<br /> We represent the probability of all states at time t with a vector &lt;math&gt;\underline{\mu_t}&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;\underline{\mu_t}~=(\mu_t(1), \mu_t(2),...\mu_t(n))&lt;/math&gt; where &lt;math&gt;\underline{\mu_t(1)}&lt;/math&gt; is the probability of being on state 1 at time t.&lt;br/&gt;<br /> and in general, &lt;math&gt;\underline{\mu_t(i)}&lt;/math&gt; shows the probability of being on state i at time t.&lt;br/&gt;<br /> For example, if there are two states a and b, then &lt;math&gt;\underline{\mu_5}&lt;/math&gt;=(0.1, 0.9) means that the chance of being in state a at time 5 is 0.1 and the chance of being on state b at time 5 is 0.9. &lt;br/&gt;<br /> If we generate a chain for many times, the frequency of states at each time shows marginal distribution of the chain at that time. &lt;br/&gt;<br /> The vector &lt;math&gt;\underline{\mu_0}&lt;/math&gt; is called the initial distribution. &lt;br/&gt;<br /> <br /> &lt;math&gt; P^2~=P\cdot P &lt;/math&gt; (as verified above) <br /> <br /> In general,<br /> &lt;math&gt; P^n~= \Pi_{i=1}^{n} P&lt;/math&gt; (P multiplied n times)&lt;br/&gt;<br /> &lt;math&gt;\mu_n~=\mu_0 P^n&lt;/math&gt;&lt;br/&gt;<br /> where &lt;math&gt;\mu_0&lt;/math&gt; is the initial distribution,<br /> and &lt;math&gt;\mu_{m+n}~=\mu_m P^n&lt;/math&gt;&lt;br/&gt;<br /> N can be negative, if P is invertible.<br /> <br /> &lt;br/&gt;<br /> 0 a a b a&lt;br/&gt;<br /> 1 a b a a&lt;br/&gt;<br /> 2 b a a b&lt;br/&gt;<br /> 3 b a b b&lt;br/&gt;<br /> 4 a a a b&lt;br/&gt;<br /> 5 a b b a&lt;br/&gt;<br /> &lt;math&gt;\mu_4~=(3/4, 1/4)&lt;/math&gt;&lt;br/&gt;<br /> <br /> if we simulate a chain many times, frequency of states at time t show the marginal distribution at time t &lt;br /&gt;<br /> <br /> <br /> <br /> <br /> Marginal Distribution<br /> <br /> &lt;math&gt;\mu_1~ = \mu_0P&lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt;\mu_2~ = \mu_1P = \mu_0PP = \mu_0P^2&lt;/math&gt; &lt;br&gt;<br /> <br /> In general, &lt;math&gt;\mu_n~ = \mu_0P^n&lt;/math&gt;&lt;br /&gt;<br /> Property: If &lt;math&gt;\mu_n~\neq\mu_t~&lt;/math&gt;(for any t less than n), then we say P does not converge. &lt;br /&gt;<br /> <br /> <br /> <br /> <br /> ==== Stationary Distribution ====<br /> <br /> <br /> <br /> <br /> &lt;math&gt;\pi&lt;/math&gt; is stationary distribution of the chain if &lt;math&gt;\pi&lt;/math&gt;P = &lt;math&gt;\pi&lt;/math&gt;<br /> <br /> where &lt;math&gt;\pi&lt;/math&gt; is a probability vector &lt;math&gt;\pi&lt;/math&gt;=(&lt;math&gt;\pi&lt;/math&gt;&lt;sub&gt;i&lt;/sub&gt; | &lt;math&gt;i \in X&lt;/math&gt;) such that all the entries are nonnegative and sum to 1. It is the eigenvector in this case.<br /> <br /> In other words, if X''&lt;sub&gt;0&lt;/sub&gt;'' is draw from &lt;math&gt;\pi&lt;/math&gt;. Then marginally, X''&lt;sub&gt;n&lt;/sub&gt;'' is also drawn from the same distribution &lt;math&gt;\pi&lt;/math&gt; for every n≥0.<br /> <br /> The above conditions are used to find the stationary distribution<br /> In matlab, we could use &lt;math&gt;P^n&lt;/math&gt; to find the stationary distribution.(n is usually larger than 100)&lt;br/&gt;<br /> <br /> <br /> '''Comments:'''&lt;br/&gt;<br /> As n gets bigger and bigger, &lt;math&gt;\mu_n&lt;/math&gt; will possibly stop changing, so the quantity &lt;math&gt;\pi&lt;/math&gt; &lt;sub&gt;i&lt;/sub&gt; can also be interpreted as the limiting probability that the chain is in the state &lt;math&gt;j&lt;/math&gt;<br /> <br /> Comments: &lt;br/&gt;<br /> 1. &lt;math&gt;\pi&lt;/math&gt; may not exist and even if it exists, it may not always be unique. &lt;br/&gt;<br /> 2. If &lt;math&gt;\pi&lt;/math&gt; exists and is unique, then &lt;math&gt;\pi&lt;/math&gt;&lt;sub&gt;i&lt;/sub&gt; is called the long-run proportion of the process in state i and the stationary distribution is also the limiting distribution of the process.&lt;br/&gt;<br /> <br /> How long do you have to wait until you reach a steady sate?<br /> Ans: There is not clear way to find that out <br /> <br /> How do you increase the time it takes to reach the steady state?<br /> Ans: Make the probabilities of transition much smaller, to reach from state 0 to state 1 and vice-versa p=0.005. And make the probabilities of staying in the same state extremely high. To stay in state 0 or state 1 p=0.995, then the matrix is in a &quot;sticky state&quot;<br /> <br /> <br /> EXAMPLE : Random Walk on the cycle S={0,1,2}<br /> <br /> &lt;math&gt;P^2 = \left[ \begin{array}{ccc}<br /> 2pq &amp; q^2 &amp; p^2 \\<br /> p^2 &amp; 2pq &amp; q^2 \\<br /> q^2 &amp; p^2 &amp; 2pq \end{array} \right]&lt;/math&gt;<br /> <br /> Suppose&lt;br/&gt;<br /> &lt;math&gt;P(x_0=0)=\frac{1}{4}&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;P(x_0=1)=\frac{1}{2}&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;P(x_0=2)=\frac{1}{4}&lt;/math&gt;&lt;br/&gt;<br /> Thus&lt;br/&gt;<br /> &lt;math&gt;\pi_0 = \left[ \begin{array}{c} \frac{1}{4} \\ \frac{1}{2} \\ \frac{1}{4} \end{array} \right]&lt;/math&gt;&lt;br/&gt;<br /> so&lt;br/&gt;<br /> &lt;math&gt;\,\pi^2 = \pi_0 * P^2 &lt;/math&gt;<br /> &lt;math&gt;= \left[ \begin{array}{c} \frac{1}{4} \\ \frac{1}{2} \\ \frac{1}{4} \end{array} \right] * \left[ \begin{array}{ccc}<br /> 2pq &amp; q^2 &amp; p^2 \\<br /> p^2 &amp; 2pq &amp; q^2 \\<br /> q^2 &amp; p^2 &amp; 2pq \end{array} \right]&lt;/math&gt;<br /> &lt;math&gt;= \left[ \begin{array}{c} \frac{1}{2}pq + \frac{1}{2}p^2+\frac{1}{4}q^2 \\ \frac{1}{4}q^2+pq+\frac{1}{4}p^2 \\ \frac{1}{4}p^2+\frac{1}{2}q^2+\frac{1}{2}pq\end{array} \right]&lt;/math&gt;<br /> <br /> ==== MatLab Code ====<br /> &lt;pre style='font-size:14px'&gt;<br /> <br /> In Matlab, you can find the stationary distribution by:<br /> <br /> &gt;&gt; p=[.7 .3;.2 .8] % Input the matrix P<br /> <br /> p =<br /> <br /> 0.7000 0.3000<br /> 0.2000 0.8000<br /> <br /> &gt;&gt; p^2 % one state to another state by 2 steps transition<br /> <br /> ans =<br /> <br /> 0.5500 0.4500<br /> 0.3000 0.7000<br /> <br /> &gt;&gt; mu=[.9 .1] <br /> <br /> mu =<br /> <br /> 0.9000 0.1000<br /> <br /> &gt;&gt; mu*p % enter mu=mu*P, repeat multiple times until the value of the vector mu remains unchanged<br /> <br /> ans =<br /> <br /> 0.6500 0.3500<br /> <br /> &gt;&gt; mu=mu*p<br /> <br /> mu =<br /> <br /> 0.4002 0.5998<br /> <br /> &gt;&gt; mu=mu*p %The vector mu will be your stationary distribution<br /> <br /> mu =<br /> <br /> 0.4000 0.6000<br /> <br /> <br /> &gt;&gt; p^100 % it is limiting distribution of chain which finally gives a stable matrix<br /> <br /> ans =<br /> <br /> 0.4000 0.6000<br /> 0.4000 0.6000<br /> <br /> <br /> &lt;/pre&gt;<br /> The definition of stationary distribution is that &lt;math&gt;\pi&lt;/math&gt; is the stationary distribution of the chain if &lt;math&gt;\pi=\pi~P&lt;/math&gt;, where &lt;math&gt;\pi&lt;/math&gt; is a probability vector. For every n&lt;math&gt;&gt;=&lt;/math&gt;0.<br /> <br /> However, just because X&lt;sub&gt;''n''&lt;/sub&gt; ~ &lt;math&gt;\pi&lt;/math&gt; for every n&lt;math&gt;&gt;=&lt;/math&gt;0 does ''not'' mean every state is independently identically distributed.<br /> <br /> '''Limiting distribution''' of the chain refers the transition matrix that reaches the stationary state. If the lim(n-&gt; infinite)P^n -&gt; c, where c is a constant, then, we say this Markov chain is coverage; otherwise, it's not coverage.<br /> <br /> Example: Find the stationary distribution of P= &lt;math&gt;\left[ {\begin{array}{ccc}<br /> 1/3 &amp; 1/3 &amp; 1/3 \\<br /> 1/4 &amp; 3/4 &amp; 0 \\<br /> 1/2 &amp; 0 &amp; 1/2 \end{array} } \right]&lt;/math&gt;<br /> <br /> Solution:<br /> &lt;math&gt;\pi=\left[ {\begin{array}{ccc}<br /> \pi_0 &amp; \pi_1 &amp; \pi_2 \end{array} } \right]&lt;/math&gt;<br /> <br /> Using the stationary distribution property &lt;math&gt;\pi=\pi~P&lt;/math&gt; we get, &lt;br&gt;<br /> &lt;math&gt;\pi_0=\frac{1}{3}\pi_0+\frac{1}{4}\pi_1+\frac{1}{2}\pi_2 &lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\pi_1=\frac{1}{3}\pi_0+\frac{3}{4}\pi_1+0\pi_2 &lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\pi_2=\frac{1}{3}\pi_0+0\pi_1+\frac{1}{2}\pi_2 &lt;/math&gt;&lt;br&gt;<br /> <br /> And since &lt;math&gt;\pi&lt;/math&gt; is a probability vector, &lt;br&gt;<br /> &lt;math&gt; \pi_{0}~ + \pi_{1} + \pi_{2} = 1 &lt;/math&gt;<br /> <br /> Solving the 4 equations for the 3 unknowns gets, &lt;br&gt;<br /> &lt;math&gt;\pi_{0}~=1/3&lt;/math&gt;, &lt;math&gt;\pi_{1}~=4/9&lt;/math&gt;, and &lt;math&gt;\pi_{2}~=2/9&lt;/math&gt; &lt;br&gt;<br /> Therefore &lt;math&gt;\pi=\left[ {\begin{array}{ccc}<br /> 1/3 &amp; 4/9 &amp; 2/9 \end{array} } \right]&lt;/math&gt;<br /> <br /> Example 2: Find the stationary distribution of P= &lt;math&gt;\left[ {\begin{array}{ccc}<br /> 1/3 &amp; 1/3 &amp; 1/3 \\<br /> 1/4 &amp; 1/2 &amp; 1/4 \\<br /> 1/6 &amp; 1/3 &amp; 1/2 \end{array} } \right]&lt;/math&gt;<br /> <br /> Solution:<br /> &lt;math&gt;\pi=\left[ {\begin{array}{ccc}<br /> \pi_0 &amp; \pi_1 &amp; \pi_2 \end{array} } \right]&lt;/math&gt;<br /> <br /> Using the stationary distribution property &lt;math&gt;\pi=\pi~P&lt;/math&gt; we get, &lt;br&gt;<br /> &lt;math&gt;\pi_0=\frac{1}{3}\pi_0+\frac{1}{4}\pi_1+\frac{1}{6}\pi_2 &lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\pi_1=\frac{1}{3}\pi_0+\frac{1}{2}\pi_1+\frac{1}{3}\pi_2 &lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\pi_2=\frac{1}{3}\pi_0+\frac{1}{4}\pi_1+\frac{1}{2}\pi_2 &lt;/math&gt;&lt;br&gt;<br /> <br /> And since &lt;math&gt;\pi&lt;/math&gt; is a probability vector, &lt;br&gt;<br /> &lt;math&gt; \pi_{0}~ + \pi_{1} + \pi_{2} = 1 &lt;/math&gt;<br /> <br /> Solving the 4 equations for the 3 unknowns gets, &lt;br&gt;<br /> &lt;math&gt;\pi_{0}=\frac {6}{25}&lt;/math&gt;, &lt;math&gt;\pi_{1}~=\frac {2}{5}&lt;/math&gt;, and &lt;math&gt;\pi_{2}~=\frac {9}{25}&lt;/math&gt; &lt;br&gt;<br /> Therefore &lt;math&gt;\pi=\left[ {\begin{array}{ccc}<br /> \frac {6}{25} &amp; \frac {2}{5} &amp; \frac {9}{25} \end{array} } \right]&lt;/math&gt;<br /> <br /> The above two examples are designed to solve for the stationary distribution of the matrix P however they also give us the limiting distribution of the matrices as we have mentioned earlier that the stationary distribution is equivalent to the limiting distribution.<br /> <br /> '''Alternate Method of Computing the Stationary Distribution''' &lt;br&gt;<br /> <br /> Recall that if &lt;math&gt;\lambda v = A v&lt;/math&gt;, then &lt;math&gt;\lambda&lt;/math&gt; is the eigenvalue of &lt;math&gt;A&lt;/math&gt; corresponding to the eigenvector &lt;math&gt;v&lt;/math&gt;&lt;br&gt;<br /> <br /> By definition of stationary distribution, &lt;math&gt;\pi = \pi P&lt;/math&gt;&lt;br&gt;<br /> Taking the transpose, &lt;math&gt;\pi^T = (\pi P)^T &lt;/math&gt;&lt;br&gt;<br /> then &lt;math&gt;I \pi^T = P^T \pi^T \Rightarrow (P^T-I) \pi^T = 0 &lt;/math&gt;&lt;br&gt;<br /> So &lt;math&gt;\pi^T &lt;/math&gt; is an eigenvector of &lt;math&gt;P^T&lt;/math&gt; with corresponding eigenvalue 1. &lt;br&gt;<br /> <br /> the transpose method to calculate the pi matrix probability.<br /> <br /> It is thus possible to compute the stationary distribution by taking the eigenvector of the transpose of the transition matrix corresponding to 1, and normalize it such that all elements are non-negative and sum to one so that the elements satisfy the definition of a stationary distribution. The transformed vector is still an eigenvector since a linear transformation of an eigenvector is still within the eigenspace. Taking the transpose of this transformed eigenvector gives the stationary distribution. &lt;br&gt; <br /> <br /> <br /> <br /> <br /> &lt;span style=&quot;background:#F5F5DC&quot;&gt;<br /> Generating Random Initial distribution&lt;br&gt;<br /> &lt;math&gt;\mu~=rand(1,n)&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\mu~=\frac{\mu}{\Sigma(\mu)}&lt;/math&gt;&lt;/span&gt;<br /> <br /> &lt;span style=&quot;background:#F5F5DC&quot;&gt;<br /> Doubly Stochastic Matrices&lt;br&gt;&lt;/span&gt;<br /> We say that the transition matrix &lt;math&gt;\, P=(p_{ij})&lt;/math&gt; is doubly stochastic if both rows and columns sum to 1, i.e.,&lt;br&gt;<br /> &lt;math&gt;\, \sum_{i} p_{ji} = \sum_{j} p_{ij} = 1 &lt;/math&gt;&lt;br&gt;<br /> It is easy to show that the stationary distribution of an nxn doubly stochastic matrix P is:&lt;br&gt;<br /> &lt;math&gt; (\frac{1}{n}, \ldots , \frac{1}{n}) &lt;/math&gt;<br /> <br /> === Properties of Markov Chain ===<br /> <br /> A Markov chain is a random process usually characterized as '''memoryless''': the next state depends only on the current state and not on the sequence of events that preceded it. This specific kind of &quot;memorylessness&quot; is called the Markov property. Markov chains have many applications as statistical models of real-world processes.<br /> <br /> 1. Reducibility &lt;br&gt;<br /> State &lt;math&gt;j&lt;/math&gt; is said to be accessible from State &lt;math&gt;i&lt;/math&gt; (written &lt;math&gt;i \rightarrow j&lt;/math&gt;) if a system started in State &lt;math&gt;i&lt;/math&gt; has a non-zero probability of transitioning into State &lt;math&gt;j&lt;/math&gt; at some point. Formally, State &lt;math&gt;j&lt;/math&gt; is accessible from State &lt;math&gt;i&lt;/math&gt; if there exists an integer &lt;math&gt;n_{ij} \geq 0&lt;/math&gt; such that &lt;br&gt;<br /> &lt;math&gt;P(X_{n_{ij}} =j \vert X_0 =i) &gt; 0&lt;/math&gt;&lt;br&gt;<br /> <br /> This integer is allowed to be different for each pair of states, hence the subscripts in &lt;math&gt;n_{ij}&lt;/math&gt;. By allowing n to be zero, every state is defined to be accessible from itself.&lt;br /&gt;<br /> <br /> 2. Periodicity &lt;br&gt;<br /> State &lt;math&gt;i&lt;/math&gt; has period &lt;math&gt;k&lt;/math&gt; if any return to State &lt;math&gt;i&lt;/math&gt; must occur in multiples of &lt;math&gt;k&lt;/math&gt; time steps. Formally, the period of a state is defined as &lt;br&gt;<br /> &lt;math&gt;k= \gcd\{n:P(X_n =j \vert X_0 =i)&gt;0\} &lt;/math&gt;&lt;br /&gt;<br /> <br /> 3. Recurrence &lt;br&gt;<br /> State &lt;math&gt;i&lt;/math&gt; is said to be transient if, given that we start in State &lt;math&gt;i&lt;/math&gt;, there is a non-zero probability that we will never return to &lt;math&gt;i&lt;/math&gt;. Formally, let the random variable &lt;math&gt;T_i&lt;/math&gt; be the first return time to State &lt;math&gt;i&lt;/math&gt; (the &quot;hitting time&quot;): &lt;br&gt;<br /> &lt;math&gt;T_i = \min\{n \geq 1:X_n=i \vert X_0=i\}&lt;/math&gt;&lt;br /&gt;<br /> <br /> (The properties are from<br /> http://www2.math.uu.se/~takis/L/McRw/mcrw.pdf)<br /> <br /> CHAPMAN-KOLMOGOROV EQUATION<br /> For all &lt;math&gt;n&lt;/math&gt; and &lt;math&gt;m&lt;/math&gt;, and any state &lt;math&gt;i&lt;/math&gt; and &lt;math&gt;j&lt;/math&gt;,<br /> &lt;math&gt;P^{n+m}(X_n+m = j \vert X_0 =i)= \sum_{k} P^n(X_1 = k \vert X_0 = i)*P^m(X_1 = j \vert X_0 =k)&lt;/math&gt;<br /> <br /> == Class 14 - Thursday June 20th 2013 ==<br /> <br /> Example: Find the stationary distribution of &lt;math&gt; P= \left[ {\begin{array}{ccc}<br /> \frac{1}{3} &amp; \frac{1}{3} &amp; \frac{1}{3} \\[6pt]<br /> \frac{1}{4} &amp; \frac{3}{4} &amp; 0 \\[6pt]<br /> \frac{1}{2} &amp; 0 &amp; \frac{1}{2} \end{array} } \right]&lt;/math&gt;<br /> <br /> &lt;math&gt;\displaystyle \pi=\pi p&lt;/math&gt;<br /> <br /> Solve the system of linear equations to find a stationary distribution<br /> <br /> &lt;math&gt;\displaystyle \pi=(\frac{1}{3},\frac{4}{9}, \frac{2}{9})&lt;/math&gt;<br /> <br /> Note that &lt;math&gt;\displaystyle \pi=\pi p&lt;/math&gt; looks similar to eigenvectors/values &lt;math&gt;\displaystyle \lambda vec{u}=A vec{u}&lt;/math&gt;<br /> <br /> &lt;math&gt;\pi&lt;/math&gt; can be considered as an eigenvector of P with eigenvalue = 1. But note that the vector &lt;math&gt;vec{u}&lt;/math&gt; is a column vector and o we need to transform our &lt;math&gt;\pi&lt;/math&gt; into a column vector.<br /> <br /> &lt;math&gt;=&gt; \pi&lt;/math&gt;&lt;sup&gt;T&lt;/sup&gt;= P&lt;sup&gt;T&lt;/sup&gt;&lt;math&gt;\pi&lt;/math&gt;&lt;sup&gt;T&lt;/sup&gt;&lt;br/&gt;<br /> Then &lt;math&gt;\pi&lt;/math&gt;&lt;sup&gt;T&lt;/sup&gt; is an eigenvector of P&lt;sup&gt;T&lt;/sup&gt; with eigenvalue = 1. &lt;br /&gt;<br /> MatLab tips:[V D]=eig(A), where D is a diagonal matrix of eigenvalues and V is a matrix of eigenvectors of matrix A&lt;br /&gt;<br /> ==== MatLab Code ====<br /> &lt;pre style='font-size:14px'&gt;<br /> <br /> P = [1/3 1/3 1/3; 1/4 3/4 0; 1/2 0 1/2]<br /> <br /> pii = [1/3 4/9 2/9]<br /> <br /> [vec val] = eig(P') %% P' is the transpose of matrix P<br /> <br /> vec(:,1) = [-0.5571 -0.7428 -0.3714] %% this is in column form<br /> <br /> a = -vec(:,1)<br /> <br /> &gt;&gt; a = <br /> [0.5571 0.7428 0.3714] <br /> <br /> %% a is in column form<br /> <br /> %% Since we want this vector a to sum to 1, we have to scale it<br /> <br /> b = a/sum(a)<br /> <br /> &gt;&gt; b =<br /> [0.3333 0.4444 0.2222] <br /> <br /> %% b is also in column form<br /> <br /> %% Observe that b' = pii<br /> <br /> &lt;/pre&gt;<br /> &lt;/br&gt;<br /> ==== Limiting distribution ====<br /> A Markov chain has limiting distribution &lt;math&gt;\pi&lt;/math&gt; if<br /> <br /> &lt;math&gt;\lim_{n\to \infty} P^n= \left[ {\begin{array}{ccc}<br /> \pi_1 \\<br /> \vdots \\<br /> \pi_n \\<br /> \end{array} } \right]&lt;/math&gt;<br /> <br /> That is &lt;math&gt;\pi_j=\lim[P^n]_{ij}&lt;/math&gt; exists and is independent of i.&lt;br/&gt; <br /> <br /> A Markov Chain is convergent if and only if its limiting distribution exists. &lt;br/&gt;<br /> <br /> If the limiting distribution &lt;math&gt;\pi&lt;/math&gt; exists, it must be equal to the stationary distribution.&lt;br/&gt;<br /> <br /> This convergence means that,in the long run(n to infinity),the probability of finding the &lt;br/&gt;<br /> Markov chain in state j is approximately &lt;math&gt;\pi_j&lt;/math&gt; no matter in which state &lt;br/&gt;<br /> the chain began at time 0. &lt;br/&gt;<br /> <br /> '''Example:'''<br /> For a transition matrix &lt;math&gt; P= \left [ \begin{matrix}<br /> 0 &amp; 1 &amp; 0 \\[6pt]<br /> 0 &amp; 0 &amp; 1 \\[6pt]<br /> 1 &amp; 0 &amp; 0 \\[6pt]<br /> \end{matrix} \right] &lt;/math&gt;<br /> , find stationary distribution.&lt;br/&gt;<br /> We have:&lt;br/&gt;<br /> &lt;math&gt;0\times \pi_0+0\times \pi_1+1\times \pi_2=\pi_0&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;1\times \pi_0+0\times \pi_1+0\times \pi_2=\pi_1&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;0\times \pi_0+1\times \pi_1+0\times \pi_2=\pi_2&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;\,\pi_0+\pi_1+\pi_2=1&lt;/math&gt;&lt;br/&gt;<br /> this gives &lt;math&gt;\pi = \left [ \begin{matrix}<br /> \frac{1}{3} &amp; \frac{1}{3} &amp; \frac{1}{3} \\[6pt]<br /> \end{matrix} \right] &lt;/math&gt; &lt;br/&gt;<br /> However, there does not exist a limiting distribution. &lt;math&gt; \pi &lt;/math&gt; is stationary but is not limiting.&lt;br/&gt;<br /> &lt;br/&gt;<br /> In general, there are chains with stationery distributions that don't converge, this means that they have stationary distribution but are not limiting.&lt;br/&gt;<br /> <br /> === MatLab Code ===<br /> &lt;pre style='font-size:14px'&gt;<br /> MATLAB<br /> &gt;&gt; P=[0, 1, 0;0, 0, 1; 1, 0, 0]<br /> <br /> P =<br /> <br /> 0 1 0<br /> 0 0 1<br /> 1 0 0<br /> <br /> &gt;&gt; pii=[1/3, 1/3, 1/3]<br /> <br /> pii =<br /> <br /> 0.3333 0.3333 0.3333<br /> <br /> &gt;&gt; pii*P<br /> <br /> ans =<br /> <br /> 0.3333 0.3333 0.3333<br /> <br /> &gt;&gt; P^1000<br /> <br /> ans =<br /> <br /> 0 1 0<br /> 0 0 1<br /> 1 0 0<br /> <br /> &gt;&gt; P^10000<br /> <br /> ans =<br /> <br /> 0 1 0<br /> 0 0 1<br /> 1 0 0<br /> <br /> &gt;&gt; P^10002<br /> <br /> ans =<br /> <br /> 1 0 0<br /> 0 1 0<br /> 0 0 1<br /> <br /> &gt;&gt; P^10003<br /> <br /> ans =<br /> <br /> 0 1 0<br /> 0 0 1<br /> 1 0 0<br /> <br /> &gt;&gt; %P^10000 = P^10003<br /> &gt;&gt; % This chain does not have limiting distribution, it has a stationary distribution. <br /> <br /> This chain does not converge, it has a cycle.<br /> &lt;/pre&gt;<br /> <br /> The first condition of limiting distribution is satisfied; however, the second condition where &lt;math&gt;\pi&lt;/math&gt;&lt;sub&gt;j&lt;/sub&gt; has to be independent of i (i.e. all rows of the matrix are the same) is not met.&lt;br&gt;<br /> <br /> This example shows the distinction between having a stationary distribution and convergence(having a limiting distribution).Note: &lt;math&gt;\pi=(1/3,1/3,1/3)&lt;/math&gt; is the stationary distribution as &lt;math&gt;\pi=\pi*p&lt;/math&gt;. However, upon repeatedly multiplying P by itself (repeating the step &lt;math&gt;P^n&lt;/math&gt; as n goes to infinite) one will note that the results become a cycle (of period 3) of the same sequence of matrices. The chain has a stationary distribution, but does not converge to it. Thus, there is no limiting distribution.&lt;br&gt;<br /> <br /> '''Example:'''<br /> <br /> &lt;math&gt; P= \left [ \begin{matrix}<br /> \frac{4}{5} &amp; \frac{1}{5} &amp; 0 &amp; 0 \\[6pt]<br /> \frac{1}{5} &amp; \frac{4}{5} &amp; 0 &amp; 0 \\[6pt]<br /> 0 &amp; 0 &amp; \frac{4}{5} &amp; \frac{1}{5} \\[6pt]<br /> 0 &amp; 0 &amp; \frac{1}{10} &amp; \frac{9}{10} \\[6pt]<br /> \end{matrix} \right] &lt;/math&gt;<br /> <br /> This chain converges but is not a limiting distribution as the rows are not the same and it doesn't converge to the stationary distribution.&lt;br /&gt;<br /> &lt;br /&gt;<br /> Double Stichastic Matrix: a double stichastic matrix is a matrix whose all colums sum to 1 and all rows sum to 1.&lt;br /&gt;<br /> If a given transition matrix is a double stichastic matrix with n colums and n rows, then the stationary distribution matrix has all&lt;br/&gt;<br /> elements equals to 1/n.&lt;br/&gt;<br /> &lt;br/&gt;<br /> Example:&lt;br/&gt;<br /> For a stansition matrix &lt;math&gt; P= \left [ \begin{matrix}<br /> 0 &amp; \frac{1}{2} &amp; \frac{1}{2} \\[6pt]<br /> \frac{1}{2} &amp; 0 &amp; \frac{1}{2} \\[6pt]<br /> \frac{1}{2} &amp; \frac{1}{2} &amp; 0 \\[6pt]<br /> \end{matrix} \right] &lt;/math&gt;,&lt;br/&gt;<br /> We have:&lt;br/&gt;<br /> &lt;math&gt;0\times \pi_0+\frac{1}{2}\times \pi_1+\frac{1}{2}\times \pi_2=\pi_0&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;\frac{1}{2}\times \pi_0+0\times \pi_1+\frac{1}{2}\times \pi_2=\pi_1&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;\frac{1}{2}\times \pi_0+\frac{1}{2}\times \pi_1+0\times \pi_2=\pi_2&lt;/math&gt;&lt;br/&gt;<br /> &lt;math&gt;\pi_0+\pi_1+\pi_2=1&lt;/math&gt;&lt;br/&gt;<br /> The stationary distribution is &lt;math&gt;\pi = \left [ \begin{matrix}<br /> \frac{1}{3} &amp; \frac{1}{3} &amp; \frac{1}{3} \\[6pt]<br /> \end{matrix} \right] &lt;/math&gt; &lt;br/&gt;<br /> <br /> <br /> &lt;span style=&quot;font-size:20px;color:red&quot;&gt;The following contents are problematic. Please correct it if possible.&lt;/span&gt;&lt;br /&gt;<br /> Suppose we're given that the limiting distribution &lt;math&gt; \pi &lt;/math&gt; exists for stochastic matrix P, that is, &lt;math&gt; \pi = \pi \times P &lt;/math&gt; &lt;br&gt;<br /> <br /> WLOG assume P is diagonalizable, (if not we can always consider the Jordan form and the computation below is exactly the same. &lt;br&gt;<br /> <br /> Let &lt;math&gt; P = U \Sigma U^{-1} &lt;/math&gt; be the eigenvalue decomposition of &lt;math&gt; P &lt;/math&gt;, where &lt;math&gt;\Sigma = diag(\lambda_1,\ldots,\lambda_n) ; |\lambda_i| &gt; |\lambda_j|, \forall i &lt; j &lt;/math&gt;&lt;br&gt;<br /> <br /> Suppose &lt;math&gt; \pi^T = \sum a_i u_i &lt;/math&gt; where &lt;math&gt; a_i \in \mathcal{R} &lt;/math&gt; and &lt;math&gt; u_i &lt;/math&gt; are eigenvectors of &lt;math&gt; P &lt;/math&gt; for &lt;math&gt; i = 1\ldots n &lt;/math&gt; &lt;br&gt;<br /> <br /> By definition: &lt;math&gt; \pi^k = \pi P = \pi P^k \implies \pi = \pi(U \Sigma U^{-1}) (U \Sigma U^{-1} ) \ldots (U \Sigma U^{-1}) &lt;/math&gt; &lt;br&gt;<br /> <br /> Therefore &lt;math&gt; \pi^k = \sum a_i \lambda_i^k u_i &lt;/math&gt; since &lt;math&gt; &lt;u_i , u_j&gt; = 0, \forall i\neq j &lt;/math&gt;. &lt;br&gt;<br /> <br /> Therefore &lt;math&gt; \lim_{k \rightarrow \infty} \pi^k = \lim_{k \rightarrow \infty} \lambda_i^k a_1 u_1 = u_1 &lt;/math&gt;<br /> <br /> === MatLab Code ===<br /> &lt;pre style='font-size:14px'&gt;<br /> &gt;&gt; P=[1/3, 1/3, 1/3; 1/4, 3/4, 0; 1/2, 0, 1/2] % We input a matrix P. This is the same matrix as last class. <br /> <br /> P =<br /> <br /> 0.3333 0.3333 0.3333<br /> 0.2500 0.7500 0<br /> 0.5000 0 0.5000<br /> <br /> &gt;&gt; P^2<br /> <br /> ans =<br /> <br /> 0.3611 0.3611 0.2778<br /> 0.2708 0.6458 0.0833<br /> 0.4167 0.1667 0.4167<br /> <br /> &gt;&gt; P^3<br /> <br /> ans =<br /> <br /> 0.3495 0.3912 0.2593<br /> 0.2934 0.5747 0.1319<br /> 0.3889 0.2639 0.3472<br /> <br /> &gt;&gt; P^10<br /> <br /> The example of code and an example of stand distribution, then the all the pi probability in the matrix are the same.<br /> <br /> ans =<br /> <br /> 0.3341 0.4419 0.2240<br /> 0.3314 0.4507 0.2179<br /> 0.3360 0.4358 0.2282<br /> <br /> &gt;&gt; P^100 % The stationary distribution is [0.3333 0.4444 0.2222] since values keep unchanged.<br /> <br /> ans =<br /> <br /> 0.3333 0.4444 0.2222<br /> 0.3333 0.4444 0.2222<br /> 0.3333 0.4444 0.2222<br /> <br /> <br /> &gt;&gt; [vec val]=eigs(P') % We can find the eigenvalues and eigenvectors from the transpose of matrix P.<br /> <br /> vec =<br /> <br /> -0.5571 0.2447 0.8121<br /> -0.7428 -0.7969 -0.3324<br /> -0.3714 0.5523 -0.4797<br /> <br /> <br /> val =<br /> <br /> 1.0000 0 0<br /> 0 0.6477 0<br /> 0 0 -0.0643<br /> <br /> &gt;&gt; a=-vec(:,1) % The eigenvectors can be mutiplied by (-1) since λV=AV can be written as λ(-V)=A(-V)<br /> <br /> a =<br /> <br /> 0.5571<br /> 0.7428<br /> 0.3714<br /> <br /> &gt;&gt; sum(a)<br /> <br /> ans =<br /> <br /> 1.6713<br /> <br /> &gt;&gt; a/sum(a)<br /> <br /> ans =<br /> <br /> 0.3333<br /> 0.4444<br /> 0.2222<br /> &lt;/pre&gt;<br /> <br /> This is &lt;math&gt;\pi_j = lim[p^n]_(ij)&lt;/math&gt; exist and is independent of i<br /> <br /> Another example:<br /> <br /> <br /> Find the stationary distribution of P= &lt;math&gt;\left[ {\begin{array}{ccc}<br /> 0.5 &amp; 0 &amp; 0 \\<br /> 1 &amp; 0 &amp; 0.5 \\<br /> 0 &amp; 1 &amp; 0.5 \end{array} } \right]&lt;/math&gt;<br /> <br /> &lt;math&gt;\pi=\pi~P&lt;/math&gt;&lt;br&gt;<br /> <br /> &lt;math&gt;\pi=&lt;/math&gt; [&lt;math&gt;\pi&lt;/math&gt;&lt;sub&gt;0&lt;/sub&gt;, &lt;math&gt;\pi&lt;/math&gt;&lt;sub&gt;1&lt;/sub&gt;, &lt;math&gt;\pi&lt;/math&gt;&lt;sub&gt;2&lt;/sub&gt;]&lt;br&gt;<br /> <br /> The system of equations is:<br /> <br /> &lt;math&gt;0.5\pi_0+1\pi_1+0\pi_2= \pi_0=&gt; 2\pi_1 = \pi_0&lt;/math&gt;&lt;br&gt; <br /> &lt;math&gt;0\pi_0+0\pi_1+1\pi_2= \pi_1 =&gt; \pi_1=\pi_2&lt;/math&gt;&lt;br&gt; <br /> &lt;math&gt;0\pi_0+0.5\pi_1+0.5\pi_2 = \pi_2 =&gt; \pi_1 = \pi_2&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\pi_0+\pi_1+\pi_2 = 1&lt;/math&gt;&lt;br&gt;<br /> <br /> &lt;math&gt;2\pi_1+\pi_1+\pi_1 = 4\pi_1 = 1&lt;/math&gt;, which gives &lt;math&gt;\pi_1=\frac {1}{4}&lt;/math&gt; &lt;br&gt;<br /> Also, &lt;math&gt;\pi_1 = \pi_2 = \frac {1}{4}&lt;/math&gt; &lt;br&gt;<br /> So, &lt;math&gt;\pi = [\frac{1}{2}, \frac{1}{4}, \frac{1}{4}]&lt;/math&gt; &lt;br&gt;<br /> <br /> === Ergodic Chain ===<br /> <br /> A Markov chain is called an ergodic chain if it is possible to go from every state to every state (not necessarily in one move). For instance, note that we can claim a Markov chain is ergodic if it is possible to somehow start at any state i and end at any state j in the matrix. We could have a chain with states 0, 1, 2, 3, 4 where it is not possible to go from state 0 to state 4 in just one step. However, it may be possible to go from 0 to 1, then from 1 to 2, then from 2 to 3, and finally 3 to 4 so we can claim that it is possible to go from 0 to 4 and this would satisfy a requirement of an ergodic chain. The example below will further explain this concept.<br /> <br /> '''Note:'''if there's a finite number N then every other state can be reached in N steps.<br /> '''Note:'''Also note that a Ergodic chain is irreducible (all states communicate) and aperiodic (d = 1). An Ergodic chain is promised to have a stationary and limiting distribution.&lt;br/&gt;<br /> '''Ergodicity:''' A state i is said to be ergodic if it is aperiodic and positive recurrent. In other words, a state i is ergodic if it is recurrent, has a period of 1 and it has finite mean recurrence time. If all states in an irreducible Markov chain are ergodic, then the chain is said to be ergodic.&lt;br/&gt; <br /> '''Some more:'''It can be shown that a finite state irreducible Markov chain is ergodic if it has an aperiodic state. A model has the ergodic property if there's a finite number N such that any state can be reached from any other state in exactly N steps. In case of a fully connected transition matrix where all transitions have a non-zero probability, this condition is fulfilled with N=1.&lt;br/&gt;<br /> <br /> <br /> ====Example====<br /> &lt;math&gt; P= \left[ \begin{matrix}<br /> \frac{1}{3} \; &amp; \frac{1}{3} \; &amp; \frac{1}{3} \\ \\<br /> \frac{1}{4} \; &amp; \frac{3}{4} \; &amp; 0 \\ \\<br /> \frac{1}{2} \; &amp; 0 \; &amp; \frac{1}{2}<br /> \end{matrix} \right] &lt;/math&gt;&lt;br /&gt;<br /> <br /> <br /> &lt;math&gt; \pi=\left[ \begin{matrix}<br /> \frac{1}{3} &amp; \frac{4}{9} &amp; \frac{2}{9}<br /> \end{matrix} \right] &lt;/math&gt;&lt;br /&gt;<br /> <br /> <br /> There are three states in this example.<br /> <br /> [[File:ab.png]]<br /> <br /> In this case, state a can go to state a, b, or c; state b can go to state a, b, or c; and state c can go to state a, b, or c so it is possible to go from every state to every state. (Although state b cannot directly go into c in one move, it must go to a, and then to c.).<br /> <br /> A k-by-k matrix indicates that the chain has k states.<br /> <br /> - Ergodic Markov chains are irreducible.<br /> <br /> - A Markov chain is called a '''regular''' chain if some power of the transition matrix has only positive elements.&lt;br /&gt;<br /> *Any transition matrix that has no zeros determines a regular Markov chain<br /> *However, it is possible for a regular Markov chain to have a transition matrix that has zeros.<br /> &lt;br /&gt;<br /> For example, recall the matrix of the Land of Oz<br /> <br /> &lt;math&gt;P = \left[ \begin{matrix}<br /> &amp; R &amp; N &amp; S \\<br /> R &amp; 1/2 &amp; 1/4 &amp; 1/4 \\<br /> N &amp; 1/2 &amp; 0 &amp; 1/2 \\<br /> S &amp; 1/4 &amp; 1/4 &amp; 1/2 \\<br /> \end{matrix} \right]&lt;/math&gt;&lt;br /&gt;<br /> <br /> === Theorem ===<br /> An ergodic Markov chain has a unique stationary distribution &lt;math&gt;\pi&lt;/math&gt;. The limiting distribution exists and is equal to &lt;math&gt;\pi&lt;/math&gt;&lt;br/&gt;<br /> Note: Ergodic Markov Chain is irreducible, aperiodic and positive recurrent.<br /> <br /> Example: Consider the markov chain of &lt;math&gt;\left[\begin{matrix}0 &amp; 1 \\ 1 &amp; 0\end{matrix}\right]&lt;/math&gt;, the stationary distribution is obtained by solving &lt;math&gt;\pi P = \pi&lt;/math&gt;, getting &lt;math&gt;\pi=[0.5, 0.5]&lt;/math&gt;, but from the assignment we know that it does not converge, ie. there is no limiting distribution, because the Markov chain is not aperiodic and cycle repeats &lt;math&gt;P^2=\left[\begin{matrix}1 &amp; 0 \\ 0 &amp; 1\end{matrix}\right]&lt;/math&gt; and &lt;math&gt;P^3=\left[\begin{matrix}0 &amp; 1 \\ 1 &amp; 0\end{matrix}\right]&lt;/math&gt;<br /> <br /> '''Another Example'''<br /> <br /> &lt;math&gt;P=\left[ {\begin{array}{ccc}<br /> \frac{1}{4} &amp; \frac{3}{4} \\[6pt]<br /> \frac{1}{5} &amp; \frac{4}{5} \end{array} } \right]&lt;/math&gt; &lt;br&gt;<br /> <br /> <br /> <br /> [[File:Untitled*.jpg]]<br /> <br /> This matrix means that there are two points in the space, let's call them a and b&lt;br/&gt;<br /> Starting from a, the probability of staying in a is 1/4 &lt;br/&gt;<br /> Starting from a, the probability of going from a to b is 3/4 &lt;br/&gt;<br /> Starting from b, the probability of going from b to a is 1/5 &lt;br/&gt;<br /> Starting from b, the probability of staying in b is 4/5 &lt;br/&gt;<br /> <br /> Solve the equation &lt;math&gt; \pi = \pi P &lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt; \pi_0 = .25 \pi_0 + .2 \pi_1 &lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt; \pi_1 = .75 \pi_0 + .8 \pi_1 &lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt; \pi_0 + \pi_1 = 1 &lt;/math&gt; &lt;br&gt;<br /> Solving this system of equations we get: &lt;br&gt;<br /> &lt;math&gt; \pi_0 = \frac{4}{15} \pi_1 &lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt; \pi_1 = \frac{15}{19} &lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt; \pi_0 = \frac{4}{19} &lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt; \pi = [\frac{4}{19}, \frac{15}{19}] &lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt; \pi &lt;/math&gt; is the long run distribution<br /> <br /> We can use the stationary distribution to compute the expected waiting time to return to state 'a' &lt;br/&gt;<br /> given that we start at state 'a' and so on.. Formula for this will be : &lt;math&gt; E[T_{i,i}]=\frac{1}{\pi_i}&lt;/math&gt;&lt;br/&gt;<br /> In the example above this will mean that that expected waiting time for the markov process to return to&lt;br/&gt;<br /> state 'a' given that we start at state 'a' is 19/4.&lt;br/&gt;<br /> <br /> definition of limiting distribution.<br /> <br /> remark：satisfied balance of &lt;math&gt;\pi_i P_{ij} = P_{ji} \pi_j&lt;/math&gt;, so there is other way to calculate the step probability.<br /> <br /> === MatLab Code ===<br /> In the following, P is the transition matrix. eye(n) refers to the n by n Identity matrix. L is the Laplacian matrix, L = (I - P). The Laplacian matrix will have at least 1 zero Eigenvalue. For every 0 in the diagonal, there is a component. If there is exactly 1 zero Eigenvalue, then the matrix is connected and has only 1 component. The number of zeros in the Laplacian matrix is the number of parts in your graph/process. If there is more than one zero on the diagonal of this matrix, means there is a disconnect in the graph.<br /> <br /> <br /> &lt;pre style='font-size:14px'&gt;<br /> &gt;&gt; P=[1/3, 1/3, 1/3; 1/4, 3/4, 0; 1/2, 0, 1/2]<br /> <br /> P =<br /> <br /> 0.3333 0.3333 0.3333<br /> 0.2500 0.7500 0<br /> 0.5000 0 0.5000<br /> <br /> &gt;&gt; eye(3) %%returns 3x3 identity matrix<br /> <br /> ans =<br /> <br /> 1 0 0<br /> 0 1 0<br /> 0 0 1<br /> <br /> &gt;&gt; L=(eye(3)-P) <br /> <br /> L =<br /> <br /> 0.6667 -0.3333 -0.3333<br /> -0.2500 0.2500 0<br /> -0.5000 0 0.5000<br /> <br /> &gt;&gt; [vec val]=eigs(L)<br /> <br /> vec =<br /> <br /> -0.7295 0.2329 0.5774<br /> 0.2239 -0.5690 0.5774<br /> 0.6463 0.7887 0.5774<br /> <br /> <br /> val =<br /> <br /> 1.0643 0 0<br /> 0 0.3523 0<br /> 0 0 -0.0000<br /> <br /> %% Only one value of zero on the diagonal means the chain is connected<br /> <br /> &gt;&gt; P=[0.8, 0.2, 0, 0;0.2, 0.8, 0, 0; 0, 0, 0.8, 0.2; 0, 0, 0.1, 0.9]<br /> <br /> P =<br /> <br /> 0.8000 0.2000 0 0<br /> 0.2000 0.8000 0 0<br /> 0 0 0.8000 0.2000<br /> 0 0 0.1000 0.9000<br /> <br /> &gt;&gt; eye(4)<br /> <br /> ans =<br /> <br /> 1 0 0 0<br /> 0 1 0 0<br /> 0 0 1 0<br /> 0 0 0 1<br /> <br /> &gt;&gt; L=(eye(4)-P)<br /> <br /> L =<br /> <br /> 0.2000 -0.2000 0 0<br /> -0.2000 0.2000 0 0<br /> 0 0 0.2000 -0.2000<br /> 0 0 -0.1000 0.1000<br /> <br /> &gt;&gt; [vec val]=eigs(L)<br /> <br /> vec =<br /> <br /> 0.7071 0 0.7071 0<br /> -0.7071 0 0.7071 0<br /> 0 0.8944 0 0.7071<br /> 0 -0.4472 0 0.7071<br /> <br /> <br /> val =<br /> <br /> 0.4000 0 0 0<br /> 0 0.3000 0 0<br /> 0 0 -0.0000 0<br /> 0 0 0 -0.0000<br /> <br /> %% Two values of zero on the diagonal means there are two 'islands' of chains<br /> <br /> &lt;/pre&gt;<br /> <br /> &lt;math&gt;\Pi&lt;/math&gt; satisfies detailed balance if &lt;math&gt;\Pi_i P_{ij}=P_{ji} \Pi_j&lt;/math&gt;. Detailed balance guarantees that &lt;math&gt;\Pi&lt;/math&gt; is stationary distribution.&lt;br /&gt;<br /> <br /> '''Adjacency matrix''' - a matrix &lt;math&gt;A&lt;/math&gt; that dictates which states are connected and way of portraying which vertices in the matrix are adjacent. Two vertices are adjacent if there exists a path between them of length 1.If we compute &lt;math&gt;A^2&lt;/math&gt;, we can know which states are connected with paths of length 2.&lt;br /&gt;<br /> <br /> A '''Markov chain''' is called an irreducible chain if it is possible to go from every state to every state (not necessary in one more).&lt;br /&gt;<br /> Theorem: An '''ergodic''' Markov chain has a unique stationary distribution &lt;math&gt;\pi&lt;/math&gt;. The limiting distribution exists and is equal to &lt;math&gt;\pi&lt;/math&gt;. &lt;br /&gt;<br /> <br /> <br /> Markov process satisfies detailed balance if and only if it is a '''reversible''' Markov process<br /> where P is the matrix of Markov transition.&lt;br /&gt;<br /> <br /> Satisfying the detailed balance condition guarantees that &lt;math&gt;\pi&lt;/math&gt; is stationary distributed.<br /> <br /> &lt;math&gt; \pi &lt;/math&gt; satisfies detailed balance if &lt;math&gt; \pi_i P_{ij} = P_{ji} \pi_j &lt;/math&gt; &lt;br&gt;<br /> which is the same as the Markov process equation.<br /> <br /> Example in the class: <br /> &lt;math&gt;P= \left[ {\begin{array}{ccc}<br /> \frac{1}{3} &amp; \frac{1}{3} &amp; \frac{1}{3} \\[6pt]<br /> \frac{1}{4} &amp; \frac{3}{4} &amp; 0 \\[6pt]<br /> \frac{1}{2} &amp; 0 &amp; \frac{1}{2} \end{array} } \right]&lt;/math&gt;<br /> <br /> and &lt;math&gt;\pi=(\frac{1}{3},\frac{4}{9}, \frac{2}{9})&lt;/math&gt;<br /> <br /> &lt;math&gt;\pi_1 P_{1,2} = 1/3 \times 1/3 = 1/9,\, P_{2,1} \pi_2 = 1/4 \times 4/9 = 1/9 \Rightarrow \pi_1 P_{1,2} = P_{2,1} \pi_2 &lt;/math&gt;&lt;br&gt;<br /> <br /> &lt;math&gt;\pi_2 P_{2,3} = 4/9 \times 0 = 0,\, P_{3,2} \pi_3 = 0 \times 2/9 = 0 \Rightarrow \pi_2 P_{2,3} = P_{3,2} \pi_3&lt;/math&gt;&lt;br&gt;<br /> Remark：Detailed balance of &lt;math&gt; \pi_i \times Pij = Pji \times \pi_j&lt;/math&gt; ， so there is other way to calculate the step probability&lt;br /&gt;<br /> &lt;math&gt;\pi&lt;/math&gt; is stationary but is not limiting.<br /> Detailed balance implies that &lt;math&gt;\pi&lt;/math&gt; = &lt;math&gt;\pi&lt;/math&gt; * P as shown in the proof and guarantees that &lt;math&gt;\pi&lt;/math&gt; is stationary distribution.<br /> <br /> == Class 15 - Tuesday June 25th 2013 ==<br /> === Announcement ===<br /> Note to all students, the first half of today's lecture will cover the midterm's solution; however please do not post the solution on the Wikicoursenote.&lt;br /&gt;<br /> <br /> ====Detailed balance====<br /> &lt;div style=&quot;border:2px solid black&quot;&gt;<br /> &lt;b&gt;Definition (from wikipedia)&lt;/b&gt;<br /> The principle of detailed balance is formulated for kinetic systems which are decomposed into elementary processes (collisions, or steps, or elementary reactions): At equilibrium, each elementary process should be equilibrated by its reverse process.<br /> &lt;/div&gt;<br /> Let &lt;math&gt;P&lt;/math&gt; be the transition probability matrix of a Markov chain. If there exists a distribution vector &lt;math&gt;\pi&lt;/math&gt; such that &lt;math&gt;\pi_i \cdot P_{ij}=P_{ji} \cdot \pi_j, \; \forall i,j&lt;/math&gt;, then the Markov chain is said to have '''detailed balance'''. A detailed balanced Markov chain must have &lt;math&gt;\pi&lt;/math&gt; given above as a stationary distribution, that is &lt;math&gt;\pi=\pi P&lt;/math&gt;, where &lt;math&gt;\pi&lt;/math&gt; is a 1 by n matrix and P is a n by n matrix.&lt;br&gt;<br /> <br /> <br /> need to remember：<br /> '''Proof:''' &lt;br&gt;<br /> &lt;math&gt;\; [\pi P]_j = \sum_i \pi_i P_{ij} =\sum_i P_{ji}\pi_j =\pi_j\sum_i P_{ji} =\pi_j ,\forall j&lt;/math&gt;<br /> <br /> :Note: Since &lt;math&gt;\pi_j&lt;/math&gt; is a sum of column j and we can do this proof for every element in matrix P; in general, we can prove &lt;math&gt;\pi=\pi P&lt;/math&gt; &lt;br&gt;<br /> <br /> Hence &lt;math&gt;\pi&lt;/math&gt; is always a stationary distribution of &lt;math&gt;P(X_{n+1}=j|X_n=i)&lt;/math&gt;, for every n. <br /> <br /> In other terms, &lt;math&gt; P_{ij} = P(X_n = j| X_{n-1} = i) &lt;/math&gt;, where &lt;math&gt;\pi_j&lt;/math&gt; is the equilibrium probability of being in state j and &lt;math&gt;\pi_i&lt;/math&gt; is the equilibrium probability of being in state i. &lt;math&gt;P(X_{n-1} = i) = \pi_i&lt;/math&gt; is equivalent to &lt;math&gt;P(X_{n-1} = i, Xn = j)&lt;/math&gt; being symmetric in i and j.<br /> <br /> Keep in mind that the detailed balance is a sufficient but not required condition for a distribution to be stationary. <br /> i.e. A distribution satisfying the detailed balance is stationary, but a stationary distribution does not necessarily satisfy the detailed balance.<br /> <br /> In the stationary distribution &lt;math&gt;\pi=\pi P&lt;/math&gt;, in the proof the sum of the p is equal 1 so the &lt;math&gt;\pi P=\pi&lt;/math&gt;.<br /> <br /> === PageRank (http://en.wikipedia.org/wiki/PageRank) ===<br /> <br /> *PageRank is a probability distribution used to represent the likelihood that a person randomly clicking on links will arrive at any particular page. PageRank can be calculated for collections of documents of any size.<br /> *PageRank is a link-analysis algorithm developed by and named after Larry Page from Google; used for measuring a website's importance, relevance and popularity.<br /> *PageRank is a graph containing web pages and their links to each other.<br /> *Many social media sites use this (such as Facebook and Twitter)<br /> *It can also be used to find criminals (ie. theives, hackers, terrorists, etc.) by finding out the links. <br /> This is what made Google the search engine of choice over Yahoo, Bing, etc.- What made Google's search engine a huge success is not its search function, but rather the algorithm it used to rank the pages. (Ex. If we come up with 100 million search results, how do you list them by relevance and importance so the users can easily find what they are looking for. Most users will not go past the first 3 or so search pages to find what they are looking for. It is this ability to rank pages that allow Google to remain more popular than Yahoo, Bink, AskJeeves, etc.). It should be noted that after using the PageRank algorithm, Google uses other processes to filter results.&lt;br/&gt;<br /> <br /> &lt;br /&gt;'''The order of importance'''&lt;br /&gt;<br /> 1. A web page is more important if many other pages point to it&lt;br /&gt;<br /> 2. The more important a web page is, the more weight should be assigned to its outgoing links&lt;br/ &gt;<br /> 3. If a webpage has many outgoing links, then its links have less value (ex: if a page links to everyone, like 411, it is not as important as pages that have incoming links)&lt;br /&gt;<br /> <br /> &lt;br /&gt;<br /> [[File:diagram.jpg]]<br /> &lt;math&gt;L= <br /> \left[ {\begin{matrix}<br /> 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\<br /> 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \\<br /> 1 &amp; 1 &amp; 0 &amp; 1 &amp; 0 \\<br /> 0 &amp; 1 &amp; 0 &amp; 0 &amp; 1 \\<br /> 0 &amp; 0 &amp; 0 &amp; 0 &amp; 0 \end{matrix} } \right]&lt;/math&gt;<br /> <br /> The first row indicates who gives a link to 1. As shown in the diagram, nothing gives a link to 1, and thus it is all zero.<br /> The second row indicates who gives a link to 2. As shown in the diagram, only 1 gives a link to 2, and thus column 1 is a 1 for row 2, and the rest are all zero.<br /> <br /> ie: According to the above example &lt;br/ &gt;<br /> Page 3 is the most important since it has the most links pointing to it, therefore more weigh should be placed on its outgoing links.&lt;br/ &gt;<br /> Page 4 comes after page 3 since it has the second most links pointing to it&lt;br/ &gt;<br /> Page 2 comes after page 4 since it has the third most links pointing to it&lt;br/&gt;<br /> Page 1 and page 5 are the least important since no links point to them&lt;br/ &gt;<br /> As page 1 and page 2 have the most outgoing links, then their links have less value compared to the other pages. &lt;br/ &gt;<br /> <br /> :&lt;math&gt;<br /> Lij = \begin{cases}<br /> 1, &amp; \text{if j has a link to i} \\<br /> 0, &amp; \text{otherwise}<br /> \end{cases}&lt;/math&gt;<br /> <br /> &lt;br /&gt;<br /> &lt;math&gt;C_j=&lt;/math&gt; The number of outgoing links of page &lt;math&gt;j&lt;/math&gt;:<br /> &lt;math&gt;C_j=\sum_i L_{ij}&lt;/math&gt;<br /> (i.e. sum of entries in column j)&lt;br /&gt;<br /> &lt;br /&gt;<br /> &lt;math&gt;P_j&lt;/math&gt; is the rank of page &lt;math&gt;j&lt;/math&gt;.&lt;br /&gt;<br /> Suppose we have &lt;math&gt;N&lt;/math&gt; pages, &lt;math&gt;P&lt;/math&gt; is a vector containing ranks of all pages.&lt;br /&gt;<br /> - &lt;math&gt;P&lt;/math&gt; is a &lt;math&gt;N \times 1&lt;/math&gt; vector.<br /> <br /> - &lt;math&gt;P_i&lt;/math&gt; counts the number of incoming links of page &lt;math&gt;i&lt;/math&gt;<br /> &lt;math&gt;P_i=\sum_j L_{ij}&lt;/math&gt; &lt;br /&gt;(i.e. sum of entries in row i)<br /> <br /> For each row of &lt;math&gt;L&lt;/math&gt;, if there is a 1 in the third column, it means page three point to that page.<br /> <br /> However, we should not define the rank of the page this way because links shouldn't be treated the same. The weight of the link is based on different factors. One of the factors is the importance of the page that link is coming from. For example, in this case, there are two links going to Page 4: one from Page 2 and one from Page 5. So far, both links have been treated equally with the same weight 1. But we must rerate the two links based on the importance of the pages they are coming from.<br /> <br /> A PageRank results from a mathematical algorithm based on the webgraph, created by all World Wide Web pages as nodes and hyperlinks as edges, taking into consideration authority hubs such as cnn.com or usa.gov. The rank value indicates an importance of a particular page. A hyperlink to a page counts as a vote of support. (This would be represented in our diagram as an arrow pointing towards the page. Hence in our example, Page 3 is the most important, since it has the most 'votes of support). The PageRank of a page is defined recursively and depends on the number and PageRank metric of all pages that link to it (&quot;incoming links&quot;). A page that is linked to by many pages with high PageRank receives a high rank itself. If there are no links to a web page, then there is no support for that page (In our example, this would be Page 1 and Page 5).<br /> (source:http://en.wikipedia.org/wiki/PageRank#Description)<br /> <br /> For those interested in PageRank, here is the original paper by Google co-founders Brin and Page: http://infolab.stanford.edu/pub/papers/google.pdf<br /> <br /> === Example of Page Rank Application in Real Life ===<br /> <br /> '''Page Rank checker'''<br /> - This is a free service to check Google™ page rank instantly via online PR checker or by adding a PageRank checking button to the web pages.<br /> &lt;font size=&quot;3&quot;&gt;(http://www.prchecker.info/check_page_rank.php)&lt;/font&gt;<br /> <br /> <br /> GoogleMatrix G = d * [ (Hyperlink Matrix H) + (Dangling Nodes Matrix A) ] + ((1-d)/N) * (NxN Matrix U of all 1's)<br /> <br /> <br /> [[File:Google matrix.png]]<br /> <br /> <br /> (source: https://googledrive.com/host/0B2GQktu-wcTiaWw5OFVqT1k3bDA/)<br /> <br /> == Class 16 - Thursday June 27th 2013 ==<br /> <br /> === Page Rank ===<br /> *&lt;math&gt;<br /> L_{ij} = \begin{cases}<br /> 1, &amp; \text{if j has a link to i } \\<br /> 0, &amp; \text{otherwise} \end{cases} &lt;/math&gt; &lt;br/&gt;<br /> <br /> *&lt;math&gt;C_j&lt;/math&gt;: number of outgoing links for page j, where &lt;math&gt;c_j=\sum_i L_{ij}&lt;/math&gt; <br /> <br /> P is N by 1 vector contains rank of all N pages; for page i, the rank is &lt;math&gt;P_i&lt;/math&gt;<br /> <br /> &lt;math&gt;P_i= (1-d) + d\cdot \sum_j \frac {L_{ji}P_j}{c_j}&lt;/math&gt;<br /> pi is the rank of a new created page(that no one knows about) is 0 since &lt;math&gt;L_ij&lt;/math&gt; is 0 &lt;br/&gt;<br /> where 0 &lt; d &lt; 1 is constant (in original page rank algorithm d = 0.8), and &lt;math&gt;L_{ij}&lt;/math&gt; is 1 if j has link to i, 0 otherwise.<br /> <br /> Note that the rank of a page is proportional to the number of its incoming links and inversely proportional to the number of its outgoing links.<br /> <br /> Interpretation of the formula:&lt;br/&gt;<br /> 1) sum of L&lt;sub&gt;ij&lt;/sub&gt; is the total number of incoming links&lt;br/&gt;<br /> 2) the above sum is weighted by page rank of the pages that contain the link to i (P&lt;sub&gt;j&lt;/sub&gt;) i.e. if a high-rank page points to page i, then this link carries more weight than links from lower-rank pages.&lt;br/&gt;<br /> 3) the sum is then weighted by the inverse of the number of outgoing links from the pages that contain links to i (c&lt;sub&gt;j&lt;/sub&gt;). i.e. if a page has more outgoing links than other pages then its links carry less weight.&lt;br/&gt;<br /> 4) finally, we take a linear combination of the page rank obtained from above and a constant 1. This ensures that every page has a rank greater than zero.&lt;br/&gt;<br /> 5) d is the damping factor. It represents the probability a user, at any page, will continue clicking to another page.&lt;br/&gt;<br /> If there is no damping (i.e. d=1), then there are no assumed outgoing links for nodes with no links. However, if there is damping (e.g. d=0.8), then these nodes are assumed to have links to all pages in the web.<br /> <br /> Note that this is a system of N equations with N unknowns.&lt;br/&gt;<br /> <br /> &lt;math&gt;c_j&lt;/math&gt; is the number of outgoing links, less outgoing links means more important.&lt;br/&gt;<br /> <br /> <br /> Let D be a diagonal N by N matrix such that &lt;math&gt; D_{ii}&lt;/math&gt; = &lt;math&gt;c_i&lt;/math&gt;<br /> <br /> Note: Ranks are arbitrary, all we want to know is the order. That is, we want to know how important the page rank relative to the other pages and are not interested in the value of the page rank. <br /> <br /> &lt;math&gt;D= <br /> \left[ {\begin{matrix}<br /> c_1 &amp; 0 &amp; ... &amp; 0 \\<br /> 0 &amp; c_2 &amp; ... &amp; 0 \\<br /> 0 &amp; 0 &amp; ... &amp; 0 \\<br /> 0 &amp; 0 &amp; ... &amp; c_N \end{matrix} } \right]&lt;/math&gt;<br /> <br /> Then &lt;math&gt;P=~(1-d)e+dLD^{-1}P&lt;/math&gt;, P is an iegenvector of matrix A corresponding to an eigenvalue equal to 1.&lt;br/&gt; where e =[1 1 ....]&lt;sup&gt;T&lt;/sup&gt; , i.e. a N by 1 vector.&lt;br/&gt;<br /> We assume that rank of all N pages sums to N. The sum of rank of all N pages can be any number, as long as the ranks have certain propotion. &lt;br/&gt;<br /> i.e. e&lt;sup&gt;T&lt;/sup&gt; P = N, then &lt;math&gt;~\frac{e^{T}P}{N} = 1&lt;/math&gt;<br /> <br /> <br /> D&lt;sup&gt;-1&lt;/sup&gt; will be: <br /> <br /> D&lt;sup&gt;-1&lt;/sup&gt;&lt;math&gt;= <br /> \left[ {\begin{matrix}<br /> \frac {1}{c_1} &amp; 0 &amp; ... &amp; 0 \\<br /> 0 &amp; \frac {1}{c_2} &amp; ... &amp; 0 \\<br /> 0 &amp; 0 &amp; ... &amp; 0 \\<br /> 0 &amp; 0 &amp; ... &amp; \frac {1}{c_N} \end{matrix} } \right]&lt;/math&gt;<br /> <br /> &lt;math&gt;P=~(1-d)e+dLD^{-1}P&lt;/math&gt; where &lt;math&gt;e=\begin{bmatrix}<br /> 1\\ <br /> 1\\ <br /> ...\\ <br /> 1<br /> \end{bmatrix}&lt;/math&gt;<br /> <br /> &lt;math&gt;P=(1-d)~\frac{ee^{T}P}{N}+dLD^{-1}P&lt;/math&gt;<br /> <br /> &lt;math&gt;P=[(1-d)~\frac{ee^T}{N}+dLD^{-1}]P&lt;/math&gt;<br /> <br /> &lt;math&gt;=&gt; P=A*P&lt;/math&gt;<br /> <br /> '''Explanation of an eigenvector'''<br /> <br /> An eigenvector is a non-zero vector '''v''' such that when multiplied by a square matrix, A, the result is a scalar times the vector '''v''' itself. &lt;br&gt;<br /> That is, A*v = c*v. Where c is the eigenvalue of A corresponding to the eigenvector v. In our case of Page Rank, the eigenvalue c=1. &lt;br&gt;<br /> <br /> We obtain that &lt;math&gt;P=AP&lt;/math&gt; where &lt;math&gt;A=(1-d)~\frac{ee^T}{N}+dLD^{-1}&lt;/math&gt;&lt;br/&gt;<br /> Thus, &lt;math&gt;P&lt;/math&gt; is an eigenvector of &lt;math&gt;P&lt;/math&gt; correspond to an eigen value equals 1.&lt;br/&gt;<br /> <br /> <br /> Since,<br /> L is a N*N matrix,<br /> D&lt;sup&gt;-1&lt;/sup&gt; is a N*N matrix, <br /> P is a N*1 matrix &lt;br/&gt;<br /> Then as a result, &lt;math&gt;LD^{-1}P&lt;/math&gt; is a N*1 matrix. &lt;br/&gt;<br /> <br /> N is a N*N matrix, d is a constant between 0 and 1.<br /> <br /> '''P=AP'''&lt;br /&gt;<br /> P is an eigenvector of A with corresponding eigenvalue equal to 1.&lt;br&gt;<br /> '''P&lt;sup&gt;T&lt;/sup&gt;=P&lt;sup&gt;T&lt;/sup&gt;A&lt;sup&gt;T&lt;/sup&gt;&lt;br&gt;'''<br /> Notice that all entries in A are non-negative and each row sums to 1. Hence A satisfies the definition of a transition probability matrix.&lt;br&gt;<br /> P&lt;sup&gt;T&lt;/sup&gt; is the stationary distribution of a Markov Chain with transition probability matrix A&lt;sup&gt;T&lt;/sup&gt;.<br /> <br /> We can consider A to be the matrix describing all possible movements following links on the internet, and P&lt;sup&gt;t&lt;/sup&gt; as the probability of being on any given webpage if we were on the internet long enough.<br /> <br /> Definition of rank page and proof it steps by steps, it shows with 3 n*n matrix and and one n*1 matrix and a constant d between 0 to 1.<br /> p is the stationary distribution so p=Ap.<br /> <br /> === Damping Factor &quot;d&quot; ===<br /> <br /> The PageRank assumes that any imaginary user who is randomly clicking on links will eventually stop clicking. The probability, at any step, that the person will keep on clicking is a damping factor, &lt;math&gt;d&lt;/math&gt;. After many studies, the approximation of &lt;math&gt;d&lt;/math&gt; is 0.85. Other values for &lt;math&gt;d&lt;/math&gt; have been used in class and may appear on assignments and exams.&lt;br/&gt;<br /> <br /> In addition, &lt;math&gt;d&lt;/math&gt; is a vector of ranks that are arbitrary. For example the rank can be [1 3 2], or [10 30 20], or [0.1 0.3 0.2]. All three of these examples are relative/equivalent since they are ranks, we could even have [1 10 3]. Therefore, &lt;math&gt;d&lt;/math&gt; must have a relative rank.&lt;br/&gt;<br /> <br /> So &lt;math&gt;P_1 + P_2 + \cdots + P_n=N&lt;/math&gt; &lt;br/&gt;<br /> Which is equivalent to:<br /> &lt;math&gt;e^{T}P= [1 \cdots 1] [P_1 \cdots P_n]^T &lt;/math&gt; &lt;br/&gt;<br /> Where &lt;math&gt;[1 \cdots 1]&lt;/math&gt; is a 1 scalar vector and &lt;math&gt;[P_1 \cdots P_n]^T&lt;/math&gt; is a rank vector. &lt;br/&gt;<br /> So &lt;math&gt;e^{T}P=N -&gt; (e^{T}P)/N = 1 &lt;/math&gt;<br /> <br /> ===Examples===<br /> &lt;span style=&quot;background:#F5F5DC&quot;&gt;<br /> <br /> ==== Example 1 ====<br /> <br /> <br /> [[File:eg1.jpg]]<br /> &lt;br /&gt;<br /> &lt;/span&gt;<br /> &lt;math&gt;L= <br /> \left[ {\begin{matrix}<br /> 0 &amp; 0 &amp; 1 \\<br /> 1 &amp; 0 &amp; 0 \\<br /> 0 &amp; 1 &amp; 0 \end{matrix} } \right]\;c= <br /> \left[ {\begin{matrix}<br /> 1 &amp; 1 &amp; 1 \end{matrix} } \right]\;D= <br /> \left[ {\begin{matrix}<br /> 1 &amp; 0 &amp; 0 \\<br /> 0 &amp; 1 &amp; 0 \\<br /> 0 &amp; 0 &amp; 1 \end{matrix} } \right]&lt;/math&gt;<br /> <br /> &lt;pre style='font-size:14px'&gt;<br /> <br /> MATLAB Code<br /> <br /> d=0.8<br /> N=3<br /> A=(1-d)*ones(N)/N+d*L*pinv(D) #pinv: Moore-Penrose inverse (pseudoinverse) of symbolic matrix<br /> We use the pinv(D) function [pseudo-inverse] instead of the inv(D) function because in <br /> the case of a non-invertible matrix, it would not crash the program. <br /> [vec val]=eigs(A) (eigen-decomposition)<br /> a=-vec(:,1) (find the eigenvector equals to 1)<br /> a=a/sum(a) (normalize a)<br /> or to show that A transpose is a stationary transition matrix<br /> (transpose(A))^200 will be the same as a=a/sum(a)<br /> &lt;/pre&gt;<br /> <br /> '''NOTE:''' Changing the value of d, does not change the ranking order of the pages. <br /> <br /> By looking at each entry after normalizing a, we can tell the ranking order of each page.&lt;br&gt;<br /> &lt;span style=&quot;background:#F5F5DC&quot;&gt;<br /> <br /> c = [1 1 1] since there are 3 pages, each page is one way recurrent to each other and there is only one outgoing for each page. Hence, D is a 3x3 standard diagonal matrix.<br /> <br /> ==== Example 2 ====<br /> <br /> [[File:Screen_shot_2013-07-02_at_3.43.04_AM.png]]<br /> <br /> <br /> &lt;math&gt;L= <br /> \left[ {\begin{matrix}<br /> 0 &amp; 0 &amp; 1 \\<br /> 1 &amp; 0 &amp; 1 \\<br /> 0 &amp; 1 &amp; 0 \end{matrix} } \right]\;<br /> c= <br /> \left[ {\begin{matrix}<br /> 1 &amp; 1 &amp; 2 \end{matrix} } \right]\;<br /> D= <br /> \left[ {\begin{matrix}<br /> 1 &amp; 0 &amp; 0 \\<br /> 0 &amp; 1 &amp; 0 \\<br /> 0 &amp; 0 &amp; 2 \end{matrix} } \right]&lt;/math&gt;<br /> <br /> &lt;pre style='font-size:14px'&gt;<br /> <br /> Matlab code<br /> <br /> &gt;&gt; L=[0 0 1;1 0 1;0 1 0];<br /> &gt;&gt; C=sum(L);<br /> &gt;&gt; D=diag(C);<br /> &gt;&gt; d=0.8;<br /> &gt;&gt; N=3;<br /> &gt;&gt; A=(1-d)*ones(N)/N+d*L*pinv(D);<br /> &gt;&gt; [vec val]=eigs(A)<br /> <br /> vec =<br /> <br /> -0.3707 -0.3536 + 0.3536i -0.3536 - 0.3536i<br /> -0.6672 -0.3536 - 0.3536i -0.3536 + 0.3536i<br /> -0.6461 0.7071 0.7071 <br /> <br /> <br /> val =<br /> <br /> 1.0000 0 0 <br /> 0 -0.4000 - 0.4000i 0 <br /> 0 0 -0.4000 + 0.4000i<br /> <br /> &gt;&gt; a=-vec(:,1)<br /> <br /> a =<br /> <br /> 0.3707<br /> 0.6672<br /> 0.6461<br /> <br /> &gt;&gt; a=a/sum(a)<br /> <br /> a =<br /> <br /> 0.2201<br /> 0.3962<br /> 0.3836<br /> &lt;/pre&gt;<br /> '''NOTE:''' Page 2 is the most important page because it has 2 incomings. Similarly, page 3 is more important than page 1 because page 3 has the incoming result from page 2.<br /> <br /> This example is similar to the first example, but here, page 3 can go back to page 2, so the matrix of the outgoing matrix, the third column of the D matrix is 3 in the third row. And we use the code to calculate the p=Ap. Therefore 2, 3, 1 is the order of importance.<br /> <br /> ==== Example 3 ====<br /> <br /> [[File:eg 3.jpg]]&lt;br&gt;<br /> <br /> &lt;math&gt;L= <br /> \left[ {\begin{matrix}<br /> 0 &amp; 1 &amp; 0 \\<br /> 1 &amp; 0 &amp; 1 \\<br /> 0 &amp; 1 &amp; 0 \end{matrix} } \right]\;<br /> c= <br /> \left[ {\begin{matrix}<br /> 1 &amp; 2 &amp; 1 \end{matrix} } \right]\;<br /> D= <br /> \left[ {\begin{matrix}<br /> 1 &amp; 0 &amp; 0 \\<br /> 0 &amp; 2 &amp; 0 \\<br /> 0 &amp; 0 &amp; 1 \end{matrix} } \right]&lt;/math&gt;<br /> <br /> &lt;math&gt;d=0.8&lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;N=3&lt;/math&gt;&lt;br&gt;<br /> <br /> <br /> &lt;/span&gt;<br /> this example is the second page have 2 outgoings.<br /> <br /> <br /> <br /> Another Example:<br /> <br /> Consider: 1 -&gt; ,&lt;-2 -&gt;3<br /> <br /> &lt;math&gt;L= <br /> \left[ {\begin{matrix}<br /> 0 &amp; 1 &amp; 0 \\<br /> 1 &amp; 0 &amp; 0 \\<br /> 0 &amp; 1 &amp; 0 \end{matrix} } \right]\;<br /> c= <br /> \left[ {\begin{matrix}<br /> 1 &amp; 1 &amp; 1 \end{matrix} } \right]\;<br /> D= <br /> \left[ {\begin{matrix}<br /> 1 &amp; 0 &amp; 0 \\<br /> 0 &amp; 1 &amp; 0 \\<br /> 0 &amp; 0 &amp; 1 \end{matrix} } \right]&lt;/math&gt;<br /> <br /> ==== Example 4 ====<br /> <br /> &lt;math&gt;1 \leftrightarrow 2 \rightarrow 3 \leftrightarrow 4 &lt;/math&gt;<br /> &lt;br /&gt;<br /> &lt;br /&gt;<br /> &lt;math&gt;L= <br /> \left[ {\begin{matrix}<br /> 0 &amp; 1 &amp; 0 &amp; 0 \\<br /> 1 &amp; 0 &amp; 0 &amp; 0 \\<br /> 0 &amp; 1 &amp; 0 &amp; 1 \\<br /> 0 &amp; 0 &amp; 1 &amp; 0 \end{matrix} } \right]\;&lt;/math&gt;&lt;br /&gt;<br /> <br /> '''Matlab Code:'''&lt;br&gt;<br /> &lt;pre style='font-size:16px'&gt;<br /> &gt;&gt; L=L= [0 1 0 0;1 0 0 0;0 1 0 1;0 0 1 0];<br /> &gt;&gt; C=sum(L);<br /> &gt;&gt; D=diag(C);<br /> &gt;&gt; d=0.8;<br /> &gt;&gt; N=4;<br /> &gt;&gt; A=(1-d)*ones(N)/N+d*L*pinv(D);<br /> &gt;&gt; [vec val]=eigs(A);<br /> &gt;&gt; a=vec(:,1);<br /> &gt;&gt; a=a/sum(a)<br /> a =<br /> 0.1029 &lt;- Page 1<br /> 0.1324 &lt;- Page 2<br /> 0.3971 &lt;- Page 3<br /> 0.3676 &lt;- Page 4<br /> <br /> % Therefore the PageRank for this matrix is: 3,4,2,1<br /> &lt;/pre&gt;<br /> &lt;br&gt;<br /> <br /> ==== Example 5 ====<br /> <br /> &lt;math&gt;L= <br /> \left[ {\begin{matrix}<br /> 0 &amp; 1 &amp; 0 &amp; 1 \\<br /> 1 &amp; 0 &amp; 1 &amp; 1 \\<br /> 1 &amp; 0 &amp; 0 &amp; 1 \\<br /> 1 &amp; 0 &amp; 0 &amp; 0 \end{matrix} } \right]&lt;/math&gt;<br /> <br /> &lt;math&gt;c= <br /> \left[ {\begin{matrix}<br /> 3 &amp; 1 &amp; 1 &amp; 3 \end{matrix} } \right]&lt;/math&gt;<br /> <br /> &lt;math&gt;D= <br /> \left[ {\begin{matrix}<br /> 3 &amp; 0 &amp; 0 &amp; 0 \\<br /> 0 &amp; 1 &amp; 0 &amp; 0 \\<br /> 0 &amp; 0 &amp; 1 &amp; 0 \\<br /> 0 &amp; 0 &amp; 0 &amp; 3 \end{matrix} } \right]&lt;/math&gt;<br /> <br /> &lt;pre style='font-size:14px'&gt;<br /> <br /> Matlab code<br /> <br /> &gt;&gt; L= [0 1 0 1; 1 0 1 1; 1 0 0 1;1 0 0 0];<br /> &gt;&gt; d = 0.8;<br /> &gt;&gt; N = 4;<br /> &gt;&gt; C = sum(L);<br /> &gt;&gt; D = diag(C);<br /> &gt;&gt; A=(1-d)*ones(N)/N+d*L*pinv(D);<br /> &gt;&gt; [vec val]=eigs(A);<br /> &gt;&gt; a=vec(:,1);<br /> &gt;&gt; a=a/sum(a)<br /> <br /> a =<br /> <br /> 0.3492<br /> 0.3263<br /> 0.1813<br /> 0.1431<br /> &lt;/pre&gt;<br /> <br /> ==== Example 6 ====<br /> &lt;math&gt;L= <br /> \left[ {\begin{matrix}<br /> 0 &amp; 1 &amp; 0 &amp; 0 &amp; 1\\<br /> 1 &amp; 0 &amp; 0 &amp; 0 &amp; 0\\<br /> 0 &amp; 1 &amp; 0 &amp; 0 &amp; 0\\<br /> 0 &amp; 1 &amp; 1 &amp; 0 &amp; 1\\<br /> 0 &amp; 0 &amp; 0 &amp; 1 &amp; 0 \end{matrix} } \right]&lt;/math&gt;<br /> &lt;br /&gt;<br /> <br /> '''Matlab Code:'''&lt;br /&gt;<br /> &lt;pre style=&quot;font-size:16px&quot;&gt;<br /> &gt;&gt; d=0.8;<br /> &gt;&gt; L=[0 1 0 0 1;1 0 0 0 0;0 1 0 0 0;0 1 1 0 1;0 0 0 1 0];<br /> &gt;&gt; c=sum(L);<br /> &gt;&gt; D=diag(c);<br /> &gt;&gt; N=5;<br /> &gt;&gt; A=(1-d)*ones(N)/N+d*L*pinv(D);<br /> &gt;&gt; [vec val]=eigs(A);<br /> &gt;&gt; a=-vec(:,1);<br /> &gt;&gt; a=a/sum(a) <br /> a =<br /> 0.1933 &lt;- Page 1<br /> 0.1946 &lt;- Page 2<br /> 0.0919 &lt;- Page 3<br /> 0.2668 &lt;- Page 4<br /> 0.2534 &lt;- Page 5<br /> <br /> % Therefore the PageRank for this matrix is: 4,5,2,1,3<br /> &lt;/pre&gt;<br /> &lt;br&gt;<br /> <br /> == Class 17 - Tuesday July 2nd 2013 ==<br /> === Markov Chain Monte Carlo (MCMC) ===<br /> <br /> ===Introduction===<br /> It is, in general, very difficult to simulate the value of a random vector X whose component random variables are dependent. We will present a powerful approach for generating a vector whose distribution is approximately that of X. This approach, called the Markov Chain Monte Carlo Methods, has the added significance of only requiring that the mass(or density) function of X be specified up to a multiplicative constant, and this, we will see, is of great importance in applications.<br /> (referenced by Sheldon M.Ross,Simulation)<br /> The basic idea used here is to generate a Markov Chain whose stationary dsitribution is the same as the target distribution.<br /> <br /> ====Definition:====<br /> Markov Chain<br /> A Markov Chain is a special form of stochastic process in which &lt;math&gt;\displaystyle X_t&lt;/math&gt; depends only on &lt;math&gt; \displaystyle X_{t-1}&lt;/math&gt;.<br /> <br /> For example,<br /> :&lt;math&gt;\displaystyle f(X_1,...X_n)= f(X_1)f(X_2|X_1)f(X_3|X_2)...f(X_n|X_{n-1})&lt;/math&gt;<br /> A random Walk is the best example of a Markov process<br /> <br /> &lt;br&gt;'''Transition Probability:'''&lt;br&gt;<br /> The probability of going from one state to another state.<br /> :&lt;math&gt;p_{ij} = \Pr(X_{n}=j\mid X_{n-1}= i). \,&lt;/math&gt;<br /> <br /> &lt;br&gt;'''Transition Matrix:'''&lt;br&gt;<br /> For n states, transition matrix P is an &lt;math&gt;N \times N&lt;/math&gt; matrix with entries &lt;math&gt;\displaystyle P_{ij}&lt;/math&gt; as below:<br /> Markov Chain Monte Carlo (MCMC) methods are a class of algorithms for sampling from probability distributions based on constructing a Markov chain that has the desired distribution as its equilibrium distribution. The state of the chain after a large number of steps is then used as a sample of the desired distribution. The quality of the sample improves as a function of the number of steps. (http://en.wikipedia.org/wiki/Markov_chain_Monte_Carlo)&lt;/span&gt;<br /> <br /> &lt;a style=&quot;color:red&quot; href=&quot;http://www.eecs.berkeley.edu/Pubs/TechRpts/2010/EECS-2010-165.pdf&quot;&gt;some notes form UCb&lt;/a&gt;<br /> <br /> '''One of the main purposes of MCMC''' : to simulate samples from a joint distribution where the joint random variables are dependent. In general, this is not easily sampled from. Other methods learned in class allow us to simulate i.i.d random variables, but not dependent variables . In this case, we could sample non-independent random variables using a Markov Chain. Its Markov properties help to simplify the simulation process.<br /> <br /> <br /> &lt;b&gt;Basic idea:&lt;/b&gt; Given a probability distribution &lt;math&gt;\pi&lt;/math&gt; on a set &lt;math&gt;\Omega&lt;/math&gt;, we want to generate random elements of &lt;math&gt;\Omega&lt;/math&gt; with distribution &lt;math&gt;\pi&lt;/math&gt;. MCMC does that by constructing a Markov Chain with stationary distribution &lt;math&gt;\pi&lt;/math&gt; and simulating the chain. After a large number of iterations, the Markov Chain will reach its stationary distribution. By sampling from the Markov chain for large amount of iterations, we are effectively sampling from the desired distribution as the Markov Chain would converge to its stationary distribution &lt;br/&gt; <br /> <br /> Idea: generate a Markov chain whose stationary distribution is the same as target distribution. &lt;br/&gt;<br /> <br /> <br /> '''Notes'''<br /> <br /> # Regardless of the chosen starting point, the Markov Chain will converge to its stationary distribution (if it exists). However, the time taken for the chain to converge depends on its chosen starting point. Typically, the burn-in period is longer if the chain is initialized with a value of low probability density.<br /> # Markov Chain Monte Carlo can be used for sampling from a distribution, estimating the distribution, and computing the mean and optimization (e.g. simulated annealing, more on that later).<br /> # Markov Chain Monte Carlo is used to sample using “local” information. It is used as a generic “problem solving technique” to solve decision/optimization/value problems, but is not necessarily very efficient.<br /> # MCMC methods do not suffer as badly from the &quot;curse of dimensionality&quot; that badly affects efficiency in the acceptance-rejection method. This is because a point is always generated at each time-step according to the Markov Chain regardless of how many dimensions are introduced.<br /> # The goal when simulating with a Markov Chain is to create a chain with the same stationary distribution as the target distribution.<br /> # The MCMC method is usually used in continuous cases but a discrete example is given below.<br /> <br /> <br /> '''Some properties of the stationary distribution &lt;math&gt;\pi&lt;/math&gt;'''<br /> <br /> &lt;math&gt;\pi&lt;/math&gt; indicates the proportion of time the process spends in each of the states 1,2,...,n. Therefore &lt;math&gt;\pi&lt;/math&gt; satisfies the following two inequalities: &lt;br&gt;<br /> <br /> # &lt;math&gt;\pi_j = \sum_{i=1}^{n}\pi_i P_{ij}&lt;/math&gt; &lt;br /&gt; This is because &lt;math&gt;\pi_i&lt;/math&gt; is the proportion of time the process spends in state i, and &lt;math&gt;P_{ij}&lt;/math&gt; is the probability the process transition out of state i into state j. Therefore, &lt;math&gt;\pi_i p_{ij}&lt;/math&gt; is the proportion of time it takes for the process to enter state j. Therefore, &lt;math&gt;\pi_j&lt;/math&gt; is the sum of this probability over overall states i.<br /> #&lt;math&gt; \sum_{i=1}^{n}\pi_i= 1 &lt;/math&gt; as &lt;math&gt;\pi&lt;/math&gt; shows the proportion of time the chain is in each state. If we view it as the probability of the chain being in state i at time t for t sufficiently large, then it should sum to one as the chain must be in one of the states.<br /> <br /> ====Motivation example====<br /> - Suppose we want to generate a random variable X according to distribution &lt;math&gt;\pi=(\pi_1, \pi_2, ... , \pi_m)&lt;/math&gt; &lt;br/&gt;<br /> X can take m possible different values from &lt;math&gt;{1,2,3,\cdots, m}&lt;/math&gt;&lt;br /&gt;<br /> - We want to generate &lt;math&gt;\{X_t: t=0, 1, \cdots\}&lt;/math&gt; according to &lt;math&gt;\pi&lt;/math&gt;&lt;br /&gt;<br /> <br /> Suppose our example is of a bias die. &lt;br/&gt;<br /> Now we have m=6, &lt;math&gt;\pi=[0.1,0.1,0.1,0.2,0.3,0.2]&lt;/math&gt;, &lt;math&gt;X \in [1,2,3,4,5,6]&lt;/math&gt;&lt;br/&gt;<br /> <br /> Suppose &lt;math&gt;X_t=i&lt;/math&gt;. Consider an arbitrary probability transition matrix Q with entry &lt;math&gt;q_{ij}&lt;/math&gt; being the probability of moving to state j from state i. (&lt;math&gt;q_{ij}&lt;/math&gt; can not be zero.) &lt;br/&gt; <br /> <br /> &lt;math&gt; \mathbf{Q} = <br /> \begin{bmatrix}<br /> q_{11} &amp; q_{12} &amp; \cdots &amp; q_{1m} \\<br /> q_{21} &amp; q_{22} &amp; \cdots &amp; q_{2m} \\<br /> \vdots &amp; \vdots &amp; \ddots &amp; \vdots \\<br /> q_{m1} &amp; q_{m2} &amp; \cdots &amp; q_{mm}<br /> \end{bmatrix}<br /> &lt;/math&gt; &lt;br/&gt;<br /> <br /> <br /> We generate Y = j according to the i-th row of Q. Note that the i-th row of Q is a probability vector that shows the probability of moving to any state j from the current state i, i.e.&lt;math&gt;P(Y=j)=q_{ij}&lt;/math&gt;&lt;br /&gt;<br /> <br /> In the following algorithm: &lt;br&gt;<br /> &lt;math&gt;q_{ij}&lt;/math&gt; is the &lt;math&gt;ij^{th}&lt;/math&gt; entry of matrix Q. It is the probability of Y=j given that &lt;math&gt;x_t = i&lt;/math&gt;. &lt;br/&gt;<br /> &lt;math&gt;r_{ij}&lt;/math&gt; is the probability of accepting Y as &lt;math&gt;x_{t+1}&lt;/math&gt;. &lt;br/&gt;<br /> <br /> <br /> '''How to get the acceptance probability?'''<br /> <br /> If &lt;math&gt;\pi &lt;/math&gt; is the stationary distribution, then it must satisfy the detailed balance condition:&lt;br/&gt;<br /> If &lt;math&gt;\pi_i P_{ij}&lt;/math&gt; = &lt;math&gt;\pi_j P_{ji}&lt;/math&gt;&lt;br/&gt;then &lt;math&gt;\pi &lt;/math&gt; is the stationary distribution of the chain<br /> <br /> Since &lt;math&gt;P_{ij}&lt;/math&gt; = &lt;math&gt;q_{ij} r_{ij}&lt;/math&gt;, we have &lt;math&gt;\pi_i q_{ij} r_{ij}&lt;/math&gt; = &lt;math&gt;\pi_j q_{ji} r_{ji}&lt;/math&gt;.&lt;br/&gt;<br /> We want to find a general solution: &lt;math&gt;r_{ij} = a(i,j) \pi_j q_{ji}&lt;/math&gt;, where a(i,j) = a(j,i).&lt;br/&gt; <br /> <br /> '''Recall'''<br /> &lt;math&gt;r_{ij}&lt;/math&gt; is the probability of acceptance, thus it must be that &lt;br/&gt;<br /> <br /> 1.&lt;math&gt;r_{ij} = a(i,j)&lt;/math&gt; &lt;math&gt;\pi_j q_{ji} &lt;/math&gt;≤1, then we get: &lt;math&gt;a(i,j) &lt;/math&gt;≤ &lt;math&gt;1/(\pi_j q_{ji})&lt;/math&gt;<br /> <br /> 2. &lt;math&gt;r_{ji} = a(j,i) &lt;/math&gt; &lt;math&gt;\pi_i q_{ij} &lt;/math&gt; ≤ 1, then we get: &lt;math&gt;a(j,i)&lt;/math&gt; ≤ &lt;math&gt;1/(\pi_i q_{ij})&lt;/math&gt;<br /> <br /> So we choose a(i,j) as large as possible, but it needs to satisfy the two conditions above.&lt;br/&gt;<br /> <br /> &lt;math&gt;a(i,j) = \min \{\frac{1}{\pi_j q_{ji}},\frac{1}{\pi_i q_{ij}}\} &lt;/math&gt;&lt;br/&gt;<br /> <br /> Thus, &lt;math&gt; r_{ij} = \min \{\frac{\pi_j q_{ji}}{\pi_i q_{ij}}, 1\} &lt;/math&gt;&lt;br/&gt;<br /> <br /> '''Note''': <br /> 1 is the upper bound to make r&lt;sub&gt;ij&lt;/sub&gt; a probability<br /> <br /> <br /> '''Algorithm:''' &lt;br/&gt;<br /> *&lt;math&gt; (*) P(Y=j) = q_{ij} &lt;/math&gt;. &lt;math&gt;\frac{\pi_j q_{ji}}{\pi_i q_{ij}}&lt;/math&gt; is a positive ratio.<br /> <br /> *&lt;math&gt; r_{ij} = \min \{\frac{\pi_j q_{ji}}{\pi_i q_{ij}}, 1\} &lt;/math&gt; &lt;br/&gt;<br /> *&lt;math&gt;<br /> x_{t+1} = \begin{cases}<br /> Y, &amp; \text{with probability } r_{ij} \\<br /> x_t, &amp; \text{otherwise} \end{cases} &lt;/math&gt; &lt;br/&gt;<br /> * go back to the first step (*) &lt;br/&gt;<br /> <br /> We can compare this with the Acceptance-Rejection model we learned before. &lt;br/&gt;<br /> * &lt;math&gt;U&lt;/math&gt; ~ &lt;math&gt;Uniform(0,1)&lt;/math&gt; &lt;br/&gt;<br /> * If &lt;math&gt;U &lt; r_{ij}&lt;/math&gt;, then accept. &lt;br/&gt;<br /> EXCEPT that a point is always generated at each time-step. &lt;br&gt;<br /> <br /> The algorithm generates a stochastic sequence that only depends on the last state, which is a Markov Chain.&lt;br&gt;<br /> <br /> ====Metropolis Algorithm====<br /> <br /> '''Proposition: ''' Metropolis works:<br /> <br /> The &lt;math&gt;P_{ij}&lt;/math&gt;'s from Metropolis Algorithm satisfy detailed balance property w.r.t &lt;math&gt;\pi&lt;/math&gt; . i.e. &lt;math&gt;\pi_i P_{ij} = \pi_j P_{ji}&lt;/math&gt;. The new Markov Chain has a stationary distribution &lt;math&gt;\pi&lt;/math&gt;. &lt;br/&gt;<br /> '''Remarks:''' &lt;br/&gt;<br /> 1) We only need to know ratios of values of &lt;math&gt;\pi_i&lt;/math&gt;'s.&lt;br/&gt;<br /> 2) The MC might converge to &lt;math&gt;\pi&lt;/math&gt; at varying speeds depending on the proposal distribution and the value the chain is initialized with&lt;br/&gt;<br /> <br /> <br /> This algorithm generates &lt;math&gt;\{x_t: t=0,...,m\}&lt;/math&gt;. &lt;br/&gt;<br /> In the long run, the marginal distribution of &lt;math&gt; x_t &lt;/math&gt; is the stationary distribution &lt;math&gt;\underline{\Pi} &lt;/math&gt;&lt;br&gt;<br /> &lt;math&gt;\{x_t: t = 0, 1,...,m\}&lt;/math&gt; is a Markov chain with probability transition matrix (PTM), P.&lt;br&gt;<br /> <br /> This is a Markov Chain since &lt;math&gt; x_{t+1} &lt;/math&gt; only depends on &lt;math&gt; x_t &lt;/math&gt;, where &lt;br&gt;<br /> &lt;math&gt; P_{ij}= \begin{cases}<br /> q_{ij} r_{ij}, &amp; \text{if }i \neq j (q_{ij} \text{is the probability of generating j from i and } r_{ij} \text{ is the probiliity of accepting)}\\[6pt]<br /> 1 - \displaystyle\sum_{k \neq i} q_{ik} r_{ik}, &amp; \text{if }i = j \end{cases} &lt;/math&gt;&lt;br /&gt;<br /> <br /> &lt;math&gt;q_{ij}&lt;/math&gt; is the probability of generating state j; &lt;br/&gt;<br /> &lt;math&gt; r_{ij}&lt;/math&gt; is the probability of accepting state j as the next state. &lt;br/&gt;<br /> <br /> Therefore, the final probability of moving from state i to j when i does not equal to j is &lt;math&gt;q_{ij}*r_{ij}&lt;/math&gt;. &lt;br/&gt;<br /> For the probability of moving from state i to state i, we deduct all the probabilities of moving from state i to any j that are not equal to i, therefore, we get the second probability.<br /> <br /> ===Proof of the proposition:===<br /> <br /> A good way to think of the detailed balance equation is that they balance the probability from state i to state j with that from state j to state i. <br /> We need to show that the stationary distribition of the Markov Chain is &lt;math&gt;\underline{\Pi}&lt;/math&gt;, i.e. &lt;math&gt;\displaystyle \underline{\Pi} = \underline{\Pi}P&lt;/math&gt;&lt;br /&gt;<br /> &lt;div style=&quot;text-size:20px&quot;&gt;<br /> Recall&lt;br/&gt;<br /> If a Markov chain satisfies the detailed balance property, i.e. &lt;math&gt;\displaystyle \pi_i P_{ij} = \pi_j P_{ji} \, \forall i,j&lt;/math&gt;, then &lt;math&gt;\underline{\Pi}&lt;/math&gt; is the stationary distribution of the chain.&lt;br /&gt;&lt;br /&gt;<br /> &lt;/div&gt;<br /> <br /> '''Proof:'''<br /> <br /> WLOG, we can assume that &lt;math&gt;\frac{\pi_j q_{ji}}{\pi_i q_{ij}}&lt;1&lt;/math&gt;&lt;br/&gt;<br /> <br /> LHS:&lt;br /&gt;<br /> &lt;math&gt;\pi_i P_{ij} = \pi_i q_{ij} r_{ij} = \pi_i q_{ij} \cdot \min(\frac{\pi_j q_{ji}}{\pi_i q_{ij}},1) = \cancel{\pi_i q_{ij}} \cdot \frac{\pi_j q_{ji}}{\cancel{\pi_i q_{ij}}} = \pi_j q_{ji}&lt;/math&gt;&lt;br /&gt;<br /> <br /> RHS:&lt;br /&gt;<br /> Note that by our assumption, since &lt;math&gt;\frac{\pi_j q_{ji}}{\pi_i q_{ij}}&lt;1&lt;/math&gt;, its reciprocal &lt;math&gt;\frac{\pi_i q_{ij}}{\pi_j q_{ji}} \geq 1&lt;/math&gt;&lt;br /&gt;<br /> So &lt;math&gt;\displaystyle \pi_j P_{ji} = \pi_ j q_{ji} r_{ji} = \pi_ j q_{ji} \cdot \min(\frac{\pi_i q_{ij}}{\pi_j q_{ji}},1) = \pi_j q_{ji} \cdot 1 = \pi_ j q_{ji}&lt;/math&gt;&lt;br /&gt;<br /> <br /> Hence LHS=RHS <br /> <br /> If we assume that &lt;math&gt;\frac{\pi_j q_{ji}}{\pi_i q_{ij}}=1&lt;/math&gt;&lt;br/&gt; (essentially &lt;math&gt;\frac{\pi_j q_{ji}}{\pi_i q_{ij}}&gt;=1&lt;/math&gt;)&lt;br/&gt;<br /> <br /> LHS:&lt;br /&gt;<br /> &lt;math&gt;\pi_i P_{ij} = \pi_i q_{ij} r_{ij} = \pi_i q_{ij} \cdot \min(\frac{\pi_j q_{ji}}{\pi_i q_{ij}},1) =\pi_i q_{ij} \cdot 1 = \pi_i q_{ij}&lt;/math&gt;&lt;br /&gt;<br /> <br /> RHS:&lt;br /&gt;<br /> '''Note''' &lt;br/&gt;<br /> by our assumption, since &lt;math&gt;\frac{\pi_j q_{ji}}{\pi_i q_{ij}}\geq 1&lt;/math&gt;, its reciprocal &lt;math&gt;\frac{\pi_i q_{ij}}{\pi_j q_{ji}} \leq 1 &lt;/math&gt; &lt;br /&gt;<br /> <br /> So &lt;math&gt;\displaystyle \pi_j P_{ji} = \pi_ j q_{ji} r_{ji} = \pi_ j q_{ji} \cdot \min(\frac{\pi_i q_{ij}}{\pi_j q_{ji}},1) = \cancel{\pi_j q_{ji}} \cdot \frac{\pi_i q_{ij}}{\cancel{\pi_j q_{ji}}} = \pi_i q_{ij}&lt;/math&gt;&lt;br /&gt;<br /> <br /> Hence LHS=RHS which indicates &lt;math&gt;pi_i*P_{ij} = pi_j*P_{ji}&lt;/math&gt;&lt;math&gt;\square&lt;/math&gt;&lt;br /&gt;&lt;br /&gt;<br /> <br /> '''Note'''&lt;br /&gt; <br /> 1) If we instead assume &lt;math&gt;\displaystyle \frac{\pi_i q_{ij}}{\pi_j q_{ji}} \geq 1&lt;/math&gt;, the proof is similar with LHS= RHS = &lt;math&gt; \pi_i q_{ij} &lt;/math&gt; &lt;br /&gt;<br /> <br /> 2) If &lt;math&gt;\displaystyle i = j&lt;/math&gt;, then detailed balance is satisfied trivially.&lt;br /&gt;<br /> <br /> since &lt;math&gt;{\pi_i q_{ij}}&lt;/math&gt;, and &lt;math&gt;{\pi_j q_{ji}}&lt;/math&gt; are smaller than one. so the above steps show the proof of &lt;math&gt;\frac{\pi_i q_{ij}}{\pi_j q_{ji}}&lt;1&lt;/math&gt;.<br /> <br /> == Class 18 - Thursday July 4th 2013 ==<br /> === Last class ===<br /> <br /> Recall: The Acceptance Probability,<br /> &lt;math&gt;r_{ij}=min(\frac {{\pi_j}q_{ji}}{{\pi_i}q_{ij}},1)&lt;/math&gt; &lt;br /&gt;<br /> <br /> 1) &lt;math&gt;r_{ij}=\frac {{\pi_j}q_{ji}}{{\pi_i}q_{ij}}&lt;/math&gt;, and &lt;math&gt;r_{ji}=1 &lt;/math&gt;, (&lt;math&gt;\frac {{\pi_j}q_{ji}}{{\pi_i}q_{ij}} &lt; 1&lt;/math&gt;) &lt;br /&gt;<br /> <br /> <br /> 2) &lt;math&gt;r_{ji}=\frac {{\pi_i}q_{ij}}{{\pi_j}q_{ji}}&lt;/math&gt;, and &lt;math&gt; r{ij}=1 &lt;/math&gt;, (&lt;math&gt;\frac {{\pi_j}q_{ji}}{{\pi_i}q_{ij}} \geq 1&lt;/math&gt; ) &lt;br /&gt;<br /> <br /> ===Example: Discrete Case===<br /> <br /> <br /> Consider a biased die,<br /> &lt;math&gt;\pi&lt;/math&gt;= [0.1, 0.1, 0.2, 0.4, 0.1, 0.1]<br /> <br /> We could use any &lt;math&gt;6 x 6 &lt;/math&gt; matrix &lt;math&gt; \mathbf{Q} &lt;/math&gt; as the proposal distribution &lt;br&gt;<br /> For the sake of simplicity ,using a discrete uniform distribution is the simplest. This is because all probabilities are equivalent, hence during the calculation of r, qxy and qyx will cancel each other out.<br /> <br /> &lt;math&gt; \mathbf{Q} = <br /> \begin{bmatrix}<br /> 1/6 &amp; 1/6 &amp; \cdots &amp; 1/6 \\<br /> 1/6 &amp; 1/6 &amp; \cdots &amp; 1/6 \\<br /> \vdots &amp; \vdots &amp; \ddots &amp; \vdots \\<br /> 1/6 &amp; 1/6 &amp; \cdots &amp; 1/6<br /> \end{bmatrix}<br /> &lt;/math&gt; &lt;br/&gt;<br /> <br /> <br /> <br /> '''Algorithm''' &lt;br&gt;<br /> 1. &lt;math&gt;x_t=5&lt;/math&gt; (sample from the 5th row, although we can initialize the chain from anywhere within the support)&lt;br /&gt;<br /> 2. Y~Unif[1,2,...,6]&lt;br /&gt;<br /> 3. &lt;math&gt; r_{ij} = \min \{\frac{\pi_j q_{ji}}{\pi_i q_{ij}}, 1\} = \min \{\frac{\pi_j 1/6}{\pi_i 1/6}, 1\} = \min \{\frac{\pi_j}{\pi_i}, 1\}&lt;/math&gt;&lt;br&gt;<br /> Note: current state &lt;math&gt;i&lt;/math&gt; is &lt;math&gt;X_t&lt;/math&gt;, the candidate state &lt;math&gt;j&lt;/math&gt; is &lt;math&gt;Y&lt;/math&gt;. &lt;br&gt;<br /> Note: since &lt;math&gt;q_{ij}= q_{ji}&lt;/math&gt; for all i and j, that is, the proposal distribution is symmetric, we have &lt;math&gt; r_{ij} = \min \{\frac{\pi_j}{\pi_i }, 1\} &lt;/math&gt;&lt;br/&gt;<br /> 4. U~Unif(0,1)&lt;br/&gt;<br /> if &lt;math&gt;u \leq r_{ij}&lt;/math&gt;, X&lt;sub&gt;t+1&lt;/sub&gt;=Y&lt;br /&gt;<br /> else X&lt;sub&gt;t+1&lt;/sub&gt;=X&lt;sub&gt;t&lt;/sub&gt;&lt;br /&gt;<br /> go back to 2&lt;br&gt;<br /> <br /> Notice how a point is always generated for X&lt;sub&gt;t+1&lt;/sub&gt;, regardless of whether the candidate state Y is accepted &lt;br&gt;<br /> <br /> '''Matlab'''<br /> &lt;pre style=&quot;font-size:14px&quot;&gt;<br /> pii=[.1,.1,.2,.4,.1,.1]; <br /> x(1)=5; <br /> for ii=2:1000 <br /> Y=unidrnd(6); %%% Unidrnd(x) is a built-in function which generates a number between (0) and (x) <br /> r = min (pii(Y)/pii(x(ii-1)), 1);<br /> u=rand; <br /> if u&lt;r <br /> x(ii)=Y;<br /> else<br /> x(ii)=x(ii-1);<br /> end<br /> end<br /> hist(x,6) %generate histogram displaying all 1000 points<br /> xx = x(501,end); %After 500, the chain will mix well and converge. <br /> hist(xx,6) % The result should be better.<br /> &lt;/pre&gt;<br /> [[File:MH_example1.jpg|300px]]<br /> <br /> <br /> '''NOTE:''' Generally, we generate a large number of points (say, 1500) and throw away some of the points that were first generated(say, 500). Those first points are called the [[burn-in period]]. A chain will converge to the limiting distribution eventually, but not immediately. The burn-in period is that beginning period before the chain has converged to the desired distribution. By discarding those 500 points, our data set will be more representative of the desired limiting distribution; once the burn-in period is over, we say that the chain &quot;mixes well&quot;.<br /> <br /> ===Alternate Example: Discrete Case===<br /> <br /> <br /> Consider the weather. If it is sunny one day, there is a 5/7 chance it will be sunny the next. If it is rainy, there is a 5/8 chance it will be rainy the next.<br /> &lt;math&gt;\pi= [\pi_1 \ \pi_2] &lt;/math&gt;<br /> <br /> Use a discrete uniform distribution as the proposal distribution, because it is the simplest.<br /> <br /> &lt;math&gt; \mathbf{Q} = <br /> \begin{bmatrix}<br /> 5/7 &amp; 2/7 \\<br /> 3/8 &amp; 5/8\\<br /> <br /> \end{bmatrix}<br /> &lt;/math&gt; &lt;br/&gt;<br /> <br /> <br /> <br /> '''Algorithm''' &lt;br&gt;<br /> 1. Set initial chain state: &lt;math&gt;X_t=1&lt;/math&gt; (i.e. sample from the 1st row, although we could also choose the 2nd row)&lt;br /&gt;<br /> 2. Sample from proposal distribution: Y~q(y|x) = Unif[1,2]&lt;br /&gt;<br /> 3. &lt;math&gt; r_{ij} = \min \{\frac{\pi_j q_{ji}}{\pi_i q_{ij}}, 1\} = \min \{\frac{\pi_j 1/6}{\pi_i 1/6}, 1\} = \min \{\frac{\pi_j}{\pi_i}, 1\}&lt;/math&gt;&lt;br&gt;<br /> '''Note:''' Current state &lt;math&gt;i&lt;/math&gt; is &lt;math&gt;X_t&lt;/math&gt;, the candidate state &lt;math&gt;j&lt;/math&gt; is &lt;math&gt;Y&lt;/math&gt;. Since &lt;math&gt;q_{ij}= q_{ji}&lt;/math&gt; for all i and j, that is, the proposal distribution is symmetric, we have &lt;math&gt; r_{ij} = \min \{\frac{\pi_j}{\pi_i }, 1\} &lt;/math&gt;<br /> <br /> 4. U~Unif(0,1)&lt;br&gt;<br /> If &lt;math&gt;U \leq r_{ij}&lt;/math&gt;, then&lt;br&gt;<br /> &lt;math&gt;X_t=Y&lt;/math&gt;&lt;br&gt;<br /> else&lt;br /&gt;<br /> &lt;math&gt;X_{t+1}=X_t&lt;/math&gt;&lt;br&gt;<br /> end if&lt;br /&gt;<br /> 5. Go back to step 2&lt;br&gt;<br /> <br /> <br /> '''Generalization of the above framework to the continuous case'''&lt;br&gt;<br /> <br /> In place of &lt;math&gt;\pi&lt;/math&gt; use &lt;math&gt;f(x)&lt;/math&gt;<br /> In place of r&lt;sub&gt;ij&lt;/sub&gt; use &lt;math&gt;q(y|x)&lt;/math&gt; &lt;br&gt;<br /> In place of r&lt;sub&gt;ij&lt;/sub&gt; use &lt;math&gt;r(x,y)&lt;/math&gt; &lt;br&gt;<br /> Here, q(y|x) is a friendly distribution that is easy to sample, usually a symmetric distribution will be preferable, such that &lt;math&gt;q(y|x) = q(x|y)&lt;/math&gt; to simplify the computation for &lt;math&gt;r(x,y)&lt;/math&gt;.<br /> <br /> <br /> '''Remarks'''&lt;br&gt;<br /> 1. The chain may not get to a stationary distribution if the # of steps generated are small. That is it will take a very large amount of steps to step through the whole support&lt;br&gt;<br /> 2. The algorithm can be performed with a &lt;math&gt;\pi&lt;/math&gt; that is not even a probability mass function, it merely needs to be proportional to the probability mass function we wish to sample from. This is useful as we do not need to calculate the normalization factor. &lt;br&gt;<br /> <br /> For example, if we are given &lt;math&gt;\pi^'=\pi\alpha=[5,10,11,2,100,1]&lt;/math&gt;, we can normalize this vector by dividing the sum of all entries &lt;math&gt;s&lt;/math&gt;.&lt;br&gt;<br /> However we notice that when calculating &lt;math&gt;r_{ij}&lt;/math&gt;, &lt;br&gt;<br /> &lt;math&gt;\frac{\pi^'_j/s}{\pi^'_i/s}\times\frac{q_{ji}}{q_{ij}}=\frac{\pi^'_j}{\pi^'_i}\times\frac{q_{ji}}{q_{ij}}&lt;/math&gt; &lt;br&gt;<br /> &lt;math&gt;s&lt;/math&gt; cancels out in this case. Therefore it is not necessary to calculate the sum and normalize the vector.&lt;br&gt;<br /> <br /> This also applies to the continuous case,where we merely need &lt;math&gt; f(x) &lt;/math&gt; to be proportional to the pdf of the distribution we wish to sample from. &lt;br&gt;<br /> <br /> ===Metropolis–Hasting Algorithm===<br /> <br /> '''Definition''': &lt;br&gt;<br /> Metropolis–Hastings algorithm is a Markov chain Monte Carlo (MCMC) method for obtaining a sequence of random samples from a probability distribution for which direct sampling is difficult. The Metropolis–Hastings algorithm can draw samples from any probability distribution P(x), provided you can compute the value of a function f(x) which is proportional to the density of P. &lt;br&gt;<br /> <br /> <br /> <br /> '''Purpose''': &lt;br&gt;<br /> &quot;The purpose of the Metropolis-Hastings Algorithm is to &lt;b&gt;generate a collection of states according to a desired distribution&lt;/b&gt; &lt;math&gt;P(x)&lt;/math&gt;. &lt;math&gt;P(x)&lt;/math&gt; is chosen to be the stationary distribution of a Markov process, &lt;math&gt;\pi(x)&lt;/math&gt;.&quot; &lt;br&gt;<br /> Source:(http://en.wikipedia.org/wiki/Metropolis%E2%80%93Hastings_algorithm)&lt;br&gt;<br /> <br /> <br /> Metropolis-Hastings is an algorithm for constructing a Markov chain with a given limiting probability distribution. In particular, we consider what happens if we apply the Metropolis-Hastings algorithm repeatedly