Basic Usage

import markov4s._

val chain = MarkovChain[String]
// chain: MarkovChain[String] = MarkovChain(Map())
val chain1 = chain + ("a" -> "b") + ("b" -> "c") + ("c" -> "d")
// chain1: MarkovChain[String] = MarkovChain(
//   Map("a" -> Map("b" -> 1), "b" -> Map("c" -> 1), "c" -> Map("d" -> 1))
// )

// get sequence of items starting with item "a", return at most 4 items
chain1.getSeq("a", 4)
// res0: Vector[String] = Vector("a", "b", "c", "d")

Join chains

val chain2 = MarkovChain[String] + ("a" -> "b") + ("b" -> "c")
// chain2: MarkovChain[String] = MarkovChain(
//   Map("a" -> Map("b" -> 1), "b" -> Map("c" -> 1))
// )
val chain3 = MarkovChain[String] + ("c" -> "d")
// chain3: MarkovChain[String] = MarkovChain(Map("c" -> Map("d" -> 1)))

(chain2 ++ chain3).getSeq("a", 4)
// res1: Vector[String] = Vector("a", "b", "c", "d")

Create from sequence

Using fromSeq method allows us to easily construct chains from sequences. Relationships will be created taking pairs of items, e.g. chain.fromSeq(List("sunny", "cloudy", "sunny")) will be equivalent to chain + ("sunny" -> "cloudy") + ("cloudy" -> "sunny")

val chain4 = MarkovChain[String]
    .fromSeq(List("sunny", "sunny", "sunny", "sunny", "sunny", "sunny", "sunny"))
    .fromSeq(List("sunny", "cloudy", "sunny"))
    .fromSeq(List("sunny", "cloudy", "sunny"))
    .fromSeq(List("sunny", "cloudy", "sunny"))
    .fromSeq(List("sunny", "rainy", "sunny"))
    .fromSeq(List("cloudy", "cloudy", "cloudy", "cloudy", "cloudy"))
    .fromSeq(List("cloudy", "rainy", "cloudy"))
    .fromSeq(List("cloudy", "rainy", "cloudy"))
    .fromSeq(List("cloudy", "rainy", "cloudy"))
    .fromSeq(List("rainy", "cloudy"))
    .fromSeq(List("rainy", "cloudy"))
    .fromSeq(List("rainy", "rainy", "rainy"))
    .fromSeq(List("rainy", "sunny"))
    .fromSeq(List("rainy", "sunny"))
// chain4: MarkovChain[String] = MarkovChain(
//   Map(
//     "rainy" -> Map("rainy" -> 2, "cloudy" -> 5, "sunny" -> 3),
//     "cloudy" -> Map("rainy" -> 3, "cloudy" -> 4, "sunny" -> 3),
//     "sunny" -> Map("rainy" -> 1, "cloudy" -> 3, "sunny" -> 6)
//   )
// )

chain4.getSeq("sunny", 10)
// res2: Vector[String] = Vector(
//   "sunny",
//   "sunny",
//   "sunny",
//   "sunny",
//   "sunny",
//   "sunny",
//   "sunny",
//   "sunny",
//   "sunny",
//   "sunny"
// )

getSeq will pick items with highest occurance, while useful for some cases this is not generally what we’re looking for. Let’s try to ask for sequence where next item will be selected based on probability.

implicit val rand = Rand()
// rand: Rand = markov4s.Rand@ec14197

// Our 10 day weather prognosis
chain4.getSeqWithProb("sunny", 10)
// res3: Vector[String] = Vector(
//   "sunny",
//   "sunny",
//   "sunny",
//   "sunny",
//   "sunny",
//   "sunny",
//   "sunny",
//   "cloudy",
//   "cloudy",
//   "sunny"
// )
chain4.getSeqWithProb("sunny", 10)
// res4: Vector[String] = Vector(
//   "sunny",
//   "rainy",
//   "sunny",
//   "rainy",
//   "rainy",
//   "rainy",
//   "cloudy",
//   "rainy",
//   "rainy",
//   "sunny"
// )
chain4.getSeqWithProb("sunny", 10)
// res5: Vector[String] = Vector(
//   "sunny",
//   "sunny",
//   "sunny",
//   "rainy",
//   "sunny",
//   "sunny",
//   "sunny",
//   "sunny",
//   "sunny",
//   "sunny"
// )

Note: This documentation is generated which may cause some code evaluations return different result.