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.
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file prefgraph-0.5.9.tar.gz.
File metadata
- Download URL: prefgraph-0.5.9.tar.gz
- Upload date:
- Size: 17.8 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
86a5157cfedbadd10a67d182156e319f382cdc797829bc08e4276fe31ba2f0fc
|
|
| MD5 |
5def84738b0067c03815ba9d70ff9128
|
|
| BLAKE2b-256 |
6d16fada40d32339861d29e0f20db657c85cf2462f8d41bd8308b4fdddebaf6a
|
File details
Details for the file prefgraph-0.5.9-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: prefgraph-0.5.9-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08659d413e38cfaccbc2938cba0bbc9084da1a5acc5988cd27ab0b750fe71053
|
|
| MD5 |
55d061ca8f14e4b858649d2ab8c9852c
|
|
| BLAKE2b-256 |
3ec98ede573aab0973c9a24cfbd5abaf6a645697f1861685115b8d77e6f0f426
|
File details
Details for the file prefgraph-0.5.9-cp313-cp313-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: prefgraph-0.5.9-cp313-cp313-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 5.4 MB
- Tags: CPython 3.13, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
96dd752813b556de671deb25f8d0444dbcd364a5f669c78cea5ed8f69c7121b4
|
|
| MD5 |
908fb01063688022192d18d652bf76e7
|
|
| BLAKE2b-256 |
68b9054fffdbb2ec27e05fd4c88c688b33fb995cbbdf09b6212b539edf2c8f2d
|
File details
Details for the file prefgraph-0.5.9-cp313-cp313-macosx_11_0_arm64.whl.
File metadata
- Download URL: prefgraph-0.5.9-cp313-cp313-macosx_11_0_arm64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.13, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d042784139616b603c48a5d90b3cd20333f988aded7a753de30e7688769c1da
|
|
| MD5 |
7e1aa07a0fcd686eaae2097d793849d9
|
|
| BLAKE2b-256 |
11484d63ffd08faa8e38e79bb242aec6285830402d3b16a10b922ee3932c3fde
|
File details
Details for the file prefgraph-0.5.9-cp313-cp313-macosx_10_12_x86_64.whl.
File metadata
- Download URL: prefgraph-0.5.9-cp313-cp313-macosx_10_12_x86_64.whl
- Upload date:
- Size: 4.7 MB
- Tags: CPython 3.13, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba295d1a537de6548f949b2d1ddb3d592dfef66c596945ecf173819afcadbd16
|
|
| MD5 |
df81dc304dec62224482c2c193111b48
|
|
| BLAKE2b-256 |
1e1dd620a1681105c69f36e702f027c64e6c3f49f938a8e9e8d5f729d530e0dd
|
File details
Details for the file prefgraph-0.5.9-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: prefgraph-0.5.9-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e75d13dcedc87426d79299bfd16026b924d3d0d285a47db44176703c18f83607
|
|
| MD5 |
b7d70dd4e7ad9c583bfb4104cbdb6b8f
|
|
| BLAKE2b-256 |
503239bf0cc1d6bc7d0535d27132e5bafb5ba03c69d99ea57d1facdde566f80d
|
File details
Details for the file prefgraph-0.5.9-cp312-cp312-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: prefgraph-0.5.9-cp312-cp312-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 5.4 MB
- Tags: CPython 3.12, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ada4722cd2aacff718b7fa3f3d3bfb65a5a0f6f056e3f5461c53b73a140d8a0
|
|
| MD5 |
32d2b023d370834ecc404a60484ba8a3
|
|
| BLAKE2b-256 |
7c847f9c95e857185cac46d635a774f5644dd59bdf8bd65c4e0271775eacf2ed
|
File details
Details for the file prefgraph-0.5.9-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: prefgraph-0.5.9-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e85214ca97d0e2982a20252e0ecf2236736edb869f88aea017154d2cd279104c
|
|
| MD5 |
b74365c6c7e84b42f084a38f6661a42e
|
|
| BLAKE2b-256 |
5e00ec0a12b826c0269a52e4f98307ee13aee8b5751c0278d305152d89b7b318
|
File details
Details for the file prefgraph-0.5.9-cp312-cp312-macosx_10_12_x86_64.whl.
File metadata
- Download URL: prefgraph-0.5.9-cp312-cp312-macosx_10_12_x86_64.whl
- Upload date:
- Size: 4.7 MB
- Tags: CPython 3.12, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9814ab89074d67fa9a13624af46ce9b8549fa50fefb5a97f42478bc9fa4330f
|
|
| MD5 |
211fe4810d93d2253eec2d66d6c24f81
|
|
| BLAKE2b-256 |
9e11203d3411ff4d88bba63073bea914f74a45a1e9e0af9e2711817dfc153d45
|
File details
Details for the file prefgraph-0.5.9-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: prefgraph-0.5.9-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc820e1a2b9d2d4f1a62900ffc709cffd328793591fd8c4dda2f234575f2a561
|
|
| MD5 |
16582ed9649f007d7f0f6d8dd1804b07
|
|
| BLAKE2b-256 |
53930079f7faf5b029dbf654a1dc0587c7832320fa154d832a0d4c2e88a33c52
|
File details
Details for the file prefgraph-0.5.9-cp311-cp311-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: prefgraph-0.5.9-cp311-cp311-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 5.4 MB
- Tags: CPython 3.11, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
360463a255f5b2584e716d5ce83183e3ceadd2bfc789621d8a99ec65323e5a32
|
|
| MD5 |
309c899d15843503e6e24efec807fe71
|
|
| BLAKE2b-256 |
cfe72398def0d862833045b7dc6a59377d25e20128c4ff3d60d6d813286368d3
|
File details
Details for the file prefgraph-0.5.9-cp311-cp311-macosx_11_0_universal2.whl.
File metadata
- Download URL: prefgraph-0.5.9-cp311-cp311-macosx_11_0_universal2.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.11, macOS 11.0+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e18a556fb7a5a2bedff2c25bb9022cd762788863c53e78752f6721152cade9e
|
|
| MD5 |
64fae2190d507125996f5b31ff0e2d88
|
|
| BLAKE2b-256 |
292b736cfcb98581191fd9376c7f67ce3882915fab17f977405e3a4ec70b358c
|
File details
Details for the file prefgraph-0.5.9-cp311-cp311-macosx_11_0_arm64.whl.
File metadata
- Download URL: prefgraph-0.5.9-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d2814280c255818bbe5ae28df376389e4975cb1650ae4bedf613d4bcf6b84419
|
|
| MD5 |
62827c948f9250cc0f735635a9adcbe2
|
|
| BLAKE2b-256 |
f64634423fb45fe24ca84c8092255e45a569cc04a9a24551a621d9b64ec4460b
|
File details
Details for the file prefgraph-0.5.9-cp311-cp311-macosx_10_12_x86_64.whl.
File metadata
- Download URL: prefgraph-0.5.9-cp311-cp311-macosx_10_12_x86_64.whl
- Upload date:
- Size: 4.7 MB
- Tags: CPython 3.11, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
17b5f61cda279f5ff4d9b28f8e372368a653a1c4f230b570d035a8b8e997d27b
|
|
| MD5 |
acf1c6b109fa8b2a265c39caeae4426a
|
|
| BLAKE2b-256 |
a3fb67287b97fd648f99de5b9b05c3004f34493333b16c42c0a4f7dbca9faa05
|
File details
Details for the file prefgraph-0.5.9-cp310-cp310-win_amd64.whl.
File metadata
- Download URL: prefgraph-0.5.9-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b50f6b7466a7d44177cbf91e75f9dd3f01200216ffd8c45922bc51aa645dc5cc
|
|
| MD5 |
8b35c3a8c2422613121bc8a3161d3f43
|
|
| BLAKE2b-256 |
8fa791f49ad2b101172f6fda789b1ad54ecad08ae4448a1054f639c966fb23af
|
File details
Details for the file prefgraph-0.5.9-cp310-cp310-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: prefgraph-0.5.9-cp310-cp310-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 5.4 MB
- Tags: CPython 3.10, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f0f8af82ea9d0b347d211ee1ae43e1995f18978250237f20bbbe79a8ae96fe2
|
|
| MD5 |
eb2c78e86e05f12e6e93cce7377ccf12
|
|
| BLAKE2b-256 |
8d7a4e42e910db91d41a5f441f585682af2ccee5ac2e038e8da050b665d967a1
|
File details
Details for the file prefgraph-0.5.9-cp310-cp310-macosx_11_0_arm64.whl.
File metadata
- Download URL: prefgraph-0.5.9-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 4.3 MB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c61a0bd3250dcdbafe481e4a2cea07af47029b51a5bc9fb7fee7e94cad85565
|
|
| MD5 |
e8f3c3013a6d9788d45781d457099adf
|
|
| BLAKE2b-256 |
e6330519808c0947e3401f73329d2ffd17716b16d2ec2f6f8cb9600ce653e772
|
File details
Details for the file prefgraph-0.5.9-cp310-cp310-macosx_10_12_x86_64.whl.
File metadata
- Download URL: prefgraph-0.5.9-cp310-cp310-macosx_10_12_x86_64.whl
- Upload date:
- Size: 4.7 MB
- Tags: CPython 3.10, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e14f3eacf28b13fcc23ecb50b3917b937bf86848272ecdcf985eaa144a46511
|
|
| MD5 |
5d4d81242cfee463b5bb57a5e361b3a0
|
|
| BLAKE2b-256 |
aaf0142cf388ef3a55f4ca814c35499959162f8cc46f80b763ec33e302c652e2
|