# How do risk models deal with state changes over time?

Let's say you are trying to predict if a machine is going to explode (0) or successfully complete its job (1), and you are trying to determine the impact of certain states on the machine.

So say the machine has state A, B, C, and D. The machine starts in state A. Then... A-> B, B->C OR B->D, C->D. Then after D, the machine may successfully complete its job. The machine can explode in any of the states... but the likelihood of that happening is measurably different.

Say there's other "typical" variables at play, such as "job size" and "number of components", and you want to incorporate these into the prediction model as well.

Let's say you have a thousand machines that you have tracked with an hourly log... and you view the patterns (e.g. A->A->A->B->B->C->explode) or (e.g. A->A->A->B->B->C->D->D->D->success).

So for the model, you want to predict the likelihood that a machine will successfully complete the job given its state and job size and number of components.

So a couple of options I can think of:

1) Build a model for each state: Model 1: Get all the machines that have been in State A and do a logistic regression. Model 2: Get all the machines that have been in State B and do a logistic regression. Model 3: Get all the machines that have been in State C and do a logistic regression. Model 4: Get all the machines that have been in State D and do a logistic regression.

Upside: Data is nicely cross sectional... no weird time series things going on. Downside: This would be really arduous to manage more and more models as you increase the amount of states.

2) Build one model that incorporates the states as binaries... so if you see that a machine has passed through a state you tag the state binary as 1.

Upside: One model Downside: I think there are issues here with the training set. If you train the model using data that the machines ended up at... then you can only regress using the final end point for a machine. But in reality, we are estimating based on "mid life" at an individual state. Alternatively, if you try to sample from "mid life" data points in the logs, you end up either (a) if you randomly sample from the log, you are biasing the model by oversampling machines that appear more often in the logs or (b) if you are sampling one data point from individual machines, you are biasing the model by overrepresenting the machines that have exploded (by the nature of the state transitions, machines will explode more quickly than they successfully complete).

Is this ringing any bells for anyone? Not sure where to go from here...

I have attached a training set generator...

set.seed(123)

trainingData <- data.frame(trial = c(1:100), complexity = 0, jobSize = 0, A = 0, B = 0, C = 0, D = 0, success = 0) # 100 machines

complexityMean <- 50.0
jobSizeMean <- 100.0

pass <- function(test, rand, s, c, sScalar, cScalar){
value <- rand + ((s * sScalar) - 1) + ((c * cScalar) - 1)
if(value < test){
return(0)
} else {
return(1)
}
}

for(i in c(1:nrow(trainingData))){
complexity <- as.integer(rnorm(1, mean = complexityMean, sd = 10))
trainingData[i,"complexity"] <- complexity
jobSize <- as.integer(rnorm(1, mean = jobSizeMean, sd = 20))
trainingData[i,"jobSize"] <- jobSize

trainingData[i,"A"] <- 1
rand <- runif(1,0,1)
if(!pass(.10, rand, jobSize/jobSizeMean, complexity/complexityMean, 1, 1)){ # fail
next
}

# Next is State B
trainingData[i,"B"] <- 1
rand <- runif(1,0,1)
if(!pass(.05, rand, jobSize/jobSizeMean, complexity/complexityMean, 1, 1.05)){ # fail
next
} else if(!pass(.80, rand, jobSize/jobSizeMean, complexity/complexityMean, .95, 1)){ # go to State C
trainingData[i,"C"] <- 1
rand <- runif(1,0,1)
if(!pass(.15, rand, jobSize/jobSizeMean, complexity/complexityMean, .95, 1.05)){ # fail
next
}
}

# Next is State D
trainingData[i,"D"] <- 1
rand <- runif(1,0,1)
if(!pass(.05, rand, jobSize/jobSizeMean, complexity/complexityMean, .95, .90)){ # fail
next
}

# Success!
trainingData[i,"success"] <- 1
}


Drawing on this idea, you might approach as a grammar learning problem, where a grammar is a set of rewrite rules to generate strings. The tokens in your grammar are: $\{A,B,C,D,1,0\}$. The strings consist of sequences of one or more $\{A,B,C,D\}$ followed (eventually) by $\{1,0\}$. You want to learn classification of combinations of grammar rules that end either in $1$ or $0$. With this approach, you can train on full (coded) time series, without the problems associated with state compression or recoding as you described in 2).