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

Uploaded CPython 3.13Windows x86-64

prefgraph-0.6.2-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.2-cp313-cp313-macosx_11_0_arm64.whl (4.5 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.12+ x86-64

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

Uploaded CPython 3.12Windows x86-64

prefgraph-0.6.2-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.2-cp312-cp312-macosx_11_0_arm64.whl (4.5 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.12+ x86-64

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

Uploaded CPython 3.11Windows x86-64

prefgraph-0.6.2-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.2-cp311-cp311-macosx_11_0_arm64.whl (4.5 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.12+ x86-64

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

Uploaded CPython 3.10Windows x86-64

prefgraph-0.6.2-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.2-cp310-cp310-macosx_11_0_arm64.whl (4.5 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

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

File metadata

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

File hashes

Hashes for prefgraph-0.6.2.tar.gz
Algorithm Hash digest
SHA256 6f34d3d027d8b7a8df102c3d9af081c7b993d02d08f9fd630fc897d4264836e3
MD5 4776df54823d51caadd349c8345bf2f9
BLAKE2b-256 7ee2c8b141f859abc70f0bcb473d496bf44e4971674a6a859be2b54a50b5fe22

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 f78cba1e52d19852f4f423d976cf240bdda99cf9d93703824912547619995dc0
MD5 e98739cae889bb0bb10b725c1c43efd4
BLAKE2b-256 3e6f50cbc7c4d7c8a901af2b2c6cdd3f2104d399e410b853de9b4d3ab1cf4679

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.2-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 cf5fd924b7f92fe3cde5f023acadc1a032f82ad8769a4086645310e9693279c7
MD5 183ad6b5c1c2f3ef485bb3ddcf559c9a
BLAKE2b-256 5ecbfa9b4fc669ec2fb50aaab7a21cdb8dea9c6da3b353c185c43944f4a63f60

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e0490a7f2293933d9a11421036409595420e750fddf2f3af68167a8e8c653827
MD5 b268055fee13015c49f97a28c7c4d77f
BLAKE2b-256 e8c44963970196b2aa80ef1e14de935b420eb929cf84c982e11565df57db7867

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.2-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 2d8ef1e17d56b8ed51960370e9f92b3586673aed919a1ccbbaee6d8dd5c8f827
MD5 2461d1b6595cd53b7e2b6d2cd65ed55e
BLAKE2b-256 b09c03eeb20cb75876c0c86af5da4f7ba374bacf5f575994c626e283a9f6003d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 efab43cf944bbc03e448262c76ea09f42df58a96916aab23486c7de13a89b7a8
MD5 bb2ca613908672060a2a72a7437410b1
BLAKE2b-256 97e7e959479107f15a83834d66da2488a0501a8242b0cd5e5a292160cfe0704d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.2-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2f8748cd24b066cc662c7a4b801739d9154480c0a6ca74d1daed6cb5a5346d57
MD5 e88bee08e5300bd97e7d5b191acd7742
BLAKE2b-256 7fca14584090b5d7570c87038626a203432f55574e8077ecca4627ac0b301822

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 81c1aa163ff3cd4c349885f1ccddeb917c01f76cd75c29b29928f6668b3fb235
MD5 3dc05c3bf0b6f877d711cf4bef61e58c
BLAKE2b-256 2b4a4461d1cfef5f5f8ecc8f6c5ae87003a36f2df6495cf1fb26aef83b0e96a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.2-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 6357c54b5597cba1f04e89090ec37e3393dba25f6cb199cb9ff519ec1259aec2
MD5 17783e1afd3135eb1d49742d368bdfc0
BLAKE2b-256 a24a13b10869209a2b430884ddb8208a5a2c154954a64130547d7120fb89f5c9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 dedfb3e2da80e2a2e625fdf3147d07db945e97646183629b3f616b4ee9992194
MD5 0b56c69da0de42700762849c31ff492c
BLAKE2b-256 3065879b860effb478891b76ed0fe7f7fb02b7c5447b5bcbbfc9a19544593d03

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.2-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5724c65abe44f78bb9beb3b860dafc211867a77ce6b360cd16c0edd37958243c
MD5 809b08ad64e6c2b6662211bf1e15c0af
BLAKE2b-256 ee370d99d4423d42ae1063663632d8223981d2bad8252f4ca91d01e0e4bf16b1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b6322426fc70ba6e2ed95bdf3ce2a70fc0be9ba95108c27ddf705850a217dec1
MD5 2e2fe709bac32bf24aafd14c857fed8e
BLAKE2b-256 d7ced97e09cee9063efc33e84b794edca6e09611ad37697d750e19fb4c480538

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.2-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 5f1284b750cafc8f9ce8173e48579a602147656e8f53fb561cafb9e1e5821ed8
MD5 d77174ffa30afe4199a61ef9fc8056ab
BLAKE2b-256 8164c0176956495718c17456c5d7d33be14116f2ff8a24aa42aaf4ae2c7df8a9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a77285c3c904fa72467426b1cdb0c67413cb25d1f8f84c4a2e8657f618ad921e
MD5 5970580ac31b7b95794ab0d12f22f740
BLAKE2b-256 387099b92c5cbf33fb8db86cddde45902fabb270ccc502989094650a0b002613

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.2-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f037c6d3ac3f8e049c8c53a7d01fe4277b420c55ce81962d794ba329451ed101
MD5 d2c458e2c4158c5863d5954db871a419
BLAKE2b-256 653cf030813c026f45150f1a493d761d0211be3472cc0998646b51aa48d78a7b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9dd33c02c8afc2c9fb1f987ccc439f3f134bf7f896365d8148491f31ccd0ab05
MD5 4a287741204740ef06c4696f6995cbb2
BLAKE2b-256 06287f5978f2b264ab6209526abc7edc2b1aa25c3856978679fb0a81c552e88c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.2-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e2f2b4ec4aa5e8292d3ac450e431ba65a7d51115d4174bd66da37f36227e4052
MD5 6289cd63cccec82eb0695a75a3e8b6c2
BLAKE2b-256 6eb5329276083446111b75ea1773a676c14dcac9d1ad1fcf1ee1719f8cc81cce

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