38  The prototype Optimal Predictor Machine makes decisions

Published

2023-11-16

It is straightforward to implement decision-making in our prototype Optimal Predictor Machine. Let’s continue with the example from chapter  35.

38.1 Initialization and build of OPM agent

Load the necessary libraries and functions, including the decide() function, and train the agent as we did previously:

library('data.table')
library('extraDistr')
library('foreach')
library('khroma')

source('tplotfunctions.R')
source('guessmetadata.R')
source('buildagent.R')
source('infer.R')
source('decide.R')
source('mutualinfo.R')
source('rF.R')
source('plotFsamples1D.R')

options(repr.plot.width=6*sqrt(2), repr.plot.height=6)

opmall <- buildagent(metadata='meta_income_data_example.csv',
                     data='train-income_data_example.csv')

38.2 Decision matrix

We use the targeted-advertisement scenario of §  37.2, with the following utility matrix for the three ad-types:

adutilities <- matrix(c(-1,3, 2,2, 3,-1), nrow=3, byrow=TRUE, dimnames=list(ad_type=c('A','B','C'), income=c('<=50K', '>50K')))

print(adutilities)
       income
ad_type <=50K >50K
      A    -1    3
      B     2    2
      C     3   -1

38.3 Example application

First let’s apply the principle of maximal expected utility step-by-step.

Consider the example from §  37.2. The agent calculates the probabilities for the predictand income from the given predictor values:

userpredictors <- list(workclass='Private', education='Bachelors',
                       marital_status='Never-married',
                       occupation='Prof-specialty',
                       relationship='Not-in-family', race='White',
                       sex='Female', native_country='United-States')

probs <- infer(agent=opmall, predictand='income',
               predictor=userpredictors)

print(probs)
income
   <=50K     >50K 
0.833333 0.166667 

Find the expected utilities of the three possible ad-types by matrix multiplication:

adutilities %*% probs
       
ad_type     [,1]
      A -0.33333
      B  2.00000
      C  2.33333

And we see that ad-type C is optimal.


The function decide() does the previous calculations. It outputs a list with elements:

  • EUs: the expected utilities of the decisions, sorted from highest to lowest
  • optimal: one decision unsystematically chosen among the optimal ones (if more than one)
optimalad <- decide(utils=adutilities, probs=probs)

print(optimalad)
$EUs
       C        B        A 
 2.33333  2.00000 -0.33333 

$optimal
[1] "C"

38.4 Performance on test set

Finally let’s apply our prototype agent to a test set, as a demonstration, and see how much utility it yields. This procedure will be discussed in more detail in §  40.

Load the test dataset; M is the number of test data:

testdata <- fread('test-income_data_example.csv', header=TRUE)
M <- nrow(testdata)

We build the analogous of a “confusion matrix” (§  40), telling us how many times the agent chooses the three ad-types for both income levels.

confusionmatrix <- adutilities * 0L

## Use a for-loop for clarity
for(i in 1:M){
    userpredictors <- testdata[i, !'income']
    probs <- infer(agent=opmall, predictand='income',
                   predictor=userpredictors)
    decision <- decide(utils=adutilities, probs=probs)$optimal
    trueincome <- testdata[i]$income

    confusionmatrix[decision, trueincome] <- confusionmatrix[decision, trueincome] + 1L
}

print(confusionmatrix)
       income
ad_type <=50K >50K
      A   769 2149
      B 11768 5093
      C 12961 1174

The total utility yield is the total sum of the element-wise product of the confusionmatrix and the adutilities matrix

totalyield <- sum(adutilities * confusionmatrix)
averageyield <- totalyield/M

cat('\nTotal yield =', totalyield, 
'\nAverage yield =', averageyield, '\n')

Total yield = 77109 
Average yield = 2.27366 

Note that:

  • This yield is higher than what would be obtained by just choosing the neutral ad-type B for all test units (the average yield would be exactly 2).

  • This yield is also higher than would be obtained by always choosing ad-type C, targeting the majority of units, which have income='<=50K'. This strategy would yield 2.00737.

Exercises
  • Try to use some common machine-learning algorithm to perform the same task of choosing between the three ad-types. Is it difficult? why?

    If you manage to do this, then compare the performances of the machine-learning algorithm and the opmall agent.

  • Construct a scenario where the utility matrix is different depending on the sex predictor variate. Write a script to apply the opmall agent on the test set according to this new scenario.