
## com157a of august 2025,
#  doing streetcars of San Francisco again, for Bayes;
#  check exercise 8.8 though for more and for CD

#  likelihood is (1/N)^n for y_i \le N,
#  i.e. N \ge y_\max, with n the number of streetcars seen 

Nupper <- 2000
Nval <- 1:Nupper

pival <- 1/Nupper

y <- 203

post0val <- pival*(1/Nval)*(Nval >= y)
postvalA <- post0val/sum(post0val)

yy <- c(203,157,222)
nn <- length(yy) 
ymax <- max(yy)

post0val <- pival*(1/Nval)^nn*(Nval >= ymax)
postvalB <- post0val/sum(post0val)

par(bty="l")
par(cex.lab=1.4142) 

matplot(Nval,cbind(postvalA,postvalB),type="l",lwd=3,
   xlab="the number of streetcars",
   ylab="after 1 datapoint and after 3 datapoints")

## posterior means:
sum(Nval*postvalA) # 785.01 
sum(Nval*postvalB) # 398.84

# then checking to read off medians:
# 636 after 1 datapoint
# 312 after 3 datapoints 

cbind(Nval,cumsum(postvalA),cumsum(postvalB))[1:640, ] # etc. 

# posterior modes, with 0-1 loss function: 

estimA <- Nval[postvalA == max(postvalA)] # 203
estimB <- Nval[postvalB == max(postvalB)] # 222 

