# the German tank problem

the Germans have a population of $n$ tanks labeled with serial numbers $1,2,...,n$ on the tanks. The number of tanks $n$ is unknown and of interest to the Allied forces. The Allied forces randomly capture $k$ tanks from the Germans with replacement and observe their serial numbers $\{x_1, x_2, ..., x_k\}$. The goal is to, from observing the serial numbers on this random sample of the tanks, estimate $n$.

the *estimator* of $n$ maps an outcome of the experiment to an estimate of $n$, $\hat{n}$.


```julia
using StatsBase
using PyPlot
using Statistics
using Printf

PyPlot.matplotlib.style.use("seaborn-pastel")

rcParams = PyPlot.PyDict(PyPlot.matplotlib."rcParams")
rcParams["font.size"] = 16;
```

    ┌ Info: Recompiling stale cache file /home/mick/.julia/compiled/v1.2/StatsBase/EZjIG.ji for StatsBase [2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91]
    └ @ Base loading.jl:1240


## data structure for a tank

for elegance


```julia
struct Tank
    serial_no::Int
end
tank = Tank(3)
```




    Tank(3)



## visualizing the captured tanks and their serial numbers


```julia
function viz_tanks(tanks::Array{Tank}, savename=nothing)
    nb_tanks = length(tanks)
    
    img = PyPlot.matplotlib.image.imread("tank.png")

    fig, ax = subplots(1, nb_tanks)
    for (t, tank) in enumerate(tanks)
        ax[t].imshow(img)
        ax[t].set_title(tank.serial_no)
        ax[t].axis("off")
    end
    tight_layout()
    
    if ! isnothing(savename)
        savefig(savename * ".png", format="png", dpi=300)
        # Linux command line tool to trim white space
        run(`convert $savename.png -trim $savename.png`)
    end
end

n = 7
tanks = [Tank(s) for s in 1:n]
viz_tanks(tanks)
```

## simulating tank capture

write a function `capture_tanks` to simulate the random sampling of `nb_tanks_captured` tanks from all `nb_tanks` tanks the Germans have (without replacement). return a random sample of tanks.


```julia
function capture_tanks(num_captured::Int, num_tanks::Int)
    return sample([Tank(i) for i in 1:num_tanks], num_captured, replace=false)
end

viz_tanks(capture_tanks(3, 100))
```

## defining different estimators

an estimator maps an outcome $\{x_1, x_2, ..., x_k\}$ to an estimate for $n$, $\hat{n}$.

### estimator (1): maximum serial number

this is the maximum likelihood estimator.

\begin{equation}
\hat{n} = \max_i x_i
\end{equation}


```julia
function max_serial_no(captured_tanks::Array{Tank})
    return maximum([t.serial_no for t in captured_tanks])
end

max_serial_no(tanks)
```




    7



### estimator (2): maximum serial number plus initial gap

\begin{equation}
\hat{n} = \max_i x_i + \bigl(\min_i x_i -1\bigr)
\end{equation}


```julia
function max_plus_initial_gap(captured_tanks::Array{Tank})
    gap_adjustment = minimum([t.serial_no for t in captured_tanks]) - 1
    return max_serial_no(captured_tanks) + gap_adjustment
end

max_plus_initial_gap(tanks)
```




    7



### estimator (3): maximum serial number plus gap if samples are evenly spaced

\begin{equation}
\hat{n} = \max_i x_i + \bigl( \max_i x_i / k -1 \bigr)
\end{equation}


```julia
function max_plus_even_gap(captured_tanks::Array{Tank})
    gap_adjustment = maximum([t.serial_no for t in captured_tanks]) / length(captured_tanks) - 1
    return max_serial_no(captured_tanks) + Int(gap_adjustment)
end

max_plus_even_gap(tanks)
```




    7



## assessing the bias and variance of different estimators

say the Germans have `nb_tanks` tanks, and we randomly capture `nb_tanks_captured`. what is the distribution of the estimators (over different outcomes of this random experiment), and how does the distribution compare to the true `nb_tanks`?


```julia

```


```julia

```


```julia

```

notes:

## what happens as we capture more and more tanks, i.e. increase $k$?

assess estimator (3).


```julia

```

## one-sided confidence interval

how confident are we that the Germans don't have *more* tanks?

significance level: $\alpha$

test statistic = estimator (3) = $\hat{n} = \max_i x_i + \bigl( \max_i x_i / k -1 \bigr)$

**null hypothesis**: the number of tanks is $n=n_0$<br>
**alternative hypothesis**: the number of tanks is less than $n_0$

we reject the null hypothesis (say, "the data does not support the null hypothesis") that the number of tanks is $n=n_0$ if the p-value is less than $\alpha$. the p-value is the probability that, if the null hypothesis is true, we get a test statistic equal to or smaller than we observed.

we want to find the highest $n_0$ such that we have statistical power to reject the null hypothesis in favor of the alternative hypothesis. this is the upper bound on the confidence interval!

then the idea is that, if the null hypothesis is that the number of tanks is absurdly large compared to the largest serial number we saw in our sample, it would be very unlikely that we would see such small serial numbers compared to the number of tanks, so we'd reject the null hypothesis. 


```julia

```

say $\alpha=0.05$ and we seek a 95% one-sided confidence interval


```julia

```


```julia

```

209f69338c4a77ac8a0951b7e7c5ce7cc1d797f4
ipynb
Jupyter Notebook
In-Class Notes/German Tank Problem/.ipynb_checkpoints/German tank problem_sparse-checkpoint.ipynb
cartemic/CHE-599-intro-to-data-science
a2afe72b51a3b9e844de94d59961bedc3534a405
MIT
In-Class Notes/German Tank Problem/.ipynb_checkpoints/German tank problem_sparse-checkpoint.ipynb
cartemic/CHE-599-intro-to-data-science
a2afe72b51a3b9e844de94d59961bedc3534a405
MIT
In-Class Notes/German Tank Problem/.ipynb_checkpoints/German tank problem_sparse-checkpoint.ipynb
cartemic/CHE-599-intro-to-data-science
a2afe72b51a3b9e844de94d59961bedc3534a405
MIT
2019-10-02T16:11:36.000Z
2019-10-15T20:10:40.000Z
Qwen/Qwen-72B
1. YES
2. YES