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

Uploaded CPython 3.13Windows x86-64

prefgraph-0.5.15-cp313-cp313-manylinux_2_28_x86_64.whl (5.5 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

prefgraph-0.5.15-cp313-cp313-macosx_11_0_arm64.whl (4.4 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

prefgraph-0.5.15-cp313-cp313-macosx_10_12_x86_64.whl (4.8 MB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

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

Uploaded CPython 3.12Windows x86-64

prefgraph-0.5.15-cp312-cp312-manylinux_2_28_x86_64.whl (5.5 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

prefgraph-0.5.15-cp312-cp312-macosx_11_0_arm64.whl (4.4 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

prefgraph-0.5.15-cp312-cp312-macosx_10_12_x86_64.whl (4.8 MB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

prefgraph-0.5.15-cp311-cp311-win_amd64.whl (4.4 MB view details)

Uploaded CPython 3.11Windows x86-64

prefgraph-0.5.15-cp311-cp311-manylinux_2_28_x86_64.whl (5.5 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

prefgraph-0.5.15-cp311-cp311-macosx_11_0_arm64.whl (4.4 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

prefgraph-0.5.15-cp311-cp311-macosx_10_12_x86_64.whl (4.8 MB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

prefgraph-0.5.15-cp310-cp310-win_amd64.whl (4.4 MB view details)

Uploaded CPython 3.10Windows x86-64

prefgraph-0.5.15-cp310-cp310-manylinux_2_28_x86_64.whl (5.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

prefgraph-0.5.15-cp310-cp310-macosx_11_0_arm64.whl (4.4 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

prefgraph-0.5.15-cp310-cp310-macosx_10_12_x86_64.whl (4.8 MB view details)

Uploaded CPython 3.10macOS 10.12+ x86-64

File details

Details for the file prefgraph-0.5.15.tar.gz.

File metadata

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

File hashes

Hashes for prefgraph-0.5.15.tar.gz
Algorithm Hash digest
SHA256 f3dcd2b03dfbaf437d525faa6ff975e7cb8803baf56e0da608fe525a1ae7833b
MD5 6966e2d291cdbbca45105aab9eb17d0b
BLAKE2b-256 677f73c30c5cd3e8ee23b5a44c7e9736f21e3b85c4bfab40e94e21cd573ce8b7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.15-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 f2bc51166771787a978fdaeaebe38e4fde0ac957652e80081fdaa61ea4094304
MD5 620c4bf8835ab477789e9a36bcdb9664
BLAKE2b-256 da4ab7abe2e25502468faa1e497c9deb5ce7f9d9ee3584d6d24b30394b9f123f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.15-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4a19977a5f4584d802db7da07bb9a08d681dd5d1cb560b2fd4d8459839a39b2d
MD5 e1e5d0b3c15d49e22a5887dafa1192da
BLAKE2b-256 be7c54cbbeed5d3f1406427f7d502fd1239e3179d04fe2812f9394e356026a77

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.15-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8e975a05a8ddb23365d65db182b6ce63654906dc3e865c687be33456272b190a
MD5 b98d42391fc56b45ed99a7f20cb46012
BLAKE2b-256 ad176e41a03bfc0ae6627581a8e0e5e500449910b2356fe8799c298b383dac0d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.15-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 81533886f3981cf2cc8c00d6f3939e11609aca4572c40c5aec22c4eaf950f957
MD5 9ee1996f1c3b5b4c34c54460fc91e1f5
BLAKE2b-256 51eea41288cfe59fa973a4fe036ed46f67216a5a94fc0259662613a164958424

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.15-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 3b69ba1c1fcc329d3d73b91e2327b04532a9ab1471806bf617426509e3239ab3
MD5 a6ddb54698aa0e8b1d115347238b0a34
BLAKE2b-256 d55567a61718474b6fd887bf7630ba1ff476f13e3b9cd3d1e93a16dd9722184c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.15-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1da442879996b6a46e8d376860b10dd81d62cc05165a08a24eb7eaf2b083b1e2
MD5 cce283eea69b83d9adeedf51f707381a
BLAKE2b-256 7bbfa714d45c8900c9c3f6827ba71f2aed9c5cf2e1d062ab910bde0594c28a1a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.15-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4eebec41a74f8f8bfb647ad8b7a1f41901985144e0018202c456f2a3b8f8139d
MD5 25df05824a892ce681d94f12da074702
BLAKE2b-256 0225e1e3d961719ef8405447810a6aaaedca3480c933443bde1268ceb36e8b15

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.15-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d7ff7666ac11bcaffd94f2197bdac06fe4d9b49b5e7de381567dec221d81321d
MD5 b2cb55ce996d9cafb70d6b91772ce6ff
BLAKE2b-256 63c708c82a7749ed8fd6972d90f3d92b013ae93f2231d84983ae04a2ec9077bb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.15-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 09c50d6aca8e5639c49e8c33be8fa630fd9f98334ca70b037795515d4ab38669
MD5 2f7f6fabdfa817d749a0ca7ab01abc73
BLAKE2b-256 283b8846aab5d8143894061ec212b8c1a45d8629ad27e1e5cc5bcf63607b63ad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.15-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 23a38c4f15753050acaa9bba210365c3534466a650a73d3bcf000fbbda415b70
MD5 fddd13142c96f3cc1ec51dd15925d315
BLAKE2b-256 7253f77b13a0b620059a6b1e70e47803eaf5c3080fd755964f8df941c6e6a8c9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.15-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d39f38fc853f335126ff9affd501c087318a2b8d37457a9936ec25023be3cac3
MD5 9fbd376b3dac0873b9fb6cb136df3895
BLAKE2b-256 4a0909a53199b47319ec0265a8873ab9e6eba380f6abcf94b30a27774575311f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.15-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ba8bc7527be7f776eccf0cddcab262db414c1a9d6be3c1dff0ecd2f1469399b9
MD5 fb1379172306429026cfd47b5e2ce6c7
BLAKE2b-256 74852896f893c46a54ca74f6be4f12c968aa44413e0e7ba84392de3665bf35f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.15-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 f11352c2d11a76ffa4efe60e2b77907b06230dd6d49d4fcfdcb1f594dd72024b
MD5 e965dca5986ddb8059678e12c95a6017
BLAKE2b-256 d9312fa35af89f8553b3977a362a5a80606bd662cf6526bbabea67315bb23231

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.15-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4957e4eef62425f1a1e2229b3037b2489ab2dd0c04d7e2754cc77d486f264cd6
MD5 643cbc4bf1f0f92a9b85cd2d21096f12
BLAKE2b-256 804cd24b94e801394ce6bd23a9080f38957245557a9ac40f177c494105541685

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.15-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ae430594bfdb70ce36936c2d17b173fe4de1fce5cc2ce12f00523530d712b1d5
MD5 4ab20c1d8c9a91afd99e202d0078c00a
BLAKE2b-256 8d80970d97e96b1bb63b62d31847317afe9a1fd029a2e5a344452d295ee5931c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.15-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 5f59ada27210eebd7b9ad9d9a6cff385e4eef188746edc4ddbb3e8019468a58a
MD5 d7886325c51a04ce4cf9a4c846dd062b
BLAKE2b-256 d622b63e91ce188905130cc57e55b8ac67549bf9ac9d182d0b2a6505bf96adf5

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