Skip to main content

Revealed preference analysis at scale — rationality scores, efficiency indices, and utility recovery. Rust engine, Python interface.

Project description

PrefGraph

Rationality scores for every user, at scale. Rust engine, Python interface.

PyPI Documentation License: MIT

pip install "prefgraph[datasets]"

Quick Example

Score how consistently each user's choices align with rational utility maximization. Paste and run:

from prefgraph.datasets import load_demo
from prefgraph.engine import Engine

users = load_demo()  # 100 synthetic consumers, no download needed
engine = Engine(metrics=["garp", "ccei", "mpi", "harp", "hm"])
results = engine.analyze_arrays(users)

for r in results[:5]:
    print(r)
EngineResult: [+] GARP-consistent  ccei=1.0000  hm=15/15  (42us)
EngineResult: [-] 3 violations  ccei=0.8472  mpi=0.0231  hm=12/15  (38us)
EngineResult: [+] GARP-consistent  ccei=1.0000  hm=15/15  (35us)
...

Every score is a feature. Use them for fraud detection, user segmentation, A/B testing, churn prediction, or personalization.

Your Data

Have a pandas DataFrame? One line:

import prefgraph as rp

# Transaction logs (one row per item per time)
results = rp.analyze(df, user_col="user_id", item_col="product",
                     cost_col="price", action_col="quantity", time_col="week")

# Wide format (one row per observation, items as columns)
results = rp.analyze(df, user_col="user_id",
                     cost_cols=["price_A", "price_B"],
                     action_cols=["qty_A", "qty_B"])

# Menu/click data
results = rp.analyze(df, user_col="user_id",
                     menu_col="shown_items", choice_col="clicked")

Returns a pandas DataFrame with one row per user. Customize with metrics=["garp", "ccei", "mpi", "hm"]. Handle missing data with nan_policy="drop".

Scores

Score Field What it measures Range
Consistency is_garp Are choices rationalizable? (GARP) bool
Efficiency ccei How close to perfectly rational? (Afriat) 0-1
Exploitability mpi Value left on the table per choice (Karp cycle) 0-1
Homotheticity is_harp Do preferences scale with budget? bool
Noise fraction hm_consistent/hm_total Fraction of rationalizable choices (Houtman-Maks) 0-1
Utility recovery utility_success Can latent utility be reconstructed? (Afriat LP) bool
Per-obs efficiency vei_mean Average efficiency across observations (Varian) 0-1

Which API?

Engine Function API
Use case Score thousands of users Deep-dive one user
Speed 10,000+ users/sec (Rust) Single-user
Returns EngineResult (flat scores) GARPResult, AEIResult, etc. (matrices, cycles, graphs)
Metrics 7 (garp, ccei, mpi, harp, hm, utility, vei) + network 30+ algorithms
Input list[(prices, quantities)] BehaviorLog

Engine for batch scoring. Function API when you need violation details, observation/item graphs, or advanced tests:

from prefgraph import BehaviorLog, validate_consistency, compute_integrity_score
session = BehaviorLog(cost_vectors=prices, action_vectors=quantities)
garp = validate_consistency(session)       # GARPResult with violation cycles, matrices
ccei = compute_integrity_score(session)    # AEIResult with binary search details

4 Choice Categories

                Test (bool)     Score (0-1)     Recover (vector)  Structure (bool)
Budget          GARP, WARP      CCEI, MPI, HM   Utility, CV/EV    HARP, Separability
Discrete        SARP, RUM LP    HM (menu)        Ordinal utility   Congruence
Production      Prod GARP       Prod CCEI        Tech efficiency   Cost minimization
Intertemporal   Exp discount    —                Discount delta    Quasi-hyperbolic
Category Input format Example domain
Budget (prices T×K, quantities T×K) E-commerce, grocery, food delivery
Discrete (menus, choices) or (menus, frequencies) Surveys, A/B tests, recommendations
Production (input_p, input_q, output_p, output_q) Supply chain, manufacturing
Intertemporal (amounts, dates, chosen) Subscriptions, savings, loyalty

Performance

The Rust engine (rpt-core) handles graph algorithms and LP solving via Rayon thread pool. Python handles I/O and the user-facing API.

Users Metrics Time Throughput
1,000 5 0.1s 10,000/s
10,000 5 2s 5,000/s
100,000 5 20s 5,000/s

18-100x faster than pure Python. Memory stays bounded via streaming chunks.

Documentation

Full docs — examples, theory, API reference, application case studies.

E-commerce benchmarks — 6 datasets, 117K users, 12 targets. RP features add 0–2% lift over RFM baselines; on Taobao, RP-only (0.925) beats the engagement baseline (0.913).

15 dataset loaders — Dunnhumby, Instacart, H&M, Taobao, and 11 more real-world datasets.

License

MIT

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

prefgraph-0.5.13.tar.gz (17.6 MB view details)

Uploaded Source

Built Distributions

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

prefgraph-0.5.13-cp313-cp313-win_amd64.whl (4.3 MB view details)

Uploaded CPython 3.13Windows x86-64

prefgraph-0.5.13-cp313-cp313-manylinux_2_28_x86_64.whl (5.4 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

prefgraph-0.5.13-cp313-cp313-macosx_11_0_arm64.whl (4.3 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

prefgraph-0.5.13-cp313-cp313-macosx_10_12_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

prefgraph-0.5.13-cp312-cp312-win_amd64.whl (4.3 MB view details)

Uploaded CPython 3.12Windows x86-64

prefgraph-0.5.13-cp312-cp312-manylinux_2_28_x86_64.whl (5.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

prefgraph-0.5.13-cp312-cp312-macosx_11_0_arm64.whl (4.3 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

prefgraph-0.5.13-cp312-cp312-macosx_10_12_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

prefgraph-0.5.13-cp311-cp311-win_amd64.whl (4.3 MB view details)

Uploaded CPython 3.11Windows x86-64

prefgraph-0.5.13-cp311-cp311-manylinux_2_28_x86_64.whl (5.4 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

prefgraph-0.5.13-cp311-cp311-macosx_11_0_universal2.whl (4.3 MB view details)

Uploaded CPython 3.11macOS 11.0+ universal2 (ARM64, x86-64)

prefgraph-0.5.13-cp311-cp311-macosx_11_0_arm64.whl (4.3 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

prefgraph-0.5.13-cp311-cp311-macosx_10_12_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

prefgraph-0.5.13-cp310-cp310-win_amd64.whl (4.3 MB view details)

Uploaded CPython 3.10Windows x86-64

prefgraph-0.5.13-cp310-cp310-manylinux_2_28_x86_64.whl (5.4 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

prefgraph-0.5.13-cp310-cp310-macosx_11_0_arm64.whl (4.3 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

prefgraph-0.5.13-cp310-cp310-macosx_10_12_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.10macOS 10.12+ x86-64

File details

Details for the file prefgraph-0.5.13.tar.gz.

File metadata

  • Download URL: prefgraph-0.5.13.tar.gz
  • Upload date:
  • Size: 17.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.6

File hashes

Hashes for prefgraph-0.5.13.tar.gz
Algorithm Hash digest
SHA256 b883a5ff7d52beb9b703660259fa4016a761dbc52f1e5f96ccb4e03d38c38fd9
MD5 bab80be6669873e289d47bb34ecd70d3
BLAKE2b-256 1667bb35560f8f9c2cf0232c385381e462193fc9b80a50506e2ceca7ef59c672

See more details on using hashes here.

File details

Details for the file prefgraph-0.5.13-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for prefgraph-0.5.13-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 35a29a0a7f1f5bfd4596d44d20bc8b0ecbe4227c4f4a11af70236f75addfb469
MD5 ea99176dcbf4142d24b980b860df12b1
BLAKE2b-256 3f299441406095a83d2a9e2d52b38c9764d52f5dc4d0ac0b395a7661929726ee

See more details on using hashes here.

File details

Details for the file prefgraph-0.5.13-cp313-cp313-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for prefgraph-0.5.13-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 79f1bc3a49f3b867fc856459fdfeba7f5da9a9e45372311cd735e10e74f8ed4b
MD5 de43d34e81107cebd4310ce6534bd98d
BLAKE2b-256 2604cd18b85d793f096e4efe2d8567fdcaed177e7dc6f8c027521d14e3f8c533

See more details on using hashes here.

File details

Details for the file prefgraph-0.5.13-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for prefgraph-0.5.13-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6c4f2d8d46a16fc2243f3220e97aad8867f99e38db096f19fcbc41841d082a2c
MD5 f00ceca65478d30151190455db7971c4
BLAKE2b-256 c1ed889a619003935cb056a65e00aa3f90a5fa8991e3bb5a6bc6f4649a33a505

See more details on using hashes here.

File details

Details for the file prefgraph-0.5.13-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for prefgraph-0.5.13-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ab2b66174d3c1e09689a8c5ab6bac9e5ac82cc069a42e28b4ec961e8f3c974c8
MD5 fe0a815c81dd235f5b7d64220e6823e8
BLAKE2b-256 ca8667333b3409158438d83c227a35c7f962c7bc1ceb54d3146d4cea4a08f0ce

See more details on using hashes here.

File details

Details for the file prefgraph-0.5.13-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for prefgraph-0.5.13-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f6b7760d98fbd05cce331c49a5be91eace65196023b3b2b2730cee92a6336f46
MD5 5ea4d3c13f36f36c1db8660c87005284
BLAKE2b-256 afa2c5ce08b9f9f99dae86202ec54b39bc2e58ca4706ab4522631d06560526aa

See more details on using hashes here.

File details

Details for the file prefgraph-0.5.13-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for prefgraph-0.5.13-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c21a4fa5c481ff11363cfa84a770a0180e04fa9cc266c479e86e738e1ada1298
MD5 85027fc93cc1293e275c8a0147b13eff
BLAKE2b-256 a33f2a43003e8e5f358f3ad35e07f2ab845b81cc3459d07dfa1bd12153e7f6aa

See more details on using hashes here.

File details

Details for the file prefgraph-0.5.13-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for prefgraph-0.5.13-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 36601789e2b72f607ee888ded93f3b492c4f852fafe840bedbc25981a3b207d1
MD5 124bba4d3a636e98669a6387de71cb67
BLAKE2b-256 c6b8438c166bbc03365d9c07500db99bbc448949343729a987a3890626b8a51f

See more details on using hashes here.

File details

Details for the file prefgraph-0.5.13-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for prefgraph-0.5.13-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c81dd52edf66fb2b2e188c74968a04a5c57afe1cc1834495af82acf06076e0b4
MD5 d98996b646cc67adbff58d9ef54751ed
BLAKE2b-256 9f3777734223967ee645a7ac5ca420dec069d9c598008b0f60c3d0b1736907a8

See more details on using hashes here.

File details

Details for the file prefgraph-0.5.13-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for prefgraph-0.5.13-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 94f9b4d7af217bd56fd8215078f9ddf4156b5825d62df072934dab6374cdfbb0
MD5 54147b3b94578287695d36f30e11c4f5
BLAKE2b-256 049a7ba2e8734cf28021762381c05b5c946d54aa529fba007f0bf8c418dfb842

See more details on using hashes here.

File details

Details for the file prefgraph-0.5.13-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for prefgraph-0.5.13-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8c90fa9ef4ccacc7f98174954cd816b91d89e109b129c001f9fb3ade87cfdec8
MD5 9e277dcc443fd9b8343bc328365b7a6d
BLAKE2b-256 2cdc0cf47aee636761b99a484c1d17b974807e073c0758853325414af73afa5d

See more details on using hashes here.

File details

Details for the file prefgraph-0.5.13-cp311-cp311-macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for prefgraph-0.5.13-cp311-cp311-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 bfcc717f0e601e10f733ffac571b7d45870aef5c1818d99950562214924f42b4
MD5 4e623b88437070e62450fe9ae9fb669c
BLAKE2b-256 89315414e61e5eaec7505c44812152678b03f06fac807c4b441a9b91dbe39035

See more details on using hashes here.

File details

Details for the file prefgraph-0.5.13-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for prefgraph-0.5.13-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6af34cd30ef473c9203cdf5c6029fcc10e550a9c5b23102265c07f7c9c209d30
MD5 5e54e46f4777b2b3c3ac39537346c4f1
BLAKE2b-256 321ca04f9b98137a379e3b798c8fbe8c7a94bc043680db517780595eb8aedf48

See more details on using hashes here.

File details

Details for the file prefgraph-0.5.13-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for prefgraph-0.5.13-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 fe3f6ff28893e694f72e2a61d50ff1663f2740905a54584c67c8dcc1dec2ef97
MD5 5f26f5123a6c2a30a6b40d78d7094de8
BLAKE2b-256 f6daed8c74d521887eaeb2b402a0d43bae0fc18d90e68631cf7c04da5e6068b0

See more details on using hashes here.

File details

Details for the file prefgraph-0.5.13-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for prefgraph-0.5.13-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 b64df6cfa65b434cd4faa42ab173e011f03ee2b4b23102a792c28d030771794e
MD5 30c904e581df1251bdaa87decb145f95
BLAKE2b-256 f55cc4287bc349729fc8c2a0365f8d4c7e0a92d7ba2a64cf40be187b1658ba0f

See more details on using hashes here.

File details

Details for the file prefgraph-0.5.13-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for prefgraph-0.5.13-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8ae685410662e06f37c23f983af37d176a0e704ab6997e10459ca2b55b423de8
MD5 9cbc095693f4c1a94aeb6951a8770098
BLAKE2b-256 a9fc721e3ef1e8f26f02b36bba8e8c3ae0948be888b57c778bff27f15ca02938

See more details on using hashes here.

File details

Details for the file prefgraph-0.5.13-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for prefgraph-0.5.13-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ec8d731b643c615ecdb4a8dc81fbb7c91afee93dac9d12e5dfddf0f464f5c838
MD5 be9e9da55a17f2ef3a1144021b1f0a05
BLAKE2b-256 c32172603117d5e56e02ee2c5bba6f9504133a8609c6562f0ece9230ff3300c9

See more details on using hashes here.

File details

Details for the file prefgraph-0.5.13-cp310-cp310-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for prefgraph-0.5.13-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9729674857e4e33dd72b91224c15bc5219370e29a8b4d3393f7bf5bd7b1ac9ea
MD5 cfc3b54eadb4d555d2dd019a9678fb7a
BLAKE2b-256 63d639c88dde0cc95ac7e6a0e67701655a25a26742ff653bd6ab49dddea3449c

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