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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.12+ x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.12+ x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.12+ x86-64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10macOS 11.0+ ARM64

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

File metadata

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

File hashes

Hashes for prefgraph-0.6.0.tar.gz
Algorithm Hash digest
SHA256 15359105c669b17fcefb21c8ecac30e74ea66a393490eab8181f14b7bed7eb2a
MD5 b417ecaf4f2aa1cf925b1416b6d86e28
BLAKE2b-256 723296849e4bf46a97d5e008a6185884bd9120845e34e04caff1b60c9f0ea7d2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c2b3602d62fa737d1227a2917e7691a2e49ea8f498f09ad1e6e67ba32f0fa838
MD5 8b398561db0f2b48cb32ad4cf9adb087
BLAKE2b-256 5c478dcb5c9f1597ebf8358e05762e1fe586c7c43ce24a1926dc5da391cffd97

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.0-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7756697723b3303dc3a04fc0b9eac48089d07d4dc4fa1b20881af0f5f8d44183
MD5 02ebddb2d64485f22d1820f810099d96
BLAKE2b-256 cc50cb68eb191fc8c88ce8e038ac684c6a2cbd84e152bca355f7ae88b9996234

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 66a0a23e09004d9aa46219400dc6618451df48ecc6e0560e9f592302372d9edd
MD5 9efcd064b696dc88139dd457962567ba
BLAKE2b-256 07b5b82c443c5268a4349a0e173d3141232df6e311bde54c72d10aaa101bfe99

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 cee08a25927df002abb1d30dbd61224d20f7b44c36fb377f9097f32baccf0d13
MD5 0aeb242a7060d430276fb44bdcc99fb7
BLAKE2b-256 2bf2ae5b64f5d35bd9b2e940dd5692d1b970f98e7826549cfc2fd9faa57009ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ba4cdf28b2d57a40337ab3233b44be936dea26af6f14b2ca289a6c06f2424fd1
MD5 efcd2080ce96729359fcaa1e7a57e412
BLAKE2b-256 d36ee28176f1d29285f3bbacb1d852998840033b34e4bb45de6d21c6bab9d5bc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.0-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6f3376711e79ee6491281e975703c494acdca62a30d4e9759e58e495b8dba744
MD5 05bd155754d230293fca2595adcdac79
BLAKE2b-256 bcbfd19c4df71e8511c5de2626945edf7c043778c64d622ce60308055ecf58a1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4bb9c3cfedfb43508631cfa6c05261aace0409a1c3c4725b08361c079f3d2739
MD5 14a97bd0f6da3284790de1d6c9acfb91
BLAKE2b-256 5d140c117b4d4f871000df21ede2f6ed3ebb6ef793ac65dc228273fb19e8d78d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.0-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9aa89f321f16edb4be29af51d0d6554c3a57dd3e1514c3c4aa5e0a15ea9230f5
MD5 e4511ec238637d7430e9e492e9a09ec0
BLAKE2b-256 03c0cc1cc3ca9298a3ccd3adb8b9080c73680725f7d7086e6982ccd51d59da10

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 4b0750ed6c38118a88a0b0435077892521d81171aea325121fae3bd63512beda
MD5 70b5b17b835f9573dd0c4d80578acd4c
BLAKE2b-256 2dc6b6267dc2ea333142cb48a6dce658724575e12dedef1fdc736f9c8c674a1a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.0-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 192d9c34387bfd25bbd6732b8fc3bae5c68f2201a2e1daeacc1e166cfaafacbd
MD5 761ee1a6b4f643bf5aac78b0ff129246
BLAKE2b-256 947180e5d79cac23026648e114e99e14ef7b13298b0c9c637809d109d176e7b7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0f1753f0d5f83faee0658fac2f0d7de9d45e50fed092f567d6873cba22971810
MD5 b0656f352085c954953ed56e169c978e
BLAKE2b-256 b003da93ed3ad21d3952642153276eeb4f8084ed354310cd6d92a17843557e13

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.0-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 8b40aebefc4fa3dff46d7d1e89587d1ab9f9e36b11313ff6fc3212d4fe2b715d
MD5 6a9367a3ab01d8666fc9813b9348cef5
BLAKE2b-256 7db2f8befd47018942b6926bc724579894097295a4e73c6c7ab57805cba1a148

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 b718fd03c74e6498d5b633979614cec3164be729184c95e2438f52bef1de6cbf
MD5 ba9f5fca8857cc735c8db72a2e310aee
BLAKE2b-256 8552337178a12f59195f9c40a76486bb067348a7e56cf2d4c358bfd6da0a9ed0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.0-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9cf92bd7e07ced704f8449ae273b5f2c1e90ebfc34018b6e34891512ffe09ffe
MD5 1df26c52f4f08d0ad816a83fd249ca31
BLAKE2b-256 d99512d235ad535b908e9f769597a740011fa732794b69e19f72a6e72ac69cb0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bc858f3d0f36e9cc356948eea6033f0f8bebeacc727449011ad7f068dea1b2af
MD5 526940e8b487a28cd073d15fbac02ce9
BLAKE2b-256 3f044a6d49b0f716aea6934b73a665199bf620bddf10c691d958d2fe577e042a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.6.0-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d28dcbd116a0691b47f25887b6863d4413f6a10a1e88d6104c821731aa1e065e
MD5 daf9a69c0fe638e6bd247119379e06c1
BLAKE2b-256 4970db53b824a0dd48f484b501af37f7b0c1c5ebb4e43166d8e4c294f02db81c

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