Skip to main content

scripts for calculating likelihood ratios

Project description

LIR Python Likelihood Ratio Library

This library provides a collection of scripts to aid calibration, and calculation and evaluation of Likelihood Ratios.

A simple score-based LR system

A score-based LR system needs a scorer and a calibrator. The most basic setup uses a training set and a test set. Both the scorer and the calibrator are fitted on the training set.

import lir
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

# generate some data randomly from a normal distribution
X = np.concatenate([np.random.normal(loc=0, size=(100, 1)),
              np.random.normal(loc=1, size=(100, 1))])
y = np.concatenate([np.zeros(100), np.ones(100)])

# split the data into train and test
X_train, X_test, y_train, y_test = train_test_split(X, y)

# initialize a scorer and a calibrator
scorer = LogisticRegression(solver='lbfgs')  # choose any sklearn style classifier
calibrator = lir.KDECalibrator()  # use plain KDE for calibration
calibrated_scorer = lir.CalibratedScorer(scorer, calibrator)

# fit and predict
calibrated_scorer.fit(X_train, y_train)
lrs_test = calibrated_scorer.predict_lr(X_test)

# print the quality of the system as log likelihood ratio cost (lower is better)
print('The log likelihood ratio cost is', lir.metrics.cllr(lrs_test, y_test), '(lower is better)')
print('The discriminative power is', lir.metrics.cllr_min(lrs_test, y_test), '(lower is better)')

# plot calibration
import lir.plotting
with lir.plotting.show() as ax:
    ax.pav(lrs_test, y_test)

The log likelihood ratio cost (Cllr) may be used as a metric of performance. In this case it should yield a value of around .8, but highly variable due to the small number of samples. Increase the sample size to get more stable results.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lir-0.1.29.tar.gz (52.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lir-0.1.29-py3-none-any.whl (65.0 kB view details)

Uploaded Python 3

File details

Details for the file lir-0.1.29.tar.gz.

File metadata

  • Download URL: lir-0.1.29.tar.gz
  • Upload date:
  • Size: 52.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for lir-0.1.29.tar.gz
Algorithm Hash digest
SHA256 a41fe7f081b43d7c574186ac030e9ddf1c85771286c635bdb136ef51b0ab2a8d
MD5 321346fbf0905dd1c481034dfcd8762c
BLAKE2b-256 e1575b13d9903a9023741fb1b518a4a1909ba8f64ed746fa835560e2522eb9cc

See more details on using hashes here.

File details

Details for the file lir-0.1.29-py3-none-any.whl.

File metadata

  • Download URL: lir-0.1.29-py3-none-any.whl
  • Upload date:
  • Size: 65.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for lir-0.1.29-py3-none-any.whl
Algorithm Hash digest
SHA256 18c933fcb2f2b796f62156257714acc2a7cdcb5184f3611d5fd57edd6eaf13ab
MD5 137ecb9f8ac0db4b29281e377701d39b
BLAKE2b-256 8f630144bb1cd0020cf06c1c6bf642ba98b136baab0266ea64459345fb9cf6f9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page