Skip to main content

Revealed preference analysis - rationality scores, efficiency indices, and utility recovery. Rust engine, Python interface.

Project description

PrefGraph

Rationality scores for every user. Rust engine, Python interface.

PyPI Documentation License: MIT

pip install prefgraph

Quick Example

Score how consistently each user's choices align with rational utility maximization:

from prefgraph.datasets import load_demo
from prefgraph.engine import Engine

# 100 synthetic shoppers (prices x quantities), no download needed
users = load_demo()

# Engine scores every user in parallel via Rust/Rayon
engine = Engine(metrics=["garp", "ccei", "mpi", "harp", "hm"])
results = engine.analyze_arrays(users)

for r in results[:3]:
    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 you can plug into downstream models. In our benchmarks across 11 e-commerce datasets, revealed preference features rank among the top ten by model importance but add near-zero marginal lift over well-constructed baselines. See the case studies for details.

Your Data

Have a DataFrame? One line:

import prefgraph as rp

# Transaction logs (one row per user x time x item)
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, goods 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 (which items were shown, which was picked)
results = rp.analyze(df, user_col="user_id",
                     menu_col="shown_items", choice_col="clicked")

Returns a DataFrame with one row per user. Customize with metrics=["garp", "ccei", "mpi", "hm"].

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 2,000-49,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

# 3 shopping trips, 2 goods
session = BehaviorLog(cost_vectors=prices, action_vectors=quantities)

# GARP: does a consistent utility function exist?
garp = validate_consistency(session)       # GARPResult with violation cycles, matrices

# CCEI: how much must budgets shrink to remove contradictions?
ccei = compute_integrity_score(session)    # AEIResult with binary search details

Budget and Menu Choices

PrefGraph supports two primary choice domains:

Category Input format Example domain Key tests
Budget (prices T x K, quantities T x K) E-commerce, grocery, food delivery GARP, CCEI, MPI, HM, HARP, VEI
Discrete (menus, choices) or (menus, frequencies) Surveys, A/B tests, recommendations, LLM eval SARP, WARP, HM, RUM LP, IIA

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.

Configuration Throughput 10K users 100K users
GARP only ~49,000/s 0.1s 2.0s
GARP + CCEI ~2,400/s 4.2s 39.5s
Comprehensive (GARP, CCEI, MPI, HARP) ~2,000/s 6.8s 67.1s
Menu (SARP + WARP + HM) ~1,900/s 0.3s 5.2s

Memory stays flat via streaming chunks.

Documentation

Full docs at prefgraph.readthedocs.io:

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.6.1.tar.gz (418.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.6.1-cp313-cp313-win_amd64.whl (4.4 MB view details)

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

prefgraph-0.6.1-cp313-cp313-macosx_11_0_arm64.whl (4.5 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

prefgraph-0.6.1-cp313-cp313-macosx_10_12_x86_64.whl (4.9 MB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

prefgraph-0.6.1-cp312-cp312-win_amd64.whl (4.4 MB view details)

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

prefgraph-0.6.1-cp312-cp312-macosx_11_0_arm64.whl (4.5 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

prefgraph-0.6.1-cp312-cp312-macosx_10_12_x86_64.whl (4.9 MB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

prefgraph-0.6.1-cp311-cp311-macosx_11_0_arm64.whl (4.5 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

prefgraph-0.6.1-cp311-cp311-macosx_10_12_x86_64.whl (4.9 MB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

prefgraph-0.6.1-cp310-cp310-macosx_11_0_arm64.whl (4.5 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

prefgraph-0.6.1-cp310-cp310-macosx_10_12_x86_64.whl (4.9 MB view details)

Uploaded CPython 3.10macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for prefgraph-0.6.1.tar.gz
Algorithm Hash digest
SHA256 30c3e13c7fb3b8f6b771b9d0c5fe04248fdb9a298614f2f81a21f38ded27ebf7
MD5 be71e3131889dc7499285b54ffcc29b9
BLAKE2b-256 6530750d3d85fb095488bc546de0cd064ad087450bf9833483d7a201b0f6b461

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 8c11de8cd8fb900cdaa4ff1a690352b759a4a7daa7e73fa98f63a7666e898e8c
MD5 359edd6f50627104d79491d3b239c57c
BLAKE2b-256 d077f53932e58744372a60e9c92fb008cbb90af684722874bd73a19ff586b56b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.1-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d260ceefc6870489cdb8d5f6d9aaf2308a318d56c029feb28c0f84a6c4aa3fa5
MD5 7e136028008bd6a05a6578112c4c85f4
BLAKE2b-256 0632cce27cb104cc28db5f3f01570de9ad221e2d0a04bf7d4ddeff6fd04e78c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f4643a41bb56891c2c10dd2a058081bc244868ff10d14d336d50df0645a5de5d
MD5 38a22f721518310edf55d92517e4b7ed
BLAKE2b-256 d5a2982776540eecc617a7cfcc8f9588a3e7fb87f300ed4678d31fe61f3267b3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.1-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 256eb0a715987dce288b8c626be3c484e26a0e3b9c3c698f087132ae6f71f0c4
MD5 0650de3c7ac252041d1f49398d439aaa
BLAKE2b-256 b7fccbcb13a894259fa0c37604f9a44400aae35fabead3fe7de73d73ac8324e9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d6b15fa40d3423491f054de3a9ccb7d7ce6fa8570e57f0174d4582390f58c3fb
MD5 2067495f8ba81b8fc0e9c8cc6ad9c2ea
BLAKE2b-256 fbfda3a8e9ee6786d87ebf8933132a7dbde1b566193d9c41494c606f7db3a356

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.1-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ef6b54ebe49c118639d02f0915a9ffe0ebb42f2ce5243c46e84f397c88a5899f
MD5 8612b29c9c50387a132ec68f492d7b33
BLAKE2b-256 eb930bc0980779fde97fbbee8b059a7188125fcb2b82c78f76ffe37d4f81b142

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b8934dac3ad2edf08e21fbb913ec242499f811a8e78b6bb7cb583ec28a803fdb
MD5 216f8737f0752f2d921245ec420cf177
BLAKE2b-256 1ea1dca8761ed26d0733cbb6408d4043365ac995b0c9eff946e0fea4bb50245c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.1-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 82bd4e16ac1ef033c0d7999bd3fd7081e6c4e6f4665106b7df25a8690f512949
MD5 ce39a7c00e6ecdd6c068ab7462bfa22f
BLAKE2b-256 e6456e9a1aa7a62f4087f6d8b58c8caa688cb2ac09a30743e5db0ce60ae3073f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 8d8aad7ffe905717123992493c36ffb83c28db7b30bb5f0895fc03541f4ac1e1
MD5 40b29a4c44cc68bfa797405b7be30489
BLAKE2b-256 655a03074beb55fe775dfadbf57582928cc2cc9bfea38c7b41913d1b6f428c9c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.1-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e0b09065fd20d1d1457439123fdbcf052137fd312e4358936cf9927aa0ef0173
MD5 d84aecc70bd66cf9ad66c82953f05a69
BLAKE2b-256 a82c2a7d22adc6997698ab58f094e29efc834c971e513aaa6ba6499f07bfac86

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 403ba12d1f4d5c49463e89378fa4a1a3f47ae04d9914e97f13b9f5442653a559
MD5 7cd336afca699916b0286bdad21fa08a
BLAKE2b-256 3ea631a6af137c40c6fa676004f46b5db5cc68fed02caa1c4b93276c97d47d6c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.1-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 83d81175cbf1ae3a4b1e57310effb93e5e58419394b56fed28a3e60fb9b462b7
MD5 2f890ddefbcd452950c7a30f541ec924
BLAKE2b-256 4403c2c240bf297b22e1be6fff0443a583bb8280de7b39ad234176cb044e710e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a0e2c00c1a94423957bb23322cc520843082bdfc55efb8f8775f7c04e5a69005
MD5 7c02c0e226eaf3f1eda796cc2a5b667f
BLAKE2b-256 ce6e1c36ffce15375f861afd79adbed9e49bb6791b62cf19ea792c4feca4c85c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.1-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 aae13c433fb45a51e92dcff67ed9a9c8ea772f62cc45a3607370ee1b63848724
MD5 e6f5879323345ab325763ced3f450d13
BLAKE2b-256 9f3ebda487a8cd16735bde94d19c59d94dc05c59f708afbe2ac59807ecbb0295

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fcb030e145070540f1eae730682e8090fbe55441f9d7311fb7b99d5b71679557
MD5 a8c1034b81dcc98a4bc9a44d378d394f
BLAKE2b-256 1611145c658d906d40fe5a632941deb595b2e485fda3f3f86c1a8d57d4b63a08

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.1-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 5ffd26acb6875efe87326529a00696681c5e44f1f69e116c7da4107d85eac2da
MD5 895484991932db7a3cca3f666925c874
BLAKE2b-256 ce8ce479febc59f138e1ce105ac93d852668d067ef1e5a4479f9d8e47ca7d9a1

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