Skip to content

Instantly share code, notes, and snippets.

@osjayaprakash
Created August 25, 2012 07:39
Show Gist options
  • Save osjayaprakash/3462211 to your computer and use it in GitHub Desktop.
Save osjayaprakash/3462211 to your computer and use it in GitHub Desktop.
Maximum Liklihood Estimator for Laplace Density Function
/*
* Author : JAYAPRAKASH S
* Maximum liklihood estimator for Laplace density function
* Mail : first_name [at] cse [dot] iitb [dot] ac [dot] com
* Please mail me, if any error is found.
*/
import java.util.ArrayList;
import java.util.Collections;
public class Main {
/* MLE of m(Mew) and b */
double m,b;
/* samples drawn/taken for MLE task*/
ArrayList<Double> sample = new ArrayList<Double>();
int n;
void calc_m(String []a)
{
n = a.length;
if( n < 1 ) {
System.err.println("No Samples Given");
return ;
}
for( int i=0;i<n;i++)
sample.add ( Double.valueOf(a[i]) );
Collections.sort(sample);
if( n % 2 == 1)
m = sample.get(n/2);
else
{
/* we can take any value from sample[n-1/2] to sample[n/2] */
// here are taking mean of two
m = sample.get(n/2) + sample.get((n-1)/2);
m = m / 2;
}
System.out.println("Mew m = " + m);
System.out.println("b = " + calc_b());
System.out.println("f = " + calc_f());
}
public Double calc_b()
{
int n = sample.size();
for( int i=0;i<n;i++)
b = b + Math.abs(m - sample.get(i));
b = b / n;
return b;
}
public Double calc_f()
{
int n = sample.size();
Double f = 1.0,t;
for( int i=0;i<n;i++)
{
t = 1 / (2 * b);
t = t * Math.pow(Math.E, -1 * Math.abs(sample.get(i)- m) / b );
f = f *t;
}
return f;
}
public static void main(String []a)
{
Main c = new Main();
c.calc_m(a);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment