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.11.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.11-cp313-cp313-win_amd64.whl (4.3 MB view details)

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.12+ x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.12+ x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.12+ x86-64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10macOS 11.0+ ARM64

prefgraph-0.5.11-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.11.tar.gz.

File metadata

  • Download URL: prefgraph-0.5.11.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.11.tar.gz
Algorithm Hash digest
SHA256 9fe805fc773826cca372325310a544f5ae4ee7bb8f60f8a71bfc01efb0fe601f
MD5 e00ce28eb8276755b8742c403961b674
BLAKE2b-256 71bca18ddd48ce4ed5ff21a7be102c96fb8a8173698501d439260547903ff35f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.11-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 39d0ec03ebc970d073b6a10dec5d6154e7a24015b94602ab8946583162316d9d
MD5 afb1292311e66d570e4e087eea14fb96
BLAKE2b-256 dc9cc03399feda9dec6d5ac1e3088113cdad106a156ea68c043f83fada3f5163

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.11-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9b81138019cf578f5dd73c9acb4e10d9ee900c581be9407d5838374f4e796e2e
MD5 e5e6e89da4312c88dc44d914a3aa48eb
BLAKE2b-256 e73a1ce71ad6f369ada56234004e06abbd9750542defd6a68e76a6e76f14c459

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.11-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 87b1c21c2bf8a571324c0deb069a4d79de503ce8d30db8dbc18dee6cc152f55e
MD5 4d3deb04de571e1542c6de1fa1bf49e8
BLAKE2b-256 abf705cbcc68ed4f00d43fda4c9495fa7a122e10129ce0de7c17b4211c2a2df2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.11-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 078d1d935355f3c7c2b6bed859ee133f9fe4fcc6fa3370e3a686ac9d43139f8b
MD5 6bc44b1f5bdb2bd995132a61b453941e
BLAKE2b-256 c1e0ae372d1e85cac8fb1cb01a422cee451278b566ff370ff97cf08b97e0ef77

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.11-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6061cb887c0ae5469072a3c853ab7d899c591b2f43fcadfafd6ee2f8c53bcc2f
MD5 80198508088fa40761c2ab667663e128
BLAKE2b-256 e5068671bc61452adf9ffa218bb7cde724b00fff4a0a92fce195e316aefed5d2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.11-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 cbb8f937daa041d3a4fe8c346d6cc7b0d5406bd2a35201969c8e026e79dd5882
MD5 0f7a4e6db8124d91741331b31ce3a514
BLAKE2b-256 a8bc8c0eae3abec9b146f552aad3d99da37536e26f804260cdee9492b9d5d41f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.11-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e530c06c801bd69747110f7f79b968a54e2d1b978e7ac3052d1dda5d9d397f81
MD5 0254acb9804cddfc4289b0ae393e878b
BLAKE2b-256 9e174e35e0d4f2ebcffdc319204c03ed5e558ac659188e3155d077ffb68d54ff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.11-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 6452f15c88ba9d35f3352201589157fbf0139924663222e89753eb3f5ad341e7
MD5 468b84f10f5c005a317634a8c4a77567
BLAKE2b-256 e38b67d224b0e6841b953a469a25524926f7bbb35cf619b8960df9f9dd732d4b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.11-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 d5502472a0f5afcc742e77c1bb3427cc02038f651e5d1b20fac618e5db14bad9
MD5 2421a8704d6630caae1b26c0472702ee
BLAKE2b-256 c3fba0d76cb19b4e19a673486adaad1b6c85e588d6e18da6660a7925af3b3694

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.11-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1355d956a134e955a085d738eaa14b7234ec288a193030538d58a70e8bb3ce68
MD5 430a5348695f1bdd81b2ca2e465d6d89
BLAKE2b-256 e500b7863a8986ffce7cf248904d35bafd253a736cc96bf4e2698a4a9b6d0ede

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.11-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7b30afa7631f307aaf659ba9a2df3b13e70b90b718ac09bd8bc4bafda98189ff
MD5 c3cd208073fcabec73e1dd21a992a70c
BLAKE2b-256 3e566100b5c450ca2c80edcad901e546f703f8f80804e30ff6c1c15a311c4491

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.11-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 afdd40c71b84f4779ebeb408f56023bc74ef23c589956b00163fdaf8c0e59887
MD5 df29d9cbf245b82a44012526c495ad34
BLAKE2b-256 1c61be9b24bf6c334f7f52035b6e0f5401a615e3a1ded297dd75ff483642b4e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.11-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 8a3ff5d29098f165f613994699e072659dfe7fa7ebb886823d4db9d17f749517
MD5 32ab2da22a49e1d0fd8b99cc15563604
BLAKE2b-256 1f254d072d9014f8e93a19f9ae3160670d5513a66e063c7e4c942351e530c600

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.11-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0d075c56031ef21486e8b8ee24fc492373837dc334880979ccef8cc838141fe4
MD5 e2f1989c0cafb99731866d9fed3220f3
BLAKE2b-256 8c3f7ebff50fb6853836a40ac532f64fc6d1322ef13590842dc7d2c04c767d30

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.11-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 35486eabc2289f5ee98bd45e4501547f4394c37ea5c52a36c5c5c578f84e9cc0
MD5 c2ad571312431a702f47793a2d885f59
BLAKE2b-256 0e8d845ee8841561d4e2b3f3fbbc65d7462ac0f0b74e79558696050710bc2bc5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.11-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 821292e9a4b08c642dc06514c73599b1d525aed5b7d1df4704e8bc0608579d06
MD5 47d4613cdb814cacf21557aa78f33d6a
BLAKE2b-256 fd4b187e1c5b5d84d0ddb65f28f7927b3517c649f8b0e1beb8b26bee5fc624dc

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