i'm simulating the effect of log maximum likelihood estimate (for Bionomial distribution) as number of samples increases. I choose a true parameter value of 0.6 for bionomial distribution from which the responses are coming from.
but i'm getting the error even when i remove 0 from the possible parameters i'm using for my analysis.
- fsd
fods23/simulations/scripts/Binomial_MLE_simulations.py:19: RuntimeWarning: divide by zero encountered in log
cal_llh = np.log(theta**(x) * (1-theta)**(1-x))
############################################################
## Step 1#
############################################################
# function to calculate likelihood for bernoulli
def logLikelihood(theta, x):
# cal the log likelihood of each observation in the samples collected
cal_llh = np.log(theta**(x) * (1-theta)**(1-x))
tlld = np.prod(cal_llh)# cal the total likleihood
return tlld
# function to calculate
def mle_Binom(X_samples, thetas):
loglikelihood_single_theta = [logLikelihood(theta=t, x=X_samples) for t in thetas]
# mle_val=thetas[np.argmax(likelihood_single_theta)] #get the maximum likelihood estimate
return np.array(loglikelihood_single_theta)
# test the functions
true_params_Bern = 0.6
############################################################
## Step 2#
############################################################
# how does the likelihood plot changes as sample size changes
Bern_Nsamples = np.linspace(start=100, stop=1000, num=100, dtype=int)
response_Bernoulli = np.random.binomial(n=1, p=0.6, size=100)
possible_thetas = np.linspace(start=0.001, stop=1, num=100)
result_theta = np.ma.array(possible_thetas.copy())
Bern_Nsamples = np.linspace(start=100, stop=1000, num=100, dtype=int)
beta_for_mle_holder = []
def Bernoulli_optim_nSamples(Bern_stepSize, rand_sets_thetas):
for n in Bern_stepSize:
response_Bernoulli = np.random.binomial(n=1, p=0.6, size=n)
mle_out_Binom = mle_Binom(X_samples=response_Bernoulli, thetas=rand_sets_thetas) #cal lld of specific theta
max_theta_Binom = rand_sets_thetas[np.argmax(mle_out_Binom)] #which theta gave us max lld
beta_for_mle_holder.append(max_theta_Binom)
fig, ax = plt.subplots()
ax.plot(Bern_stepSize, beta_for_mle_holder)
ax.set_title('Bernoulli dist nSamples vrs MLE')
ax.hlines(y=0.6, xmin=min(Bern_stepSize), xmax=max(Bern_stepSize), linestyles="dashed", color="red", label="MLE")
plt.xlabel("nSamples")
plt.ylabel("MLE")
plt.show()
Bernoulli_optim_nSamples(Bern_stepSize=Bern_Nsamples, rand_sets_thetas=result_theta)
If I correctly understood your problem, you want to perform MLE to solve the probability
pfor BinomialB(k,n=1,p)using MLE and display the Likelihood curve to contextualize the optimum found.Lets create a MCVE to do so:
The MLE problem is formulated as follow (we use negative likelihood as we will minimize):
We create a law with your parameters:
And sample from it:
Now solving the MLE problem is as simple as:
Now we vectorize the objective function:
And draw its value for several possible parameters:
Graphically we have: