brent
module
Brent
Brent is a playful tool to help understand bayesian graphical modelling as well as do-calculus. It offers support for discrete values and comes with great plotting tools. Under the hood is merely uses pandas and networkx.
Example 1
The basics of the tool can be demonstrated via the following codeblock.
from brent import DAG, Query
from brent.common import make_fake_df
# let's start with a new dataset and visualise it's graph
df = make_fake_df(4)
dag = DAG(df).add_edge("a", "b").add_edge("b", "c").add_edge("c","d")
dag.plot()
Such a graph is nice and we can get properties from it, these can be found in the documentation page. The main usecase for it though is to use
# we can build a query dynamically
q = Query(dag).given(a=1).do(d=1)
q.plot()
The nodes that are given have double circles around them while the
nodes that are affected by a do()
-operation have greyed out arcs
going in. This is because these are usually removed for inference.
If you're interested in inferring probabilities, you can get the updated probabilities by calling the inference method on the object.
# we can also see updated probabilities
q.infer()
Example 2
We'll still be using fake data for this example, but we're going to create a more complex graph.
from brent import DAG, Query
from brent.common import make_fake_df
dag = (DAG(make_fake_df(7))
.add_edge("e", "a")
.add_edge("e", "d")
.add_edge("a", "d")
.add_edge("b", "d")
.add_edge("a", "b")
.add_edge("a", "c")
.add_edge("b", "c")
.add_edge("c", "f")
.add_edge("g", "f"))
dag.plot()
This graph will be more involved when we create a query on it.
q = Query(dag).given(d=1).do(a=0, c=1)
q.plot()
But we can still make a pretty plot appear.
Note that the plot demonstrates which edges are affected with the do
operation by making the lines dashed and gray. Nodes that have information
injected in them have double circles and edges that don't contribute to
the inference are grayed out. In this case, because d
is given and because a
is enforced with a do()
operation the arc between a -> d
does not participate
in the inference much.
Inference is still done in the same way as before. Note that you can
get marginal probabilities in json format but you can also
get pass give_table=True
if you prefer to get a full probability
table. This will give a pandas dataframe.
# we can also see updated probabilities
q.infer()
q.infer(give_table=True)
Source code
"""
Brent
-----
![](https://github.com/koaning/brent/blob/master/images/dag1.png?raw=true)
Brent is a playful tool to help understand bayesian graphical
modelling as well as do-calculus. It offers support for discrete
values and comes with great plotting tools. Under the hood is merely
uses [pandas](http://pandas.pydata.org/pandas-docs/stable/reference/index.html)
and [networkx](https://networkx.github.io/documentation/stable/index.html).
## Example 1
The basics of the tool can be demonstrated via the following codeblock.
```python
from brent import DAG, Query
from brent.common import make_fake_df
# let's start with a new dataset and visualise it's graph
df = make_fake_df(4)
dag = DAG(df).add_edge("a", "b").add_edge("b", "c").add_edge("c","d")
dag.plot()
```
![](https://github.com/koaning/brent/blob/master/images/simple-graph.png?raw=true)
Such a graph is nice and we can get properties from it, these can be
found in the documentation page. The main usecase for it though is
to use
```python
# we can build a query dynamically
q = Query(dag).given(a=1).do(d=1)
q.plot()
```
![](https://github.com/koaning/brent/blob/master/images/simple-query.png?raw=true)
The nodes that are given have double circles around them while the
nodes that are affected by a `do()`-operation have greyed out arcs
going in. This is because these are usually removed for inference.
If you're interested in inferring probabilities, you can get the
updated probabilities by calling the inference method on the object.
```python
# we can also see updated probabilities
q.infer()
```
## Example 2
We'll still be using fake data for this example, but we're going to
create a more complex graph.
```
from brent import DAG, Query
from brent.common import make_fake_df
dag = (DAG(make_fake_df(7))
.add_edge("e", "a")
.add_edge("e", "d")
.add_edge("a", "d")
.add_edge("b", "d")
.add_edge("a", "b")
.add_edge("a", "c")
.add_edge("b", "c")
.add_edge("c", "f")
.add_edge("g", "f"))
dag.plot()
```
![](https://github.com/koaning/brent/blob/master/images/complex-graph.png?raw=true)
This graph will be more involved when we create a query on it.
```
q = Query(dag).given(d=1).do(a=0, c=1)
q.plot()
```
But we can still make a pretty plot appear.
![](https://github.com/koaning/brent/blob/master/images/complex-query.png?raw=true)
Note that the plot demonstrates which edges are affected with the `do`
operation by making the lines dashed and gray. Nodes that have information
injected in them have double circles and edges that don't contribute to
the inference are grayed out. In this case, because `d` is given and because `a`
is enforced with a `do()` operation the arc between `a -> d` does not participate
in the inference much.
Inference is still done in the same way as before. Note that you can
get marginal probabilities in json format but you can also
get pass `give_table=True` if you prefer to get a full probability
table. This will give a pandas dataframe.
```python
# we can also see updated probabilities
q.infer()
q.infer(give_table=True)
```
"""
from .query import Query, SupposeQuery
from .graph import DAG
Sub-modules
brent.common
-
The
brent.common
module contains common functions that can be used while working with dataframes and brent graphs. They are also used internally by … brent.datasets
-
The
brent.datasets
module contains functions that will generate datasets. Typically these datasets are meant for educational purposes … brent.examples
brent.graph
-
The
brent.graph
module contains the DAG object. This is the main object that you'll talk to when constructing a casual graph. brent.parsers
brent.query
-
The
brent.query
module contains theQuery
object. This is the main object that you'll use to describe complex queries against casual graphs. brent.sklearn
-
The
brent.sklearn
module contains objects that be used in scikit-learn pipelines. In particulate it offers a classifier as well as an imputer.