# Example of GWLP calculation for mixed-level designs

This notebook contains example code to compute the type-specific generalized word-length pattern (GWLP) for mixed-level designs, more specifically for regular four-and-two-level designs.

In four-and-two-level designs, the four-level factors are constructed us the grouping scheme of Wu & Zhang (1989) where the levels of a four-level factor $$A$$ are based on the levels of three two-level factors $$a_1$$, $$a_2$$ and $$a_3$$, called the pseudo-factors, where $$I=a_1a_2a_3$$:

$\begin{split}\begin{array}{ccccc} a_1 & a_2 & a_3 & & A \\ 1 & 1 & 0 & \rightarrow & 0 \\ 1 & 0 & 1 & \rightarrow & 1 \\ 0 & 1 & 1 & \rightarrow & 2 \\ 0 & 0 & 0 & \rightarrow & 3 \\ \end{array}\end{split}$

The two-level factors can either be main factors or be entirely aliased with a combination of the main factors. Such aliased factors are called added factors. If one of the main factors, used in an added factor, is also used as pseudo-factor in a four-level factor, then the added factor has type $$I$$. If it is used as pseudo-factor in two distinct four-level factor, it has type $$II$$, etc $$\ldots$$.

:

import numpy as np

import oapackage


Create a $$4^{1}2^{7}$$ four-and-two-level regular design in 32 runs with 1 four-level factor and 7 two-level factors.

:

array = oapackage.exampleArray(56, 1)
array = array.selectFirstColumns(7)
array.showarraycompact()

exampleArray 56: design in OA(32, 42^{18})
0000000
0100110
0101010
0001100
0110011
0010101
0011001
0111111
1010100
1110010
1111110
1011000
1100111
1000001
1001101
1101011
2101101
2001011
2000111
2100001
2011110
2111000
2110100
2010010
3111001
3011111
3010011
3110101
3001010
3101100
3100000
3000110


The GWLP for mixed-level designs is computed using the adapted forms of equations (7) and (8) in Xu and Wu (2001).

## Distance distribution

First, the distance distribution is first computed for all combinations of $$(i,j)$$ with $$i \in \{0,1\}$$ and $$j=0,\ldots,7$$ and $$i\neq j$$. In this example with a $$4^12^{7}$$ design, that is a $$2 \times 8$$ matrix $$B$$, where $$B_{i,j}$$ is the number of rows that have $$(1-i)$$ different elements if their four-level parts and $$(7-j)$$ different elements in their two-level parts, divided by the number of runs.

:

Dm = oapackage.distance_distribution_mixed(array, 0)
D = np.array(Dm).astype(int)
print(f"distance distribution for mixed-level design\n{D}")

distance distribution for mixed-level design
[[1 0 1 4 1 0 1]
[0 2 6 8 6 2 0]]


This is verified since the sum of the matrix is 32 which is equal to $$\binom{32}{2} 32^{-1}$$.

## McWilliams Transforms

Now, the second step in the computation of the GWLP, is to use the McWilliams Transforms to obtain the GWLP from the distance distribution matrix. The McWilliams Transform will create another $$2 \times 8$$ matrix $$B^{\prime}$$, obtained using the following formula

$B_{j_{1}, j_{2}}^{\prime}=N^{-1} \sum_{i_{1}=0}^{m} \sum_{i_{2}=0}^{n} B_{i_{1}, i_{2}} P_{j_{1}}\left(i_{1} ; 1, 4\right) P_{j_{2}}\left(i_{2} ; 7, 2\right)$

where $$P_j(x,n,s)$$ is the Krawtchouck polynomials for a total of $$n$$ factors with $$s$$ levels.

:

N = array.n_rows
factor_levels_for_groups = arrayclass.factor_levels_column_groups()
Bprime = oapackage.macwilliams_transform_mixed(Dm, N, factor_levels_for_groups, verbose=0)
print("MacWilliams transform:")
print(np.array(Bprime).astype(int))

MacWilliams transform:
[[1. 0. 0. 0. 1. 0. 0.]
[0. 0. 2. 0. 4. 0. 0.]]


This matrix is equivalent to the type specific generalized word-length pattern, where the row index indicates the type of words (0 to 1) and the column index indicates the length of the words (0 to 7).

The generalized word-length pattern ($$A$$) can be obtained by summing the rows of the $$B^{\prime}$$ matrix anti-diagonally. That is:

$A_j(D) = \sum_{i^{\prime}+j^{\prime}=j} B^{\prime}_{i^{\prime},j^{\prime}}$
:

gwlp_mixed = oapackage.GWLPmixed(array, 0)
print(f"GWLP: {gwlp_mixed}")

GWLP: (1.0, 0.0, 0.0, 2.0, 1.0, 4.0, 0.0, 0.0)