Get started

ScoreQuant turns score vectors into a few hard labels that keep as much Fisher information as possible. You supply one score vector per event; ScoreQuant chooses the bins and reports what they kept.

This page fits a rule, uses it on new data and saves it. The walkthroughs follow complete problems, and the user guide (opens in a new tab) covers inputs, criteria, solvers and nuisance parameters.

Install​

uv add scorequant

or pip install scorequant, on Python 3.12 or newer.

Fit a quantizer​

For a Gaussian location model with identity covariance, the score at the origin is the observation itself, s(x) = x, so the example draws scores directly. Everything after this point is the same whatever produced your scores. The cells share one namespace and together form one file you can run.

import tempfile
from pathlib import Path

import numpy as np

import scorequant as sq

rng = np.random.default_rng(21)
# NumPy at float64, so your output matches this page; see the last section.
execution = sq.ExecutionConfig(backend="numpy", precision="float64", device="cpu")

# N(mu, I_2) at mu0 = 0 has score s(x) = x: these observations already
# are the score vectors.
scores = rng.normal(size=(1_200, 2))
weights = np.ones(scores.shape[0])

Fit five bins:

fit = sq.fit_quantizer(
    sq.ScoreSample(scores, weights),
    n_bins=5,
    criterion=sq.DOptimality(),
    config=sq.SoftVoronoiConfig(seed=21, initializer_restarts=4, max_iter=120, record_every=20),
    execution=execution,
)

report = fit.train_report
print(f"D-efficiency    {report.geometric_mean_retention:.4f}")
print(f"effective rank  {report.effective_rank}")
Output
D-efficiency    0.6987
effective rank  2

The D-efficiency is the share of the information the five labels keep, averaged geometrically over the parameter directions; one would mean nothing was lost. The effective rank counts the directions the labels still see. A rank below the number of parameters means a direction is gone, whatever the average says.

Use the rule on new data​

fit_quantizer returns a rule in score space, and it labels scores it has never seen:

new_scores = rng.normal(loc=0.2, size=(300, 2))
labels = fit.predict_scores(new_scores)
counts = ", ".join(f"{count}" for count in np.bincount(labels, minlength=5))

print(f"bin counts  [{counts}]")
Output
bin counts  [38, 86, 52, 68, 56]

Prediction takes scores, not raw observations, so how you compute the scores stays in your own code and in view.

Save and load​

The rule saves to a small archive of arrays, not a pickle, and loads where JAX is not installed:

with tempfile.TemporaryDirectory() as folder:
    path = fit.quantizer.save(Path(folder) / "rule.sqz")
    rule = sq.Quantizer.load(path)

reloaded = rule.predict_scores(new_scores, execution=execution)
print(f"same labels after loading  {np.array_equal(reloaded, labels)}")
Output
same labels after loading  True

Only need labels for this sample?​

If no new events will arrive, optimize_partition labels the rows you already have:

partition = sq.optimize_partition(
    scores,
    weights=weights,
    n_bins=5,
    criterion=sq.DOptimality(),
    config=sq.DExchangeConfig(seed=21),
    execution=execution,
)

print(f"D-efficiency  {partition.train_report.geometric_mean_retention:.4f}")
Output
D-efficiency  0.6988

It returns labels, not a rule. Many rules agree on one sample and disagree elsewhere, so PartitionResult has no predict method. This fit is exact and deterministic, so you can rerun it in your browser:

Committed result0.6988D-efficiency printed by get_started_program.py

Fetches the same 1,200 rows this page fits, then runs the same D-optimal exchange solver on the NumPy backend inside your browser.

Backends and precision​

JAX is the default backend; NumPy is the portable one, and the one your browser runs. JAX computes in single precision unless the application turns on 64-bit mode, which the library never does for you. The cells above pass an explicit NumPy, double-precision, CPU execution so that the output printed here is the output you get. The API guide (opens in a new tab) has the details.