MLE Cauchi distrubution

Hi

I'v written a code for MLE cauchi distrubtion.
I tried to make it as complicated as possible, We have competition to write code as complicated as possible . (for example add "for" loops or "if" with break, because now I have only "while" loop ).
does anyone is good with complex codes? (:wink:

here is my code -

#load my data
data<-read.table(file.choose(), header = TRUE, sep= "")
my_vec<-as.numeric(as.character(unlist(data[[1]])))

nr_method<-function(x,my_eps=0.001){

start_value<-median(x)
n=length(x);
theta<-start_value
#This is my main function
main_fun <- function (theta, x)
{
-length(my_vec)*log(pi) - sum(log(1+(my_vec-theta)^2))

}

#my first deriviative
first_derivate<-2*sum((x-theta)/(1+(x-theta)^2))
#using Newton Raphson method until the first derivative

while(abs(first_derivate)>my_eps){
#my second derivative
second_derivate<-2sum(((x-theta)^2-1)/(1+(x-theta)^2)^2)
#update the estimate of theta
theta_new<-theta-first_derivate/second_derivate
theta<-theta_new; #collecting results
# again first derivative
first_derivate<-2
sum((x-theta)/(1+(x-theta)^2))

}
theta
}

nr_method(my_vec)

main_fun(theta = median(my_vec), x = my_vec)

my_theta<-seq(-10,10,by=0.1)
y_axi<-rep(0,length(my_theta) , by= 0.1)

for (i in 1:length(my_theta)) {

y_axi[i] <- main_fun(median(my_theta[i]),x = my_vec)

}
plot(my_theta ,y_axi,type="l")

try1<-nr_method(x = my_vec) # my try
abline(v = try1)

Thank you!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.