4
$\begingroup$

Suppose, we have 100 observations of variable X. X is the day of the week (Sunday – Saturday) when a specific event occurs on regular basis. E.g. when a postman delivers a package every week for 100 weeks. Note: the event typically should happen on the same day of the week, unless there are special cases (holiday, etc). Let’s take the following distribution:

Sunday: 78
Monday: 5
Wednesday: 9
Saturday: 8
Sum = 100

Based on the above data, we need to predict the day of the week the postman delivers a package next week and it's certainty in %.

Possible Solution 78% of the time the delivery day is Sunday. Educated guess tells us that since we expect the event to happen on same day every week (except holidays, etc), the expected package day is Sunday. Binomial uncertainty of that is: 0.78*(1-0.78)/sqrt(100) = 0.017 Using bootstrap in R:

library(boot)

x = c(rep(1,78),rep(2,5),rep(3,0),rep(4,9),rep(5,0),rep(6,0),rep(7,8))

h = hist(x+0.01,breaks=c(1,2,3,4,5,6,7,8))
plot(h)

GetMaxBinFractionBoot = function(x,i){
  .sample = x[i]
  .hist = hist(.sample+0.01,breaks=c(1,2,3,4,5,6,7,8),plot=FALSE)
  .max = max(.hist$counts)
  .frac = .max / length(.sample)
  return(.frac)
}

GetMaxBinPositionBoot = function(x,i){
  .sample = x[i]
  .hist = hist(.sample+0.01,breaks=c(1,2,3,4,5,6,7,8),plot=FALSE)
  .pos = which.max(.hist$counts)
  return(.pos)  
}

myBoot = boot(data=x,
              statistic=GetMaxBinFractionBoot,
              R=100)
print(myBoot)
hist(myBoot$t)

myBoot2 = boot(data=x,
               statistic=GetMaxBinPositionBoot,
               R=1000)
print(myBoot2)
hist(myBoot2$t)

Results from R:

Call:
boot(data = x, statistic = GetMaxBinFractionBoot, R = 100)


Bootstrap Statistics :
    original  bias    std. error
t1*     0.78   0.004      0.0432

Call:
boot(data = x, statistic = GetMaxBinPositionBoot, R = 1000)


Bootstrap Statistics :
    original  bias    std. error
t1*        1       0           0

And then I am stuck with the second part of the problem - how to quantify how certain we are that the day is Sunday in this case. Any help greatly appreciated! Thank you!

$\endgroup$

1 Answer 1

2
$\begingroup$

This problem is a natural fit for straightforward Bayesian methods. In Bayesian terms, what you're asking is the posterior probability that the next draw from this categorical distribution will be Sunday.

The answer depends on the prior distribution we choose for this variable, which can be thought of as our beliefs, prior to seeing any data, about what day the postman would deliver the package. A prior that expresses a weak belief that each day is equally likely seems reasonable. The conjugate prior of the categorical distribution is the Dirichlet, and a flat Dirichlet, with parameters (1, 1, 1, 1, 1, 1, 1), expresses a weak belief in uniformity, as desired.

Now the posterior probability of a new draw being each day of the week is easy to calculate: it's equal to the ratio of observations for that day (plus 1 for the prior) to the total number of observations (plus 7 for the prior). We get

(79 / 107, 6 / 107, 1 / 107, 10 / 107, 1 / 107, 1 / 107, 9 / 107)
= (.738, .056, .009, .093, .009, 0.009, 0.084).

The highest probability is the one for Sunday, so that's the single most likely day. Its value is .738, so the package's probability of delivery on Sunday is .738.

Notice that .738 is a little less than 78%, which is the observed rate in the data. This makes sense because we have a lot of data relative to the prior, so our posterior belief should mostly reflect the data, with a slight slant towards our prior belief in uniformity (towards .143, or 1/7).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.