[![Coverage Status](https://coveralls.io/repos/github/mellesies/thomas-core/badge.svg?branch=master)](https://coveralls.io/github/mellesies/thomas-core?branch=master)
[![Build Status](https://travis-ci.org/mellesies/thomas-core.svg?branch=master)](https://travis-ci.org/mellesies/thomas-core)
[![badge](https://img.shields.io/badge/launch-JupyterLab%20binder-579ACA.svg?logo=data:image/png;base64, )](https://mybinder.org/v2/gh/mellesies/thomas-core/master?urlpath=%2Flab%2Ftree%2Fnotebooks)

# Thomas
Very simple (almost naive ;-) bayesian network implementation.

Contains examples (`thomas.core.examples`) from the book "Probabilistic
Graphical Models: Principles and Techniques" from Koller and Friedman ([PGM
Stanford](http://pgm.stanford.edu)) and from the lecture by [Adnan
Darwiche](http://web.cs.ucla.edu/~darwiche/) on YouTube: * [6a. Inference by
Variable Elimination I (Chapter
6)](https://www.youtube.com/watch?v=7oRReD_ayWo). * [6b. Inference by Variable
Elimination II (Chapter 6)](https://www.youtube.com/watch?v=QSSmx1ndUvg).

## Installation

### Regular installation
To install the latest version from **PyPI**:

```bash
    pip install thomas-core
```

Install from PyyPI TEST:

```bash
    pip install -i https://test.pypi.org/simple/ --extra-index-url=https://pypi.python.org/simple thomas-core
    pip install -i https://test.pypi.org/simple/ --extra-index-url=https://pypi.python.org/simple thomas-jupyter-widget
```


To install the latest version from **source**:

```bash
    pip install git+https://github.com/mellesies/thomas-core
```

### Development installation
To do a development install:

```bash
    git clone https://github.com/mellesies/thomas-core
    cd thomas-core
    pip install -e .
```

### Additional packages
If you're using JupyterLab, I'd recommend also installing the Widget that can display Bayesian Networks.

## Docker
A Docker image is available should you just want to try things out.The following command will start a JupyterLab server, listening on [http://localhost:8888](http://localhost:8888).

```bash
    docker run --rm -it -p 8888:8888 mellesies/thomas-core
```


## Usage
To get started with querying a network, try the following:
```python
from thomas.core import examples

# Load an example network
Gs = examples.get_student_network()

# This should output the prior probability of random variable 'S' (SAT score).
print(Gs.P('S'))
print()

# Expected output:
# P(S)
# S
# s0    0.725
# s1    0.275
# dtype: float64

# Query for the conditional probability of S given the student is intelligent.
print(Gs.P('S|I=i1'))

# Expected output:
# P(S)
# S
# s0    0.2
# s1    0.8
# dtype: float64
```

Alternatively, you can have a go at the example notebooks through [Binder](https://mybinder.org):
* [notebooks/1. Factors.ipynb](https://mybinder.org/v2/gh/mellesies/thomas-core/master?filepath=notebooks%2F1.%20Factors.ipynb)
* [notebooks/2. Bags of factors.ipynb](https://mybinder.org/v2/gh/mellesies/thomas-core/master?filepath=notebooks%2F2.%20Bags%20of%20factors.ipynb)
* [notebooks/3. Conditional probability tables.ipynb](https://mybinder.org/v2/gh/mellesies/thomas-core/master?filepath=notebooks%2F3.%20Conditional%20probability%20tables.ipynb)
* [notebooks/4. Bayesian Networks.ipynb](https://mybinder.org/v2/gh/mellesies/thomas-core/master?filepath=notebooks%2F4.%20Bayesian%20Networks.ipynb)
