Preprocessing
Generators for the per-user assets (SEC split, initial known features, baseline
queries). See coshop/data/preprocessing/README.md for the test and extended
splits, how to run the generators, and how the data was built.
Generators for the per-user dataset assets.
Each benchmark user needs three assets on top of their transaction files:
- :mod:
~coshop.data.preprocessing.compute_sec_split— the per-user search/experience/credence feature split. Offline and deterministic. - :mod:
~coshop.data.preprocessing.initial_known_features— the S1 feature set the simulated user knows at the start. Needs a gpt-oss-120b vLLM server and the vector-search server. - :mod:
~coshop.data.preprocessing.generate_baseline_queries— the z0 baseline search query. Needs a gpt-oss-120b vLLM server.
Run them in that order. By default they fill in the extended split (users
TEST_SET_SIZE and up) and write to the *_extended asset files; the
test-set asset files are never modified. See README.md in this directory.
Feature Classification by Preference Novelty
Overview
For a given user i and target item x*, we classify each feature f into one of three categories reflecting how "known" the user's preference for x*'s value was prior to the purchase:
- Search: the user has a well-established prior preference for this value.
They likely knew they wanted it and could have found it by
filtering or searching directly.
- Experience: the user has some exposure to this value but no strong
directional preference. Familiarity is uncertain.
- Credence: the user has never encountered this value in their purchase
history, or the feature is structurally unobserved. The
preference could only have been formed by experiencing x*
itself, or requires domain knowledge to evaluate.
This taxonomy loosely follows the search / experience / credence goods distinction in information economics (Nelson 1970, Darby & Karni 1973): search attributes are verifiable before purchase, experience attributes are learned through consumption, and credence attributes may never be fully verifiable. Here we adapt the terminology to describe not product attributes per se, but the user's epistemic relationship to each attribute value at the time of purchase.
Scoring
Features are scored using a base-rate-adjusted surprise metric. For feature f with target value v = x*_f, the score is the pointwise mutual information (PMI) between the user's empirical purchase distribution and the catalog marginal:
s(f, i, x*) = log2[ p_catalog(f=v) / p_user(f=v) ]
= -log2 p_user(f=v) + log2 p_catalog(f=v)
This measures how much more surprised we should be to see value v under the user's history than under the catalog prior. The catalog correction is critical: without it, a user who frequently buys a rare material (e.g. viscose) would appear to have an "unknown" preference for it, simply because it is rare in the world. The PMI removes this artifact by anchoring surprise to the base rate.
Interpretation of thresholds
The score has two thresholds that partition features into the three categories:
s < EXPERIENCE_THRESHOLD (= -1):
The user buys this value at more than twice the catalog rate, which is
strong evidence of a settled prior preference. Classified as Search.
EXPERIENCE_THRESHOLD <= s < CREDENCE_THRESHOLD (= 1):
The user selects this value within a factor of two of the catalog
rate. No strong preference signal in either direction. Classified as
Experience.
s >= CREDENCE_THRESHOLD (= 1), or value never seen:
The user buys this value at most half as often as the catalog offers
it, or has never purchased it at all. In the never-seen case, the
score is undefined (log 0) and we assign a large sentinel value to
place it at the top of the credence ranking. Classified as Credence.
Edge cases
Three edge cases are handled before scoring:
1. x*_f is missing or OPEN_TO_ANYTHING: the target item does not have a
meaningful value for this feature (structural N/A). Excluded entirely.
2. v never appears in the user's history: p_user(f=v) = 0, so the score
is undefined. Rather than smooth, we assign SENTINEL_VALUE and classify
directly as Credence. This is conceptually clean: a value the user has
never encountered is maximally novel, regardless of its catalog rate.
3. Feature is almost always missing in the catalog
(p_catalog(OPEN_TO_ANYTHING) >= MISSINGNESS_THRESHOLD = 0.95): the
feature is structurally unobserved for nearly all items, making the
catalog marginal unreliable as a base rate. Classified as Credence.
Manual overrides
Some features are overridden regardless of their score:
ALWAYS_SEARCH_FEATURES: features like product_type that are definitionally
known before purchase -- the user selected this category explicitly.
ALWAYS_CREDENCE_FEATURES: features that require external knowledge or
institutional context to evaluate (e.g. publisher, production company,
Bechdel test result), regardless of purchase history.
Usage
python -m coshop.data.preprocessing.compute_sec_split --dataset all
python -m coshop.data.preprocessing.compute_sec_split --dataset hm --start 100 --end 200
classify_features(scores, dataset_name)
Partition scored features into search / experience / credence.
rank_features_for_user(xstar_simulator_view, historical_ids, simulator_catalog, catalog_marginals=None)
Score each feature of the target item; higher = more novel to the user.
catalog_marginals caches the per-column catalog value distribution
across users (it does not depend on the user).
Initial known features (S1) by incremental search recall.
For each user, start from the always-included search feature (product type for H&M, genres for MovieLens/Goodreads) and repeatedly:
- ask an LM (LOTUS sem_map) to turn the known attributes of x* into one search query,
- run the query through vector search and check whether x* is in the top K,
- if not, add one random search feature (from the user's SEC split) that has a value on x*.
The saved set is the last one before x* became retrievable, so every row has
recall == 0 by construction. Requires a gpt-oss-120b vLLM server and the
vector-search server. The SEC split must exist for every user first
(:mod:~coshop.data.preprocessing.compute_sec_split).
Usage
python -m coshop.data.preprocessing.initial_known_features --dataset hm \ --vllm-url http://localhost:3005 --vector-search-url http://localhost:3004
add_random_feature(known_features, pool, rng)
Return one feature from pool not already in known_features, or None.
build_search_only_pool(target_row, search_column_names)
Build the list of search features available for this target item.
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List of feature dicts: {"type": "column", "name": col, "value": val}. |
List[Dict[str, Any]]
|
Only columns with a non-missing, non-OPEN_TO_ANYTHING value are included. |
build_user_row(spec, always_included)
Initial loop state for one user, or None if it has no search features.
compute_recall_at_k(retrieved_ids, xstar_ids, k)
Return 1 if any xstar is in top k retrieved, else 0.
feature_names_from_known(known_features)
Return list of feature names for the known set: column name for columns, 'tag:
format_known_features_for_lm(known_features)
Format known features as text for LM input (e.g. 'Known attributes: col: val; tag: t').
parse_search_query_from_lm(raw)
Extract search query string from LOTUS sem_map _map output. Handles raw query text or JSON like {"search_query": "..."}.
run_incremental_recall_loop(users_df, query_function, instruction_template, chunk_size=50, max_iter=20, k=K, seed=0, retrieval_workers=1)
Run the incremental recall loop: sem_map to get query, run query, compute recall, drop recalled, add feature.
LOTUS must already be configured. Each user draws features from its own
random.Random(f"{seed}:{user_idx}"), so a user's result does not depend
on which other users are in the batch.
If a user's query can't be generated (after retries) or retrieval fails, that round is redone in the next iteration instead of being counted as a non-recalling round, so failures never add a feature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
users_df
|
DataFrame
|
DataFrame with columns user_idx, xstar_ids, known_features, pool. known_features and pool are lists of feature dicts (format from build_search_only_pool). |
required |
query_function
|
Callable[[Optional[str], int], DataFrame]
|
Callable(q, m) -> DataFrame with "id" column |
required |
instruction_template
|
str
|
LOTUS instruction string; must contain "{lm_input}". |
required |
chunk_size
|
int
|
Rows per LOTUS sem_map batch. |
50
|
max_iter
|
int
|
Max iterations (add-feature steps) per user. |
20
|
k
|
int
|
Recall at k. |
K
|
seed
|
int
|
Base random seed for adding features. |
0
|
retrieval_workers
|
int
|
Number of retrieval calls to run concurrently in each round. Calls are independent, so this changes speed, not results. |
1
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List of result dicts: user_idx, n_features_used, query, feature_names, recall (always 0). |
Generate z0 baseline queries.
For each user, prompt an LLM (LOTUS sem_map) to write one baseline search query from the spec's z0 preference summary. Used by the random and popularity baseline policies. Requires a gpt-oss-120b vLLM server. The SEC split and initial known features must exist first (z0 is built from them).
Output JSON format
{
"100": {"z0": {"query": "
Usage
python -m coshop.data.preprocessing.generate_baseline_queries --dataset all \ --vllm-url http://localhost:3005