Skip to content

Instantly share code, notes, and snippets.

@niteshjindal-7
Last active November 15, 2020 11:36
Show Gist options
  • Save niteshjindal-7/3db3d3e36eecf4382f77f0f959793244 to your computer and use it in GitHub Desktop.
Save niteshjindal-7/3db3d3e36eecf4382f77f0f959793244 to your computer and use it in GitHub Desktop.
Maximum Likelihood Estimation
#Maximum Likelihood Estimation for a univariate Distribution.
library(dplyr)
'create dataframe with two columns of 0 values'
m = 20
n = 2
df = data.frame(matrix(0, nrow = m, ncol = n, dimnames = list(NULL, paste0("ColumnName_", 1:n))))
names(df) = c("member_id", "income")
'update member_id column values '
for(i in 1:nrow(df)){
df$member_id[i] = i
}
'update income column values'
income <- c(131,107,88,75,83,136,72,113,109,130,80,97,98,92,76,103,84,91,124,82)
for(i in 1:nrow(df)){
df$income[i] = income[i]
}
'define range of values for population mean(??)'
population_mean_vec = seq(92, 102, 1)
population_var_vec = (seq(360, 395, 1) )
'log likelihood values for samples with different mean and variance = 380'
log_likelihood_vec = c()
for(i in 1:length(population_mean_vec))
{
likelihood_val = round(dnorm(df$income, mean = population_mean_vec[i], sd = sqrt(380), log = FALSE), 4)
# dnorm returns the value of the probability density function for the normal distribution given parameters for x, ??, and ??
df$likelihood_value = likelihood_val #add a new column with the likelihood values corresponding to each obs
df$log_likelihood_value = round(log(df$likelihood_value), 4)
log_likelihood_vec[i] = sum(df$log_likelihood_value)
# sum of log likelihood values = -87.7661 for mean = 100 and standard deviation = sqrt(380)
}
'plot the log likelihoo values of samples with different population mean parameter'
plot(population_mean_vec, log_likelihood_vec, type = "b", xlab=" Population Mean Values",ylab="Sample Log Likelihood Values")
grid()
group <- data.frame(population_mean_vec, log_likelihood_vec)
max_mean = subset(group, log_likelihood_vec == max(log_likelihood_vec))[, c("population_mean_vec")]
'log likelihood values for samples with mean = max_mean = 98 and different values of population standard deviation'
log_likelihood_vec = c()
for(j in 1:length(population_var_vec))
{
likelihood_val = round(dnorm(df$income, mean = max_mean, sd = sqrt(population_var_vec[j]), log = FALSE), 4)
# dnorm returns the value of the probability density function for the normal distribution given parameters for x, µ, and σ2
df$likelihood_value = likelihood_val #add a new column with the likelihood values corresponding to each obs
df$log_likelihood_value = round(log(df$likelihood_value), 4)
log_likelihood_vec[j] = sum(df$log_likelihood_value)
# sum of log likelihood values = -87.7661 for mean = 100 and standard deviation = sqrt(380)
}
'plot the log likelihoo values of samples with different population variance parameter'
plot(population_var_vec, log_likelihood_vec, type = "b", xlab=" Population Variance Values",ylab="Sample Log Likelihood Values",
)
group1 <- data.frame(population_var_vec, log_likelihood_vec)
max_var = subset(group1, log_likelihood_vec == max(log_likelihood_vec))[, c("population_var_vec")] #
max_mean
max_var
mean(df$income)
var(df$income)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment