Monday, July 16, 2018

Mixed level factorial design algorithm

Assume you have a set of factors let's say [A,B,C] and each factor may be assigned different values (let's call these values "levels"). For the purpose of testing (using general full factorial design) you want to try all combinations of all factor levels with each other knowing that each factor may have number of levels different from the other factor. The following table will be used as an example:


Theoretically, to solve this problem, nested "For" loops will be needed where number of loops equals number of factors. This may be impractical to implement specially when number of factors is not constant.

The proposed algorithm simply mimics the mechanical analog counter (or more specifically Tally counter) like the one shown in the picture below but with a slight difference in implementation. In analog counter, the rightmost digit wheel is the driving wheel and when it increases above 9 it will reset back to 0 and the next digit one the left will increment by one. The same rule applies for all other digits: when the digit value increases above 9 it resets to 0 and increment the next digit on the left side by one.  

Reference: https://www.shutterstock.com/video/clip-4902245-analog-mechanical-counter-numbers-that-turned-quickly

In mechanical analog counter, all digits have an upper value of 9 and lower value of 0. On the other side, in this algorithm, each digit will have its own upper value which equals the number of levels while the lower value will be 1.

Let's assume that factor A will be represented by the digit wheel on the leftmost side and factor C will be on the rightmost side so that factor C will be the driving wheel.

By knowing the number of levels of each factor we can calculate the total number of combinations N by multiplying all factor levels (N=3*2*4=24 in this example).

Now, we will increment factor C for N times and after each increment we will do a check for all factors (digits), if any factor exceed its upper bound, it will reset to 1 and increment the next digit on the left side by one.

The counting process of the previous example will look like the simple animation shown below:



This algorithm is very efficient and -theoretically- has no limitation on number of factors or number of levels.

The following VBA code can be used to implement the previous algorithm

Sub CreateTreatments()

Const NoOfFactors = 3

Dim MaxLevels(NoOfFactors) As Integer
Dim FactorLevels(NoOfFactors) As Integer
Dim Letters(NoOfFactors) As String


' Define factor letters
Letters(0) = " A"
Letters(1) = " B"
Letters(2) = " C"

' Define number of levels of each factor
MaxLevels(0) = 3
MaxLevels(1) = 2
MaxLevels(2) = 4

' Reset all factors to the minimum level
FactorLevels(0) = 1
FactorLevels(1) = 1
FactorLevels(2) = 1

' Calculate number of runs (combinations)
NoOfRuns = 1
For i = 0 To NoOfFactors - 1
NoOfRuns = NoOfRuns * MaxLevels(i)
Next i

For i = 1 To NoOfRuns

' Create combination text
CombinationString = ""
For j = NoOfFactors To 1 Step -1
CombinationString = Letters(j - 1) + CStr(FactorLevels(j - 1)) + CombinationString
Next j

FactorLevels(NoOfFactors - 1) = FactorLevels(NoOfFactors - 1) + 1    'Increment rightmost digit

For j = NoOfFactors To 2 Step -1    'For all digits
'If factor level is higher than number of levels, then reset it and increment the left digit
If FactorLevels(j - 1) > MaxLevels(j - 1) Then
FactorLevels(j - 1) = 1
FactorLevels(j - 2) = FactorLevels(j - 2) + 1
End If
Next j

Debug.Print CombinationString

Next i

End Sub


This code will output the following lines in debug window:

A1 B1 C1
A1 B1 C2
A1 B1 C3
A1 B1 C4
A1 B2 C1
A1 B2 C2
A1 B2 C3
A1 B2 C4
A2 B1 C1
A2 B1 C2
A2 B1 C3
A2 B1 C4
A2 B2 C1
A2 B2 C2
A2 B2 C3
A2 B2 C4
A3 B1 C1
A3 B1 C2
A3 B1 C3
A3 B1 C4
A3 B2 C1
A3 B2 C2
A3 B2 C3
A3 B2 C4


Keywords:

Mixed level factorial design algorithm

No comments:

Post a Comment