'Solving the lighthouse problem for unknown alpha and beta
I am trying to solve the lighthouse problem (see here for an overview http://pmaweb.caltech.edu/~physlab/lab_21_current/Ph21_4a_Bayesian_Analysis.pdf). First I solved it for known beta and unknown alpha successfully with code as follows:
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import cauchy
a = 3
b = 3
samples = cauchy.rvs(loc = a, scale = b, size = 1000)
def posterior(x, a, b):
post = np.ones(len(a))
for x_k in x:
post *= b / (np.pi * (b ** 2 + (x_k - a) ** 2))
post /= np.sum(post)
return post
def plot_posterior(n_samples):
a_s = np.linspace(0, 5, 1001)
plt.plot(alphas, posterior(samples[:n_samples], a_s, b))
for i in (1, 2, 4, 8, 16, 32, 64, 128, 256):
plot_posterior(i)
plt.annotate('N=' + str(i), xy=(1, 0.1), xycoords='axes fraction', fontsize=12,
horizontalalignment='right', verticalalignment='bottom')
plt.show()
Now I am trying to solve it for both unknown alpha and beta by modifying the plot_posterior function as follows to create a contour plot:
def plot_posterior(n_samples):
a_s = np.linspace(0, 5, 1001)
b_s = np.linspace(0,5,1001)
[X,Y] = np.meshgrid(a_s ,b_s )
Z = posterior(samples[:n_samples], a_s , b_s )
plt.contour(X,Y,Z)
But I keep getting errors like "TypeError: Cannot interpret '1001' as a data type", any ideas where I am going wrong?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
