advanced_design#
Functions
|
Sample subparameter values conditioned on the parent category assignment. |
|
Calculate A-optimality (trace of the inverse information matrix) for the continuous and subparameter columns. |
|
Calculate the condition number of the information matrix for the continuous and subparameter columns. |
|
Calculate D-optimality (determinant of the information matrix) for the continuous and subparameter columns. |
Calculate the maximum Cramér's V association among all pairs of categorical columns. |
|
|
Calculate the maximum absolute Pearson correlation among all pairs of continuous and subparameter columns. |
|
Calculate the maximum association between categorical and continuous/subparameter columns using eta-squared (sqrt). |
|
Compute a pairwise correlation matrix that handles mixed variable types: |
Calculate the coefficient of variation (CV) of pairwise Euclidean distances between design points in the continuous/subparameter space. |
|
|
Compute Cramér's V association statistic for a contingency table. |
|
Compute the eta-squared effect size between a categorical and a numeric variable. |
|
Generate |
|
Evaluate a design using the specified quality metrics. |
|
Extend an existing design by finding the best set of |
|
Generate |
|
Generate a single candidate design and compute its quality metrics. |
Canonical column-type rule: object/category dtype -> categorical, else numerical. |
|
|
Infer the subparameter mapping from |
|
Draw LHS-stratified categorical samples using inverse-transform sampling with per-level frequency weights. |
|
Search for the category assignment (for the variable that has a subparam mapping) that minimizes the maximum inter-category correlation. |
|
Plot a heatmap of the mixed correlation matrix. |
|
Plot histograms for continuous parameters and bar charts for categorical variables, with stacked histograms for subparameters colored by parent category. |
|
Plot per-metric bar charts over trial seeds to visualize design quality evolution. |
|
Plot an MDS projection of the continuous and subparameter columns. |
|
Plot a PCA projection of the continuous and subparameter columns. |
|
Plot a UMAP projection of the continuous and subparameter columns. |
|
Draw LHS samples for all continuous parameters, mapping [0, 1) uniform samples to the discrete level sets defined in |
|
Generate a complete experimental design by LHS-sampling all continuous and categorical parameters. |
Classes
|
An advanced experimental designer that extends ExpDesigner with support for biased/constrained sampling, categorical subparameters, and design quality metrics. |
- class obsidian.experiment.advanced_design.AdvExpDesigner(continuous_params: dict | None = None, conditional_subparameters: dict | None = None, subparam_mapping: dict | None = None, design_df: DataFrame | None = None, X_space=None, seed: int | None = None, n_category_trials: int = 100, corr_threshold: float = 0.01)[source]#
Bases:
ExpDesignerAn advanced experimental designer that extends ExpDesigner with support for biased/constrained sampling, categorical subparameters, and design quality metrics.
Extends ExpDesigner so it can be passed directly to Campaign as the
designerargument. WhenX_spaceis provided,campaign.initialize()will callgenerate_design()and honor all biases and constraints defined incontinuous_paramsandconditional_subparameters.- compare_frequencies(design, verbose=True)[source]#
Compares the empirical frequencies of categorical variables in the design with the expected frequencies defined in
conditional_subparameters.- Parameters:
design – The design DataFrame to analyze.
verbose – If True, print the frequency table to stdout. Defaults to True.
- Returns:
- A DataFrame with columns
['categorical_var', 'level', 'expected', 'empirical']containing one row per level of each categorical variable.
- Return type:
pd.DataFrame
- evaluate_design(design, metrics_to_optimize=None)[source]#
Evaluates the quality of the given design based on specified metrics.
- Parameters:
design – The design DataFrame to evaluate.
metrics_to_optimize – List of metric names to evaluate. Defaults to all metrics in
DEFAULT_METRICS.
- Returns:
Computed metric values keyed by metric name.
- Return type:
dict
- extend_design(existing_design, n, seed=None, n_trials=10, metrics_to_optimize=None, maximize_metrics=None, max_workers=None)[source]#
Extends an existing design by appending the best-scoring set of new samples chosen from multiple candidates.
- Parameters:
existing_design – The existing design DataFrame to extend.
n – Number of new samples to add.
seed – Optional random seed for reproducibility.
n_trials – Number of candidate extensions to evaluate. Defaults to 10.
metrics_to_optimize – List of metric names to include in scoring. Defaults to all seven standard metrics.
maximize_metrics – List of booleans indicating whether to maximize each metric. Defaults to
[True, False, False, ...].max_workers – Number of parallel worker processes.
- Returns:
(extended_design, metrics_summary)whereextended_designcontains all original rows plus the best new rows, and
metrics_summaryis a pd.DataFrame of candidate scores.
- Return type:
tuple
- generate_design(seed, n_samples, optimize_categories=True)[source]#
Generates a design by sampling from the given parameter space.
- Parameters:
seed – Random seed for reproducibility.
n_samples – Number of samples to generate.
optimize_categories – Whether to optimize categorical assignments to reduce inter-category correlation. Defaults to True.
- Returns:
The generated sample design.
- Return type:
pd.DataFrame
Note
When
optimize_categories=True, only the first subparam-mapped category (as determined bysubparam_mapping) is optimized. Additional categorical variables are assigned with a single random draw.
- initialize(m_initial=None, method='LHS', sample_custom=None, optimize_categories=False)[source]#
Generates an initial experimental design honoring all biases and constraints defined in
continuous_paramsandconditional_subparameters.Overrides
ExpDesigner.initialize()so that a Campaign whosedesigneris an AdvExpDesigner will automatically use biased/constrained sampling.- Parameters:
m_initial – Number of initial experiments. Defaults to
2 * n_dimwhen X_space is provided, or raises if neither is available.method –
Sampling strategy.
'LHS'(default): callsgenerate_design()with LHS + biases.'Optimized': callsoptimize_design()to maximize D-optimality across multiple trials (slower but higher-quality).
sample_custom – Ignored; retained for API compatibility with ExpDesigner.
optimize_categories – Whether to optimize categorical assignments to minimize correlation (passed to
generate_design()). Defaults to False.
- Returns:
The generated design.
- Return type:
pd.DataFrame
- Raises:
ValueError – If m_initial cannot be inferred (no X_space and no m_initial given).
- classmethod load_state(obj_dict: dict, X_space=None, seed: int | None = None) AdvExpDesigner[source]#
Reconstruct an
AdvExpDesignerfrom a saved state dictionary.- Parameters:
obj_dict (dict) – Output of
save_state().X_space – Override for the parameter space. When provided (typically by
Campaign.load_state()), the X_space payload inobj_dictis ignored. Defaults toNone.seed (int | None, optional) – Override for the seed. When provided,
obj_dict['seed']is ignored. Defaults toNone.
- Returns:
A new designer instance equivalent to the saved one.
- Return type:
- optimize_design(n_trials, n_samples, metrics_to_optimize=None, maximize_metrics=None, seed_start=0, max_workers=None)[source]#
Optimizes the design by generating multiple candidates and selecting the best according to a composite score over the specified metrics.
- Parameters:
n_trials – Number of candidate designs to generate and evaluate.
n_samples – Number of experiments in each candidate design.
metrics_to_optimize – List of metric names to include in the composite score. Defaults to all seven standard metrics.
maximize_metrics – List of booleans, one per metric, indicating whether each metric should be maximized (True) or minimized (False). Defaults to
[True, False, False, ...]— maximize D-optimality only.seed_start – Starting random seed for candidate generation. Defaults to 0.
max_workers – Maximum number of parallel worker processes. Defaults to
None(uses all available CPUs).
- Returns:
(best_design, metrics_df)wherebest_designis thehighest-scoring pd.DataFrame and
metrics_dfis a pd.DataFrame summarizing all candidates.
- Return type:
tuple
- plot_correlation(design)[source]#
Plots a mixed correlation matrix heatmap for the design’s parameters.
- Parameters:
design – The design DataFrame to visualize.
- plot_histograms(design)[source]#
Plots histograms (continuous) and bar charts (categorical) for each parameter in the design.
- Parameters:
design – The design DataFrame to visualize.
- plot_mds(design, hue=None)[source]#
Performs Multidimensional Scaling (MDS) on the continuous parameters and plots the two-dimensional embedding.
- Parameters:
design – The design DataFrame to analyze.
hue – Name of a categorical column to use for color-coding points.
- plot_pca(design, hue=None)[source]#
Performs PCA on the continuous parameters and plots the first two components.
- Parameters:
design – The design DataFrame to analyze.
hue – Name of a categorical column to use for color-coding points.
- plot_quality_evolution(metrics_df)[source]#
Plots per-metric bar charts over trial seeds to visualize design quality evolution.
- Parameters:
metrics_df – DataFrame containing trial metrics (must include a ‘seed’ column).
- plot_umap(design, hue=None, verbose=False)[source]#
Performs UMAP dimensionality reduction on the continuous parameters and plots the two-dimensional embedding.
- Parameters:
design – The design DataFrame to analyze.
hue – Name of a categorical column to use for color-coding points.
verbose – Whether to show UMAP’s internal progress log. Defaults to False.