Design of Experiments

Version 12 | Created: 2026-08-30 | Updated: 2026-11-07 13:46 UTC
- 1. Purpose
- 2. Summary
- 3. Taxonomy and its Hierarchy
- 4. Principle
- 5. Full Factorial Designs
- 6. Fractional Factorial Designs
- 7. Response Surface Designs
- 8. D-Optimal Designs
- References
- Appendix A. Terminology
- Appendix B. Python Implementation
- Problem Statement: In passively collected data several factors explain the same change in the response, so a model fitted to that data cannot separate the effect of each factor.
- Goal: Build a DOE User's Guide. A reader who finishes it can pick the design that fits the target model, the run budget and the constraints on the factor space, and can state its assumptions and limits.
- Non-Goal: The analysis after the experiment (ANOVA, model fitting and testing) and the use of any particular software are out of scope.
Designing an experiment is choosing the rows of the design matrix so that the covariance of the coefficient estimates is small within a run budget. The target model picks the design: a full factorial for every effect, a fractional factorial for screening many factors, a response surface design for optimisation with curvature, and a D-optimal design for irregular budgets, constraints and covariates.
Design families differ in what they assume about the model, and the stronger the assumption the fewer runs they need. This document follows the four families of the MathWorks Statistics and Machine Learning Toolbox [1].
The five axes that separate designs are shown in Fig 1.
Axis Values
----------------- ----------------------------------------------------------------------
Target model all effects | main effects + some interactions | main effects only
| full quadratic | any stated model
Levels per factor 2 | 3 | any
Run count product of levels | power of 2 | multiple of 4 | set by geometry | any
Factor space regular cube | constrained region
Factor type continuous and set | categorical | recorded covariate
Fig 1. Axes of the design taxonomy
The five axes are independent. With the same full quadratic target model, a cubic factor space calls for a response surface design and a constrained region calls for a D-optimal design.
What each design assumes about the model, and what it pays for that, follows the hierarchy in Fig 2.
Family / design Assumes Gives up
----------------------- ----------------------------------- ---------------------------------------
Full factorial nothing about the model small run count: N1 x ... x Nk runs
+- General fractional high-order interactions are small separation of confounded effects
+- Plackett-Burman only main effects matter every interaction (resolution III)
Response surface response is at most quadratic terms above second order
+- Central composite settings at +/-alpha are reachable fewest runs: embeds a full 2^k
+- Box-Behnken three or more factors corner runs and an embedded factorial
D-optimal the stated model is correct regular geometry and orthogonality
Fig 2. Hierarchy of design families
Each step down from the full factorial adds an assumption about the model and removes runs. A general fractional design saves runs by treating high-order interactions as small, and a Plackett-Burman design by treating every interaction as small. The response surface family caps the model at second order and adds a third level to estimate curvature. A D-optimal design chooses its runs for one stated model, so it needs neither a cubic factor space nor a fixed run count.
Table 1 gives the criteria for choosing a design.
Table 1. Comparison of designs
| Design | Model aimed at | Run count | Use when |
|---|---|---|---|
| Full factorial | All effects and interactions | Few factors, every combination affordable | |
| General fractional | Main effects, some interactions | Screening many factors with a chosen resolution | |
| Plackett-Burman | Main effects | Smallest multiple of 4 above |
Main-effect screening needing a run count between powers of 2 |
| Central composite | Full quadratic model | Optimisation near a known operating point | |
| Box-Behnken | Full quadratic model | Optimisation of a process that cannot set every factor to an extreme at once | |
| D-optimal | Any stated model | Set to the budget | Irregular budgets, constraints, covariates or categorical factors |
The design fixes the precision of the coefficient estimates before any response is measured. Write
The covariance in equation (1) contains no
Two-level factors are coded as
A full factorial design measures the response at every combination of the factor levels and estimates every effect and
interaction the factors can produce. With
- Assumption: Few factors and levels, so a run at every combination is affordable.
-
Settings: The level count
$N_i$ of each factor. -
Breaks when: More factors multiply the run count by their level counts; even at two levels
$2^k$ exceeds the budget. - Used for: Experiments with few factors, and the starting point of the fractional and response surface families.
Factors need not share a level count. The design is the cartesian product of the level sets, listed so that the first column varies fastest. One two-level factor and one three-level factor give six runs (Appendix B.2).
When every factor has two levels the design has
A fractional factorial design measures a subset of the treatments, chosen so that the effects believed to matter stay
estimable. Most of the
The price of fewer runs is confounding. Each retained column carries the sum of several effects, and no experiment run on that design can tell them apart. The resolution names the degree of confounding and is the length of the shortest word in the defining relation.
Table 2. Design resolution [2]
| Resolution | Main effects confounded with | Two-way interactions confounded with |
|---|---|---|
| III | Two-way interactions | Each other |
| IV | Three-way interactions | Each other |
| V | Four-way interactions | Three-way interactions |
Under resolution III the main effects hold only if two-way interactions are small; resolution IV estimates the main effects apart from two-way interactions; resolution V estimates both the main effects and the two-way interactions apart from every other effect of second order or lower.
When only main effects are considered significant, a Plackett-Burman design gives a resolution III design whose run count is a multiple of 4 rather than a power of 2, and so fills the gaps between the two-level factorials [3]. Eleven factors are screened in 12 runs instead of 16.
The design is built from a Hadamard matrix. Where the run count is a power of 2 the columns of the Hadamard matrix are
used directly; otherwise a final row of
- Assumption: Interactions are negligible and only main effects are significant.
-
Settings: The number of factors
$k$ . The run count is the smallest multiple of 4 above$k$ , and needs a generator row when it is not a power of 2. - Breaks when: A large two-way interaction is confounded with a main effect, so the main-effect estimate carries that interaction.
- Used for: Screening many factors.
A general fractional design starts from a full factorial in a set of basic factors and defines the remaining factors as products of them.
Definition: Generator
When a factor is defined as a product of basic factors, that product is the generator of the factor.
The generators fix both the design and its confounding. The generators a b c abc handle four factors in eight runs, half of the sixteen a full factorial needs.
The defining relation is generated by the words that the generators imply, and the resolution is the length of the shortest word in the subgroup those words generate. Table 3 gives the resolution of four generator sets.
Table 3. Resolution of four generator sets
| Generators | Runs | Resolution |
|---|---|---|
| a b ab | 4 | III |
| a b c ab | 8 | III |
| a b c abc | 8 | IV |
| a b c d abcd | 16 | V |
The two eight-run designs cost the same and differ only in the generator, yet a b c ab confounds main effects with
two-way interactions and a b c abc does not. At a fixed run count the generator sets the resolution.
- Assumption: The interactions that the resolution confounds are small (Table 2).
-
Settings: The number of basic factors
$b$ (run count$2^{b}$ ) and a generator for each added factor. - Breaks when: A large confounded interaction makes the column estimate a sum of effects that cannot be told apart.
- Used for: Screening many factors when the run count stays a power of 2 and the resolution must be chosen.
A response surface design adds a third level to each factor to fit a full quadratic model, which carries curvature. Once the factors that matter are known and the goal is optimisation, the model needs curvature: an optimum is a stationary point, and a first-order model has none.
The full quadratic model in
A two-level design cannot fit equation (2). The square term
A central composite design consists of a two-level factorial at the corners of a cube, star points on the factor axes at
a distance
The three variants differ in where the star points sit.
-
Circumscribed: Star points outside the cube at
$\pm\alpha$ , which needs factor settings beyond the two-level range. -
Faced: Star points on the faces of the cube at
$\alpha = 1$ . -
Inscribed: The circumscribed shape rescaled so that the star points, not the corners, sit at
$\pm 1$ .
A circumscribed design in two factors with one centre run has nine runs, with the star points at
- Assumption: Near the operating point the response is approximated by a full quadratic model, and there are two or more factors.
-
Settings:
$\alpha$ ($(2^k)^{1/4}$ for rotatable, 1 for faced), the variant, and the centre run count$n_c \ge 1$ . The run count is$2^k + 2k + n_c$ . -
Breaks when: The process cannot reach the
$\pm\alpha$ settings, which rules out the circumscribed design in favour of faced or inscribed. - Used for: Optimisation near a known operating point.
A Box-Behnken design fits the full quadratic model without ever setting two factors to an extreme at the same time [4]. It puts a two-level factorial in each pair of factors with the rest held at the centre, so its points sit at the midpoints of the edges of the design space and at the centre. The corners of the cube are left out, so no run combines the extremes of every factor, and there is no embedded factorial design.
Table 4. Run counts of the two response surface designs, three centre runs each
| Factors | Quadratic terms | Box-Behnken | Central composite |
|---|---|---|---|
| 3 | 10 | 15 | 17 |
| 4 | 15 | 27 | 27 |
| 5 | 21 | 43 | 45 |
The pairwise construction reproduces the published Box-Behnken designs for three to five factors. Beyond five the published designs use a balanced incomplete block design and are smaller than every pair taken in turn. The pairwise construction stays valid there but is no longer minimal.
- Assumption: The response is approximated by a full quadratic model, and there are three or more factors.
-
Settings: The centre run count
$n_c$ (default 3 in the Appendix B implementation). The run count is$4\binom{k}{2} + n_c$ . - Breaks when: Beyond five factors the pairwise construction does not give the minimum run count.
- Used for: Optimising a process that cannot set every factor to an extreme at once.
A D-optimal design takes the model and the run budget as given and searches for the set of runs that minimises the
covariance of the coefficients by maximising the determinant of the information matrix
- Assumption: The stated model describes the response correctly. The design is optimal only for the coefficients of that model.
- Settings: The model (linear, interaction, quadratic), the run count, the grid level count of continuous factors, the number of random starts, and the candidate set.
- Breaks when: Fewer runs than model terms cannot estimate the model. With too few starts the search can stop at a design short of the optimum.
- Used for: Irregular run budgets, augmentation of staged experiments, recorded covariates, categorical factors, and constrained factor spaces.
D-efficiency normalises the determinant of the information matrix so that designs of different sizes can be compared. In
equation (3)
The two-level full factorial reaches a D-efficiency of 1 for the linear model. When an orthogonal design exists, a D-optimal search cannot find a better one.
A D-optimal design comes from an iterative search. The coordinate-exchange algorithm starts from a random design and repeats one move until nothing improves: take one factor of one run, try every value on a grid, and keep the best [5]. The result depends on where the search starts, so the search is repeated from several random starts and the best design is kept.
Twelve runs requested for a quadratic model in three factors give a design with D-efficiency 0.4498 (Appendix B.5). That is three runs fewer than the smallest Box-Behnken design of Table 4 and two above the ten coefficients the model has.
An experiment often runs in stages, and the runs already performed cannot be chosen again. Augmentation holds the performed runs fixed and searches only for the new ones, so the added runs fill what the existing design is missing. The search uses row exchange, which swaps whole runs against rows of a candidate set rather than single coordinates; when the runs are drawn from a fixed list, exchanging rows is the fitting move.
The four corner runs alone cannot fit the six-term quadratic model. Two more runs make it estimable, and the search puts
both of them off the corners, where the existing design has nothing, at
A covariate is a factor that the experimenter records rather than sets, such as ambient temperature, the operator on shift, or the age of a batch. Its values are known in advance for each run but cannot be chosen. The design problem is then to choose the controlled factors given the covariate column, so that the model separates the controlled effects from the covariate effect.
When the covariate drifts evenly from
A categorical factor has no numeric scale, so its levels cannot be pushed towards
Three categorical factors at three levels each give 27 candidate treatments. Asked for nine runs, the search returns a design in which every level of every factor appears three times and every pair of levels from two factors appears exactly once.
Row exchange takes a candidate set, the list of runs it may choose from. Where the factor space is a cube, the candidate set is a grid over it.
Passing the candidate set explicitly handles factor spaces that are not cubes. A mixture whose components must sum to one, a pair of settings that cannot be high together, and a machine that cannot run cold and fast are all constraints that remove rows from the grid. The search never proposes a run that is not on the list, so deleting those rows from the candidate set carries the constraint into the design.
[1] MathWorks. Design of Experiments — Statistics and Machine Learning Toolbox Documentation.
[2] Montgomery, D. C. (2019). Design and Analysis of Experiments (10th ed.). Wiley.
ISBN 978-1-119-49249-8.
[3] Plackett, R. L., & Burman, J. P. (1946). The Design of Optimum Multifactorial Experiments.
Biometrika, 33(4), 305–325.
[4] Box, G. E. P., & Behnken, D. W. (1960). Some New Three Level Designs for the Study of
Quantitative Variables. Technometrics, 2(4), 455–475.
[5] Meyer, R. K., & Nachtsheim, C. J. (1995). The Coordinate-Exchange Algorithm for Constructing
Exact Optimal Experimental Designs. Technometrics, 37(1), 60–69.
- Basic factor: a factor whose column comes from the full factorial a fractional design starts from, rather than from a generator.
- Candidate set: the list of factor settings from which a D-optimal search may choose runs.
- Centre run: a run with every continuous factor at the middle of its range.
- Confounding: two effects carried by the same design column, so that no analysis of the experiment can separate them.
- Covariate: a factor that the experimenter records for each run rather than sets.
- Defining relation: the set of products of factor columns that equal the column of ones in a fractional factorial design; its shortest word gives the resolution.
-
Effect coding: coding of a categorical factor with
$L$ levels into$L - 1$ columns, with the last level set to$-1$ in every column. - Factor: an input whose value the experimenter sets or records.
- Generator: the product of basic factors that defines an added factor in a fractional factorial design.
-
Hadamard matrix: a square matrix of
$\pm 1$ entries whose columns are mutually orthogonal. - Interaction: the extent to which the effect of one factor depends on the level of another.
- Level: one of the values a factor takes in a design.
- Main effect: the change in the response when one factor changes level, averaged over the other factors.
- Response: the measured output of a run.
- Rotatable: a design whose prediction variance depends only on the distance from the centre of the factor space.
- Run: one execution of the experiment at one combination of factor levels; one row of a design.
- Screening: an experiment that picks out, from many factors, those with a significant effect on the response.
- Star point: a run of a central composite design that moves one factor off the centre and holds the rest at it.
- Treatment: a combination of factor levels.
- Word: a group of factor names whose product is the column of ones in the defining relation; its length is the number of factors in it.
The Python blocks of this appendix are meant to run in order. Each one uses the names defined before it, on top of the
header in B.1, and the text block right after it holds its output. Running them needs numpy and scipy.
# Python
import itertools
from typing import Sequence
import numpy as np
from scipy.linalg import hadamard
np.set_printoptions(linewidth=100, suppress=True, precision=4)The cartesian product construction of section 5.1, making six runs from a two-level and a three-level factor.
# Python
def full_factorial(levels: Sequence[int]) -> np.ndarray:
"""Full factorial design. Column i takes levels 0..levels[i]-1; the first column varies fastest."""
levels = list(levels)
if any(v < 2 for v in levels):
raise ValueError(f"every factor needs at least 2 levels; got {levels}")
grids = np.meshgrid(*[np.arange(v) for v in levels], indexing='ij')
return np.column_stack([g.ravel(order='F') for g in grids])
print(full_factorial([2, 3]))[[0 0]
[1 0]
[0 1]
[1 1]
[0 2]
[1 2]]
The
# Python
def two_level_factorial(n_factors: int) -> np.ndarray:
"""Two-level full factorial design coded as -1 and +1."""
if n_factors < 1:
raise ValueError(f"n_factors must be at least 1; got {n_factors}")
return 2.0 * full_factorial([2] * n_factors) - 1.0
print(two_level_factorial(3))[[-1. -1. -1.]
[ 1. -1. -1.]
[-1. 1. -1.]
[ 1. 1. -1.]
[-1. -1. 1.]
[ 1. -1. 1.]
[-1. 1. 1.]
[ 1. 1. 1.]]
The construction of section 6.2. The last line checks that the 11 columns of the 12-run design are mutually orthogonal. A run count that is neither a power of 2 nor covered by a generator raises instead of falling back on a different design.
# Python
PB_GENERATORS = {
12: '++-+++---+-',
20: '++--++++-+-+----++-',
24: '+++++-+-++--++--+-+----',
}
def plackett_burman(n_factors: int) -> np.ndarray:
"""Plackett-Burman design with the smallest run count that is a multiple of 4 and exceeds n_factors."""
if n_factors < 1:
raise ValueError(f"n_factors must be at least 1; got {n_factors}")
n_runs = 4 * int(np.ceil((n_factors + 1) / 4))
if n_runs & (n_runs - 1) == 0:
design = hadamard(n_runs)[:, 1:] * 1.0
elif n_runs in PB_GENERATORS:
row = np.array([1.0 if c == '+' else -1.0 for c in PB_GENERATORS[n_runs]])
design = np.vstack([np.roll(row, k) for k in range(n_runs - 1)] + [-np.ones(n_runs - 1)])
else:
raise ValueError(f"no Plackett-Burman construction available for {n_runs} runs")
return design[:, :n_factors]
design = plackett_burman(11)
print(design.shape, np.allclose(design.T @ design, 12 * np.eye(11)))(12, 11) True
The generator construction of section 6.3, making the eight-run design in four factors from the generators a b c abc.
# Python
def fractional_factorial(generators: str) -> np.ndarray:
"""Fractional factorial design from generators such as 'a b c abc'. Single letters are the basic factors."""
terms = generators.split()
basic = ''.join(t for t in terms if len(t) == 1)
if not basic:
raise ValueError(f"generators must contain at least one single-letter basic factor; got {generators!r}")
if len(set(basic)) != len(basic):
raise ValueError(f"basic factors must be distinct; got {basic!r}")
base = two_level_factorial(len(basic))
columns = []
for term in terms:
unknown = set(term) - set(basic)
if unknown:
raise ValueError(f"term {term!r} uses factors {sorted(unknown)} that are not basic factors")
columns.append(np.prod(base[:, [basic.index(c) for c in term]], axis=1))
return np.column_stack(columns)
print(fractional_factorial('a b c abc'))[[-1. -1. -1. -1.]
[ 1. -1. -1. 1.]
[-1. 1. -1. 1.]
[ 1. 1. -1. -1.]
[-1. -1. 1. 1.]
[ 1. -1. 1. -1.]
[-1. 1. 1. -1.]
[ 1. 1. 1. 1.]]
Computes the resolution of Table 3 as the shortest word in the subgroup generated by the words the generators imply.
# Python
def design_resolution(generators: str) -> int:
"""Resolution of a fractional factorial design: the length of the shortest word in the defining relation."""
terms = generators.split()
basic = ''.join(t for t in terms if len(t) == 1)
names = [chr(ord('A') + i) for i in range(len(terms))]
words = []
for name, term in zip(names, terms):
if len(term) == 1:
continue
words.append(frozenset([names[basic.index(c)] for c in term] + [name]))
if not words:
return len(terms) + 1 # a full factorial has no defining relation, so no effect is confounded
subgroup = set()
for size in range(1, len(words) + 1):
for combination in itertools.combinations(words, size):
word = frozenset()
for w in combination:
word = word.symmetric_difference(w)
if word:
subgroup.add(word)
return min(len(w) for w in subgroup)
for spec in ['a b ab', 'a b c ab', 'a b c abc', 'a b c d abcd']:
print(f'{spec:16s} runs={len(fractional_factorial(spec)):2d} resolution={design_resolution(spec)}')a b ab runs= 4 resolution=3
a b c ab runs= 8 resolution=3
a b c abc runs= 8 resolution=4
a b c d abcd runs=16 resolution=5
The three variants of section 7.2, chosen by kind. An invalid kind raises instead of falling back on a default, so a
misspelled variant cannot produce a design the caller did not ask for.
# Python
def central_composite(n_factors: int, n_center: int = 1, kind: str = 'circumscribed') -> np.ndarray:
"""Central composite design. kind is 'circumscribed', 'inscribed' or 'faced'."""
if n_factors < 2:
raise ValueError(f"a central composite design needs at least 2 factors; got {n_factors}")
if n_center < 1:
raise ValueError(f"n_center must be at least 1; got {n_center}")
if kind not in ('circumscribed', 'inscribed', 'faced'):
raise ValueError(f"kind must be 'circumscribed', 'inscribed' or 'faced'; got {kind!r}")
cube = two_level_factorial(n_factors)
alpha = 1.0 if kind == 'faced' else (2.0 ** n_factors) ** 0.25
star = np.zeros((2 * n_factors, n_factors))
for i in range(n_factors):
star[2 * i, i] = alpha
star[2 * i + 1, i] = -alpha
if kind == 'inscribed':
cube, star = cube / alpha, star / alpha
return np.vstack([cube, star, np.zeros((n_center, n_factors))])
print(central_composite(2, n_center=1))[[-1. -1. ]
[ 1. -1. ]
[-1. 1. ]
[ 1. 1. ]
[ 1.4142 0. ]
[-1.4142 0. ]
[ 0. 1.4142]
[ 0. -1.4142]
[ 0. 0. ]]
The pairwise construction of section 7.3, making the fifteen runs of Table 4 from three factors and three centre runs.
# Python
def box_behnken(n_factors: int, n_center: int = 3) -> np.ndarray:
"""Box-Behnken design: a two-level factorial in each pair of factors with the rest held at the centre."""
if n_factors < 3:
raise ValueError(f"a Box-Behnken design needs at least 3 factors; got {n_factors}")
if n_center < 1:
raise ValueError(f"n_center must be at least 1; got {n_center}")
block = two_level_factorial(2)
rows = []
for i, j in itertools.combinations(range(n_factors), 2):
edge = np.zeros((len(block), n_factors))
edge[:, [i, j]] = block
rows.append(edge)
return np.vstack(rows + [np.zeros((n_center, n_factors))])
print(box_behnken(3, n_center=3))[[-1. -1. 0.]
[ 1. -1. 0.]
[-1. 1. 0.]
[ 1. 1. 0.]
[-1. 0. -1.]
[ 1. 0. -1.]
[-1. 0. 1.]
[ 1. 0. 1.]
[ 0. -1. -1.]
[ 0. 1. -1.]
[ 0. -1. 1.]
[ 0. 1. 1.]
[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]]
Computes equation (3) and checks the scale with the D-efficiency of 1 that the two-level full factorial reaches for the linear model. Fewer runs than model terms raise an error.
# Python
def model_matrix(design: np.ndarray, model: str = 'linear') -> np.ndarray:
"""Model matrix with an intercept. model is 'linear', 'interaction' or 'quadratic'."""
if model not in ('linear', 'interaction', 'quadratic'):
raise ValueError(f"model must be 'linear', 'interaction' or 'quadratic'; got {model!r}")
design = np.atleast_2d(design)
n_runs, n_factors = design.shape
columns = [np.ones(n_runs), *design.T]
if model in ('interaction', 'quadratic'):
for i, j in itertools.combinations(range(n_factors), 2):
columns.append(design[:, i] * design[:, j])
if model == 'quadratic':
for i in range(n_factors):
columns.append(design[:, i] ** 2)
return np.column_stack(columns)
def d_efficiency(design: np.ndarray, model: str = 'linear') -> float:
"""D-efficiency, the normalised determinant of the information matrix. Larger is better; 1 is the maximum."""
x = model_matrix(design, model=model)
n_runs, n_terms = x.shape
if n_runs < n_terms:
raise ValueError(f"{n_runs} runs cannot fit {n_terms} model terms")
determinant = np.linalg.det(x.T @ x)
if determinant <= 0:
return 0.0
return determinant ** (1.0 / n_terms) / n_runs
print(round(d_efficiency(two_level_factorial(2), model='linear'), 4))1.0
The search of section 8.2, choosing twelve runs for a quadratic model in three factors. The first output line is the D-efficiency.
# Python
def coordinate_exchange(n_runs: int, n_factors: int, model: str = 'linear', n_levels: int = 3,
n_tries: int = 5, seed: int = None) -> np.ndarray:
"""D-optimal design by coordinate exchange over a grid of n_levels values spanning [-1, 1]."""
if n_levels < 2:
raise ValueError(f"n_levels must be at least 2; got {n_levels}")
rng = np.random.default_rng(seed)
grid = np.linspace(-1.0, 1.0, n_levels)
best, best_score = None, -np.inf
for _ in range(n_tries):
design = rng.choice(grid, size=(n_runs, n_factors))
score = d_efficiency(design, model=model)
improved = True
while improved:
improved = False
for run in range(n_runs):
for factor in range(n_factors):
current = design[run, factor]
for value in grid:
design[run, factor] = value
trial = d_efficiency(design, model=model)
if trial > score + 1e-12:
score, current, improved = trial, value, True
design[run, factor] = current
if score > best_score:
best, best_score = design.copy(), score
return best
design = coordinate_exchange(n_runs=12, n_factors=3, model='quadratic', n_levels=3, n_tries=5, seed=1)
print(round(d_efficiency(design, model='quadratic'), 4))
print(design)0.4498
[[ 1. 1. 1.]
[-1. -1. -1.]
[ 1. 1. -1.]
[-1. 1. 1.]
[-1. 1. -1.]
[-1. 0. 0.]
[ 0. 0. -1.]
[ 0. 1. 0.]
[ 1. -1. 1.]
[ 1. -1. -1.]
[ 1. 0. 1.]
[-1. -1. 1.]]
The search of section 8.3; rows passed in fixed are not exchanged. Two runs are added to the four corner runs.
# Python
def row_exchange(candidates: np.ndarray, n_runs: int, model: str = 'linear', n_tries: int = 5,
seed: int = None, fixed: np.ndarray = None) -> np.ndarray:
"""D-optimal design chosen from a candidate set by row exchange. Rows in fixed are kept and not exchanged."""
if n_runs < 1:
raise ValueError(f"n_runs must be at least 1; got {n_runs}")
rng = np.random.default_rng(seed)
fixed = np.empty((0, candidates.shape[1])) if fixed is None else np.atleast_2d(fixed)
best, best_score = None, -np.inf
for _ in range(n_tries):
chosen = candidates[rng.choice(len(candidates), size=n_runs, replace=True)]
score = d_efficiency(np.vstack([fixed, chosen]), model=model)
improved = True
while improved:
improved = False
for run in range(n_runs):
current = chosen[run].copy()
for row in candidates:
chosen[run] = row
trial = d_efficiency(np.vstack([fixed, chosen]), model=model)
if trial > score + 1e-12:
score, current, improved = trial, row.copy(), True
chosen[run] = current
if score > best_score:
best, best_score = chosen.copy(), score
return best
existing = two_level_factorial(2)
allowed = np.array(list(itertools.product([-1.0, 0.0, 1.0], repeat=2)))[:, ::-1]
added = row_exchange(allowed, n_runs=2, model='quadratic', n_tries=5, seed=3, fixed=existing)
print(added)
print(round(d_efficiency(np.vstack([existing, added]), model='quadratic'), 4))[[ 0. 1.]
[-1. 0.]]
0.42
The search of section 8.4; the last column is the evenly drifting covariate.
# Python
def covariate_exchange(covariates: np.ndarray, n_factors: int, model: str = 'linear', n_levels: int = 3,
n_tries: int = 5, seed: int = None) -> np.ndarray:
"""D-optimal design whose last columns are the given fixed covariates, one run per covariate row."""
covariates = np.atleast_2d(covariates)
rng = np.random.default_rng(seed)
grid = np.linspace(-1.0, 1.0, n_levels)
n_runs = len(covariates)
best, best_score = None, -np.inf
for _ in range(n_tries):
controlled = rng.choice(grid, size=(n_runs, n_factors))
score = d_efficiency(np.hstack([controlled, covariates]), model=model)
improved = True
while improved:
improved = False
for run in range(n_runs):
for factor in range(n_factors):
current = controlled[run, factor]
for value in grid:
controlled[run, factor] = value
trial = d_efficiency(np.hstack([controlled, covariates]), model=model)
if trial > score + 1e-12:
score, current, improved = trial, value, True
controlled[run, factor] = current
if score > best_score:
best, best_score = controlled.copy(), score
return np.hstack([best, covariates])
drift = np.linspace(-1.0, 1.0, 8).reshape(-1, 1)
print(covariate_exchange(drift, n_factors=2, model='linear', n_levels=3, n_tries=3, seed=4))[[-1. -1. -1. ]
[ 1. -1. -0.7143]
[ 1. 1. -0.4286]
[-1. 1. -0.1429]
[-1. 1. 0.1429]
[ 1. 1. 0.4286]
[ 1. -1. 0.7143]
[-1. -1. 1. ]]
The effect coding and row exchange of section 8.5, with the chosen runs mapped back to their levels and sorted.
# Python
def effect_code(levels: np.ndarray, n_levels: int) -> np.ndarray:
"""Effect coding of one categorical factor into n_levels - 1 columns."""
levels = np.asarray(levels, dtype=int)
if levels.min() < 0 or levels.max() >= n_levels:
raise ValueError(f"levels must lie in 0..{n_levels - 1}; got range {levels.min()}..{levels.max()}")
coded = np.zeros((len(levels), n_levels - 1))
for column in range(n_levels - 1):
coded[levels == column, column] = 1.0
coded[levels == n_levels - 1, :] = -1.0
return coded
levels = np.array(list(itertools.product(range(3), repeat=3)))[:, ::-1]
coded = np.hstack([effect_code(levels[:, k], 3) for k in range(3)])
selected = row_exchange(coded, n_runs=9, model='linear', n_tries=5, seed=7)
chosen = np.array([levels[int(np.where((coded == r).all(axis=1))[0][0])] for r in selected])
print(chosen[np.lexsort((chosen[:, 2], chosen[:, 1], chosen[:, 0]))])[[0 0 1]
[0 1 0]
[0 2 2]
[1 0 0]
[1 1 2]
[1 2 1]
[2 0 2]
[2 1 1]
[2 2 0]]
Builds the cubic grid of section 8.6 and chooses six runs for a quadratic model in two factors.
# Python
def candidate_set(n_factors: int, n_levels: int = 3) -> np.ndarray:
"""Candidate set: the full factorial of n_levels values spanning [-1, 1] in every factor."""
if n_levels < 2:
raise ValueError(f"n_levels must be at least 2; got {n_levels}")
grid = np.linspace(-1.0, 1.0, n_levels)
return np.array(list(itertools.product(grid, repeat=n_factors)))[:, ::-1]
selected = row_exchange(candidate_set(2, n_levels=3), n_runs=6, model='quadratic', n_tries=5, seed=2)
print(selected)[[ 1. 1.]
[-1. 1.]
[ 0. 1.]
[-1. -1.]
[ 1. 0.]
[ 1. -1.]]
Version 12 | Created: 2026-09-04 | Updated: 2026-11-07 13:46 UTC
- 1. Purpose
- 2. Summary
- 3. Taxonomy and its Hierarchy
- 4. Principle
- 5. Full Factorial Designs
- 6. Fractional Factorial Designs
- 7. Response Surface Designs
- 8. D-Optimal Designs
- References
- Appendix A. Terminology
- Appendix B. Python Implementation
- Problem Statement: 수동적으로 모은 자료에서는 여러 factor 가 반응의 같은 변화를 함께 설명하므로, 그 자료에 맞춘 model 이 factor 마다의 효과를 갈라내지 못한다.
- Goal: DOE User's Guide 를 만든다. 다 읽은 독자가 목표 model, run 예산, factor 공간의 제약에 맞는 design 을 고르고 그 design 의 가정과 한계를 말할 수 있어야 한다.
- Non-Goal: 실험을 마친 뒤의 분석 (ANOVA, model 적합과 검정) 과 특정 software 의 사용법은 다루지 않는다.
실험계획은 run 예산 안에서 계수 추정값의 공분산이 작아지도록 design matrix 의 행을 고르는 일이다. Design 은 목표 model 로 고른다. 모든 효과는 full factorial, 많은 factor 의 screening 은 fractional factorial, 곡률이 있는 최적화는 response surface design, 불규칙한 예산·제약·covariate 는 D-optimal design 이 맡는다.
Design 계열은 model 에 두는 가정으로 갈리며, 가정이 강할수록 run 이 적게 든다. 이 문서는 MathWorks Statistics and Machine Learning Toolbox 가 나누는 네 계열을 따른다 [1].
Design 을 가르는 다섯 축은 Fig 1 과 같다.
Axis Values
----------------- ----------------------------------------------------------------------
Target model all effects | main effects + some interactions | main effects only
| full quadratic | any stated model
Levels per factor 2 | 3 | any
Run count product of levels | power of 2 | multiple of 4 | set by geometry | any
Factor space regular cube | constrained region
Factor type continuous and set | categorical | recorded covariate
Fig 1. Axes of the design taxonomy
다섯 축은 서로 독립이다. 목표 model 이 같은 완전 이차 model 이어도 factor 공간이 정육면체이면 response surface design 을 쓰고, 제약이 있는 영역이면 D-optimal design 을 쓴다.
각 design 이 model 에 두는 가정과 그 대가는 Fig 2 의 계층을 따른다.
Family / design Assumes Gives up
----------------------- ----------------------------------- ---------------------------------------
Full factorial nothing about the model small run count: N1 x ... x Nk runs
+- General fractional high-order interactions are small separation of confounded effects
+- Plackett-Burman only main effects matter every interaction (resolution III)
Response surface response is at most quadratic terms above second order
+- Central composite settings at +/-alpha are reachable fewest runs: embeds a full 2^k
+- Box-Behnken three or more factors corner runs and an embedded factorial
D-optimal the stated model is correct regular geometry and orthogonality
Fig 2. Hierarchy of design families
Full factorial 에서 한 단계 내려갈 때마다 model 에 두는 가정이 늘고 run 수가 준다. General fractional design 은 고차 교호작용을, Plackett-Burman design 은 모든 교호작용을 작다고 보는 대가로 run 을 줄인다. Response surface 계열은 model 의 차수를 이차로 묶고 세 번째 수준을 더해 곡률을 추정한다. D-optimal design 은 명시한 model 하나에 맞춰 run 을 고르므로 정육면체 모양의 factor 공간도, 정해진 run 수도 요구하지 않는다.
Design 을 고르는 기준은 Table 1 이다.
Table 1. Comparison of designs
| Design | Model aimed at | Run count | Use when |
|---|---|---|---|
| Full factorial | 모든 효과와 교호작용 | factor 가 적고 모든 조합을 감당할 수 있다 | |
| General fractional | 주효과와 일부 교호작용 | 많은 factor 를 screening 하며 resolution 을 고른다 | |
| Plackett-Burman | 주효과 |
|
주효과만 보는 screening 에 2 의 거듭제곱 사이 run 수가 필요하다 |
| Central composite | 완전 이차 model | 알려진 작업점 근처에서 최적화한다 | |
| Box-Behnken | 완전 이차 model | 모든 factor 를 동시에 극단에 둘 수 없는 공정을 최적화한다 | |
| D-optimal | 명시한 임의의 model | 예산에 맞춰 지정 | 예산이 불규칙하거나 제약, covariate, categorical factor 가 있다 |
Design 은 반응을 재기 전에 계수 추정의 정밀도를 정한다. 측정한
식 (1) 의 공분산에는
이 문서에서 두 수준 factor 는
Full factorial design 은 factor 수준의 모든 조합에서 반응을 재어, factor 들이 만들 수 있는 모든 효과와 교호작용을
추정한다. 수준이
- 가정: factor 와 수준이 적어 모든 조합의 run 을 감당할 수 있다.
-
설정값: factor 마다의 수준 수
$N_i$ . -
깨지는 조건: factor 가 늘면 run 수가 수준 수의 곱으로 늘고, 두 수준이어도
$2^k$ 이 예산을 넘는다. - 쓰는 곳: factor 가 적은 실험, 그리고 fractional 계열과 response surface 계열의 출발점.
Factor 마다 수준 수가 달라도 된다. Design 은 수준 집합들의 곱집합이며, 첫 열이 가장 빨리 변하도록 나열한다. 두 수준 factor 하나와 세 수준 factor 하나는 run 6 개를 준다 (Appendix B.2).
모든 factor 가 두 수준이면 run 은
Fractional factorial design 은 treatment 의 부분집합만 재되, 중요하다고 보는 효과는 추정할 수 있도록 부분집합을
고른다. Full factorial 의
Run 을 줄인 대가는 confounding 이다. 남은 각 열은 여러 효과의 합을 지니며, 그 design 으로 한 실험은 그 효과들을 갈라내지 못한다. Resolution 은 confounding 의 정도를 나타내며, defining relation 에서 가장 짧은 word 의 길이이다.
Table 2. Design resolution [2]
| Resolution | Main effects confounded with | Two-way interactions confounded with |
|---|---|---|
| III | 2차 교호작용 | 서로 |
| IV | 3차 교호작용 | 서로 |
| V | 4차 교호작용 | 3차 교호작용 |
Resolution III 은 2차 교호작용이 작아야 주효과를 믿을 수 있고, IV 는 주효과를 2차 교호작용과 갈라 추정하며, V 는 주효과와 2차 교호작용을 모두 다른 2차 이하 효과와 갈라 추정한다.
Plackett-Burman design 은 주효과만 유의하다고 볼 때 run 수가 2 의 거듭제곱이 아니라 4 의 배수인 resolution III design 을 주어, 두 수준 factorial 사이의 빈틈을 메운다 [3]. Factor 11 개를 run 16 개가 아니라 12 개로 screening 한다.
Design 은 Hadamard matrix 로 만든다. Run 수가 2 의 거듭제곱이면 Hadamard matrix 의 열을 그대로 쓰고, 아니면
알려진 generator 행의 순환행렬 아래에
- 가정: 교호작용은 무시할 만하고 주효과만 유의하다.
-
설정값: factor 수
$k$ . Run 수는$k$ 보다 큰 가장 작은 4 의 배수이며, 그 수가 2 의 거듭제곱이 아니면 generator 행이 있어야 한다. - 깨지는 조건: 2차 교호작용이 크면 주효과와 confound 되어, 주효과 추정값이 그 교호작용을 함께 지닌다.
- 쓰는 곳: 많은 factor 의 screening.
General fractional design 은 basic factor 들로 이루어진 full factorial 에서 출발해, 나머지 factor 를 basic factor 의 곱으로 정의한다.
Definition: Generator
한 factor 를 basic factor 의 곱으로 정의할 때, 그 곱을 그 factor 의 generator 라 한다.
Generator 가 design 과 그 confounding 을 함께 정한다. Generator a b c abc 는 factor 4 개를 full factorial 이 요구하는 16 개의 절반인 run 8 개로
다룬다.
Defining relation 은 generator 가 함의하는 word 들이 생성하며, resolution 은 그 word 들이 생성하는 부분군에서 가장 짧은 word 의 길이이다. 네 generator 집합의 resolution 은 Table 3 이다.
Table 3. Resolution of four generator sets
| Generators | Runs | Resolution |
|---|---|---|
| a b ab | 4 | III |
| a b c ab | 8 | III |
| a b c abc | 8 | IV |
| a b c d abcd | 16 | V |
Run 8 개짜리 두 design 은 비용이 같고 generator 만 다르지만, a b c ab 는 주효과를 2차 교호작용과 confound
시키고 a b c abc 는 그러지 않는다. 같은 run 수에서 resolution 은 generator 가 정한다.
- 가정: design 의 resolution 이 confound 시키는 교호작용은 작다 (Table 2).
-
설정값: basic factor 수
$b$ (run 수$2^{b}$ ) 와 더해지는 factor 마다의 generator. - 깨지는 조건: confound 된 교호작용이 크면 그 열의 추정값은 여러 효과의 합이 되어, 어느 효과인지 가릴 수 없다.
- 쓰는 곳: 많은 factor 의 screening 에서 run 수를 2 의 거듭제곱으로 두고 resolution 을 골라야 할 때.
Response surface design 은 각 factor 에 세 번째 수준을 더해, 곡률을 지닌 완전 이차 model 을 맞춘다. 중요한 factor 를 알고 목표가 최적화이면 model 에 곡률이 있어야 한다. 최적점은 정류점이고, 일차 model 에는 정류점이 없다.
Factor 가
두 수준 design 으로는 식 (2) 를 맞출 수 없다. 제곱항
Central composite design 은 정육면체 꼭짓점의 두 수준 factorial, factor 축 위에서 중심으로부터 거리
세 변형은 star point 를 두는 위치에서 다르다.
-
Circumscribed: star point 를 정육면체 바깥
$\pm\alpha$ 에 두므로, 두 수준 범위를 넘는 factor 설정이 필요하다. -
Faced:
$\alpha = 1$ 로 하여 star point 를 정육면체의 면 위에 둔다. -
Inscribed: circumscribed 의 모양을 유지하되, 꼭짓점이 아니라 star point 가
$\pm 1$ 에 오도록 척도를 줄인다.
Factor 2 개와 centre run 1 개의 circumscribed design 은 run 9 개이며, star point 는
- 가정: 작업점 근처에서 반응이 완전 이차 model 로 근사되고, factor 가 2 개 이상이다.
-
설정값:
$\alpha$ (rotatable 이면$(2^k)^{1/4}$ , faced 이면 1), 변형, centre run 수$n_c \ge 1$ . Run 수는$2^k + 2k + n_c$ . -
깨지는 조건: 공정이
$\pm\alpha$ 설정을 낼 수 없으면 circumscribed design 을 쓸 수 없어, faced 나 inscribed 로 바꾼다. - 쓰는 곳: 알려진 작업점 근처에서의 최적화.
Box-Behnken design 은 완전 이차 model 을 맞추면서 두 factor 를 동시에 극단에 두지 않는다 [4]. Factor 짝마다 두 수준 factorial 을 두고 나머지 factor 는 중심에 두므로, 점들이 design 공간의 모서리 중점과 중심에 놓인다. 정육면체의 꼭짓점이 빠져 모든 factor 의 극단을 묶은 run 이 없고, 안에 박힌 factorial design 도 없다.
Table 4. Run counts of the two response surface designs, three centre runs each
| Factors | Quadratic terms | Box-Behnken | Central composite |
|---|---|---|---|
| 3 | 10 | 15 | 17 |
| 4 | 15 | 27 | 27 |
| 5 | 21 | 43 | 45 |
짝 단위 구성은 factor 3 개에서 5 개까지 발표된 Box-Behnken design 을 그대로 재현한다. 5 개를 넘으면 발표된 design 은 balanced incomplete block design 을 써서 모든 짝을 차례로 도는 구성보다 작다. 짝 단위 구성은 그때도 타당하지만 최소가 아니다.
- 가정: 반응이 완전 이차 model 로 근사되고, factor 가 3 개 이상이다.
-
설정값: centre run 수
$n_c$ (Appendix B 구현의 기본값 3). Run 수는$4\binom{k}{2} + n_c$ . - 깨지는 조건: factor 가 5 개를 넘으면 짝 단위 구성은 최소 run 수를 주지 못한다.
- 쓰는 곳: 모든 factor 를 동시에 극단에 둘 수 없는 공정의 최적화.
D-optimal design 은 model 과 run 예산을 주어진 것으로 받고, 정보행렬
- 가정: 명시한 model 이 반응을 맞게 기술한다. Design 은 그 model 의 계수에 대해서만 최적이다.
- 설정값: model (linear, interaction, quadratic), run 수, 연속 factor 의 grid 수준 수, 무작위 출발점 수, candidate set.
- 깨지는 조건: run 수가 model 항 수보다 적으면 model 을 추정할 수 없다. 출발점이 적으면 탐색이 최적에 못 미치는 design 에 머물 수 있다.
- 쓰는 곳: 불규칙한 run 예산, 단계 실험의 augmentation, 기록만 하는 covariate, categorical factor, 제약이 있는 factor 공간.
D-efficiency 는 정보행렬의 행렬식을 정규화하여 크기가 다른 design 끼리 견주게 한다. 식 (3) 에서
두 수준 full factorial 은 linear model 에서 D-efficiency 1 에 이른다. 직교 design 이 있으면 D-optimal 탐색은 그보다 나은 design 을 찾지 못한다.
D-optimal design 은 반복 탐색으로 얻는다. Coordinate-exchange algorithm 은 무작위 design 에서 출발해, run 하나의 factor 하나를 골라 grid 위의 모든 값을 넣어 보고 가장 좋은 값을 남기는 이동을 더 나아지지 않을 때까지 되풀이한다 [5]. 결과가 출발점에 따라 다르므로, 무작위 출발점 여러 개에서 탐색을 되풀이하고 가장 좋은 design 을 남긴다.
Factor 3 개의 quadratic model 에 run 12 개를 요청하면 D-efficiency 0.4498 인 design 이 나온다 (Appendix B.5). Table 4 의 가장 작은 Box-Behnken design 보다 run 이 3 개 적고, model 의 계수 10 개보다 2 개 많다.
실험은 흔히 단계를 나누어 진행하며, 이미 수행한 run 은 다시 고를 수 없다. Augmentation 은 수행한 run 을 고정한 채 새 run 만 탐색하므로, 기존 design 에 없는 정보를 채우는 run 을 고른다. 이 탐색은 좌표 하나가 아니라 run 전체를 candidate set 의 행과 맞바꾸는 row exchange 를 쓰며, run 을 고정된 목록에서 뽑을 때는 행 단위 교환이 알맞은 이동이다.
꼭짓점 run 4 개만으로는 항이 6 개인 quadratic model 을 맞출 수 없다. Run 2 개를 더하면 추정할 수 있게 되며,
탐색은 두 run 을 모두 기존 design 에 없는 꼭짓점 밖의 점
Covariate 는 실험자가 정하지 않고 기록만 하는 factor 이며, 주위 온도, 근무 중인 작업자, batch 의 경과 시간이 그 예이다. Covariate 값은 run 마다 미리 알려져 있지만 고를 수 없다. 이때 design 문제는 covariate 열이 주어진 상태에서 통제 가능한 factor 를 골라, model 이 통제 효과와 covariate 효과를 갈라내게 하는 일이 된다.
Run 8 개에 걸쳐 covariate 가
Categorical factor 에는 numeric 척도가 없어 수준을
수준이 3 개인 categorical factor 3 개는 후보 treatment 27 개를 준다. Run 9 개를 요청하면, 탐색은 모든 factor 의 모든 수준이 세 번씩 나타나고 두 factor 의 수준 짝이 모두 정확히 한 번씩 나타나는 design 을 돌려준다.
Row exchange 는 고를 수 있는 run 의 목록인 candidate set 을 받는다. Factor 공간이 정육면체이면 candidate set 은 그 위의 grid 이다.
Candidate set 을 명시적으로 건네면 정육면체가 아닌 factor 공간도 다룰 수 있다. 성분의 합이 1 이어야 하는 mixture, 함께 높을 수 없는 두 설정, 차가운 상태로 빠르게 돌 수 없는 기계는 모두 grid 에서 행을 덜어내는 제약이다. 탐색은 목록에 없는 run 을 제안하지 않으므로, candidate set 에서 그 행들을 지우면 제약이 design 에 반영된다.
[1] MathWorks. Design of Experiments — Statistics and Machine Learning Toolbox Documentation.
[2] Montgomery, D. C. (2019). Design and Analysis of Experiments (10th ed.). Wiley.
ISBN 978-1-119-49249-8.
[3] Plackett, R. L., & Burman, J. P. (1946). The Design of Optimum Multifactorial Experiments.
Biometrika, 33(4), 305–325.
[4] Box, G. E. P., & Behnken, D. W. (1960). Some New Three Level Designs for the Study of
Quantitative Variables. Technometrics, 2(4), 455–475.
[5] Meyer, R. K., & Nachtsheim, C. J. (1995). The Coordinate-Exchange Algorithm for Constructing
Exact Optimal Experimental Designs. Technometrics, 37(1), 60–69.
- Basic factor: generator 가 아니라 fractional design 이 출발점으로 삼는 full factorial 에서 열이 나오는 factor.
- Candidate set: D-optimal 탐색이 run 을 고를 수 있는 factor 설정의 목록.
- Centre run: 모든 연속 factor 가 자기 범위의 가운데에 있는 run.
- Confounding: 두 효과가 같은 design 열에 실려, 그 실험의 어떤 분석으로도 갈라낼 수 없는 상태.
- Covariate: 실험자가 정하지 않고 run 마다 기록하는 factor.
- Defining relation: fractional factorial design 에서 1 로 채워진 열과 같아지는 factor 열들의 곱의 모임이며, 그중 가장 짧은 word 가 resolution 을 준다.
-
Effect coding: 수준이
$L$ 개인 categorical factor 를$L - 1$ 개의 열로 바꾸되, 마지막 수준을 모든 열에서$-1$ 로 두는 부호화. - Factor: 실험자가 값을 정하거나 기록하는 입력.
- Generator: fractional factorial design 에서 더해지는 factor 를 정의하는 basic factor 들의 곱.
-
Hadamard matrix: 원소가
$\pm 1$ 이고 열들이 서로 직교하는 정사각행렬. - Interaction: 한 factor 의 효과가 다른 factor 의 수준에 따라 달라지는 정도.
- Level: 한 design 에서 factor 가 갖는 값 가운데 하나.
- Main effect: 한 factor 의 수준을 바꿀 때 다른 factor 에 대해 평균한 반응의 변화.
- Response: run 의 측정된 출력.
- Rotatable: 예측분산이 factor 공간 중심으로부터의 거리에만 의존하는 design.
- Run: factor 수준의 한 조합에서 실험을 한 번 수행하는 것이며, design 의 한 행.
- Screening: 많은 factor 가운데 반응에 유의한 factor 를 가려내는 실험.
- Star point: central composite design 에서 factor 하나를 중심 밖으로 옮기고 나머지는 중심에 두는 run.
- Treatment: factor 수준의 한 조합.
- Word: defining relation 에서 곱이 1 로 채워진 열이 되는 factor 이름들의 묶음이며, 그 길이는 묶인 factor 수이다.
이 Appendix 의 Python block 은 순서대로 실행하도록 되어 있다. 각 block 은 B.1 의 header 위에서 앞선 block 이 정의한
이름을 쓰며, 바로 뒤의 text block 은 그 출력이다. 실행에는 numpy 와 scipy 가 필요하다.
# Python
import itertools
from typing import Sequence
import numpy as np
from scipy.linalg import hadamard
np.set_printoptions(linewidth=100, suppress=True, precision=4)Section 5.1 의 곱집합 구성이며, 두 수준 factor 와 세 수준 factor 로 run 6 개를 만든다.
# Python
def full_factorial(levels: Sequence[int]) -> np.ndarray:
"""Full factorial design. Column i takes levels 0..levels[i]-1; the first column varies fastest."""
levels = list(levels)
if any(v < 2 for v in levels):
raise ValueError(f"every factor needs at least 2 levels; got {levels}")
grids = np.meshgrid(*[np.arange(v) for v in levels], indexing='ij')
return np.column_stack([g.ravel(order='F') for g in grids])
print(full_factorial([2, 3]))[[0 0]
[1 0]
[0 1]
[1 1]
[0 2]
[1 2]]
Section 5.2 의
# Python
def two_level_factorial(n_factors: int) -> np.ndarray:
"""Two-level full factorial design coded as -1 and +1."""
if n_factors < 1:
raise ValueError(f"n_factors must be at least 1; got {n_factors}")
return 2.0 * full_factorial([2] * n_factors) - 1.0
print(two_level_factorial(3))[[-1. -1. -1.]
[ 1. -1. -1.]
[-1. 1. -1.]
[ 1. 1. -1.]
[-1. -1. 1.]
[ 1. -1. 1.]
[-1. 1. 1.]
[ 1. 1. 1.]]
Section 6.2 의 구성이다. 마지막 줄은 run 12 개 design 의 열 11 개가 서로 직교함을 확인한다. Run 수가 2 의 거듭제곱도 아니고 generator 도 없으면, 다른 design 으로 바꾸지 않고 오류를 낸다.
# Python
PB_GENERATORS = {
12: '++-+++---+-',
20: '++--++++-+-+----++-',
24: '+++++-+-++--++--+-+----',
}
def plackett_burman(n_factors: int) -> np.ndarray:
"""Plackett-Burman design with the smallest run count that is a multiple of 4 and exceeds n_factors."""
if n_factors < 1:
raise ValueError(f"n_factors must be at least 1; got {n_factors}")
n_runs = 4 * int(np.ceil((n_factors + 1) / 4))
if n_runs & (n_runs - 1) == 0:
design = hadamard(n_runs)[:, 1:] * 1.0
elif n_runs in PB_GENERATORS:
row = np.array([1.0 if c == '+' else -1.0 for c in PB_GENERATORS[n_runs]])
design = np.vstack([np.roll(row, k) for k in range(n_runs - 1)] + [-np.ones(n_runs - 1)])
else:
raise ValueError(f"no Plackett-Burman construction available for {n_runs} runs")
return design[:, :n_factors]
design = plackett_burman(11)
print(design.shape, np.allclose(design.T @ design, 12 * np.eye(11)))(12, 11) True
Section 6.3 의 generator 구성이며, generator a b c abc 로 factor 4 개의 run 8 개 design 을 만든다.
# Python
def fractional_factorial(generators: str) -> np.ndarray:
"""Fractional factorial design from generators such as 'a b c abc'. Single letters are the basic factors."""
terms = generators.split()
basic = ''.join(t for t in terms if len(t) == 1)
if not basic:
raise ValueError(f"generators must contain at least one single-letter basic factor; got {generators!r}")
if len(set(basic)) != len(basic):
raise ValueError(f"basic factors must be distinct; got {basic!r}")
base = two_level_factorial(len(basic))
columns = []
for term in terms:
unknown = set(term) - set(basic)
if unknown:
raise ValueError(f"term {term!r} uses factors {sorted(unknown)} that are not basic factors")
columns.append(np.prod(base[:, [basic.index(c) for c in term]], axis=1))
return np.column_stack(columns)
print(fractional_factorial('a b c abc'))[[-1. -1. -1. -1.]
[ 1. -1. -1. 1.]
[-1. 1. -1. 1.]
[ 1. 1. -1. -1.]
[-1. -1. 1. 1.]
[ 1. -1. 1. -1.]
[-1. 1. 1. -1.]
[ 1. 1. 1. 1.]]
Generator 가 함의하는 word 들이 생성하는 부분군에서 가장 짧은 word 를 찾아 Table 3 의 resolution 을 계산한다.
# Python
def design_resolution(generators: str) -> int:
"""Resolution of a fractional factorial design: the length of the shortest word in the defining relation."""
terms = generators.split()
basic = ''.join(t for t in terms if len(t) == 1)
names = [chr(ord('A') + i) for i in range(len(terms))]
words = []
for name, term in zip(names, terms):
if len(term) == 1:
continue
words.append(frozenset([names[basic.index(c)] for c in term] + [name]))
if not words:
return len(terms) + 1 # a full factorial has no defining relation, so no effect is confounded
subgroup = set()
for size in range(1, len(words) + 1):
for combination in itertools.combinations(words, size):
word = frozenset()
for w in combination:
word = word.symmetric_difference(w)
if word:
subgroup.add(word)
return min(len(w) for w in subgroup)
for spec in ['a b ab', 'a b c ab', 'a b c abc', 'a b c d abcd']:
print(f'{spec:16s} runs={len(fractional_factorial(spec)):2d} resolution={design_resolution(spec)}')a b ab runs= 4 resolution=3
a b c ab runs= 8 resolution=3
a b c abc runs= 8 resolution=4
a b c d abcd runs=16 resolution=5
Section 7.2 의 세 변형을 kind 로 고른다. 잘못된 kind 는 기본값으로 바꾸지 않고 오류를 내므로, 철자를 틀린
변형이 호출자가 요청하지 않은 design 을 만들지 않는다.
# Python
def central_composite(n_factors: int, n_center: int = 1, kind: str = 'circumscribed') -> np.ndarray:
"""Central composite design. kind is 'circumscribed', 'inscribed' or 'faced'."""
if n_factors < 2:
raise ValueError(f"a central composite design needs at least 2 factors; got {n_factors}")
if n_center < 1:
raise ValueError(f"n_center must be at least 1; got {n_center}")
if kind not in ('circumscribed', 'inscribed', 'faced'):
raise ValueError(f"kind must be 'circumscribed', 'inscribed' or 'faced'; got {kind!r}")
cube = two_level_factorial(n_factors)
alpha = 1.0 if kind == 'faced' else (2.0 ** n_factors) ** 0.25
star = np.zeros((2 * n_factors, n_factors))
for i in range(n_factors):
star[2 * i, i] = alpha
star[2 * i + 1, i] = -alpha
if kind == 'inscribed':
cube, star = cube / alpha, star / alpha
return np.vstack([cube, star, np.zeros((n_center, n_factors))])
print(central_composite(2, n_center=1))[[-1. -1. ]
[ 1. -1. ]
[-1. 1. ]
[ 1. 1. ]
[ 1.4142 0. ]
[-1.4142 0. ]
[ 0. 1.4142]
[ 0. -1.4142]
[ 0. 0. ]]
Section 7.3 의 짝 단위 구성이며, factor 3 개와 centre run 3 개로 Table 4 의 run 15 개를 만든다.
# Python
def box_behnken(n_factors: int, n_center: int = 3) -> np.ndarray:
"""Box-Behnken design: a two-level factorial in each pair of factors with the rest held at the centre."""
if n_factors < 3:
raise ValueError(f"a Box-Behnken design needs at least 3 factors; got {n_factors}")
if n_center < 1:
raise ValueError(f"n_center must be at least 1; got {n_center}")
block = two_level_factorial(2)
rows = []
for i, j in itertools.combinations(range(n_factors), 2):
edge = np.zeros((len(block), n_factors))
edge[:, [i, j]] = block
rows.append(edge)
return np.vstack(rows + [np.zeros((n_center, n_factors))])
print(box_behnken(3, n_center=3))[[-1. -1. 0.]
[ 1. -1. 0.]
[-1. 1. 0.]
[ 1. 1. 0.]
[-1. 0. -1.]
[ 1. 0. -1.]
[-1. 0. 1.]
[ 1. 0. 1.]
[ 0. -1. -1.]
[ 0. 1. -1.]
[ 0. -1. 1.]
[ 0. 1. 1.]
[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]]
식 (3) 을 계산하며, 두 수준 full factorial 의 linear model D-efficiency 1 로 척도를 확인한다. Run 수가 model 항 수보다 적으면 오류를 낸다.
# Python
def model_matrix(design: np.ndarray, model: str = 'linear') -> np.ndarray:
"""Model matrix with an intercept. model is 'linear', 'interaction' or 'quadratic'."""
if model not in ('linear', 'interaction', 'quadratic'):
raise ValueError(f"model must be 'linear', 'interaction' or 'quadratic'; got {model!r}")
design = np.atleast_2d(design)
n_runs, n_factors = design.shape
columns = [np.ones(n_runs), *design.T]
if model in ('interaction', 'quadratic'):
for i, j in itertools.combinations(range(n_factors), 2):
columns.append(design[:, i] * design[:, j])
if model == 'quadratic':
for i in range(n_factors):
columns.append(design[:, i] ** 2)
return np.column_stack(columns)
def d_efficiency(design: np.ndarray, model: str = 'linear') -> float:
"""D-efficiency, the normalised determinant of the information matrix. Larger is better; 1 is the maximum."""
x = model_matrix(design, model=model)
n_runs, n_terms = x.shape
if n_runs < n_terms:
raise ValueError(f"{n_runs} runs cannot fit {n_terms} model terms")
determinant = np.linalg.det(x.T @ x)
if determinant <= 0:
return 0.0
return determinant ** (1.0 / n_terms) / n_runs
print(round(d_efficiency(two_level_factorial(2), model='linear'), 4))1.0
Section 8.2 의 탐색이며, factor 3 개의 quadratic model 에 run 12 개를 고른다. 출력의 첫 줄이 D-efficiency 이다.
# Python
def coordinate_exchange(n_runs: int, n_factors: int, model: str = 'linear', n_levels: int = 3,
n_tries: int = 5, seed: int = None) -> np.ndarray:
"""D-optimal design by coordinate exchange over a grid of n_levels values spanning [-1, 1]."""
if n_levels < 2:
raise ValueError(f"n_levels must be at least 2; got {n_levels}")
rng = np.random.default_rng(seed)
grid = np.linspace(-1.0, 1.0, n_levels)
best, best_score = None, -np.inf
for _ in range(n_tries):
design = rng.choice(grid, size=(n_runs, n_factors))
score = d_efficiency(design, model=model)
improved = True
while improved:
improved = False
for run in range(n_runs):
for factor in range(n_factors):
current = design[run, factor]
for value in grid:
design[run, factor] = value
trial = d_efficiency(design, model=model)
if trial > score + 1e-12:
score, current, improved = trial, value, True
design[run, factor] = current
if score > best_score:
best, best_score = design.copy(), score
return best
design = coordinate_exchange(n_runs=12, n_factors=3, model='quadratic', n_levels=3, n_tries=5, seed=1)
print(round(d_efficiency(design, model='quadratic'), 4))
print(design)0.4498
[[ 1. 1. 1.]
[-1. -1. -1.]
[ 1. 1. -1.]
[-1. 1. 1.]
[-1. 1. -1.]
[-1. 0. 0.]
[ 0. 0. -1.]
[ 0. 1. 0.]
[ 1. -1. 1.]
[ 1. -1. -1.]
[ 1. 0. 1.]
[-1. -1. 1.]]
Section 8.3 의 탐색이며, fixed 에 넣은 행은 바꾸지 않는다. 꼭짓점 run 4 개에 run 2 개를 더한다.
# Python
def row_exchange(candidates: np.ndarray, n_runs: int, model: str = 'linear', n_tries: int = 5,
seed: int = None, fixed: np.ndarray = None) -> np.ndarray:
"""D-optimal design chosen from a candidate set by row exchange. Rows in fixed are kept and not exchanged."""
if n_runs < 1:
raise ValueError(f"n_runs must be at least 1; got {n_runs}")
rng = np.random.default_rng(seed)
fixed = np.empty((0, candidates.shape[1])) if fixed is None else np.atleast_2d(fixed)
best, best_score = None, -np.inf
for _ in range(n_tries):
chosen = candidates[rng.choice(len(candidates), size=n_runs, replace=True)]
score = d_efficiency(np.vstack([fixed, chosen]), model=model)
improved = True
while improved:
improved = False
for run in range(n_runs):
current = chosen[run].copy()
for row in candidates:
chosen[run] = row
trial = d_efficiency(np.vstack([fixed, chosen]), model=model)
if trial > score + 1e-12:
score, current, improved = trial, row.copy(), True
chosen[run] = current
if score > best_score:
best, best_score = chosen.copy(), score
return best
existing = two_level_factorial(2)
allowed = np.array(list(itertools.product([-1.0, 0.0, 1.0], repeat=2)))[:, ::-1]
added = row_exchange(allowed, n_runs=2, model='quadratic', n_tries=5, seed=3, fixed=existing)
print(added)
print(round(d_efficiency(np.vstack([existing, added]), model='quadratic'), 4))[[ 0. 1.]
[-1. 0.]]
0.42
Section 8.4 의 탐색이며, 마지막 열이 고르게 변하는 covariate 이다.
# Python
def covariate_exchange(covariates: np.ndarray, n_factors: int, model: str = 'linear', n_levels: int = 3,
n_tries: int = 5, seed: int = None) -> np.ndarray:
"""D-optimal design whose last columns are the given fixed covariates, one run per covariate row."""
covariates = np.atleast_2d(covariates)
rng = np.random.default_rng(seed)
grid = np.linspace(-1.0, 1.0, n_levels)
n_runs = len(covariates)
best, best_score = None, -np.inf
for _ in range(n_tries):
controlled = rng.choice(grid, size=(n_runs, n_factors))
score = d_efficiency(np.hstack([controlled, covariates]), model=model)
improved = True
while improved:
improved = False
for run in range(n_runs):
for factor in range(n_factors):
current = controlled[run, factor]
for value in grid:
controlled[run, factor] = value
trial = d_efficiency(np.hstack([controlled, covariates]), model=model)
if trial > score + 1e-12:
score, current, improved = trial, value, True
controlled[run, factor] = current
if score > best_score:
best, best_score = controlled.copy(), score
return np.hstack([best, covariates])
drift = np.linspace(-1.0, 1.0, 8).reshape(-1, 1)
print(covariate_exchange(drift, n_factors=2, model='linear', n_levels=3, n_tries=3, seed=4))[[-1. -1. -1. ]
[ 1. -1. -0.7143]
[ 1. 1. -0.4286]
[-1. 1. -0.1429]
[-1. 1. 0.1429]
[ 1. 1. 0.4286]
[ 1. -1. 0.7143]
[-1. -1. 1. ]]
Section 8.5 의 effect coding 과 row exchange 이며, 고른 run 을 원래 수준으로 되돌려 정렬해 보인다.
# Python
def effect_code(levels: np.ndarray, n_levels: int) -> np.ndarray:
"""Effect coding of one categorical factor into n_levels - 1 columns."""
levels = np.asarray(levels, dtype=int)
if levels.min() < 0 or levels.max() >= n_levels:
raise ValueError(f"levels must lie in 0..{n_levels - 1}; got range {levels.min()}..{levels.max()}")
coded = np.zeros((len(levels), n_levels - 1))
for column in range(n_levels - 1):
coded[levels == column, column] = 1.0
coded[levels == n_levels - 1, :] = -1.0
return coded
levels = np.array(list(itertools.product(range(3), repeat=3)))[:, ::-1]
coded = np.hstack([effect_code(levels[:, k], 3) for k in range(3)])
selected = row_exchange(coded, n_runs=9, model='linear', n_tries=5, seed=7)
chosen = np.array([levels[int(np.where((coded == r).all(axis=1))[0][0])] for r in selected])
print(chosen[np.lexsort((chosen[:, 2], chosen[:, 1], chosen[:, 0]))])[[0 0 1]
[0 1 0]
[0 2 2]
[1 0 0]
[1 1 2]
[1 2 1]
[2 0 2]
[2 1 1]
[2 2 0]]
Section 8.6 의 정육면체 grid 를 만들고, factor 2 개의 quadratic model 에 run 6 개를 고른다.
# Python
def candidate_set(n_factors: int, n_levels: int = 3) -> np.ndarray:
"""Candidate set: the full factorial of n_levels values spanning [-1, 1] in every factor."""
if n_levels < 2:
raise ValueError(f"n_levels must be at least 2; got {n_levels}")
grid = np.linspace(-1.0, 1.0, n_levels)
return np.array(list(itertools.product(grid, repeat=n_factors)))[:, ::-1]
selected = row_exchange(candidate_set(2, n_levels=3), n_runs=6, model='quadratic', n_tries=5, seed=2)
print(selected)[[ 1. 1.]
[-1. 1.]
[ 0. 1.]
[-1. -1.]
[ 1. 0.]
[ 1. -1.]]
