0% found this document useful (0 votes)
18 views10 pages

SlidesCourse 21 Oct

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views10 pages

SlidesCourse 21 Oct

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

IE255 Course 21st October 2024 Part1

https://bucloud.bogazici.edu.tr/s/WABgda87beMqDBQ
IE255 Course 21st October 2024 Part2
https://bucloud.bogazici.edu.tr/s/73zEik6LZ3AQ2ZF

MIDTERM November 20 at 17.00 in M1100, M1170, M1171

Lecture Monday October 21st


Example: We cast a die 5 times: What is the probability that the number of “6” shown is at
least 2 and smaller than 4.
Solution: Without using R:
X ... # of 6 shown in 5 trials X~ binomial(n=5,p=1/6)
P( 2 < = X < 4) = f(2) + f(3) = “5 chose 2” (1/6)^2 (5/6)^3+ “5 choose 3” (1/6)^3 (5/6)^2
Solution using dbinom(): sum(dbinom(2:3 ,size=5, p=1/6))
Solution using pbinom(): pbinom(3,size=5, p=1/6) - pbinom(1 ,size=5, p=1/6)

Example Car Park: In an hour on average 20 cars arrive.


(We assume the arrivals follow a Poisson process.) realistic for eg 13.00 to 17.00
not realistic for long time intervals
Every car has to pay 3$ on entering.
You pay 50$ for the right to collect the fees for the cars arriving in one hour.
a1) What is the probability that more than 300 cars arrive in 10 hours?
X ... # cars in 10 hours X~Poisson(lam=200) P(X>300) ....
a) What is the probability that you loose money?
X~ X ... # cars in 1 hour X~Poisson(lam=20)
W = 3 X – 50 P(W < 0) = P( 3 X -50 <0) = P( X < 50/3) = ppois(16,lam=20)=0.221
b) E and Var of the money you earn. E(W) = 3 E(X) – 50 = ...
V(W) = 3^2 V(X)
c) What is the probability to wait less than 20 minutes for 10 cars?
d) What is the expectation and the variance of the waiting time till the 10th car?
a1) Y … # of cars that arrive in 10 hours Y~Poisson(lambda=20*10=200)
P(Y>300) = 1 –F(300) = 1-ppois(300,lam=200)= 1.79e-11
a) X … # of cars that arrive in one hour
X ~ Poisson(lam = 20)
W = 3.X -50 P(W<0) = P(3.X-50<0) = P(3.X < 50) = P(X < 50/3) = P(X<= 16) = F(16)
= ppois(16,lam=20)= 0.2210742
b) E(W) = E(3.X-50) = 3.E(X) – 50 = 60-50 = 10
V(W) = V(3.X-50) = 3^2.V(X) = 9 *20 = 180 SDev= sqrt(180) = 13.42
c) waiting time for the first car … exponential
waiting time for the 10th car. Sum of 10 independent exponential variates:
W …. Waiting time till the 10th car
W~Gamma(alpha=10,rate lam=20)
P(W< 20/60) = pgamma(20/60,shape=10,rate=20)= 0.1373715
Second possible way to solve c)
X … number of cars that arrive in 20 minutes
X ~Poisson(lam=20*(b-a)=20*(20-0)/60=20/3)
P(W<20min) = P(X>9) = 1 – F(9)=1 - ppois(9,lam=20/3) = 0.1373715
d) W…. Waiting time till the 10th car
W~Gamma(alpha=10,rate lam=20) E(W) =alpha/lam= 10/20 = 0.5 hours
V(W) =alpha/lam^2= 10/20^2 = 0.025 hours^2

5.8. A machine fills packages of flour. The exact quantity that is filled into the packages
follows a normal distribution with mean 1003 g and standard deviation 5g.
a) Calculate the probability that the filled quantity is less than 995g.
b) Calculate the approximate probability that the filled quantity is between
990.35 and 990.45g without using the R-command dnorm().
Compare the result with the exact result obtained with pnorm().
c) Find the filling quantity that is exceeded by 99% of all packages.
d) If a package is filled with less than 990g a fine of 10$ has to be paid.
For 500 filled packages find the expectation and the variance of the fine
and the probability that the fine is larger than 400$.
e) What is the probability that the fine is exactly 400$ ?
f) If a package is filled with less than 990g a fine of 10$ has to be paid.
Find the probability that for 10 packages at most 10$ fine must be paid. (f without R)
b) P(990.35< X <990.45) = integrate_990.35^990.45 f(x) dx =

Solutions:
a)
X ~ N(1003, sigma=5) P(X< 995) = pnorm((995-1003)/5)=pnorm(-8/5)=pnorm(-1.6)=0.0548
b) P(990.35<X<990.45) =
pnorm((990.45-1003)/5)- pnorm((990.35-1003)/5) [1] 0.0003334317
# approximate solution
dnorm(990.4,mean=1003,sd=5)*0.1 [1] 0.000333402
c) P(X < c) = 0.01 (ie. 1% quantile): mu+sigma*qnorm(0.01) = 1003+5*qnorm(0.01)
1003+5*qnorm(0.01) [1] 991.3683
d) Y ... # packages with less than 990 g out of the 500
Y~ binomial(n=500, p=pnorm((990-1003)/5) )
e) Fine = 10 Y P(Fine=400) = P(Y=40) = ... > 0
f) P(Y < = 1)

UNIT 6 Part 1: Random variate generation


Uniform Random number generation: U(0,1) (pseudo-)random numbers are generated
with deterministic recursions.
The generated numbers behave very similar to real independent U(0,1) variates.
In R runif(n) to generate vector of n independent U(0,1) RV
With set.seed() and <- .Random.seed

set.seed(123);runif(3)
[1] 0.2875775 0.7883051 0.4089769
> set.seed(123);runif(3)
[1] 0.2875775 0.7883051 0.4089769
> runif(3)
[1] 0.8830174 0.9404673 0.0455565
>
>
>
> myseed<- .Random.seed
> runif(4)
[1] 0.5281055 0.8924190 0.5514350 0.4566147
>
> .Random.seed <- myseed; runif(4)
[1] 0.5281055 0.8924190 0.5514350 0.4566147
Generating Continuous RVs:
Idea of “Inversion method” (book p53 it is called inverse transform method:)

X = F-1(U) = quantile(U)
For standard distributions and with R that is easy. Use qnorm(), qgamma() …
How can we find the quantile function (asked in Unit 5 questions)
….

Example 6.A: For a continuous random variate X with density


f(x) = 1 + x for -0.5 < x < 0.5 and 0 else
a) design an algorithm that generates random variates from X
b) Implement the algorithm in R
c) Compare the histogram of a sample of size n=1.e6 with the density.
d) Compare the exact expectation with the average of the sample.
a)
F(x) = x +x^2/2 +c F(-0.5) = 0 F(-0.5)= -0.5+0.125+c=0  c = 0.5-0.125 = 0.375
F(x) = x +x^2/2 +0.375
to obtain the inverse CDF we have to solve: u = x +x^2/2 +0.375 for x
x^2/2 +x + 0.375-u = 0
solution x12 = -1 +/- sqrt(1-4*0.5*(0.375-u)) =
= -1 +/- sqrt(1+2*u-0.75) =
F-1(u) = -1 + sqrt(0.25+2*u) for 0<u<1
OR the root with negative sign before sqrt()
we check for that solution F-1(0) = -1 + sqrt( 1/4) = -0.5 is the lower border of the domain
and F-1(1) = -1 + sqrt(2.25) = 0.5 is the upper border of the domain;
so selecting the “plus” sign before sqrt() was correct, everything ok
Algorithm: Generate a uniform U(0,1) rv U.
return( -1 +sqrt(0.25+2*u) ) (for u=0 should get -0.5, for u=1 gives 0.5)
b)
rf <- function(n){
u <- runif(n);
return( -1 +sqrt(0.25+2*u) )
}
c)
x <- rf(1.e6); hist(x,freq=F,breaks=100);
f <- function(x){ifelse(x> -0.5 & x<0.5,1+x,0)}
lines(xv<- seq(-1,1,0.001),f(xv))

d) mean(x) E(X) = integral_(x -0.5,0.5) x (1+x) dx = (x^2/2+x^3/3)_(-0.5 to 0.5) = 7/24-


(5/24)
0.08296508 = 2/24 =1/12 = 0.083333 results close
it is also possible to calculate E(X) using numeric integration with hte R-command
integrate()
Generating discrete RVs:
We can use a figure of the CDF of a discrete RV X:

We can use again inversion to generate from that random variate and can
use a simple sequential search to implement this idea in R.

Example 6.B: Discrete RV X with f(0)=0.1, f(1)=0.2, f(2) = 0.6, f(3)=0.1


a) Write R-code that generates from X using just runif() and simple arithmetic?
b) Generate X using a different R-command
a)
rB <- function(n){
u <-runif(n);
res<- rep(0,n)
res <- ifelse( u < 0.1, 0,
ifelse(u < 0.1+0.2, 1,
ifelse(u<0.1+0.2+0.6,2,3)))
return(res)
}
x<-rB(1.e5)
table(x)
#x
# 0 1 2 3
#10086 19892 60137 9885

To implement the same idea for a discrete RV with domain on 0, 1, 2, ...


and an arbitrary pmf is also no problem:
rdiscr <- function(n,pmfv=c(0.2,0.5,0.3),lb=0){
# n ... number of RVs generated
# pmfv vector of values of the pmf
# lb ... lowest value of the domain
u <-runif(n);
res <- rep(lb,n)
cdf <- 0
for(i in 1:(length(pmfv)-1)){
cdf <- cdf + pmfv[i]
iv <- u > cdf
res[iv] <- res[iv] + 1
}
return(res)
}
x<-rdiscr(1.e5, pmfv=c(0.2,0.5,0.3))
table(x)
x
0 1 2
20008 49958 30034
x<-rdiscr(1.e6, pmfv= dbinom(0:8,8,0.4))
table(x)
x
0 1 2 3 4 5 6 7 8
16671 89356 208983 279457 232084 123428 41494 7877 650

b)
In R also the function “sample()” can be used
?sample
X<- sample(1:3,size=1.e6,replace=T,prob=c(0.5,0.3,0.2))
table(X)
X
1 2 3
500397 299699 199904

To generate RVs of the binomial distribution with n=8 and p=0.4 :


X<- sample(0:8,size=1.e6,replace=T,prob=dbinom(0:8,8,0.4))
table(X)
X
# 0 1 2 3 4 5 6 7 8
#16984 89391 209249 279280 231553 123848 41136 7928 631
round(dbinom(0:8,8,0.4),5)
#.01680 .08958 .20902 .27869 .23224 .12386 .04129 .00786 .00066

UNIT 6 Part 2: Newsboy Problem


A newsboy can select his stock s in the morning of the day and buys “s” newspapers at a
price of “p0”=50. During the day he sells his newspapers at a price of “p1”=100. At the end
of the day the newspapers are thrown away as they have no value anymore.
How many papers should a newsboy buy in the morning?
Aim is that he can sell as many as possible but does not have too many in the end.

Of course the selection of the stock s of newspapers depends on the distribution of the
random demand D.
If D is deterministic and known the solution is clear. The newsboy has to take s=D papers.
If the demand is known to have the pmf f(10)=0.25, f(11)=0.5 and f(12)=0.25 things get
more interesting. We define the random variable: N ... Net Profit
Clear that a newsboy wants to select s such that he maximizes N.
Selecting swe can calculate the values of N = min(D,s)*p1 – s*p0
Note that it is not difficult to prove that s < 10 or s > 12 is never optimal !
s= 10 s=11 s=12
D = 10, 11, 12 D = 10, 11, 12 D = 10, 11, 12
N = 500 500 500 N = 450 550 500 N = 400 500 600
If we want to avoid risk s=10 is best as the Variance of N is 0.
Of course it is sensible to compare the expectations of N:
s=10: E(N) =500 s=11 E(N) = (450+500)*0.25 + 550*0.5 = 512.5
s=12 E(N) = (400+600)*0.25+ 0.5*500 = 500

typical approach: maximize the expected Net Profit.


Sensible approach if the “game” is repeated often

for just one investment: important to also consider the size of the risk
risk = variance of the net profit.
our example: s=10 is better than s=12 as it has a smaller variance and the same expected
Net profit.

...

If we assume the general situation where the demand D is a discrete random variate with
known pmf f(x) it is not difficult to define the net profit and to find its distribution:
As notation we use:
s … the stock level for that day (it is the decision variable of the optimisation)
p0 … price the newsboy has to pay for the papers
p1 … price the newsboy gets for sold papers
D… daily demand; discrete random variable with pmf f(x) for x= 0,1,2,…
Sold … number of sold paper; it clear that we can never sell more than s or more than D.
Thus
Sold = min( s, D)
We define:
"net profit" = N = Sold*p1 – s*p0
The next step is that we have to find the pmf of “Sold” f_S. To do that we can rewrite
the definition of sold:
Sold = D for D<=s
s for D>=s
pmf of Sold = f_S(x) = P(Sold=x) = P(D=x) for x <s.
But for x = s we have f_S(x) =P(Sold=x) = P(D>=s) = 1-P(D<s) for x = s
So we can write:f_S(x) = f(x) for x < s
f_S(x) = 1-P(D<s) = 1- sum_(i = 0,1,2,…,s-1) f(i) for x=s
f_S(x) = 0 for x > s
So in words: for x <= s-1 the pmf of Sold is equal to the pmf of the demand.
for x = s the pmf of Sold is equal to Prob(D>=s) = 1 –Prob(D<s)
for x > s the pmf of Sold is 0

Definition Conditional Expectation for discrete RVs: eg E(X| X>3)


Using the definition of the pmf and of the conditional probability we get the
conditional pmf: f(x|X>=s) = P(X=x|X>=s) = P(X=x and X>=s)/P(X>=s) = P(X=x)/P(X>=s) =
= P(X=x)/(1-F(s-1)) for x >= s
f(x|X>=s) = P(X=x|X>=s) = 0 for x < s
Thus E(X|X>=s) = sum_(x=s,…,infinity) x . f(c|X>=s) = sum_(x=s,…,infinity) x . P(X=x)/P(X>=s)
Simple example: X~Poisson(lam=20)
E(X|X>=10) = sum( (10:100)*dpois(10:100,lam=20)/sum(dpois(10:100,lam=20)) , lam=20)
sum((10:100)*dpois(10:100,lam=20)/sum(dpois(10:100,lam=20)),lam=20 )
#[1] 40.05846 # E(X|X>=10)
sum((10:200)*dpois(10:200,lam=20)/sum(dpois(10:200,lam=20)),lam=20 )
#[1] 40.05846

General code is given in Video 6.e

You might also like