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
Rationalizable 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: https://prefgraph.readthedocs.io/ - examples, theory, API reference, and applications.

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.12+ x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.12+ x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.12+ x86-64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10macOS 11.0+ ARM64

prefgraph-0.5.14-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.14.tar.gz.

File metadata

  • Download URL: prefgraph-0.5.14.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.14.tar.gz
Algorithm Hash digest
SHA256 d18086233a1de780b82e9a508eaa332cc22313671b96fe9d6bd3ecd30d4df994
MD5 8599f78c9a72e94d85d226c900059acf
BLAKE2b-256 412ae9eae4779b98089bcb79ab715e933364bb363fbbe76a4aec0e697404bd66

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.14-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 1b3522ca4204de821729f1c2896a13087dbb817222e0f227108be01170851e67
MD5 18d7cf4288496fa3c6629f5c7a849a40
BLAKE2b-256 577ca9c3de8ce39adceb3610fe8bb06fe55693500bd7c721ceda2cf0277eabc7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.14-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e52393af8d56a3def0e35327af92ab7296b79e3d23af98146008231497da4663
MD5 fefd54ce499287febbfd65c4fe5b0ec6
BLAKE2b-256 4085f969b956c542aaceb8b476684d50f7a02b3defb9bc86585d201280fb2500

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.14-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 565478d54fd8a43f2a791cff327e8d85879d8806c908f97990436ad4d706262b
MD5 d06085a1008db3e1f1144d780af2e10f
BLAKE2b-256 3d514e991f573310099f95e98fb0cf1fde87b64d62fc3ef9d38084512d7fd418

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.14-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b554c523bb59af74138ba863a654aafa1c04d4897ad33e76c4b4ac9f7763587c
MD5 82dd5d74294d26903e54ec34ecffc4f9
BLAKE2b-256 063c6c1a738895c47d1afd4774cf60ce8b0ee9109e9acd345b8e0a86f09e2cf4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.14-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d49219822fc025d6d6a4281ebaa41927c1f8988ec37571417c227043f16c5b2e
MD5 b557a134e5a081356530d57e0a819c4d
BLAKE2b-256 48f6f5851eb8796cf992d5e0afaae0299944548d700bbda9f426cfc7663fbd49

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.14-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 fa3c3ddd9683ea0bb0490e27b32cf6f11e3c310294622de57e808d005a18462e
MD5 8d3e38dea3be7364b0da138ef64507ef
BLAKE2b-256 7595bab1b2400ddb0bed7fbcde62871969e78467306cee653906ad01b166df96

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.14-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5fc727537698da85daa11a711d59620a5f62774625dc5bfad5ffd6dd77c7ecfe
MD5 800b4aac3116ded9005125e7a399320a
BLAKE2b-256 98acfa9c898797a29db994992c09e0d063e753453d5fb72a06226d4945718538

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.14-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b43c3a45d5ae3d60667fed5a1b70adb2429bd8230c605b74256f99d219719c4b
MD5 38fc64f8c732716eaf56bf48b1c6e3b2
BLAKE2b-256 40220320db160d9bee2bc9b6fb2f51d26e9b61764d3db45b8334756683191ff6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.14-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 18e86ed4d104b48a394ae9f760cfab696248152dd4433a509232eae5caf95896
MD5 0c5bfc9af69aa2f5c0409e8eaf7eda53
BLAKE2b-256 3eea698bac573e207881b553898c76a6391222920e26c59fd1d9772ebb2d624e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.14-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4126917d333258b5e87443a07ece0beaf0274fa81e28db54ea20c988068f0e27
MD5 34d8205eaf39cc465dba42f3a96e5793
BLAKE2b-256 ae9471f072783c7b3c000753682440ba39af3b146546476afdbb28a86e9b4431

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.14-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 92b0423058c456f7814700f41089eddfb302ed95831c73b89613ff13a480fe66
MD5 40bb719cb3595258392a73f65e5cdd28
BLAKE2b-256 1202d3c31390e44b4565936aa3375ecc2bfe8771e54d09fe45df98b8b0ee137d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.14-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 58fac759b0ce112ebf613ea5125314645979674c52cfdb1e75debf544035ece8
MD5 a40a248a227ec7199bd5c78e7f1c8567
BLAKE2b-256 782cfcd0b5d30b1bc444821391885662fcdab1e4c1b87410ad40ace3a45f7c97

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.14-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 1c74943ad0d97ef8f59a7e0c9c392d0ec3f533232496dac357d87741a9061afc
MD5 c78af065dfd4b5122d726e64500bd4cf
BLAKE2b-256 6198427815f98c0360832339b5e8a138feabdd497c4fcfbc8a6e9465fa800ca5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.14-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6be3afcb6d2416e475ec39e88a495228e4b2625cfcf07d525c6c868b69631f33
MD5 573b688b02668862ae378781f976e5d3
BLAKE2b-256 6cd198f5e0a7690ae5ac9b88bc05f5df0d68c0ac657b17e7ab69c4ca607fd3e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.14-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7eb9d528672f4c2142957a8edfcc75294aab925248a167a24214f34f5a37f6e3
MD5 b72553b15d9d33e3c73aa09399952200
BLAKE2b-256 b9dc5bd038c15e04b09f8f08c0deec36b5b5ea92ae64e8bf6bcb10b800117d83

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.14-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c5c75b8c24f177e06f06739a1c286d699454916f8fa05e7a48ec5d65b43c8dd8
MD5 20e2c07f740dfda1105accee3066b244
BLAKE2b-256 690d3527159383b6a8fd3c65aa874454422396e73b242e898c9dfe9692a48689

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