Design¶
Experimental design: factors, grids, and screening.
trade_study.FactorType
¶
Bases: Enum
Type of design factor.
trade_study.Factor(name, factor_type, levels=None, bounds=None)
dataclass
¶
A single design factor.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Factor identifier (e.g. "alpha", "layer1_method"). |
factor_type |
FactorType
|
Continuous, discrete, or categorical. |
levels |
list[Any] | None
|
For categorical/discrete: list of allowed values. |
bounds |
tuple[float, float] | None
|
For continuous: (low, high) tuple. |
__post_init__()
¶
Validate factor constraints.
Raises:
| Type | Description |
|---|---|
ValueError
|
If name is empty, continuous factor has missing or invalid bounds, or discrete/categorical factor has empty levels. |
Source code in src/trade_study/design.py
trade_study.build_grid(factors, *, method='full', n_samples=100, seed=42, scramble=True)
¶
Build an experimental design grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factors
|
list[Factor]
|
List of design factors. |
required |
method
|
str
|
Design method. One of:
- "full": Full factorial (categorical/discrete only).
- "lhs": Latin hypercube sampling (continuous factors, maps
categorical factors to uniform random selection).
- "sobol": Scrambled Sobol' sequence via |
'full'
|
n_samples
|
int
|
Number of samples for LHS / QMC methods. |
100
|
seed
|
int
|
Random seed. |
42
|
scramble
|
bool
|
Whether to apply scrambling to QMC sequences (Sobol / Halton). Ignored for other methods. |
True
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of config dictionaries, one per design point. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an unknown design method is specified. |
Source code in src/trade_study/design.py
trade_study.reduce_factors(factors, importance, *, threshold=0.1)
¶
Keep only factors whose max importance exceeds threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factors
|
list[Factor]
|
Original factor list. |
required |
importance
|
dict[str, NDArray[floating[Any]]]
|
Output of |
required |
threshold
|
float
|
Minimum importance to retain a factor. |
0.1
|
Returns:
| Type | Description |
|---|---|
list[Factor]
|
Reduced list of influential factors. |
Source code in src/trade_study/design.py
trade_study.screen(run_fn, factors, *, method='morris', n_trajectories=100, seed=42)
¶
Screen factors for influence on observables via SALib.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run_fn
|
Callable[[dict[str, Any]], dict[str, float]]
|
Callable that takes a config dict and returns a dict of observable name → scalar score. |
required |
factors
|
list[Factor]
|
List of continuous factors to screen. |
required |
method
|
str
|
Screening method ( |
'morris'
|
n_trajectories
|
int
|
Number of Morris trajectories. For Sobol, this controls the base sample size N; the total number of model evaluations is N x (num_vars + 2). |
100
|
seed
|
int
|
Random seed. |
42
|
Returns:
| Type | Description |
|---|---|
dict[str, NDArray[floating[Any]]]
|
Dictionary mapping observable names to arrays of factor importance |
dict[str, NDArray[floating[Any]]]
|
(mu_star for Morris, S1 for Sobol), one value per factor. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If method is unknown or no continuous factors are provided. |