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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.12+ x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.12+ x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.12+ x86-64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10macOS 11.0+ ARM64

prefgraph-0.5.12-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.12.tar.gz.

File metadata

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

File hashes

Hashes for prefgraph-0.5.12.tar.gz
Algorithm Hash digest
SHA256 b379e9b256147d07b2b3b7279043dd914b190a35e3f89c06a2c5805d9b00fd09
MD5 92e0f62c500dbb72e09c7affccd594df
BLAKE2b-256 a75c32fb0c5a6c3dfc5e9473f17016c392651bb440a2afba24489f00a9229556

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.12-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 33a93e11cc3b867ea3b996237d497bc6a3afeeea7de38291b0d995403ff4acd3
MD5 ffb2b1d5ebc35e52b065b5cd49e385ec
BLAKE2b-256 3875baa84aa634be7f6d54b6e71063f6af0af4440b07c48c311ba6ea1d89d7e3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.12-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ee9d785202e0e79e684965a0aed93420c06e0aa399cd48f4cd8eaca06e152eec
MD5 6f33258ad9c3a3e3756ef2ad61a282e4
BLAKE2b-256 dbd2ee8d4a14c3311fe1e85c36fb8e0a6c058dd578f97127e3a6fc437d979f4d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.12-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 10ce88fcb78aa579812f9f532e7047ad3cd38ec41a1c51b32ed93afff7b4276d
MD5 5da6aa4fcc8b4891b74eccbcc624115b
BLAKE2b-256 2c355fc039b4c971c0b2d622709f02dd329a78a2ebff19768eebdaaac2525aa0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.12-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 848814c9194505ff990a289d177cdb7d7cab5438744a55ae298fe967e4d02281
MD5 1b7da8d47b564c51504a3b4078d64b3f
BLAKE2b-256 29ddf7961cb7af34cfa0bec30877f35bfe2a93204301ac7716777e34568bbce7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.12-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d8e00da523b5c09ca4bcd6ab2c16312727a5acdbb671ad4a6bda39453bca7c3c
MD5 1c70bab7ab78335b83527df1ce872095
BLAKE2b-256 0491d446a89cc88d00dd355a14b65a8779a140e6d690518c0beec50e3f12bcf2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.12-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a6d88f8e234bcf1d9eabdf063fac1bf4196c9d10d87031a849c54aba814f4507
MD5 6fbb0b5374e2a28b0c77bfd5edf6b2e3
BLAKE2b-256 203f339c4af92d319b8970de9644fe915fcc9a6fd61def96740a639ed0221a98

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.12-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 596cc62fd327f67da7f3aa395c5cce0adc9a60b9bf07b7975b87f9d1ae99829c
MD5 4be234c0508c27e9b97db96766657ab9
BLAKE2b-256 550e2ae8f2a89cf6dcfc93f8643bf1b66da4f20c1cc3cd39d0f9a94a57c9c187

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.12-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 2d65993d4bcb0ea609d0ad2e3f167a4aee868efb65df385526f38daca963140f
MD5 08d45570fd6afd14da2fad4ab5cfad37
BLAKE2b-256 17c3220f640549241fa77179f4867823b0b2da0ea757bd456e8f61de016b78ed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.12-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e4aceec0ae1cb3f5a9a59568b4eb6b9efc18440e993178a245321042fe828d92
MD5 dfc229add15bd252ceeef04a365367ec
BLAKE2b-256 06b5b81fb9fc10dfcf9166a2e6cc8d580ba5fbaf623aefe6f6decea600a5dc00

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.12-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4874f32154e857b239fbe2ec8c4dbb01a81014695720cf7a84a52f626495ddba
MD5 b71c7863978880e8a13bb89ac0ce5636
BLAKE2b-256 90efbbb1381668b607352fb40b95a24575ed76936fd31bce1ff3dc7a7625428e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.12-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7b0652b090781f291f951e7d89eaa1e8e9541e13a32c7f70ae9ef0fe8af11fdb
MD5 495eb2b64c8686ae788e0735df5c6c37
BLAKE2b-256 8f610c8badea8fb806faf5ecf0741657bddc2bc89c8115821487d4c378467f67

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.12-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ac412a6c28f5162dcb6d4e3494edbb800bf6e9b0467a47ba82bae1864843dd8b
MD5 4ad6e4a525d512437db5de92801bf0bc
BLAKE2b-256 f0bb5b3cab93b916dfb3b34ba5a0158999be3664e05c82b9961fb243717a2ae7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.12-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 fe8dff4a84a63e57a0b868889692f5fc2dabe1a63243bf11b71e0444e935992c
MD5 7ef10015bfdfbcf580102d2bd6a98bcc
BLAKE2b-256 20cff9bbf00c32d3b204bc9fc9539b623976d25e26920c423c14eab8cff9411c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.12-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e695b6f0fdaa90a1e1c8f4f69910814355ae5fcc350e1555b861e4e7b26abc09
MD5 c245280bce2fc4b464365e3ecf976cbe
BLAKE2b-256 8b28a36b06e52f15b99716d5595b5e8799dd79561e14c8bb0a95fc06446ea577

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.12-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f51c449b809317e93ba8d8dda13c236c22e000adb218f7fa0b9174ca6a596aed
MD5 b47616aa289b17d5cd21d52895345877
BLAKE2b-256 c728cae691ad3144eeca22fec383a99a33d58e4b94dc91627a2188ce808f202f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.12-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d2513bed63bb0d85b2edbe117f559623cd355bd291e5f1eb8ba44cdf595a50bc
MD5 539b3bc9564730611a5db52edc327aa6
BLAKE2b-256 9700abe4eca97f3ec0942081fa0abf9e7830b276631177fe9ad290aa8fa9a100

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