Last active
August 29, 2019 21:40
-
-
Save xemoe/21534067e27484b957e1e20f5e2ed69b to your computer and use it in GitHub Desktop.
Learning Bayes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sum = function(members) { | |
var sumvalue = 0; | |
for(var key in members) { | |
if (members.hasOwnProperty(key)) { | |
sumvalue += members[key] | |
} | |
} | |
return sumvalue | |
} | |
var prob = function(i,looking,choose){ | |
return choose * i.members[looking] / sum(i.members) | |
}; | |
//////////////////////////////////////////////// | |
// | |
// P(FromBox) | |
// : P(A) | |
// | |
console.assert(1/2 == prob({members:{X: 1, Y: 1}}, "X", 1)) // P(FromBox="X") | |
console.assert(1/2 == prob({members:{X: 1, Y: 1}}, "Y", 1)) // P(FromBox="Y") | |
// | |
// P(GotTicket) | |
// : P(B) | |
// | |
console.assert(5/20 == prob({members:{Yes: 5, No: 15}}, "Yes", 1)) // P(GotTicket="Yes") | |
console.assert(15/20 == prob({members:{Yes: 5, No: 15}}, "No", 1)) // P(GotTicket="No") | |
// | |
// กำหนดให้มีตั๋วในกล่อง X จำนวน 3 ใบ | |
// และที่เหลือในกล่อง Y จำนวน 2 ใบ | |
// และแบ่งตั๋วไว้กล่องละ 10 ใบ | |
// | |
// | |
// P(GotTicket|FromBox="X") | |
// : P(B|A="X") | |
// | |
console.assert(3/10 == prob({members:{Yes: 3, No: 7}}, "Yes", 1)) // P(GotTicket="Yes"|FromBox="X") | |
console.assert(7/10 == prob({members:{Yes: 3, No: 7}}, "No", 1)) // P(GotTicket="No"|FromBox="X") | |
// | |
// P(GotTicket|FromBox="Y") | |
// : P(B|A="Y") | |
// | |
console.assert(2/10 == prob({members:{Yes: 2, No: 8}}, "Yes", 1)) // P(GotTicket="Yes"|FromBox="Y") | |
console.assert(8/10 == prob({members:{Yes: 2, No: 8}}, "No", 1)) // P(GotTicket="No"|FromBox="Y") | |
// | |
// Joint probability | |
// : P(B,A) = P(B|A) * P(A) | |
// | |
// P(GotTicket,FromBox="X") | |
// : P(B,A="X") | |
// | |
console.assert(3/20 == ( | |
prob({members:{Yes: 3, No: 7}}, "Yes", 1) * prob({members:{X: 1, Y: 1}}, "X", 1) // P(GotTicket="Yes",FromBox="X") | |
)) | |
console.assert(7/20 == ( | |
prob({members:{Yes: 3, No: 7}}, "No", 1) * prob({members:{X: 1, Y: 1}}, "X", 1) // P(GotTicket="No",FromBox="X") | |
)) | |
// | |
// P(GotTicket,FromBox="Y") | |
// : P(B,A="Y") | |
// | |
console.assert(2/20 == ( | |
prob({members:{Yes: 2, No: 8}}, "Yes", 1) * prob({members:{X: 1, Y: 1}}, "Y", 1) // P(GotTicket="Yes",FromBox="Y") | |
)) | |
console.assert(8/20 == ( | |
prob({members:{Yes: 2, No: 8}}, "No", 1) * prob({members:{X: 1, Y: 1}}, "Y", 1) // P(GotTicket="No",FromBox="Y") | |
)) | |
// | |
// จาก | |
// : P(B,A) = P(B|A) * P(A) | |
// : P(A,B) = P(A|B) * P(B) | |
// เมื่อ | |
// : P(A,B) = P(B,A) | |
// จะได้ | |
// : P(A|B) * P(B) = P(B|A) * P(A) | |
// | |
// เมื่อย้ายข้างสมการ | |
// จะสรุปความสัมพันธ์เป็นสูตรได้ | |
// : P(A|B) = P(B|A) * P(A) / P(B) | |
// หรือ | |
// : P(B|A) = P(A|B) * P(B) / P(A) | |
// | |
// P(FromBox="X"|GotTicket="Yes") == 3/5 | |
// = P(GotTicket="Yes|FromBox="X") * P(FromBox="X") / P(GotTicket="Yes") | |
// | |
console.assert(3/5 == ( | |
prob({members:{Yes: 3, No: 7}}, "Yes", 1) // P(GotTicket="Yes"|FromBox="X") | |
* prob({members:{X: 1, Y:1}}, "X", 1) // P(FromBox="X") | |
/ prob({members:{Yes: 5, No: 15}}, "Yes", 1) // P(GotTicket="Yes") | |
)) | |
// | |
// P(FromBox="Y"|GotTicket="Yes") == 2/5 | |
// = P(GotTicket="Yes|FromBox="Y") * P(FromBox="Y") / P(GotTicket="Yes") | |
// | |
console.assert(2/5 == ( | |
prob({members:{Yes: 2, No: 8}}, "Yes", 1) // P(GotTicket="Yes"|FromBox="Y") | |
* prob({members:{X: 1, Y:1}}, "Y", 1) // P(FromBox="Y") | |
/ prob({members:{Yes: 5, No: 15}}, "Yes", 1) // P(GotTicket="Yes") | |
)) |
Author
xemoe
commented
Aug 14, 2019
•
const Loves = () => (Math.floor(Math.random() * Math.floor(2)) ? "Love" : "Liar")
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment