Teradata R Package Function Reference - 16.20 - HMMDecoder - Teradata R Package

Teradata® R Package Function Reference

prodname
Teradata R Package
vrm_release
16.20
created_date
February 2020
category
Programming Reference
featnum
B700-4007-098K

Description

The HMMDecoder function finds the state sequence with the highest probability, given the learned model and observed sequences.

Usage

  td_hmm_decoder_mle (
      init.state.prob = NULL,
      state.transition.prob = NULL,
      emission.prob = NULL,
      observation = NULL,
      state.model.key = NULL,
      state.key = NULL,
      state.prob.key = NULL,
      trans.model.key = NULL,
      trans.from.key = NULL,
      trans.to.key = NULL,
      trans.prob.key = NULL,
      emit.model.key = NULL,
      emit.state.key = NULL,
      emit.observed.key = NULL,
      emit.prob.key = NULL,
      model.key = NULL,
      sequence.key = NULL,
      observed.key = NULL,
      sequence.max.size = 2147483647,
      skip.key = NULL,
      accumulate = NULL,
      observation.sequence.column = NULL,
      init.state.prob.sequence.column = NULL,
      state.transition.prob.sequence.column = NULL,
      emission.prob.sequence.column = NULL,
      observation.partition.column = NULL,
      init.state.prob.partition.column = NULL,
      state.transition.prob.partition.column = NULL,
      emission.prob.partition.column = NULL,
      observation.order.column = NULL
  )

Arguments

init.state.prob

Required Argument.
Specifies a tbl_teradata that contains the initial state table.

init.state.prob.partition.column

Required Argument.
Specifies the Partition By columns for argument "init.state.prob". Values to this argument can be provided as vector, if multiple columns are used for ordering.

state.transition.prob

Required Argument.
Specifies a tbl_teradata that contains the state transition table.

state.transition.prob.partition.column

Required Argument.
Specifies the Partition By columns for argument "state.transition.prob". Values to this argument can be provided as vector, if multiple columns are used for ordering.

emission.prob

Required Argument.
Specifies a tbl_teradata that contains the emission probability table.

emission.prob.partition.column

Required Argument.
Specifies the Partition By columns for argument "emission.prob". Values to this argument can be provided as vector, if multiple columns are used for ordering.

observation

Required Argument.
Specifies a tbl_teradata that contains the observation table.

observation.partition.column

Required Argument.
Specifies the Partition By columns for argument "observation". Values to this argument can be provided as vector, if multiple columns are used for ordering.

observation.order.column

Required Argument.
Specifies the Order By columns for argument "observation". Values to this argument can be provided as vector, if multiple columns are used for ordering.

state.model.key

Required Argument.
Specifies the name of the model attribute column in the "init.state.prob" table.

state.key

Required Argument.
Specifies the name of the state attribute column in the "init.state.prob" table.

state.prob.key

Required Argument.
Specifies the name of the initial probability column in the "init.state.prob" table.

trans.model.key

Required Argument.
Specifies the name of the model attribute column in the "state.transition.prob" table.

trans.from.key

Required Argument.
Specifies the name of the source state transition column in the "state.transition.prob" table.

trans.to.key

Required Argument.
Specifies the name of the target state transition column in the "state.transition.prob" table.

trans.prob.key

Required Argument.
Specifies the name of the state transition probability column in the "state.transition.prob" table.

emit.model.key

Required Argument. Specifies the name of the model attribute column in the "emission.prob" table.

emit.state.key

Required Argument.
Specifies the name of the state attribute column in the "emission.prob" table.

emit.observed.key

Required Argument.
Specifies the name of the observation attribute column in the "emission.prob" table.

emit.prob.key

Required Argument.
Specifies the name of the emission probability column in the "emission.prob" table.

model.key

Required Argument.
Specifies the name of the column that contains the model attribute. It must match one of the column names specified in the "observation.partition.column" argument.

sequence.key

Required Argument.
Specifies the name of the column that contains the sequence attribute. It must match one of the column names specified in the "observation.partition.column" argument.

observed.key

Required Argument.
Specifies the name of the column that contains the observed symbols.
Note: Observed symbols are case-sensitive.

sequence.max.size

Optional Argument.
Specifies the maximum length (in rows), of a sequence in the "observation" table. Default Value: 2147483647

skip.key

Optional Argument.
Specifies the name of the column whose values determine whether the function skips the row. The function skips the row if the value is "true", "yes", "y", or "1". The function does not skip the row if the value is "false", "f", "no", "n", "0", or NULL.

accumulate

Optional Argument.
Specifies the names of the columns in the input argument "observation" that the function copies to the output table.

observation.sequence.column

Optional Argument.
Specifies the vector of column(s) that uniquely identifies each row of the input argument "observation". The argument is used to ensure deterministic results for functions which produce results that vary from run to run.

init.state.prob.sequence.column

Optional Argument.
Specifies the vector of column(s) that uniquely identifies each row of the input argument "init.state.prob". The argument is used to ensure deterministic results for functions which produce results that vary from run to run.

state.transition.prob.sequence.column

Optional Argument.
Specifies the vector of column(s) that uniquely identifies each row of the input argument "state.transition.prob". The argument is used to ensure deterministic results for functions which produce results that vary from run to run.

emission.prob.sequence.column

Optional Argument.
Specifies the vector of column(s) that uniquely identifies each row of the input argument "emission.prob". The argument is used to ensure deterministic results for functions which produce results that vary from run to run.

Value

Function returns an object of class "td_hmm_decoder_mle" which is a named list containing Teradata tbl object.
Named list member can be referenced directly with the "$" operator using name: result.

Examples

    # Get the current context/connection
    con <- td_get_context()$connection
    
    # Load example data.
    loadExampleData("hmmsupervised_example", "customer_loyalty")
    loadExampleData("hmmunsupervised_example", "loan_prediction")
    
    # Example 1 - This example uses loan status updates to build a Unupservised HMM
    # model and then predict loan defaults.
    loadExampleData("hmmdecoder_example", "test_loan_prediction")
    loan_prediction = tbl(con, "loan_prediction")
    td_hmm_unsup_out <- td_hmm_unsupervised_mle(vertices=loan_prediction,
                                  vertices.partition.column=c('model_id','seq_id'),
                                  vertices.order.column='seq_vertex_id',
                                  sequence.key='seq_id',
                                  observed.key='observed_id',
                                  model.key='model_id',
                                  hidden.states.num=3
    )
    
    test_loan_prediction <- tbl(con, "test_loan_prediction")
  
    td_hmm_decoder_out1 <- td_hmm_decoder_mle(init.state.prob = td_hmm_unsup_out$output.initialstate.table,
                             init.state.prob.partition.column = c("model_id"),
                             state.transition.prob = td_hmm_unsup_out$output.statetransition.table,
                             state.transition.prob.partition.column = c("model_id"),
                             emission.prob = td_hmm_unsup_out$output.emission.table,
                             emission.prob.partition.column = c("model_id"),
                             observation = test_loan_prediction,
                             observation.partition.column = c("model_id "),
                             observation.order.column = c("model_id", "seq_id", "seq_vertex_id"),
                             state.model.key = "model_id",
                             state.key = "state",
                             state.prob.key = "probability",
                             trans.model.key = "model_id",
                             trans.from.key = "from_state",
                             trans.to.key = "to_state",
                             trans.prob.key = "probability",
                             emit.model.key = "model_id",
                             emit.state.key = "state",
                             emit.observed.key = "observed",
                             emit.prob.key = "probability",
                             model.key = "model_id",
                             sequence.key = "seq_id",
                             observed.key = "observed_id",
                             accumulate = c("seq_vertex_id")
                            )
    
    # Example 2 - This example uses the output of a HMM Supervised model with the input
    # to determine the loyalty levels of customers from the new sequence of purchases.
    loadExampleData("hmmdecoder_example", "customer_loyalty_newseq")
    customer_loyalty = tbl(con, "customer_loyalty")
  
    td_hmm_sup_out <- td_hmm_supervised_mle(vertices=customer_loyalty,
                                        vertices.partition.column=c('user_id','seq_id'),
                                        vertices.order.column=c('user_id','seq_id','purchase_date'),
                                        state.key='loyalty_level',
                                        observed.key='observation',
                                        sequence.key='seq_id',
                                        model.key='user_id'
                                       )
    
    customer_loyalty_newseq = tbl(con, "customer_loyalty_newseq")
  
    td_hmm_decoder_out2 <- td_hmm_decoder_mle(init.state.prob = td_hmm_sup_out$output.initialstate.table,
                             init.state.prob.partition.column = c("user_id"),
                             state.transition.prob = td_hmm_sup_out$output.statetransition.table,
                             state.transition.prob.partition.column = c("user_id"),
                             emission.prob = td_hmm_sup_out$output.emission.table,
                             emission.prob.partition.column = c("user_id"),
                             observation = customer_loyalty_newseq,
                             observation.partition.column = c("user_id"),
                             observation.order.column = c("user_id", "seq_id", "purchase_date"),
                             state.model.key = "user_id",
                             state.key = "state",
                             state.prob.key = "probability",
                             trans.model.key = "user_id",
                             trans.from.key = "from_state",
                             trans.to.key = "to_state",
                             trans.prob.key = "probability",
                             emit.model.key = "user_id",
                             emit.state.key = "state",
                             emit.observed.key = "observed",
                             emit.prob.key = "probability",
                             model.key = "user_id",
                             sequence.key = "seq_id",
                             observed.key = "observation",
                             accumulate = "purchase_date"
                            )
    
    # Example 3 - Part of Speech Tagging example
    loadExampleData("hmmdecoder_example", "initial", "state_transition", "emission", "phrases")
    initial <- tbl(con, "initial")
    state_transition <- tbl(con, "state_transition")
    emission <- tbl(con, "emission")
    phrases <- tbl(con, "phrases")
    
    td_hmm_decoder_out3 <- td_hmm_decoder_mle(init.state.prob = initial,
                                         init.state.prob.partition.column = c("model"),
                                         state.transition.prob = state_transition,
                                         state.transition.prob.partition.column = c("model"),
                                         emission.prob = emission,
                                         emission.prob.partition.column = c("model"),
                                         observation = phrases,
                                         observation.partition.column = c("model"),
                                         observation.order.column = c("model", "phrase_id"),
                                         state.model.key = "model",
                                         state.key = "tag",
                                         state.prob.key = "probability",
                                         trans.model.key = "model",
                                         trans.from.key = "from_tag",
                                         trans.to.key = "to_tag",
                                         trans.prob.key = "probability",
                                         emit.model.key = "model",
                                         emit.state.key = "tag",
                                         emit.observed.key = "word",
                                         emit.prob.key = "probability",
                                         model.key = "model",
                                         sequence.key = "phrase_id",
                                         observed.key = "word"
                                         )
    
    # Example 4 - This example uses HMMDecoder to find the propensity of customer churn, 
    # given the actions or transactions of a bank customer.
    loadExampleData("hmmdecoder_example", "churn_initial", "churn_state_transition", "churn_emission", "churn_data")
    churn_initial <- tbl(con, "churn_initial")
    churn_state_transition <- tbl(con, "churn_state_transition")
    churn_emission <- tbl(con, "churn_emission")
    churn_data <- tbl(con, "churn_data")
    
    td_hmm_decoder_out4 <- td_hmm_decoder_mle(init.state.prob = churn_initial,
                                         init.state.prob.partition.column = c("model"),
                                         state.transition.prob = churn_state_transition,
                                         state.transition.prob.partition.column = c("model"),
                                         emission.prob = churn_emission,
                                         emission.prob.partition.column = c("model"),
                                         observation = churn_data,
                                         observation.partition.column = c("model"),
                                         observation.order.column = c("model","id", "path_id"),
                                         state.model.key = "model",
                                         state.key = "tag",
                                         state.prob.key = "probability",
                                         trans.model.key = "model",
                                         trans.from.key = "from_tag",
                                         trans.to.key = "to_tag",
                                         trans.prob.key = "probability",
                                         emit.model.key = "model",
                                         emit.state.key = "state",
                                         emit.observed.key = "observed",
                                         emit.prob.key = "probability",
                                         model.key = "model",
                                         sequence.key = "id",
                                         observed.key = "action",
                                         accumulate = c("path_id")
                                         )