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.

7-dataset benchmark — 167K users, 15 tasks. RP features add 0–0.7% AUC over RFM baselines.

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.12+ x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.12+ x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.12+ x86-64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10macOS 11.0+ ARM64

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

File metadata

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

File hashes

Hashes for prefgraph-0.5.7.tar.gz
Algorithm Hash digest
SHA256 e44db85531ca1197948c9cdbe0ff4ca6de85562f42bdecadb8371d12c6828ce2
MD5 0a901727235ec9790d065a1c2996ac1a
BLAKE2b-256 11e994b5ebc1f7dd74726bb4cca89decafc2c2c263049d2d30242c98783651a3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: prefgraph-0.5.7-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 4.3 MB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.6

File hashes

Hashes for prefgraph-0.5.7-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c5a4a4fbe46b179e8ec45a8cbbb3ac3daf58a45fc8659e3dff312c18531506ef
MD5 40c96cd743272320816f3d6f19adac84
BLAKE2b-256 59e9d259daf5d58ae614257b8cdba5d6bcedaa5f320211e6b7ff4529d22fd114

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.7-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 618a41ac068d95992a93c12701198ad79d8c750ead4014dcd593d3a257acc54b
MD5 dae83a7e824488997521e05e838f72b1
BLAKE2b-256 a0abe91194a63efa84b1267fa853a89423e0ad5341735f6250520a4341d6cfb2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.7-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1d6832f9742c6816998e0e91e258c26ee7856c52113b671a86b208e70bb40f50
MD5 ae51e72f109ba8545f253319d13660d5
BLAKE2b-256 4a43011187cbcb32ce51cb96a4eceb0dd79337e0da5ece2901a5d668e8b23aa2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.7-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 509c198cb7963446795e12a4fa97bc223245287baea8361de917af45bf1c7b8f
MD5 14f3a9d6788e32947350231b1bd10ef1
BLAKE2b-256 a6341fabf561abaf74eba36e0309a10d76f9c49af8c77c360001f03b6240cb2e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: prefgraph-0.5.7-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 4.3 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.6

File hashes

Hashes for prefgraph-0.5.7-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 5f97f54dc2f7506faf28be78f9d3c1ee10472f9f1bc4862a052448d5750a0718
MD5 2df6f3f13b90877aaf96ff80a0bc4fa7
BLAKE2b-256 4d9d59ad2ebbe5f415037418168cd48ce8a4d66f863e143a95f3665723f9da02

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.7-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c490179d8c8df82959b066ae25af0b4d112bf865e91acc2b93aff032d00a409b
MD5 f47032e1eb79bb45b494ca612794eb0f
BLAKE2b-256 6444195319d3559f5a0e677abd38a87e1865f967b5e44c0057480c80673bcc4a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.7-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1ca7b41b4cb290032a7edcc11b48cdef6f3c16998f41d8dcdab81b736bcd9b65
MD5 b9e0308a59b089d30c4b2eae8e724e00
BLAKE2b-256 565cb6df66bd174bdf1cae91f23ea8cc1fc2b92e47649de61f896aedcbc1072c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.7-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 7b42274b043c6e176117fa3e24b11ebc5e3c13e70854b57a372b39ab9c00a1fe
MD5 1ecd33ae3433dbe0c9ab94db6dfda153
BLAKE2b-256 6d6b34a278e529e0b366d1025d8ec02b8c95fb87a8c0444bd26d2c8866c1737c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: prefgraph-0.5.7-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 4.3 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.6

File hashes

Hashes for prefgraph-0.5.7-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 44cb213003255267ad184f67c371b3999070057534be27e0cc8c643d2d74db37
MD5 c11b8b7047f7aed223f34853ab45b25b
BLAKE2b-256 fe6bae39c2e4245256b996893140eeb2b7c2ee38284e89c1cce4f4ccf0d2af78

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.7-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7ec4f05acbe3fe178b8d59f7696b9383d0cdea37f1686c3ce2d14fab83fca079
MD5 53c48c05e07ced19c4d4f3c64bda2cdd
BLAKE2b-256 5122b887fd7ae9f7662c2a54f6a426b8686b63baa168b728320086b1b4d40082

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.7-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 242b816fadf1283d33519964c50f2ea679e2d3841a68995e42a08798d64f241f
MD5 e0ec8bbf50aa974e2d99ff0b3e848f6f
BLAKE2b-256 bb063372240efc065317d90333a5d767f57dcea5b8d192a858ca920304f0baad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.7-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 46525759714aabcf935ac2b73c415995d6d483cb6b9db5608e66a1416edf1972
MD5 e610da8cfd60d36f30af7f4c3c428476
BLAKE2b-256 cd1a5b0ff102b940d252738c01d6e8db56dfebb779893ace2a7e597117fe7731

See more details on using hashes here.

File details

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

File metadata

  • Download URL: prefgraph-0.5.7-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 4.3 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.6

File hashes

Hashes for prefgraph-0.5.7-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 cf9027097209a74e6669bc76bc26e8dfe3c6ba5e0d28917fecb53105b07ce87c
MD5 47b4b3b021bc7b26456c67d006bb84a9
BLAKE2b-256 90a3bb0779a9cd42c5c8817f97ef0e1f5f8d1b7b83e4eb0af99962e5c5228c29

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.7-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a333e73add799fa72a9f23694e22bc855269e9ec763237995d33c54740e205e0
MD5 16d540b561b1d0bc2c46e3135ecb4beb
BLAKE2b-256 7d0e05667833a7f04241e470c026d78e489a09ad2e0dd2014c10457c48c7a1a3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.7-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bf257878800f2718602e1148b5baa2aff365b08b69d6883ffe582ecddb656493
MD5 2084271b37dc29239e4253354b690b6b
BLAKE2b-256 209372af049292da37d766b085dc0f53c8c40950e562194976dabadc50643017

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.7-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a21c3dbe615b8e61f7200ceb5a646bbecbd6a31de0bac1e4d9d8e539b1d61d4a
MD5 22b627d5e6e0dd6d4e28857b2cd3334c
BLAKE2b-256 860c482b12ee2823c82d9abf54768c69c116167b107253af29f6c45117991502

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