# Difference between revisions of "Eleisha's Segment 11: Random Deviates"

Line 104: | Line 104: | ||

− | <b> Back to </b> [[Eleisha Jackson]] | + | <b> Back to: </b> [[Eleisha Jackson]] |

## Revision as of 10:46, 18 February 2014

**To Calculate: **

1. For the Cauchy distribution (Segment 8, Slide 3), find the inverse function of the CDF.

You can easily use mathematica to find the inverse function of the CDF for the Cauchy distribution.

OR You could just you pencil and paper...

**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle P(x) = \frac{1}{2} + \frac{1}{\pi}\arctan\left[ \frac{x - \mu}{\sigma} \right] }**

Let **Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle P(x) = P }**

Now we just find the inverse of the function:

**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle P - \frac{1}{2} = \frac{1}{\pi}\arctan\left[ \frac{x - \mu}{\sigma} \right] }**

**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \pi\left( P - \frac{1}{2} \right) = \arctan\left[ \frac{x - \mu}{\sigma} \right] }**

**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \tan\left[\pi\left( P - \frac{1}{2} \right)\right] = \frac{x - \mu}{\sigma} }**

**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \sigma \tan\left[\pi\left( P - \frac{1}{2} \right)\right] + \mu = x }**

So the CDF, **Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle F(P) }**
is:

**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle F(P) = \sigma \tan\left[\pi\left( P - \frac{1}{2} \right)\right] + \mu }**

2. In your favorite programming language, write a function that returns independent Cauchy deviates.

Here is some python code that generates independent Cauchy deviates:

import math import random mu = 0 sigma = 1 N = 10 #The inverse of the Cauchy CDF def get_inv_cauch_cdf(p_y, mu, sigma): cdf_val = mu + sigma*math.tan((p_y - 0.5)*math.pi) return cdf_val #This is a function that will generate an independent Cauchy deviate using the Transformation #Method def get_variate(): x = random.random() y = get_inv_cauch_cdf(x, mu, sigma) return y def get_cauchy_deviates(N): deviates = [] for i in xrange(0, N): deviates.append(get_variate()) return deviates rand_deviates = get_cauchy_deviates(N) print str(N) + " Random Deviates from a Cauchy Distribution: " print rand_deviates

Here is some same output generated by the code above:

10 Random Deviates from a Cauchy Distribution: [9.0851283407409813, -0.65383372885854252, -6.7627447406781362, -1.8271582934958259, 0.57399828693079358, -0.54025381399970152, -2.5448556887215492, 0.96565869875860988, 0.80155776954380109, 0.56424562260925804]

** To Think About: **

1. Suppose you want a function that returns deviates for Student(**Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \nu }**
). Could you use the Cauchy pdf (or some scaling of it) as a bounding function in a rejection method? How efficient is this (i.e., what fraction of the time does it reject)?
2. Explain the three inequality tests in the "while" statement in Leva's algorithm (slide 7) and why they are hooked together with logical operators in the way shown.

** Class Activity **

Group: Eleisha Jackson and Daniel Shepard

During class, we attempted to develop our own (pseudo) random number generator.
Our method:
In order to create a generator that uniformly sampled between the range of zero and one we used a linear algorithm based on the the time as a random seed. Using the time as input, we developed a mathematical algorithm that generated a sequence of random numbers. The first number in the sequence: **Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle x_0 }**
was a deterministic value that was calculated using the time. Then every **Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle x_k }**
afterwards was a generated as a function of **Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle x_{k-1} }**
.

Here is our MATLAB code:

function [x] = randUniform(N) t = clock; m = 2^48; a = 25214903917; c = 11; sec = t(6); seed = sec*m/60; y = size(N,1); y(1) = mod(a*seed,m); for(k = 2:N) y(k) = mod(a*y(k-1) + c,m); end x = y/m;

Using this method we were able to verify that out random generator equally sample the range between (0, 1). Unfortunately our random number generator did not pass the required p-value test. We believe this is because each number in the sequence is dependent on the initial starting number **Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle x_0 }**
and therefore can be predicted. This means that each number in the randomly generated sequence of 1 million points that we test are not independent.

** Back to: ** Eleisha Jackson