Chapter 5 Estimating Demand

set.seed(123456789)
N <- 1000
u <- sort(rnorm(N, mean = 1, sd = 3))

p <- 2
mean(u - p > 0)
## [1] 0.386
1 - pnorm(p, mean = 1, sd = 3)
## [1] 0.3694413
p <- runif(9, min = -10, max = 10)
s <- matrix(NA, length(p), 1)
for (i in 1:length(p)) {
  s[i, 1] <- mean(u - p[i] > 0)
}
s
##        [,1]
##  [1,] 0.968
##  [2,] 0.999
##  [3,] 0.875
##  [4,] 0.092
##  [5,] 0.996
##  [6,] 0.809
##  [7,] 0.936
##  [8,] 0.503
##  [9,] 0.086
plot(1 - ecdf(u)(u), u, type = "l", lwd = 3, lty = 1, col = 1,
     xlab = "s", ylab = "p", xlim = c(0, 1))
lines(sort(s), p[order(s)], type = "l", lwd = 3, lty = 2)
abline(h = 0, lty = 2)
legend("bottomleft", c("True", "Est."), lwd = 3, lty = c(1:2))