-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBernoulliLikelihood.java
More file actions
90 lines (73 loc) · 2.82 KB
/
BernoulliLikelihood.java
File metadata and controls
90 lines (73 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package cs475;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
public class BernoulliLikelihood {
static public LinkedList<Option> options = new LinkedList<Option>();
public static void main(String[] args) throws IOException {
// Parse the command line.
String[] manditory_args = { "data"};
createCommandLineOptions();
CommandLineUtilities.initCommandLineParameters(args, BernoulliLikelihood.options, manditory_args);
String dataFile = CommandLineUtilities.getOptionValue("data");
BernoulliLikelihood bl = new BernoulliLikelihood();
ArrayList<Integer> data = bl.readData(dataFile);
double parameter = bl.computeMaximumLikelihood(data);
double llhood = bl.computeLogLikelihood(data, parameter);
System.out.println("Maximum Likelihood Solution: " + Double.toString(parameter));
System.out.println("Log-likelihood: " + Double.toString(llhood));
}
public static void registerOption(String option_name, String arg_name, boolean has_arg, String description) {
OptionBuilder.withArgName(arg_name);
OptionBuilder.hasArg(has_arg);
OptionBuilder.withDescription(description);
Option option = OptionBuilder.create(option_name);
BernoulliLikelihood.options.add(option);
}
private static void createCommandLineOptions() {
registerOption("data", "String", true, "The data file to read.");
}
public double computeMaximumLikelihood(ArrayList<Integer> data) {
// TODO: Fill in here
// float result = 0;
int sum = 0;
for(int i=0; i<data.size();i++){
sum = sum + data.get(i);
// System.out.println(data.get(i));
}
// result = (float)sum/data.size();
// System.out.println(result);
return (double)sum/data.size();
}
public double computeLogLikelihood(ArrayList<Integer> data, double parameter) {
// TODO: Fill in here
int n = data.size();
int sum = 0;
for(int i=0; i<data.size();i++){
sum = sum + data.get(i);
}
// double y = computeMaximumLikelihood(data);
double result = sum*Math.log(parameter)+(data.size()-sum)*Math.log(1-parameter);
return result;
}
public ArrayList<Integer> readData(String filename) throws FileNotFoundException {
Scanner scanner = new Scanner(new BufferedInputStream(new FileInputStream(filename)));
ArrayList<Integer> data = new ArrayList<Integer>();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.trim().length() == 0)
continue;
String result = line.trim();
int value = Integer.parseInt(result);
data.add(value);
}
scanner.close();
return data;
}
}