Your mobile phone is constantly trying to keep track of where you are. At any given point in time, for all nearby locations, your phone stores a probability that you are in that location.
Right now your phone believes that you are in one of the $100\times 100$ discretized grid cells with some probabilities. Those probabilities are given in $P^{\text{prior}}\in\R^{100\times 100}$, where $P^{\text{prior}}_{ij}$ is the probability that you are at $(i,j)$-th discretized cell. The matrix is shown below.
import numpy as np
import matplotlib.pyplot as plt
N = 100
P_prior = np.zeros((N,N))
x_j = np.arange(N)
x_i = x_j
W = np.array([[200, 150], [150, 400]])*2
inv_W = np.linalg.inv(W)
b = np.array([65, 55])
for i in range(N):
for j in range(N):
x = [x_j[j], x_i[i]]
P_prior[i,j] = np.exp(-(x-b).T@inv_W@(x-b))
P_prior[:50,:45] = 0
P_prior[80:,60:] = 0
P_prior /= np.sum(P_prior)
plt.figure(figsize=(8,6), dpi=100)
plt.imshow(P_prior, cmap='gray')
plt.colorbar()
plt.xlabel(r'$j$-coordinate')
plt.ylabel(r'$i$-coordinate')
plt.title('Prior belief of location')
plt.show()
Small questions:
(Problem 1) What is the sum of all the probabilities, $\sum_{i,j}P_{ij}$?
# your code here
So in the picture, the white locations (with large $P^\text{prior}_{ij}$) are where you probably are , and the dark locations (with small $P^\text{prior}_{ij}$) are where you are not likely to be. If someone asks for the user's position now, you will want to find the position with highest $P^\text{prior}_{ij}$.
(Problem 2) What is the location at which the user is most likely to be?
# your code here
Now your phone connects to some known base towers and records two bars of signal. For each grid location $(i,j)$, you know the probability of observing two bars from this particular tower, given that the mobile phone is in location $(i,j)$. That is, you know $P(\text{observe two bar} \mid \text{you are at} (i,j))$. We denote this probability by $P^\text{observe}_{ij}$, and the matrix $P^\text{observe}$ is shown below. Note that this value is based on knowledge of the dynamics of this particular base tower and stochasticity of signal strength.
P_observe = np.zeros((N,N))
for i in range(N):
for j in range(N):
dist1 = np.linalg.norm([x_j[j]-80, x_i[i]-0])
prob1 = np.exp(-(dist1-50)**2/200)*0.9
dist2 = (x_j[j]+x_i[i])
prob2 = np.exp(-(dist2-50)**2/1000)*0.5
P_observe[i,j] = np.max([prob1, prob2])
plt.figure(figsize=(8,6), dpi=100)
plt.imshow(P_observe, cmap='gray')
plt.colorbar()
plt.xlabel(r'$j$-coordinate')
plt.ylabel(r'$i$-coordinate')
plt.title('Probability of observing two bars')
plt.show()
(Problem 3) For each of the 10000 location positions, calculate and plot the new probability that the user is in each location given the base tower observation (the two bar observation). Recall the Bayes' theorem giving
# your code here
(Problem 4) Now someone again asks for the user's position. What is your updated answer?
# your code here