Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Borenstein committed Nov 19, 2012
0 parents commit 8ae5334
Show file tree
Hide file tree
Showing 11 changed files with 38,130 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## Examples of Applying Bayes Rule in Processing

This repository collects a series of Processing sketches demonstrating the use Bayes Rule. It was created as part of [Makematics at ITP in Fall 2012](http://makematics.com/syllabus/2012-fall).

What follows is a description of the sketches contained herein:

### A Basic Demonstration of Bayes Rule

Expressed in the classic form of an imperfect test for a rare disease. Calculates all four probabilities possible with the two different test results.

### Bayesian Localization

A demonstration of determining the position of a "robot" in 1 dimension given a set of uncertain sensor readings.

<object type="application/x-shockwave-flash" width="400" height="220" data="http://www.flickr.com/apps/video/stewart.swf?v=109786" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"> <param name="flashvars" value="intl_lang=en-us&photo_secret=05d2efc992&photo_id=8198296559"></param> <param name="movie" value="http://www.flickr.com/apps/video/stewart.swf?v=109786"></param> <param name="bgcolor" value="#000000"></param> <param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/video/stewart.swf?v=109786" bgcolor="#000000" allowfullscreen="true" flashvars="intl_lang=en-us&photo_secret=05d2efc992&photo_id=8198296559" height="220" width="400"></embed></object>

This was adapted from Unit 1 of Sebastian Thrun's [Udacity Self-Driving Car class](http://www.udacity.com/overview/Course/cs373/CourseRev/apr2012)

### Bayesian Spam Filtering

An implementation of Paul Graham's classic technique from [A Plan for Spam](http://www.paulgraham.com/spam.html). Adapted from [this java implementation by Dan Shiffman](http://www.shiffman.net/teaching/a2z/bayesian/).
22 changes: 22 additions & 0 deletions basic_bayes_rule/basic_bayes_rule.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
float frequencyOfDisease = 0.01;

float accuracyOfTest = 0.95;

float totalPositiveProbability = (accuracyOfTest*frequencyOfDisease) + (1-accuracyOfTest)*(1-frequencyOfDisease);

float probabilityOfDisesaeGivenPositiveTest = (accuracyOfTest*frequencyOfDisease)/totalPositiveProbability;
float probabilityOfHealthGivenPositiveTest = ((1-accuracyOfTest)*(1-frequencyOfDisease))/totalPositiveProbability;

float totalNegativeProbability = (accuracyOfTest * (1- frequencyOfDisease)) + (1-accuracyOfTest)*(frequencyOfDisease);


float probabilityOfDiseaseGivenNegativeTest = ((1-accuracyOfTest)*frequencyOfDisease)/totalNegativeProbability;
float probabilityOfHealthGivenNegativeTest = (accuracyOfTest*(1-frequencyOfDisease))/totalNegativeProbability;

println("probabilityOfDisesaeGivenPositiveTest: " + probabilityOfDisesaeGivenPositiveTest);
println("probabilityOfHealthGivenPositiveTest: " + probabilityOfHealthGivenPositiveTest);
println(probabilityOfHealthGivenPositiveTest + probabilityOfDisesaeGivenPositiveTest);

println("probabilityOfDiseaseGivenNegativeTest: " + probabilityOfDiseaseGivenNegativeTest);
println("probabilityOfHealthGivenNegativeTest: " + probabilityOfHealthGivenNegativeTest);
println(probabilityOfDiseaseGivenNegativeTest + probabilityOfHealthGivenNegativeTest);
176 changes: 176 additions & 0 deletions bayesian_localization/bayesian_localization.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
boolean[] cells;
float[] probabilities;

int numCells = 10;
int currentPosition;
int doorWidth = 20;
int doorHeight = 50;
// inaccurate senor probabilites
float hitError = 0.6;
float missError = 0.2;

boolean showPosition = false;

void setup() {
size(400, 400);
cells = new boolean[numCells];
probabilities = new float[numCells];
for (int i = 0; i < cells.length; i++) {
cells[i] = (random(1) > 0.7);
probabilities[i] = 1.0/cells.length;
}

currentPosition = (int)random(numCells);
}

void draw() {
background(255);
drawSensorReading();

if(showPosition){
fill(0);
text("True position: " + currentPosition, 50, 200);
}

translate(10, 10);
drawCells();

translate(0, 150);
drawProbabilities();
}
void sense(boolean sensorReading){
float[] newProbs = new float[probabilities.length];

for(int i = 0; i < cells.length; i++) {
if(cells[i] == sensorReading){
newProbs[i] = probabilities[i] * hitError;
} else {
newProbs[i] = probabilities[i] * missError;
}
}

probabilities = newProbs;

// now we have to normalize, i.e.
// make sure all the probabilities add up to 1
float sum = 0;
for(float i : probabilities){
sum += i;
}

for(int i = 0; i < probabilities.length; i++){
probabilities[i] /= sum;
}
}

void move(int move){
boolean[] newCells = new boolean[cells.length];

for(int i = 0; i < cells.length; i++){
int j = (i+(cells.length + move)) % cells.length;

newCells[i] = cells[j];
}

cells = newCells;
}



void drawProbabilities(){
pushMatrix();
pushStyle();
stroke(0);
noFill();
float scale = 0.8;
scale(scale);

beginShape();
for(int i = 0; i < cells.length; i++) {
float doorX = (doorWidth+5) * i * 1/scale;
vertex(doorX+doorWidth/2, probabilities[i]*-100);
pushStyle();
fill(125);
text(nf(probabilities[i],1,2), doorX, 15);
popStyle();
}
endShape();
popStyle();
popMatrix();
}

void drawSensorReading() {
pushMatrix();
pushStyle();
translate(width-100, 10);

fill(0);
text("SENSOR", 0, 10);
noFill();
stroke(0);
rect(0, 20, 90, 100);

if (takeSensorReading()) {
pushMatrix();
translate(30, 30);
drawDoor();
popMatrix();
}

popStyle();
popMatrix();
}

boolean takeSensorReading() {
// perfect measurement
return cells[currentPosition];
}

void drawCells() {

pushStyle();

pushMatrix();
for (int i = 0; i < cells.length; i++) {
int doorX = (doorWidth+5) * i;

if (cells[i]) {
pushMatrix();
translate(doorX, 0);
drawDoor();
popMatrix();
}
fill(0);
text(i, doorX-5+doorWidth/2, + doorHeight+15);
}
popMatrix();
popStyle();
}

void drawDoor() {
pushMatrix();
pushStyle();
fill(110, 50, 50);
stroke(0);
rect(0, 0, doorWidth, doorHeight);
popStyle();
popMatrix();
}

void keyPressed(){
if(key == ' '){
sense(takeSensorReading());
}

if(key == '='){
move(1);
}
if(key == '-'){
move(-1);
}

if(key == 'r'){
showPosition = !showPosition;
}
saveFrame("bayesian-localization-####.png");
}
Loading

0 comments on commit 8ae5334

Please sign in to comment.