Lace is a probabilistic cross-categorization engine written in rust with an optional interface to python. Unlike traditional machine learning methods, which learn some function mapping inputs to outputs, Lace learns a joint probability distribution over your dataset, which enables users to...
- predict or compute likelihoods of any number of features conditioned on any number of other features
- identify, quantify, and attribute uncertainty from variance in the data, epistemic uncertainty in the model, and missing features
- determine which variables are predictive of which others
- determine which records/rows are similar to which others on the whole or given a specific context
- simulate and manipulate synthetic data
- work natively with missing data and make inferences about missingness (missing not-at-random)
- work with continuous and categorical data natively, without transformation
- identify anomalies, errors, and inconsistencies within the data
- edit, backfill, and append data without retraining
and more, all in one place, without any explicit model building.
import pandas as pd
import lace
# Create an engine from a dataframe
df = pd.read_csv("animals.csv", index_col=0)
engine = lace.Engine.from_df(df)
# Fit a model to the dataframe over 5000 steps of the fitting procedure
engine.update(5000)
# Show the statistical structure of the data -- which features are likely
# dependent (predictive) on each other
engine.clustermap("depprob", zmin=0, zmax=1)

The Problem
The goal of lace is to fill some of the massive chasm between standard machine learning (ML) methods like deep learning and random forests, and statistical methods like probabilistic programming languages. We wanted to develop a machine that allows users to experience the joy of discovery, and indeed optimizes for it.
Short version
Standard, optimization-based ML methods don't help you learn about your data. Probabilistic programming tools assume you already have learned a lot about your data. Neither approach is optimized for what we think is the most important part of data science: the science part: asking and answering questions.
Long version
Standard ML methods are easy to use. You can throw data into a random forest and start predicting with little thought. These methods attempt to learn a function f(x) -> y that maps inputs x, to outputs y. This ease-of-use comes at a cost. Generally f(x) does not reflect the reality of the process that generated your data, but was instead chosen by whoever developed the approach to be sufficiently expressive to better achieve the optimization goal. This renders most standard ML completely uninterpretable and unable to yield sensible uncertainty estimate.
On the other extreme you have probabilistic tools like probabilistic programming languages (PPLs). A user specifies a model to a PPL in terms of a hierarchy of probability distributions with parameters θ. The PPL then uses a procedure (normally Markov Chain Monte Carlo) to learn about the posterior distribution of the parameters given the data p(θ|x). PPLs are all about interpretability and uncertainty quantification, but they place a number of pretty steep requirements on the user. PPL users must specify the model themselves from scratch, meaning they must know (or at least guess) the model. PPL users must also know how to specify such a model in a way that is compatible with the underlying inference procedure.
Example use cases
- Combine data sources and understand how they interact. For example, we may wish to predict cognitive decline from demographics, survey or task performance, EKG data, and other clinical data. Combined, this data would typically be very sparse (most patients will not have all fields filled in), and it is difficult to know how to explicitly model the interaction of these data layers. In Lace, we would just concatenate the layers and run them through.
- Understanding the amount and causes of uncertainty over time. For example, a farmer may wish to understand the likelihood of achieving a specific yield over the growing season. As the season progresses, new weather data can be added to the prediction in the form of conditions. Uncertainty can be visualized as variance in the prediction, disagreement between posterior samples, or multi-modality in the predictive distribution (see this blog post for more information on uncertainty)
- Data quality control. Use
surprisalto find anomalous data in the table and use-logpto identify anomalies before they enter the table. Because Lace creates a model of the data, we can also contrive methods to find data that are inconsistent with that model, which we have used to good effect in error finding.
Who should not use Lace
There are a number of use cases for which Lace is not suited
- Non-tabular data such as images and text
- Highly optimizing specific predictions
- Lace would rather over-generalize than over fit.
Quick start
Installation
Lace requires rust.
To install the CLI:
$ cargo install --locked lace-cli
To install pylace
$ pip install pylace
Examples
Lace comes with two pre-fit example data sets: Satellites and Animals.
>>> from lace.examples import Satellites
>>> engine = Satellites()
# Predict the class of orbit given the satellite has a 75-minute
# orbital period and that it has a missing value of geosynchronous
# orbit longitude, and return epistemic uncertainty via Jensen-
# Shannon divergence.
>>> engine.predict(
... 'Class_of_Orbit',
... given={
... 'Period_minutes': 75.0,
... 'longitude_radians_of_geo': None,
... },
... )
('LEO', 0.023981898950561048)
# Find the top 10 most surprising (anomalous) orbital periods in
# the table
>>> engine.surprisal('Period_minutes') \
... .sort('surprisal', reverse=True) \
... .head(10)
shape: (10, 3)
┌─────────────────────────────────────┬────────────────┬───────────┐
│ index ┆ Period_minutes ┆ surprisal │
│ --- ┆ --- ┆ --- │
│ str ┆ f64 ┆ f64 │
╞═════════════════════════════════════╪════════════════╪═══════════╡
│ Wind (International Solar-Terres... ┆ 19700.45 ┆ 11.019368 │
│ Integral (INTErnational Gamma-Ra... ┆ 4032.86 ┆ 9.556746 │
│ Chandra X-Ray Observatory (CXO) ┆ 3808.92 ┆ 9.477986 │
│ Tango (part of Cluster quartet, ... ┆ 3442.0 ┆ 9.346999 │
│ ... ┆ ... ┆ ... │
│ Salsa (part of Cluster quartet, ... ┆ 3418.2 ┆ 9.338377 │
│ XMM Newton (High Throughput X-ra... ┆ 2872.15 ┆ 9.13493 │
│ Geotail (Geomagnetic Tail Labora... ┆ 2474.83 ┆ 8.981458 │
│ Interstellar Boundary EXplorer (... ┆ 0.22 ┆ 8.884579 │
└─────────────────────────────────────┴────────────────┴───────────┘
And similarly in rust:
use lace::prelude::*;
use lace::examples::Example;
fn main() {
// In rust, you can create an Engine or and Oracle. The Oracle is an
// immutable version of an Engine; it has the same inference functions as
// the Engine, but you cannot train or edit data.
let mut engine = Example::Satellites.engine().unwrap();
// Predict the class of orbit given the satellite has a 75-minute
// orbital period and that it has a missing value of geosynchronous
// orbit longitude, and return epistemic uncertainty via Jensen-
// Shannon divergence.
engine.predict(
"Class_of_Orbit",
&Given::Conditions(vec![
("Period_minutes", Datum:Continuous(75.0)),
("Longitude_of_radians_geo", Datum::Missing),
]),
Some(PredictUncertaintyType::JsDivergence),
None,
)
}
Fitting a model
To fit a model to your own data you can use the CLI
$ lace run --csv my-data.csv -n 1000 my-data.lace
...or initialize an engine from a file or dataframe.
>>> import pandas as pd # Lace supports polars as well
>>> from lace import Engine
>>> engine = Engine.from_df(pd.read_csv("my-data.csv", index_col=0))
>>> engine.update(1_000)
>>> engine.save("my-data.lace")
You can monitor the progress of the training using diagnostic plots
>>> from lace.plot import diagnostics
>>> diagnostics(engine)

License
Lace is licensed under the MIT licenses as of v0.9.0.
Installation
Installation requires rust, which you can get here.
CLI
The lace CLI is installed with rust via the command
$ cargo install --locked lace-cli
Rust crate
To use the lace crate in a rust project add the following line under the
dependencies block in your Cargo.toml:
lace = "<version>"
Python
The python library can be installed with pip
pip install
The lace workflow
The typical workflow consists of two or three steps:
Step 1 is optional in many cases as Lace usually does a good job of inferring the types of your data. The condensed workflow looks like this.
import pandas as pd
import lace
df = pd.read_csv("mydata.csv", index_col=0)
# 1. Create a codebook (optional)
codebook = lace.Codebook.from_df(df)
# 2. Initialize a new Engine from the prior. If no codebook is provided, a
# default will be generated
engine = lace.Engine.from_df(df, codebook=codebook)
# 3. Run inference
engine.run(5000)
use polars::prelude::{SerReader, CsvReader};
use lace::prelude::*;
let df = CsvReader::from_path("mydata.csv")
.unwrap()
.has_header(true)
.finish()
.unwrap();
// 1. Create a codebook (optional)
let codebook = Codebook::from_df(&df, None, None, False).unwrap();
// 2. Build an engine
let mut engine = EngineBuilder::new(DataSource::Polars(df))
.with_codebook(codebook)
.build()
.unwrap();
// 3. Run inference
// Use `run` to fit with the default transition set and update handlers; use
// `update` for more control.
engine.run(5_000);
You can also use the CLI to create codebooks and run inference. Creating a default YAML codebook with the CLI, and then manually editing is good way to fine tune models.
$ lace codebook --csv mydata.csv codebook.yaml
$ lace run --csv data.csv --codebook codebook.yaml -n 5000 metadata.lace
Create and edit a codebook
The codebook contains information about your data such as the row and column names, the types of data in each column, how those data should be modeled, and all the prior distributions on various parameters.
The default codebook
In the lace CLI, you have the ability to initialize and run a model without specifying a codebook.
$ lace run --csv data -n 5000 metadata.lace
Behind the scenes, lace creates a default codebook by inferring the types of your columns and creating a very broad (but not quite broad enough to satisfy the frequentists) hyper prior, which is a prior on the prior.
We can also create the default codebook in code.
import polars as pl
from lace import Codebook
from lace.examples import ExamplePaths
# Here we get the path to an example csv file, but you can use any file that
# can be read into a polars or pandas dataframe
path = ExamplePaths("satellites").data
df = pl.read_csv(path)
# Infer the default codebook for df
codebook = Codebook.from_df("satellites", df)
use polars::prelude::{CsvReadOptions, SerReader};
use lace::codebook::Codebook;
use lace::examples::Example;
// Load an example file
let paths = Example::Satellites.paths().unwrap();
let df = CsvReadOptions::default()
.with_has_header(true)
.try_into_reader_with_file_path(Some(paths.data))
.unwrap()
.finish()
.unwrap();
// Create the default codebook
let codebook = Codebook::from_df(&df, None, None, None, false).unwrap();
Creating a template codebook
Lace is happy to generate a default codebook for you when you initialize a model. You can create and save the default codebook to a file using the CLI. To create a codebook from a CSV file:
$ lace codebook --csv data.csv codebook.yaml
Note that if you love quotes and brackets, and hate being able to comment, you can use json for
the codebook. Just give the output of codebook a .json extension.
$ lace codebook --csv data.csv codebook.json
If you use a data format with a schema, such as Parquet or IPC (Apache Arrow v2), you make Lace's work a bit easier.
$ lace codebook --ipc data.arrow codebook.yaml
If you want to make changes to the codebook -- the most common of which are editing the Dirichlet process prior, specifying whether certain columns are missing not-at-random, adjusting the prior distributions and disabling hyper priors -- you just open it up in your text editor and get to work.
For example, let's say we wanted to make a column of the satellites data set missing not-at-random, we first create the template codebook,
$ lace codebook --csv satellites.csv codebook-sats.yaml
open it up in a text editor and find the column of interest
- name: longitude_radians_of_geo
coltype: !Continuous
hyper:
pr_m:
mu: 0.21544247097911842
sigma: 1.570659039531299
pr_k:
shape: 1.0
rate: 1.0
pr_v:
shape: 6.066108090103747
scale: 6.066108090103747
pr_s2:
shape: 6.066108090103747
scale: 2.4669698184613824
prior: null
notes: null
missing_not_at_random: false
{
"name": "longitude_radians_of_geo",
"coltype": {
"Continuous": {
"hyper": {
"pr_m": {
"mu": 0.21544247097911842,
"sigma": 1.570659039531299
},
"pr_k": {
"shape": 1.0,
"rate": 1.0
},
"pr_v": {
"shape": 6.066108090103747,
"scale": 6.066108090103747
},
"pr_s2": {
"shape": 6.066108090103747,
"scale": 2.4669698184613824
}
},
"prior": null
}
},
"notes": null,
"missing_not_at_random": false
}
and change the column metadata to something like this:
- name: longitude_radians_of_geo
coltype: !Continuous
hyper:
pr_m:
mu: 0.21544247097911842
sigma: 1.570659039531299
pr_k:
shape: 1.0
rate: 1.0
pr_v:
shape: 6.066108090103747
scale: 6.066108090103747
pr_s2:
shape: 6.066108090103747
scale: 2.4669698184613824
prior: null
notes: "This value is only defined for GEO satellites"
missing_not_at_random: true
{
"name": "longitude_radians_of_geo",
"coltype": {
"Continuous": {
"hyper": {
"pr_m": {
"mu": 0.21544247097911842,
"sigma": 1.570659039531299
},
"pr_k": {
"shape": 1.0,
"rate": 1.0
},
"pr_v": {
"shape": 6.066108090103747,
"scale": 6.066108090103747
},
"pr_s2": {
"shape": 6.066108090103747,
"scale": 2.4669698184613824
}
},
"prior": null
}
},
"notes": null,
"missing_not_at_random": true
}
Sometimes, we have a bit of knowledge that we can transfer to lace in the form of a more-specific prior distribution. To set the prior we remove the hyper prior and set the prior. Note that doing this disabled prior parameter inference.
- name: longitude_radians_of_geo
coltype: !Continuous
hyper: null
prior:
m: 0.0
k: 1.0
v: 1.0
s2: 3.0
notes: "This value is only defined for GEO satellites"
missing_not_at_random: true
{
"name": "longitude_radians_of_geo",
"coltype": {
"Continuous": {
"hyper": null,
"prior": {
"m": 0.0,
"k": 1.0,
"v": 1.0,
"s2": 3.0
}
}
},
"notes": null,
"missing_not_at_random": true
}
For a complete list of codebook fields, see the reference.
Run/train/fit a model
Lace is a Bayesian tool so we do posterior sampling via Markov chain Monte Carlo (MCMC). A typical machine learning model will use some sort of optimization method to find one model that fits best; the objective for fitting is different in Lace.
In Lace we use a number of states (or samples), each running MCMC independently to characterize the posterior distribution of the model parameters given the data. Posterior sampling isn't meant to maximize the fit to a dataset, it is meant to help understand the conditions that created the data.
When you fit to your data in Lace, you have options to run a set number of
states for a set number of iterations (limited by a timeout). Each state is a
posterior sample. More states is better, but the run time of everything
increases linearly with the number of states; not just the fit, but also the
OracleT operations like logp and simulate. As a rule of thumb, 32 is a
good default number of states. But if you find your states tend to strongly
disagree on things, it is probably a good idea to add more states to fill in
the gaps.
As for number of iterations, you will want to monitor your convergence plots. There is no benefit of early stopping like there is with neural networks. MCMC will usually only do better the longer you run it and Lace is not likely to overfit like a deep network.

The above figure shows the MCMC algorithm partitioning a dataset into views and categories.
A (potentially useless) analogy comparing MCMC to optimization
At the risk of creating more confusion than we resolve, let us make an analogy to mountaineering. You have two mountaineers: a gradient ascent (GA) mountaineer and an MCMC mountaineer. You place each mountaineer at a random point in the Himalayas and say "go". GA's goal is to find the peak of Everest. Its algorithm for doing so is simply always to go up and never to go down. GA is guaranteed to find a peak, but unless it is very lucky in its starting position, it is unlikely ever to summit Everest.
MCMC has a different goal: to map the mountain range (posterior distribution). It does this by always going up, but sometimes going down if it doesn't end up too low. The longer MCMC explores, the better understanding it gains about the Himalayas: an understanding which likely includes a good idea of the position of the peak of Everest.
While GA achieves its goal quickly, it does so at the cost of understanding the terrain, which in our analogy represents the information within our data.
In Lace we place a troop of mountaineers in the mountain range of our posterior distribution. We call individuals mountaineers states or samples, or chains. Our hope is that our mountaineers can sufficiently map the information in our data. Of course the ability of the mountaineers to build this map depends on the size of the space (which is related to the size of the data) and the complexity of the space (the complexity of the underlying process)
In general the posterior of a Dirichlet process mixture is indeed much like the Himalayas in that there are many, many peaks (modes), which makes the mountaineer's job difficult. Certain MCMC kernels do better in certain circumstances, and employing a variety of kernels leads to better result.
Our MCMC Kernels
The vast majority of the fitting runtime is updating the row-category assignment and the column-view assignment. Other updates such as feature components parameters, CRP parameters, and prior parameters, take an (relatively) insignificant amount of time. Here we discuss the MCMC kernels responsible for the vast majority of work in Lace: the row and column reassignment kernels:
Row kernels
- slice: Proposes reassignment for each row to an instantiated category or
one of many new, empty categories. Slice is good for small tweaks in the
assignment, and it is very fast. When there are a lot of rows,
slicecan have difficulty creating new categories. - gibbs: Proposes reassignment of each row sequentially. Generally makes
larger moves than
slice. Because it is sequential, and accesses data in random order,gibbsis very slow. - sams: Proposes mergers and splits of categories. Only considers the rows in
one or two categories. Proposes large moves, but cannot make the fine
tweaks that
sliceandgibbscan. Since it proposes big moves, its proposals are often rejected as the run progresses and the state is already fitting fairly well.
Column kernels
The column kernels are generally adapted from the row kernels with some caveats.
- slice: Same as the row kernel, but over columns.
- gibbs: The same structurally as the row kernel, but uses random seed control to implement parallelism.
Gibbs is a good choice if the number of columns is high and mixing is a concern.
Fitting models in code
Though the CLI is a convenient way to fit models and generate metadata files outside of python or rust, you may often times find yourself wanting to fit in code. Lace gives you a number of options in both rust and python.
Rust
We first initialize a new Engine:
use rand::SeedableRng;
use rand_xoshiro::Xoshiro256Plus;
use polars::prelude::{CsvReadOptions, SerReader};
use lace::prelude::*;
use lace::examples::Example;
// Load an example file
let paths = Example::Satellites.paths().unwrap();
let df = CsvReadOptions::default()
.with_has_header(true)
.try_into_reader_with_file_path(Some(paths.data))
.unwrap()
.finish()
.unwrap();
// Create the default codebook
let codebook = Codebook::from_df(&df, None, None, None, false).unwrap();
// Build an rng
let rng = Xoshiro256Plus::from_os_rng();
// This initializes 32 states from the prior
let mut engine = Engine::new(
32,
codebook,
DataSource::Polars(df),
0,
rng,
).unwrap();
Now we have two options for fitting. We can use the Engine::run method, which
uses a default set of transition operations that prioritizes speed.
engine.run(1_000);
We can also tell lace exactly which transitions to run.
// Run for 1000 iterations. Use the Gibbs column reassignment kernel, and
// alternate between the merge-split (Sams) and slice row kernels
let run_config = EngineUpdateConfig::new()
.n_iters(100)
.transitions(vec![
StateTransition::ColumnAssignment(ColAssignAlg::Gibbs),
StateTransition::StatePriorProcessParams,
StateTransition::RowAssignment(RowAssignAlg::Sams),
StateTransition::ComponentParams,
StateTransition::RowAssignment(RowAssignAlg::Slice),
StateTransition::ComponentParams,
StateTransition::ViewPriorProcessParams,
StateTransition::FeaturePriors,
]);
engine.update(run_config.clone(), ()).unwrap();
Note the second argument to engine.update. This is the update handler, which
allows users to do things like attach progress bars, handle Ctrl+C, and collect
additional diagnostic information. There are a number a built-ins for common use
case, but you can implement UpdateHandler for your own types if you need extra
capabilities. () is the null update handler.
If we wanted a simple progressbar
use lace::prelude::update_handler::ProgressBar;
engine.update(run_config.clone(), ProgressBar::new()).unwrap();
Or if we wanted a progress bar and a Ctrl+C handler, we can use a tuple of UpdateHandlers.
use lace::prelude::update_handler::CtrlC;
engine.update(
run_config,
(ProgressBar::new(), CtrlC::new())
).unwrap();
Python
Let's load an Engine from an example and run it with the default transitions
for 1000 iterations.
from lace.examples import Satellites
engine = Satellites()
engine.update(100)
As in rust, we can control which transitions are run. Let's just update the row assignments a bunch of times.
from lace import RowKernel, StateTransition
engine.update(
500,
timeout=10, # each state can run for at most 10 seconds
checkpoint=250, # save progress every 250 iterations
save_path="mydata.lace",
transitions=[
StateTransition.row_assignment(RowKernel.slice()),
StateTransition.view_prior_process_params(),
],
)
Convergence
When training a neural network, we monitor for convergence in the error or loss. When, say, we see diminishing returns in our loss function with each epoch, or we see overfitting in the validation set, it is time to stop. Convergence in MCMC is a bit different. We say our Markov Chain has converged when it has settled into a situation in which it is producing draws from the posterior distribution. In the beginning state of the chain, it is rapidly moving away from the low probability area in which it was initialized and into the higher probability areas more representative of the posterior.
To monitor convergence, we observe the score (which is proportional to the likelihood) over time. If the score stops increasing and begins to oscillate, one of two things has happened: we have settled into the posterior distribution, or the Markov Chain has gotten stuck on an island of high likelihood. When a model is identifiable (meaning that each unique parameter set creates a unique model) the posterior distribution is unimodal, which means there is only one peak, which is easily mapped.
Above. Score by MCMC kernel step in the Animals dataset. Colored lines represent the scores of parallel Markov Chains; the black line is their mean.
Above. Score by MCMC kernel step in the Satellites dataset. Colored lines represent the scores of parallel Markov Chains; the Black line is their mean. Note that some of the Markov Chains experience sporadic jumps upward. This is the MCMC kernel hopping to a higher-probability mode.
A Bayesian modeler must make a compromises between expressiveness, interpretability, and identifiablity. A modeler may transform variables to create a more well-behaved posterior at the cost of the model being less interpretable. The modeler may also achieve identifiablity by reducing the complexity of the model at the cost of failing to capture certain phenomena.
To be general, a model must be expressive, and to be safe, a model must be interpretable. We have chosen to favor general applicability and interpretability over identifiablity. We fight against multimodality in three ways: deploying MCMC algorithms that are better at hopping between modes, by running many Markov Chains in parallel, and by being interpretable.
There are many metrics for convergence but none of the them are practically useful for models of this complexity. Instead we encourage users to monitor convergence via the score and by smell-testing the model. If your model is failing to pick up obvious dependencies, or is missing out on obvious intuitions, you should run it more.
Conduct an analysis
You've made a codebook, you've fit a model, now you're ready to do learn.
Let's use the built-in examples to walk through some key concepts. The
Animals example isn't the biggest, or most complex, and that's exactly why
it's so great. People have acquired a ton of intuition about animals like how
and why you might categorize animals into a taxonomy, and why animals have
certain features and what that might tell us about other features of animals.
This means, that we can see if lace recovers our intuition.
from lace import examples
# if this is your first time using the example, lace must
# build the metadata
animals = examples.Animals()
use lace::examples::Example;
use lace::prelude::*;
// You can create an Engine or an Oracle. An Oracle is
// basically an immutable Engine. You cannot add/edit data or
// extend runs (update).
let animals = Example::Animals.engine().unwrap();
Statistical structure
Usually, the first question we want to ask of a new dataset is "What questions can I answer?" This is a question about statistical dependence. Which features of our dataset share statistical dependence with which others? This is closely linked with the question "which things can I predict given which other things?"
In python, we can generate a plotly heatmap of dependence probability.
animals.clustermap(
'depprob',
color_continuous_scale='greys',
zmin=0,
zmax=1
).figure.show()
In rust, we ask about dependence probabilities between individual pairs of features
let depprob_flippers = animals.depprob(
"swims",
"flippers",
).unwrap();
Prediction
Now that we know which columns are predictive of each other, let's do some predicting. We'll predict whether an animal swims. Just an animals. Not an animals with flippers, or a tail. Any animal.
animals.predict("swims")
animals.predict(
"swims",
&Given::<usize>::Nothing,
true,
None,
);
Which outputs
(0, 0.04384630488890182)
The first number is the prediction. Lace predicts that an animal does not swims (because most of the animals in the dataset do not swim). The second number is the uncertainty. Uncertainty is a number between 0 and 1 representing the disagreement between states. Uncertainty is 0 if all the states completely agree on how to model the prediction, and is 1 if all the states completely disagree. Note that uncertainty is not tied to variance.
The uncertainty of this prediction is very low.
We can add conditions. Let's predict whether an animal swims given that it has flippers.
animals.predict("swims", given={'flippers': 1})
animals.predict(
"swims",
&Given::Conditions(vec![
("flippers", Datum::Categorical(lace::Category::UInt(1)))
]),
true,
None,
);
Output:
(1, 0.09588592928237495)
The uncertainty is a little higher, but still quite low.
Let's add some more conditions that are indicative of a swimming animal and see how that effects the uncertainty.
animals.predict("swims", given={'flippers': 1, 'water': 1})
animals.predict(
"swims",
&Given::Conditions(vec![
("flippers", Datum::Categorical(lace::Category::UInt(1))),
("water", Datum::Categorical(lace::Category::UInt(1))),
]),
true,
None,
);
Output:
(1, 0.06761776764962134)
The uncertainty is a bit lower now that we've added swim-consistent evidence.
How about we try to mess with Lace? Let's try to confuse it by asking it to predict whether an animal with flippers that does not go in the water swims.
animals.predict("swims", given={'flippers': 1, 'water': 0})
animals.predict(
"swims",
&Given::Conditions(vec![
("flippers", Datum::Categorical(lace::Category::UInt(1))),
("water", Datum::Categorical(lace::Category::UInt(0))),
]),
true,
None,
);
Output:
(0, 0.36077426258767503)
The uncertainty is really high! We've successfully confused lace.
Evaluating likelihoods
Let's compute the likelihood to see what is going on
import polars as pl
animals.logp(
pl.Series("swims", [0, 1]),
given={'flippers': 1, 'water': 0}
).exp()
animals.logp(
&["swims"],
&[
vec![Datum::Categorical(lace::Category::UInt(0))],
vec![Datum::Categorical(lace::Category::UInt(1))],
],
&Given::Conditions(vec![
("flippers", Datum::Categorical(lace::Category::UInt(1))),
("water", Datum::Categorical(lace::Category::UInt(0))),
]),
None,
)
.unwrap()
.iter()
.map(|&logp| logp.exp())
.collect::<Vec<_>>();
Output:
# polars
shape: (2,)
Series: 'logp' [f64]
[
0.589939
0.410061
]
Anomaly detection
animals.surprisal("fierce")\
.sort("surprisal", descending=True)\
.head(10)
Output:
# polars
shape: (10, 3)
┌──────────────┬────────┬───────────┐
│ index ┆ fierce ┆ surprisal │
│ --- ┆ --- ┆ --- │
│ str ┆ u32 ┆ f64 │
╞══════════════╪════════╪═══════════╡
│ pig ┆ 1 ┆ 1.565845 │
│ rhinoceros ┆ 1 ┆ 1.094639 │
│ buffalo ┆ 1 ┆ 1.094639 │
│ chihuahua ┆ 1 ┆ 0.802085 │
│ ... ┆ ... ┆ ... │
│ collie ┆ 0 ┆ 0.594919 │
│ otter ┆ 0 ┆ 0.386639 │
│ hippopotamus ┆ 0 ┆ 0.328759 │
│ persian+cat ┆ 0 ┆ 0.322771 │
└──────────────┴────────┴───────────┘
Probabilistic Cross Categorization
Lace is built on a Bayesian probabilistic model called Probabilistic Cross-Categorization (PCC). PPC groups \(m\) columns into \(1, ..., m\) views, and within each view, groups the \(n\) rows into \(1, ..., n\) categories. PCC uses a non-parametric prior process (the Dirichlet process) to learn the number of view and categories. Each column (feature) is then modeled as a mixture distribution defined by the category partition. For example, a continuous-valued column will be modeled as a mixture of Gaussian distributions. For references on PCC, see the appendix.
Differences between PCC and Traditional ML
Inputs and outputs
Regression and classification are defined in terms of learning a funciton \(f(x) \rightarrow y \) that maps inputs, \(x\), to outputs, \(y\). PCC has no notion of inputs and outputs. There is only data. PCC learns a joint distribution \(p(x_1, x_2, ..., x_m)\) from which the user can create condition distributions. To predict \(x_1\) given \(x_2\) and \(x_3\), you find \(\text{argmax}_{x_1} p(x_1|x_2, x_3)\).
Supported data types
Most ML models are designed to handle one type of input data, generally
continuous. This means if you have categorical data, you have to transform it:
you can convert it to a float (e.g. float(x) in python) and just
sweep the categorical-ness of the data under the rug, you can do something like
one-hot encoding, which significantly
increases dimensionality, or you can use some kind of embedding like in
natural language processing,
which destroys
interpretability. PCC allows your data to stay as they are.
The learning method
Most machine learning models use an optimization algorithm to find a set of parameters that achieves a local minima in the loss function. For example, Deep Neural Networks may use stochastic gradient descent to minimize cross entropy. This results in one parameter set representing one model.
In Lace, we use Markov Chain Monte Carlo to do posterior sampling. That is, we attempt to draw a number of PCC states from the posterior distribution. These states provide a kind of topographical map of the PCC posterior distribution which we can use to do a number of things including computing likelihoods and uncertainties.
Dependence probability
The dependence probability (often referred to in code as depprob) between two columns, x and y, is the probability that there is a path of statistical dependence between x and y. The technology underlying the Lace platform clusters columns into views. Each state has an individual clustering of columns. The dependence probability is the proportion of states in which x and y are in the same view,
\[ D(X; Y) = \frac{1}{|S|} \sum_{s \in S} [z^{(s)}_x = z^{(s)}_y] \]
where S is the set of states and z is the assignment of columns to views.
Above. A dependence probability clustermap. Each cell depresents the probability of dependence between two columns. Zero is white and black is one. The dendrogram, generated by seaborn, clusters mutual dependent columns.
It is important to note that dependence probability is meant to tell you whether a dependence exists; it does not necessarily provide information about the strength of dependencies. Dependence probability could potentially be high between independent columns if they are linked by dependent columns. For example, in the three-variable model
all three columns will be in the same view since Z is dependent on both X and Y, so there will be a high dependence probability between X and Y even though they are statistically independent, but they are dependent given Z.
Dependence probability is the go-to for structure modeling because it is fast to compute and well-behaved for all data. If you need more information about the strength of dependencies, use mutual information.
Mutual information
Mutual information (often referred to in code as mi) is a measure of the
information shared between two variables. Is is mathematically defined as
\[ I(X;Y) = \sum_{x \in X} \sum_{y \in Y} p(x,y) \log \frac{p(x, y)}{p(x)p(y)}, \]
or in terms of entropies,
\[ I(X;Y) = H(X) - H(X|Y). \]
Mutual information is well behaved for discrete data types (count and categorical), for which the sum applies; but for continuous data types for which the sum becomes an integral, mutual information can break down because differential entropy is no longer guaranteed to be positive.
For example, the following plots show the dependence probability and mutual information heatmaps for the zoo dataset, which is composed entirely of binary variables:
from lace import examples
animals = examples.Animals()
animals.clustermap('depprob', color_continuous_scale='greys', zmin=0, zmax=1)
Above. A dependence probability cluster map for the Animals dataset.
animals.clustermap('mi', color_continuous_scale='greys')
Above. A mutual information clustermap. Each cell represents the Mutual Information between two columns. Note that compared to dependence probability, the matrix is quite sparse. Also note that the diagonal entries are the entropies for each column.
And below are the dependence probability and mutual information heatmaps of the satellites dataset, which is composed of a mixture of categorical and continuous variables:
satellites = examples.Satellites()
satellites.clustermap('depprob', color_continuous_scale='greys', zmin=0, zmax=1)
Above. The dependence probability cluster map for the satellites date set.
satellites.clustermap('mi', color_continuous_scale='greys')
Above. The normalized mutual information cluster map for the satellites date set. Note that the values are no longer bounded between 0 and 1 due to inconsistencies caused by differential entropies.
satellites.clustermap(
'mi',
color_continuous_scale='greys',
fn_kwargs={'mi_type': 'linfoot'}
)
Above. The Linfoot-transformed mutual information cluster map for the satellites date set. The Linfoot information transformation often helps to mediate the weirdness that can arise from differential entropy.
Normalization Methods
Mutual information can be difficult to interpret because it does not have well-behaved bounds. In all but the continuous case (in which the mutual information could be negative), mutual information is only guaranteed to be greater than zero. To create an upper bound, we have a number of options:
Normalized
Knowing that the mutual information cannot exceed the minimum of the total information in (the entropy of) either X or Y, we can normalize by the minimum of the two component entropies:
\[ \hat{I}(X;Y) = \frac{I(X; Y)}{\min \left[H(X), H(Y) \right]} \]
animals.clustermap(
'mi',
color_continuous_scale='greys',
fn_kwargs={'mi_type': 'normed'}
)
Above. Normalized mutual information cluster map for the animals dataset.
IQR
In the Information Quality Ratio (IQR), we normalize by the joint entropy.
\[ \hat{I}(X;Y) = \frac{I(X; Y)}{H(X, Y)} \]
animals.clustermap(
'mi',
color_continuous_scale='greys',
fn_kwargs={'mi_type': 'iqr'}
)
Above. IQR Normalized mutual information cluster map for the animals dataset.
Jaccard
To compute the Jaccard distance, we subtract the IQR from 1. Thus, columns with more shared information have smaller distance
\[ \hat{I}(X;Y) = 1 - \frac{I(X; Y)}{H(X, Y)} \]
animals.clustermap(
'mi',
color_continuous_scale='greys',
fn_kwargs={'mi_type': 'jaccard'}
)
Above. Jaccard distance cluster map for the animals dataset.
Pearson
To compute something akin to the Pearson Correlation coefficient, we normalize by the square root of the product of the component entropies:
\[ \hat{I}(X;Y) = \frac{I(X; Y)}{\sqrt{H(X) H(Y)}} \]
animals.clustermap(
'mi',
color_continuous_scale='greys',
fn_kwargs={'mi_type': 'pearson'}
)
Above. Pearson normalized mutual information matrix cluster map for the animals dataset.
Linfoot
Linfoot information is the solution to solving for the correlation between the X and Y components of a bivariate Gaussian distribution with given mutual information.
\[ \hat{I}(X;Y) = \sqrt{ 1 - \exp(2 - I(X;Y)) } \]
animals.clustermap(
'mi',
color_continuous_scale='greys',
fn_kwargs={'mi_type': 'linfoot'}
)
Linfoot is often the most well-behaved normalization method especially when using continuous variables.