fewlab 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
fewlab-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 finite-sample
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
fewlab-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,35 @@
1
+ Metadata-Version: 2.4
2
+ Name: fewlab
3
+ Version: 0.1.0
4
+ Summary: Pick the fewest items to label for unbiased OLS on shares
5
+ Author: Gaurav Sood
6
+ Project-URL: Homepage, https://github.com/finite-sample/fewlab
7
+ Requires-Python: >=3.9
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Requires-Dist: numpy>=1.23
11
+ Requires-Dist: pandas>=1.5
12
+ Dynamic: license-file
13
+
14
+ # fewlab
15
+
16
+ Pick the **few**est items to **lab**el for unbiased OLS on shares — fast, deterministic, and simple.
17
+
18
+ ## What it does
19
+
20
+ Given:
21
+ - a counts matrix `C` (units × items), and
22
+ - a covariate matrix `X` (units × predictors),
23
+
24
+ `fewlab.items_to_label(C, X, K)` returns **which K items** to label to make OLS on the trait share unbiased (Horvitz–Thompson over items) and **variance-efficient** in the A-optimal sense.
25
+
26
+ It computes, for each item `j`:
27
+ - `v_j = c_{·j} / T` (row-normalized exposure),
28
+ - `g_j = X^T v_j`,
29
+ - `w_j = g_j^T (X^T X)^{-1} g_j`,
30
+ then picks the **top-K** items by `w_j`. This is the deterministic fixed-budget version of the square-root A-optimal allocation.
31
+
32
+ ## Install
33
+
34
+ ```bash
35
+ pip install -e .
fewlab-0.1.0/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # fewlab
2
+
3
+ Pick the **few**est items to **lab**el for unbiased OLS on shares — fast, deterministic, and simple.
4
+
5
+ ## What it does
6
+
7
+ Given:
8
+ - a counts matrix `C` (units × items), and
9
+ - a covariate matrix `X` (units × predictors),
10
+
11
+ `fewlab.items_to_label(C, X, K)` returns **which K items** to label to make OLS on the trait share unbiased (Horvitz–Thompson over items) and **variance-efficient** in the A-optimal sense.
12
+
13
+ It computes, for each item `j`:
14
+ - `v_j = c_{·j} / T` (row-normalized exposure),
15
+ - `g_j = X^T v_j`,
16
+ - `w_j = g_j^T (X^T X)^{-1} g_j`,
17
+ then picks the **top-K** items by `w_j`. This is the deterministic fixed-budget version of the square-root A-optimal allocation.
18
+
19
+ ## Install
20
+
21
+ ```bash
22
+ pip install -e .
@@ -0,0 +1,35 @@
1
+ Metadata-Version: 2.4
2
+ Name: fewlab
3
+ Version: 0.1.0
4
+ Summary: Pick the fewest items to label for unbiased OLS on shares
5
+ Author: Gaurav Sood
6
+ Project-URL: Homepage, https://github.com/finite-sample/fewlab
7
+ Requires-Python: >=3.9
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Requires-Dist: numpy>=1.23
11
+ Requires-Dist: pandas>=1.5
12
+ Dynamic: license-file
13
+
14
+ # fewlab
15
+
16
+ Pick the **few**est items to **lab**el for unbiased OLS on shares — fast, deterministic, and simple.
17
+
18
+ ## What it does
19
+
20
+ Given:
21
+ - a counts matrix `C` (units × items), and
22
+ - a covariate matrix `X` (units × predictors),
23
+
24
+ `fewlab.items_to_label(C, X, K)` returns **which K items** to label to make OLS on the trait share unbiased (Horvitz–Thompson over items) and **variance-efficient** in the A-optimal sense.
25
+
26
+ It computes, for each item `j`:
27
+ - `v_j = c_{·j} / T` (row-normalized exposure),
28
+ - `g_j = X^T v_j`,
29
+ - `w_j = g_j^T (X^T X)^{-1} g_j`,
30
+ then picks the **top-K** items by `w_j`. This is the deterministic fixed-budget version of the square-root A-optimal allocation.
31
+
32
+ ## Install
33
+
34
+ ```bash
35
+ pip install -e .
@@ -0,0 +1,10 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ fewlab/fewlab.egg-info/PKG-INFO
5
+ fewlab/fewlab.egg-info/SOURCES.txt
6
+ fewlab/fewlab.egg-info/dependency_links.txt
7
+ fewlab/fewlab.egg-info/requires.txt
8
+ fewlab/fewlab.egg-info/top_level.txt
9
+ tests/test_api.py
10
+ tests/test_quality.py
@@ -0,0 +1,2 @@
1
+ numpy>=1.23
2
+ pandas>=1.5
@@ -0,0 +1,18 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "fewlab"
7
+ version = "0.1.0"
8
+ description = "Pick the fewest items to label for unbiased OLS on shares"
9
+ authors = [{name = "Gaurav Sood"}]
10
+ readme = "README.md"
11
+ requires-python = ">=3.9"
12
+ dependencies = ["numpy>=1.23", "pandas>=1.5"]
13
+
14
+ [project.urls]
15
+ Homepage = "https://github.com/finite-sample/fewlab"
16
+
17
+ [tool.setuptools.packages.find]
18
+ where = ["fewlab"]
fewlab-0.1.0/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,19 @@
1
+ import pandas as pd
2
+ from fewlab import items_to_label
3
+ from .data_synth import make_synth
4
+
5
+ def test_basic_api():
6
+ counts, X = make_synth(n=50, m=80, p=5, seed=0)
7
+ K = 15
8
+ chosen = items_to_label(counts, X, K=K)
9
+ assert isinstance(chosen, list)
10
+ assert len(chosen) == K
11
+ # ensure they are item column names
12
+ assert set(chosen).issubset(set(counts.columns))
13
+
14
+ def test_zero_rows_are_dropped():
15
+ counts, X = make_synth(n=30, m=40, p=4, seed=1)
16
+ # force some zero-total rows
17
+ counts.iloc[:3, :] = 0
18
+ chosen = items_to_label(counts, X, K=10)
19
+ assert len(chosen) == 10
@@ -0,0 +1,32 @@
1
+ import numpy as np
2
+ import pandas as pd
3
+ from fewlab import items_to_label
4
+ from .data_synth import make_synth
5
+
6
+ def compute_w(counts, X, ridge=None):
7
+ T = counts.sum(axis=1).to_numpy(float)
8
+ keep = T > 0
9
+ counts = counts.loc[keep]; X = X.loc[keep]; T = T[keep]
10
+ V = counts.to_numpy(float) / T[:, None]
11
+ Xn = X.to_numpy(float)
12
+ XtX = Xn.T @ Xn
13
+ if ridge is None:
14
+ cond = np.linalg.cond(XtX)
15
+ if not np.isfinite(cond) or cond > 1e12:
16
+ ridge = 1e-8
17
+ if ridge:
18
+ XtX = XtX + ridge * np.eye(XtX.shape[0])
19
+ XtX_inv = np.linalg.inv(XtX)
20
+ G = Xn.T @ V
21
+ w = np.einsum("jp,pk,kj->j", G.T, XtX_inv, G)
22
+ return w, counts.columns
23
+
24
+ def test_selected_items_have_higher_w():
25
+ counts, X = make_synth(n=120, m=200, p=6, seed=1234)
26
+ K = 30
27
+ chosen = items_to_label(counts, X, K=K)
28
+ w, cols = compute_w(counts, X)
29
+ w_map = {c: w[i] for i, c in enumerate(cols)}
30
+ w_chosen = np.median([w_map[c] for c in chosen])
31
+ w_rest = np.median([w_map[c] for c in cols if c not in chosen])
32
+ assert w_chosen >= w_rest, "Selected items should have larger median w"