Class: Chain

Chain(optionsopt)

A time-homogeneous Markov chain with optional memory.

Constructor

new Chain(optionsopt)

Parameters:
Name Type Attributes Default Description
options object <optional>
{}

Options object

Properties
Name Type Attributes Default Description
corpus Array.<Array.<any>> <optional>
[]

Sample runs of the process

order number <optional>
0

Size of the chain's memory

useTokenMap boolean <optional>
false

Whether to map token to states

model Map.<Tuple.<any>, any> <optional>

Prebuilt state space

Source:

Members

(readonly) initialState :Tuple.<any>

Initial state with BEGIN tokens.

Type:
Source:

(readonly) model :Map.<Tuple.<any>, any>

Map of chain states.

Type:
Source:

(readonly) order :number

Order of chain.

Type:
  • number
Source:

(readonly) tokenMap :Map.<any, Tuple.<any>>

Map of token to state.

Type:
Source:

Methods

(static) fromJSON(jsonChain, options) → {Chain}

Creates a Chain from a JSON string.

Parameters:
Name Type Description
jsonChain string

A chain serialised with Chain.toJSON

options object

Additional options to Chain constructor

Source:
Returns:

A new chain instance

Type
Chain

(static) seed(run, chainopt)

Updates a model from a single run.

Parameters:
Name Type Attributes Default Description
run Array.<any>

Array of tokens

chain object <optional>
{}

Chain object

Properties
Name Type Attributes Description
model Map.<Tuple.<any>, any>

Model to update

initialState Tuple.<any>

Starting tuple

order number

Order of chain

tokenMap Map.<any, Tuple.<any>> <optional>

Map of token to states

Source:

run(optionsopt) → {Array.<Array.<any>>}

Walks the Markov chain and returns all steps.

The returned step array will look like:

[ [backward_steps], [starting_tokens], [forward_steps] ]

The starting tokens are only returned when forward or backward steps were actually generated from a subset of the {options.tokens} parameter.

Parameters:
Name Type Attributes Description
options object <optional>

Options object

Properties
Name Type Attributes Default Description
tokens Array.<any> <optional>
[]

Starting state tokens

backSearch boolean <optional>
true

Should walk back

useTokenMap boolean <optional>
true

Whether to use token map

runMissingTokens boolean <optional>
true

Whether to answer when tokens are not in model

Source:
Returns:

Array with back root and forward steps

Type
Array.<Array.<any>>

toJSON() → {Array.<any>}

Serialises the chain into a JSONable array.

The returned array will look like:

[ [ [state], [ [next, count], ...], [ [prev, count], ...] ], ...]
Source:
See:
Returns:

JSON array

Type
Array.<any>

(generator) walkBackward(fromStateopt) → {any}

Generates successive states until the chain reaches a BEGIN.

Parameters:
Name Type Attributes Description
fromState Tuple.<any> <optional>

Starting state of the chain walk

Source:
Yields:
Next preceeding step of the chain
Type
any

(generator) walkForward(fromStateopt) → {any}

Generates successive states until the chain reaches an END.

Parameters:
Name Type Attributes Description
fromState Tuple.<any> <optional>

Begin state of the chain walk

Source:
Yields:
Next succeding step of the chain
Type
any