Skip to content

Instantly share code, notes, and snippets.

@jbn
Created September 14, 2010 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbn/579915 to your computer and use it in GitHub Desktop.
Save jbn/579915 to your computer and use it in GitHub Desktop.
public class Classifier{
public static boolean[] stringToBoolean(String s){
boolean[] b = new boolean[s.length()];
for(int i=0; i<s.length(); ++i){
b[i] = (s.charAt(i) == '1');
}
return b;
}
public static void main(String[] args){
boolean[] testA = stringToBoolean("10011001");
boolean[] testB = stringToBoolean("");
Classifier classifier = new Classifier(
stringToBoolean("10011001"),
stringToBoolean("01111000")
);
System.out.println(classifier.probabilityOfRetaliation);
System.out.println(classifier.probabilityOfForgiveness);
classifier = new Classifier(
stringToBoolean("00000000"),
stringToBoolean("11111111")
);
System.out.println(classifier.probabilityOfRetaliation);
System.out.println(classifier.probabilityOfForgiveness);
}
public final double probabilityOfRetaliation,
probabilityOfForgiveness;
/**
Creates a classification of a after a game of b
*/
public Classifier(boolean[] a, boolean[] b){
double slights=0, retaliations=0, forgivens=0;
for(int i=2; i<a.length; ++i){
// a cooperated then b defected.
if(a[i-2] && b[i]){
slights++;
if(a[i]){
forgivens++;
}else{
retaliations++;
}
}
}
probabilityOfForgiveness = forgivens/slights;
probabilityOfRetaliation = retaliations/slights;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment