Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding | for conditional probability #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,43 @@ See:
- [next-after](https://github.com/scijs/nextafter)
- [interval-arithmetic](https://github.com/maurizzzio/interval-arithmetic)

## Probability

| represents a conditional event

P(A|B) means the probability of the event A occurring given that B occurs.


```js
// say you were on a strange planet with dogs who were blue, and green, and may or may not be bloodthirsty

var strangeDogs = [{fur: "blue", bloodthirsty: true},
{fur: "blue", bloodthirsty: false},
{fur: "green", bloodthirsty: false},
{fur: "green", bloodthirsty: false}]

// this would give you the probability that a trait has a value in a population
function returnProb (population, trait, value) {
var groupWithTrait = population.filter(x => x[trait] == value);
return groupWithTrait.length / population.length;
}
// so returnProb(strangeDogs, bloodthirsty, true) = 0.25

// | scopes your result to tell you the probablity of a A when you're picking from a group that all have trait B

function condProb (population, traitA, valueA, traitB, valueB) {
var groupWithValB = population.filter(x => x[traitB] == valueB)
var resultGroup = groupWithValB.filter(x => x[traitA] == valueA);
return resultGroup.length / groupWithValB.length
}

// condProb(strangeDogs, 'bloodthirsty', true, 'fur', "blue") == 0.5
// watch out for blue dogs!

```



## more...

Like this guide? Suggest some [more features](https://github.com/Jam3/math-as-code/issues/1) or send us a Pull Request!
Expand Down