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.10.tar.gz (367.6 kB 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.10-cp313-cp313-win_amd64.whl (4.3 MB view details)

Uploaded CPython 3.13Windows x86-64

prefgraph-0.5.10-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.10-cp313-cp313-macosx_11_0_arm64.whl (4.3 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.12+ x86-64

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

Uploaded CPython 3.12Windows x86-64

prefgraph-0.5.10-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.10-cp312-cp312-macosx_11_0_arm64.whl (4.3 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.12+ x86-64

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

Uploaded CPython 3.11Windows x86-64

prefgraph-0.5.10-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.10-cp311-cp311-macosx_11_0_arm64.whl (4.3 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.12+ x86-64

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

Uploaded CPython 3.10Windows x86-64

prefgraph-0.5.10-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.10-cp310-cp310-macosx_11_0_arm64.whl (4.3 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

prefgraph-0.5.10-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.10.tar.gz.

File metadata

  • Download URL: prefgraph-0.5.10.tar.gz
  • Upload date:
  • Size: 367.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.12.6

File hashes

Hashes for prefgraph-0.5.10.tar.gz
Algorithm Hash digest
SHA256 9a213d016f47ce00cf21d3559b83df6593b01769b53c2bd961d89449a897bf35
MD5 37f66b3aef9599d86f348edc0910664e
BLAKE2b-256 321f500820d2a12f85d2865a8ef5415bb45f0f03e745dc5e90974fc89f8525d0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.10-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 549e4a1174df800debe3e1763399ae44673171c87b255f45b5144796c678d9c1
MD5 86f293043b8492869ae32be756a896df
BLAKE2b-256 889f64f1e2bfbe611d799ff807ed5a85e8cfff3d3e193344a3ed712f39bdf4d9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.10-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 980192d8447748e8882a06ff8e33d4ab8120280361d98756aad9e87c6347dd6e
MD5 57d79f665cb57ae5a40b26319b11ffef
BLAKE2b-256 81ae3965c685a3213df4607fc58249f3578e273e1d5d98991eb923d081a88157

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.10-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9d53f537ed90734e5f80284112a15ab5f80fa4c1acbc629ea62ecb72246c5b50
MD5 882d5099d35331e03662ed7351f510e0
BLAKE2b-256 54453b588e0352b710c4354c32045f06d071763b846c8b2066283ff44e100f3b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.10-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 18e7ac2952911ab3619d4fa5b4922feee00b4b511146bbb32e9f5891b2b4b1ec
MD5 c19e83de09a95d96a1a3f929ccfe02f9
BLAKE2b-256 bb560b03203b2783153bc3dc86a8afae2b784fdff4438546b13c6dd9e05e04d4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.10-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 29bb56de6d987043cf3562461f776226bcf2301a3fbf5b017a87371e9f29b58a
MD5 e92c0f8a69c91176deaa831083384a4b
BLAKE2b-256 9aaeb06bf58b8371f8d481b659b4f4e8d147da6856126770bd250c35a0df8be6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.10-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 04f95d12f7de7b09b4264d49731cb2ac4a8d09a7e7aceb38e765fb94469871eb
MD5 eba677d83c832f2a90280ff5a6f6ea8e
BLAKE2b-256 4d9828ac9d3068009e7f8c92a55843c9dc5e8b6211dabfae6cff62afc277c582

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.10-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0a91686e5cc76e154aac2146759783f3900f47e336169aa16a010a8934c255c7
MD5 2be147c7a58a59a4487f7f9c9988eb0f
BLAKE2b-256 49bfd40cb7273383c821422c1c1d39e56cd7e1cbf1c406eea9c28f7db23dd961

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.10-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 163e5606c8516797d18f570e212748cca5da2cce2ac16c7a15e58eae43b7fcd6
MD5 06369b6680daa0a74de8dbd3190000ec
BLAKE2b-256 99819c63b712f62dedd0d935cf7c62ba94d715d2f01848cf85f3232945a0ac23

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.10-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 52962b10fb7ee2b6239138285d009d38306eecc7ad257a26ae63985494dc8120
MD5 34d804a59954d1488d93d3ec5cb6b1ef
BLAKE2b-256 d93d2995ce2a200af029590b1175f92ef4c63b736d54d1b337c13eead8d0e9ed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.10-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e7b6a02a5a478dedcce34c16cca97b9c7397518b6a55ecb892c61abfc3ad4c6e
MD5 2be338ede46e70c7e0eb8236748c7eaf
BLAKE2b-256 0e365233a8cf08fdd685bbd5430b64af6ebc0df84a26d845e124aaf24506cbde

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.10-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 438e478a9df078d79f70ef345c19238ea1394dab97daf006fc8632b9bc027b43
MD5 f82c08ecbd631db372f4057dd5b7a9f0
BLAKE2b-256 4e0607c99d9568d024c3ba10f443e9274c523764205b570d9ca16ec4051c7176

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.10-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 be5c091849b9e4fe1ec6015a5e3b282ea6565c72021e6a47ba18f31847845642
MD5 24fc26cdfb3f57cf63317eab2fee62fc
BLAKE2b-256 741343ac6772df935cb74f8151a102397d4acb428e41f0fbf0177f94bbb5b904

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.10-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 ec6d29c2716afa72e4f7de9a99b13c0809ed25711e2e585455bfad847b202658
MD5 aa89e0fc7bdd8031f5f9d012f628a0fe
BLAKE2b-256 150b762649c6d8c3faf83feb5a19069771ce2b663ddcda0ddab6667b8d1c17db

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.10-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1172f9526ab0c585ece89898be66f6f6a830e8c9a02b6f0d8cb0fbd4a54d11b2
MD5 89440fb44ac7acf1e890616df133eb0d
BLAKE2b-256 c98f2deafbfb90c615008d60f3d3666e3d4beaefc392654cb93cb615fd47d38f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.10-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5b2c7e328fe334e3e49ea78788277f04bc78ca7865d420f6331e7f81ae67557b
MD5 ebf9e4c6a5062d3ba4b63e2796acf00e
BLAKE2b-256 62c79afbcccf743d007793c873b811b21e303bff43c49ea1a8697d6157c0a556

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.10-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 15f79be9a3adfc6ff46fc85c865fbabf0b82bb5ac524394fc9abbe9a0e2bab12
MD5 f13605409b94dfbcb96db85279e716c4
BLAKE2b-256 5df23f9f0e901f64d6ae15adfb66700a08a9877def4a379c7fa97f7c51241fb7

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