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

Uploaded CPython 3.13Windows x86-64

prefgraph-0.5.16-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.16-cp313-cp313-macosx_11_0_arm64.whl (4.4 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.12+ x86-64

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

Uploaded CPython 3.12Windows x86-64

prefgraph-0.5.16-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.16-cp312-cp312-macosx_11_0_arm64.whl (4.4 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.12+ x86-64

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

Uploaded CPython 3.11Windows x86-64

prefgraph-0.5.16-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.16-cp311-cp311-macosx_11_0_arm64.whl (4.4 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.12+ x86-64

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

Uploaded CPython 3.10Windows x86-64

prefgraph-0.5.16-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.16-cp310-cp310-macosx_11_0_arm64.whl (4.4 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

prefgraph-0.5.16-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.16.tar.gz.

File metadata

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

File hashes

Hashes for prefgraph-0.5.16.tar.gz
Algorithm Hash digest
SHA256 4726f9c7a04362cfcf21311f0d067a6cff0790d4af939cb43588970afba945a1
MD5 db95202229f338f30706e8b0488de6ae
BLAKE2b-256 fb3f1490bff35cf94d9471cf1103ac5e3f4c8ebb7926f6b1944412726c00f319

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.16-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 339ac44d758eba60341370011317185ced6f4b440667142dab421bc1751cac3f
MD5 5d1b60d2672c0a94fba6eb2ef0e6ba25
BLAKE2b-256 bd3866dc04f4dc758630601c3b8579ff54cf4a47d1b16898ac9f2c1a58752efe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.16-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b0383ced896b33dde87f277ce3c9aafb2698ab774e26921116e02df84d2d4367
MD5 544ede438cae87ca6e48efb208c758ce
BLAKE2b-256 006502046af75b9392f283015ffc44c44b79c837dd79b88af479acf5c6122d46

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.16-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2537f5b311383bcb7f3bfad3a22421a231e080a12ca21697f19a43e138dadea7
MD5 7029f503c4e7a223dab4be894a22a448
BLAKE2b-256 1a85954a85c759cff3750b7ce2657c6a500a10cabc9adc7d1ba2e022e10d18fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.16-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3590204bc5f1aab9072cbe4dee6a00368b3222f8e5928ee66deffb5eaa731d3f
MD5 55140141422fea0eecbaf0018f586adc
BLAKE2b-256 4ca731807f982fecd1c570e9d56e56fe6c70222d6eb20ba90ab3df5d400f3f1c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.16-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 7ce85f56cc85422cdfd5c678a7348d96192c291584bd2e3c76dafaff27d1fd9f
MD5 2c44adbe0c6b5ae8aa003d40febd6f66
BLAKE2b-256 f8886164c5ed158bac0f773a452d7799994287f0aeac71658291a8377a640fbf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.16-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0e252204b263c53da2ca1f2edf749836d8543b6d8b72b63f3c7b363aed45f1f0
MD5 5ccba611c1f93d79d0b21e4f69964ae6
BLAKE2b-256 e830940b82a784934fce586c9d93acbf0a3da9211b6a6f221ec839554536cdaa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.16-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 749ac9ae565f79159cd8eb7d3f07249f592c1bffa5446c694d3c48951370be34
MD5 fd84176458b62d1bc0349fc32933a1cd
BLAKE2b-256 94f4d0359797a55f34cd61e92abf06a979612aebe6ecfb31c582fd88d95ee4fe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.16-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 cd4c4b116339410e7bbfeec6f404b027ee0a787ea03ce08c980e2775a165f5df
MD5 3027cd79a8237f81f1be9e35b81d93fe
BLAKE2b-256 30a0298c2e5df6077f3872cbd9b456f5a3bbdd48cb0ff48a6cfd88193ee9ff42

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.16-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 7e894bffdb3b997ace84620bbcc39b9dd0043309de0a03aba5a7d414b5db4844
MD5 07568e90aeed4e5641f19d6e2351df36
BLAKE2b-256 649726fd2676c1ad805733ef3d1edd68ff5fef130e3592c3518beb329ab4c649

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.16-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8029780b19f002beb5f0c899583fec385448747065ea7571117ac09f1e9d5087
MD5 3cad13140b37f449183da2a5f23752b3
BLAKE2b-256 044e9feb8a4b45e777a4a1ac323ed2e77aea9d326713b1f44abbb51a2d9a744e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.16-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 49e1fe83d902566a8c8cf971a9591b9672c05cc2bba1ccc53ca811ba96c24672
MD5 c7dfdab9c3e710339775437306c9c8d6
BLAKE2b-256 99305edc458d2745491c09d2d06fce9f74c28e2e30e720dc74c824f34393a2a3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.16-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3f4198979fce91307b506974eafe67096a11d4364d30b2bb832fbf5d6c466200
MD5 8bdb61cc9e77250e3e7ed73c85879da7
BLAKE2b-256 747c6de8f7448a53acf796559821cf33625f2e125fdf1ced1fdba999e41ac4de

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.16-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 d16f1c531e3f005e8fd0748ec34f742715580dc2d0329a0b35af18e0be5a5a38
MD5 cd30e6e8c44952eb32574ccd6a43e7e3
BLAKE2b-256 39f88a573ae6735014e02537a06997527c1778309f0f811feab45fcbfa206159

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.16-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 624a12be7539ecc9c09b05bf9b8b0881a5e8d813df37be71809eca66c94e5008
MD5 c35b11b1ce309f8828f1efcbd09d328f
BLAKE2b-256 cc019319bdb56f3c2c9f7cd663381d89cf577e23d2dfd2ff497fa81141c2bce9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.16-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bcec4560ce8878a4579cc534aa81c096e4a01d2813fc77af44b6df8a0f0119f5
MD5 6b85a4ef444c9ebf4437e205971614e0
BLAKE2b-256 b262d3493ef280c5cc462d94fa8feff0bbf20bf9f4abb1debb5a7bbb1afec83e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prefgraph-0.5.16-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 998e5ea921284e6e8ed3b2f06fa02904f735cd03ea1cf44a62568dd4b456e4d3
MD5 bd6eb8b744ef7de88a6246e3b4df79cf
BLAKE2b-256 f5015c8f2f1e576a3fde6a3126b6dc1085f360e2859de065d698f067967f611c

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