Factors & model¶
Factors¶
factors ¶
Factor abstractions with natural <-> coded conversion.
CategoricalFactor
dataclass
¶
Bases: Factor
Categorical factor with arbitrary level labels.
Encoding maps each level to an integer index 0 .. k-1 for dummy coding
in the model matrix; decoding maps indices back to level labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Factor name. |
required |
levels
|
sequence
|
Category labels (at least two). |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
When fewer than two levels are given. |
Examples:
ContinuousFactor
dataclass
¶
Bases: Factor
Continuous factor with standard two-level coding to [-1, +1].
Natural values on [low, high] map linearly to coded [-1, +1] for
response-surface and optimal-design routines.
Formulas
coded = 2 * (x - low) / (high - low) - 1
x = low + (coded + 1) / 2 * (high - low)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Factor name. |
required |
low
|
float
|
Lower bound in natural units. |
required |
high
|
float
|
Upper bound in natural units. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
When |
Examples:
>>> import doekit as ed
>>> f = ed.ContinuousFactor("temp", 20.0, 80.0)
>>> float(f.encode(50.0))
0.0
DiscreteFactor
dataclass
¶
Bases: Factor
Numeric factor with a finite ordered level set.
Encoding uses the range from the smallest to largest level mapped to
[-1, +1]; decoding snaps to the nearest discrete level.
Formulas
Same linear coding as :class:ContinuousFactor on [levels[0], levels[-1]];
decode selects argmin |x - level|.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Factor name. |
required |
levels
|
sequence of float
|
Allowed numeric levels (at least two, sorted on init). |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
When fewer than two levels are given. |
Examples:
Factor ¶
Bases: ABC
Common interface for experimental factors.
Concrete types (:class:~doekit.domain.factors.ContinuousFactor,
:class:~doekit.domain.factors.DiscreteFactor, etc.) implement
:meth:encode / :meth:decode between natural and coded units and
:meth:to_dict for serialization. Do not instantiate this ABC directly.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Factor name (matches a run-matrix column). |
MixtureFactor
dataclass
¶
Bases: Factor
Mixture component for Scheffé / simplex designs.
Values are proportions on [lower, upper] subject to sum x_i = 1
across components. Encoding is the identity (proportions are not mapped to
±1); the experimental region is a simplex, not a hypercube.
Formulas
Constraint: sum_i x_i = 1 with lower_i <= x_i <= upper_i.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Component name. |
required |
lower
|
float
|
Lower bound on the proportion. |
0.0
|
upper
|
float
|
Upper bound on the proportion. |
1.0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When bounds violate |
Examples:
as_factors ¶
Normalize a flexible factor specification to list[Factor].
Accepts an integer (that many default continuous factors on [-1, 1]), a
dict {name: (low, high)} or {name: [levels]}, or a sequence of
:class:Factor instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
int or dict or sequence of Factor
|
Factor specification in any supported form. |
required |
Returns:
| Type | Description |
|---|---|
list of Factor
|
Normalized factor list. |
Examples:
decode_frame ¶
Return a copy of matrix with coded columns decoded to natural units.
encode_frame ¶
Return a copy of matrix with continuous/discrete columns coded to ±1.
Categorical factors are left raw (the model dummy-codes them). Mixture proportions stay as-is (identity coding for Scheffé). Columns with no associated factor are assumed already coded.
factor_from_dict ¶
Rebuild a :class:Factor from its :meth:to_dict output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
dict
|
Serialized factor with a |
required |
Returns:
| Type | Description |
|---|---|
Factor
|
Restored factor instance. |
Raises:
| Type | Description |
|---|---|
UnknownFactorTypeError
|
When |
Examples:
register_factor_type ¶
Register a factor type key for :func:factor_from_dict (open/closed).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str
|
Serialization |
required |
factory
|
callable
|
|
required |
Model¶
model ¶
Model specification DSL and model-matrix construction.
Interaction
dataclass
¶
Intercept
dataclass
¶
Main
dataclass
¶
Main-effect term for a single factor.
The model column is the factor column from the run matrix (after any coding applied upstream).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Factor name (must match a run-matrix column). |
required |
Examples:
Model ¶
An ordered set of terms that builds a model matrix X.
Terms (:class:Intercept, :class:Main, :class:Interaction,
:class:Power) define columns of X from a run matrix. Construct via
:meth:parse, :meth:from_terms, or presets (:meth:full_quadratic,
:meth:main_effects, Scheffé helpers).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
terms
|
sequence of Term
|
Ordered list of model terms (intercept first when present). |
required |
response
|
str
|
Response variable name (metadata only; not used in matrix construction). |
None
|
Examples:
>>> import doekit as ed
>>> m = ed.Model.parse("y ~ x1 + x2 + x1:x2")
>>> "x1:x2" in [t.label() for t in m.terms]
True
factor_names
property
¶
Unique factor names referenced by all terms (order of first appearance).
column_names ¶
Return model-matrix column labels for a run frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Run matrix whose columns supply factor values. |
required |
Returns:
| Type | Description |
|---|---|
list of str
|
One label per model column (matches :meth: |
from_dict
classmethod
¶
Rebuild a :class:Model from :meth:to_dict output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
dict
|
Serialized model. |
required |
Returns:
| Type | Description |
|---|---|
Model
|
Restored model instance. |
from_terms
classmethod
¶
Build a model from an explicit term list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
terms
|
sequence of Term
|
Model terms (intercept may be included explicitly). |
required |
response
|
str
|
Response variable name. |
None
|
intercept
|
bool
|
Insert :class: |
True
|
Returns:
| Type | Description |
|---|---|
Model
|
Model with the given terms. |
full_quadratic
classmethod
¶
Full quadratic response-surface model (main + interactions + squares).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factor_names
|
sequence of str
|
Factor names for mains, pairwise interactions, and |
required |
intercept
|
bool
|
Include an intercept. |
True
|
Returns:
| Type | Description |
|---|---|
Model
|
Model with all main effects, two-factor interactions, and pure quadratics. |
Examples:
main_effects
classmethod
¶
Main-effects-only model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factor_names
|
sequence of str
|
Factor names for main-effect columns. |
required |
intercept
|
bool
|
Include an intercept. |
True
|
Returns:
| Type | Description |
|---|---|
Model
|
Model with one :class: |
Examples:
matrix ¶
Build the model matrix X from a run DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Run matrix with columns for each factor referenced by |
required |
Returns:
| Type | Description |
|---|---|
(ndarray, shape(n_runs, n_params))
|
Model matrix |
parse
classmethod
¶
Parse a formula-like string into a :class:Model.
Supports + for main effects, : for interactions, ^ for
powers, and -1 / 0 to omit the intercept.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formula
|
str
|
Formula such as |
required |
Returns:
| Type | Description |
|---|---|
Model
|
Parsed model with an intercept unless |
Examples:
scheffe_linear
classmethod
¶
Scheffé linear mixture model (no intercept).
Formulas
y = sum_i beta_i x_i with sum_i x_i = 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factor_names
|
sequence of str
|
Mixture component names. |
required |
Returns:
| Type | Description |
|---|---|
Model
|
Linear Scheffé model without intercept. |
scheffe_quadratic
classmethod
¶
Scheffé quadratic mixture model (no intercept).
Formulas
Linear Scheffé terms plus cross products x_i x_j for i < j.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factor_names
|
sequence of str
|
Mixture component names. |
required |
Returns:
| Type | Description |
|---|---|
Model
|
Quadratic Scheffé model without intercept. |
to_dict ¶
Serialize the model to a plain dict.
Returns:
| Type | Description |
|---|---|
dict
|
Keys |