| Illustration | Random Variable x | Type |
|---|---|---|
| Family size | x = Number of dependents reported on tax return | Discrete |
| Distance from home to stores on a highway | x = Distance in miles from home to the store site | Continuous |
| Own dog or cat | x = 1 if own no pet; | Discrete |
| x = 2 if own dog(s) only; | ||
| x = 3 if own cat(s) only; | ||
| x = 4 if own dog(s) and cat(s) |
\[ f(x)\geq 0 \text{ and } \sum f(x)=1 \]
\[ f(x) = \frac{1}{n} \]
where \(n\) is the number of value taken by x
\[ E(x) = \mu = \sum x f(x) \]
Find the expected value of a die:
| Number | f(x) | xf(x) |
|---|---|---|
| 1 | 1/6 | 1/6 |
| 2 | 1/6 | 2/6 |
| 3 | 1/6 | 3/6 |
| 4 | 1/6 | 4/6 |
| 5 | 1/6 | 5/6 |
| 6 | 1/6 | 1 |
\[ E(die) = 1/6 + 2/6 + 3/6 + 4/6 + 5/6 + 1 = 3.5 \]
\[ E(X) = \sum_i x_i P(X = x_i) \]
\[ E(X) = \int_{-\infty}^{\infty} x f(x) dx \]
\[ E[Y|X] = \sum y p(y|X) \]
\[ E(Y) = E(E(Y|X)) \]
Given this joint probability distribution of income, show that \(E(E(Income|Female)) = E(Income)\).
| Female | Income | Joint Probability |
|---|---|---|
| 1 | 30,000 | 0.20 |
| 1 | 40,000 | 0.30 |
| 1 | 50,000 | 0.10 |
| 0 | 45,000 | 0.10 |
| 0 | 55,000 | 0.20 |
| 0 | 65,000 | 0.10 |
For any constant \(a\) and random variable \(X\):
\[ E(aX) = aE(X) \]
For any random variables \(X\) and \(Y\):
\[ E(X + Y) = E(X) + E(Y) \]
Suppose we have a discrete distribution with random variable X which can take on n values \(x_1, x_2, ..., x_n\) with probabilities \(p_1, p_2, ..., p_n\).
What would \(E(X)\) be?
\[ E(X) = \sum_{i=1}^{n} x_i p_i \]
Now what would \(E(aX)\) for some constant \(a\)?
\[ E(aX) = \sum_{i=1}^{n} a x_i p_i = a \sum_{i=1}^{n} x_i p_i = aE(X) \]
\[ E(X + Y) = \sum_{x} \sum_{y} (x + y) p(x,y) \]
\[ = \sum_{x} \sum_{y} x p(x,y) + \sum_{x} \sum_{y} y p(x,y) \]
\[ = \sum_{x} x \sum_{y} p(x,y) + \sum_{y} y \sum_{x} p(x,y) \]
The above step is interesting… why?
What do we do now?
What is the sum of the joint distribution (\(\sum_{y} p(x,y)\)) and analogously for \(Y\)?
\[ \sum_{y} p(x,y) = p(x, y_1) + p(x, y_2) + ... + p(x, y_n) \]
This gives us the marginal probability of \(X\). What is the marginal probability of \(Y\)?
With this we can abuse the notation a little bit…
\[ = \sum_{x} x p(x) + \sum_{y} y p(y) \]
\[ = E(X) + E(Y) \]
\[ Var(x) = \sigma^2 = \sum (x-\mu)^2 f(x) \]
Question: Based on this, what sign is the variance?
What is the variance of a die?
| Number | x-\(\mu\) | \((x-\mu)^2\) | f(x) | \((x-\mu)^2 f(x)\) |
|---|---|---|---|---|
| 1 | -2.5 | 6.25 | 1/6 | 1.0417 |
| 2 | -1.5 | 2.25 | 1/6 | 0.375 |
| 3 | -0.5 | 0.25 | 1/6 | 0.0417 |
| 4 | 0.5 | 0.25 | 1/6 | 0.0417 |
| 5 | 1.5 | 2.25 | 1/6 | 0.375 |
| 6 | 2.5 | 6.25 | 1/6 | 1.0417 |
\[ Var(die) = 1.0417 + 0.375 + 0.0417 + 0.0417 + 0.375 + 1.0417 = 2.9167 \]
For any constant \(a\) and random variable \(X\):
\[ Var(aX) = a^2Var(X) \]
For any random variables \(X\) and \(Y\):
\[ Var(X + Y) = Var(X) + Var(Y) + 2Cov(X,Y) \]
If X and Y are independent, then \(Cov(X,Y) = 0\)
When we discussed the variance, I have shown it as a sum:
\[ Var(X) = \sum_{i=1}^{n} (x_i - \mu)^2 f(x_i) \]
But if you squint your eyes, what does that sort of look like? Like an expectation!
Note
\[ Var(X) = E((X - \mu)^2) \]
\[ Var(X + Y) = E((X + Y - E(X + Y))^2) \]
Note
\[ Var(X + Y) = E((X + Y - E(X + Y))^2) = E((X + Y - E(X) - E(Y))^2) \]
\[ = E((X - E(X))^2 + (Y - E(Y))^2 + 2(X - E(X))(Y - E(Y))) \]
\[ = E((X - E(X))^2) + E((Y - E(Y))^2) + 2E((X - E(X))(Y - E(Y))) \]
\[ = Var(X) + Var(Y) + 2Cov(X,Y) \]
Show that \(Var(aX + b) = a^2Var(X)\)
Note
\[ Var(aX + b) = E((aX + b - E(aX + b))^2) = E((aX + b - aE(X) - b)^2) \]
\[ = E((aX - aE(X))^2) = a^2E((X - E(X))^2) = a^2Var(X) \]
Interesting, why is the \(Var(b)=0\)?
\[ Cov(x, y) = \sigma_{xy} = \sum (x-\mu_x)(y - \mu_y) f(x,y) \]
or equivalently:
\[ Cov(X,Y) = E((X - E(X))(Y - E(Y))) \]
For any constant \(a\) and random variable \(X\):
\[ Cov(aX, Y) = aCov(X,Y) \]
For any two constants \(a\) and \(b\):
\[ Cov(aX, bY) = abCov(X,Y) \]
For any random variables \(X\), \(Y\) and \(Z\):
\[ Cov(X + Y, Z) = Cov(X,Z) + Cov(Y,Z) \]
Review:
For any random variables \(X\) and \(Y\):
\[ Var(X + Y) = Var(X) + Var(Y) + 2Cov(X,Y) \]
Note:
\[ Cov(X,Y) = 0 \]
However, the converse is not true. If the covariance of two random variables is zero, then they are not necessarily independent.
Except for a normal distribution!
“How many heads will land after 25 coin flips?”
“If a machine fails after a year at a rate of 30%, what is the probability of 2 machines failing after a year?”
\[ S = \{(HH), (HT), (TH), (TT)\} \]
What is the probability of each of the events?
1/2 * 1/2
\[ 2 \cdot \frac{1}{4} \]
\[ \text{Number of occurrences of 1 H}\cdot Pr(Getting heads)\cdot Pr(Getting Tails) \]
\[ 2 \cdot \frac{1}{4} \cdot \frac{3}{4} \]
\[ S = \{(HHH), (HHT), (HTH), (HTT), (THH), (TTH), (THT),(TTT) \} \]
What is the probability of getting one H now?
\[ 3 \cdot \frac{1}{4} \cdot \frac{3}{4} \cdot \frac{3}{4} \]
\[ 3 \cdot \frac{1}{4} \cdot (\frac{3}{4})^2 \]
Or
\[ 3 \cdot (\frac{1}{4})^{1} \cdot (\frac{3}{4})^{n-1} \]
where \(n=3\)
We can write the probability expression as as a function of the number of trials and the number of “successes” we want.
Note
Solving by induction is fun!
We can write this as:
\[ C^n_x = \frac{n!}{x!(n-x)!} \]
where n is the number of trials and x is the number of successes (Heads) we want.
\[ n! = n(n-1)(n-2)...(1) \]
Note
\[ n! = n(n-1)! \]
To get the intution we need to understand Combination’s counterpart, permutation
A permutation is an arrangement of objects in a definite order
So in the case of coin flips, permutations count HT, and TH as two distinct permutations, because the order of the occurrences matter.
Let’s calculate the number of permutations for coin flips for three trials.
___ ___ ___
\[ 3! = 3 \cdot 2 \cdot 1 \]
If we follow the same logic, but now for \(n\) trials, we will have \(n!\) distinct ways to arrange these trails. But what if we wanted to arrange only \(r\) of them. Ex: We have 10 trials, but want to arrange into 5 spots.
___ ___ ___ ___ ___
Then we would just stop when we got to \(r\).
\[ n \cdot (n-1) \cdot (n-2) \cdot ... \cdot (n-(r-1)) \]
But for mathematical niceness, we can use the properties of factorials to see that:
\[ \begin{split} & n \cdot (n-1) \cdot (n-2) \cdot ... \cdot (n-(r-1)) \\ &= \frac{n \cdot (n-1) \cdot (n-2) \cdot ... \cdot (n-(r-1))\cdot (n-r) \cdot ... \cdot (1)}{(n-r)!} \end{split} \]
And that’s a permutation!
The last part is now to note that combinations don’t care about order, so HTH is the same HHT.
In the permutation then, we are counting duplicates and there are \(r!\) ways that we are double-counting them. So in that case we we just divide by \(r!\) and get the combination formula.
\[ \frac{\frac{n!}{(n-r)!}}{r!} = \frac{n!}{r!(n-r)!} \]
With this knowledge, we have what we need to write out the probability distribution for the Bernoulli:
\[ f(x) = \frac{n!}{x!(n-x)!}\cdot p^x (1-p)^{(n-x)} \]
Over 5 trials, what is the probability that at most 1 coins will be Heads?
Hint: Use the fact that \(Pr(Heads \leq 1) = Pr(Heads=0 \cup Heads=1)\)
Evans Electronics is concerned about a low retention rate for its employees. In recent years, management has seen a turnover of 10% of the hourly employees annually.
For any hourly employee chosen at random, management estimates a probability of 0.1 that the person will not be with the company next year.
Choosing 3 hourly employees at random, what is the probability that 1 of them will leave the company this year?
\[ \begin{split} f(x) &= \frac{n!}{x!(n-x)!} p^x (1-p)^{n-x} \\ f(1) &= \frac{3!}{1!(3-1)!} (0.1)^1 (0.9)^2 = 0.243 \end{split} \]
\[ E(x) = \mu = np \]
\[ Var(x) = np(1-p) \]
Using the definition of expected value, show that the expected value of a Bernoulli distribution with \(p=\frac{1}{2}\) and 2 trials is \(E(x) = np\).
\[ f(x) = \frac{\mu^x e^{-\mu}}{x!} \]
x = the number of occurrences in an interval \(\mu\) = the mean number of occurrences in an interval
\[ \mu = \sigma^2 \]
Patients arrive at the emergency room of Mercy Hospital at the average rate of 6 per hour on weekend evenings.
What is the probability of 4 arrivals in 30 minutes on a weekend evening?
\[ \frac{3^4 e^{-3}}{4!} = 0.1680 \]

viewof N = Inputs.range([1,200], {
step: 1,
value: 10,
label: "Maximum number of flips"
})
viewof n = Inputs.range([1,N], {
step: 1,
value: 5,
label: "Number of flips"
})
viewof p = Inputs.range([0,1], {
step: 0.1,
title: "Prob of getting a head"
})
binomial = (k, n, p) => choose(n, k) * Math.pow(p, k) * Math.pow(1 - p, n - k)
fac = (function() {
// from https://stackoverflow.com/questions/3959211/fast-factorial-function-in-javascript
var f = [];
return function factorial (n) {
if (n == 0 || n == 1)
return 1;
if (f[n] > 0)
return f[n];
return f[n] = factorial(n-1) * n;
}
})()
function choose(n, k) {
if(k > n / 2) return choose(n, n - k);
let res = 1;
for(let i = 1; i <= k; i++) {
res *= (n + 1 - i) / i;
}
return res;
}
binomial_distribution = {
const height = 360;
const svg = d3.select(DOM.svg(width, height));
const margin = { top: 20, right: 40, bottom: 40, left: 60 };
const data = d3.range(0, n + 1).map(k => ({ x: k, y: binomial(k, n, p) }));
const x = d3
.scaleLinear()
.domain([0, N])
.range([margin.left, width - margin.right]);
const y = d3
.scaleLinear()
.domain([0, 1])
.range([height - margin.bottom, margin.top]);
const pts_to_path = d3
.line()
.x(function(d) {
return x(d[0]);
})
.y(function(d) {
return y(d[1]);
});
draw_curve(n * p, Math.sqrt(n * p * (1 - p)));
svg
.selectAll('line.stick')
.data(data)
.enter()
.append('line')
.attr('x1', d => x(d.x))
.attr('x2', d => x(d.x))
.attr('y1', d => y(d.y))
.attr('y2', d => height - margin.bottom)
.attr('stroke', '#95a5a6');
svg
.selectAll('circle')
.data(data)
.enter()
.append('circle')
.attr('r', 3)
.attr('cx', d => x(d.x))
.attr('cy', d => y(d.y))
.attr('fill', '#3498db');
svg
.append("g")
.attr("class", "axis axis--x")
.attr("transform", `translate(${-0.5},${height - margin.bottom})`)
.call(d3.axisBottom(x));
svg
.append("g")
.attr("class", "axis axis--y")
.attr("transform", `translate(${margin.left - 10},0)`)
.call(d3.axisLeft(y));
function draw_curve(m, s) {
function f(x) {
return (
Math.exp((-(x - m) * (x - m)) / (2 * s * s)) /
(Math.sqrt(2 * Math.PI) * s)
);
}
var graph_points = d3.range(0, N, 0.1).map(function(x) {
return [x, f(x)];
});
var g = svg
.append("path")
.attr("d", pts_to_path(graph_points))
.attr('class', 'temp')
.attr("stroke", '#666666')
.attr("stroke-width", 3)
.attr("fill", "none");
}
return svg.node();
}