npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-markov-chain

v1.0.1

Published

NPM package to easily create and use Markov chains

Downloads

6

Readme

simple-markov-chain

Markov chains are useful utilities for modelling behaviour which follows the Markov property:

In probability theory and statistics, the term Markov property refers to the memoryless property of a stochastic process.

A stochastic process has the Markov property if the conditional probability distribution of future states of the process (conditional on both past and present states) depends only upon the present state, not on the sequence of events that preceded it.

(from Wikipedia)

Installation

npm install simple-markov-chain

Usage

const MarkovChain = require('simple-markov-chain')

const chain = new MarkovChain()
chain.addTransition('a', 'b') // Transition from state 'a' to state 'b'
chain.addTransition('a', 'c') // Transition from state 'a' to state 'c'
chain.addTransition('b', 'a')
chain.addTransition('c', 'a')

chain.generateState('a') // 50% chance to return 'b', 50% chance to return 'c'
chain.generateState() // Will take last generated state as base state => either 'b' or 'c' => will return 'a'
chain.generateState() // Last generated state was 'a' => 50% chance for 'b', 50% chance for 'c'
// ...

API

All Error classes (EmptyChainError, InvalidArgumentError, InvalidStateError) are exposed through MarkovChain.errors.

constructor(isStrict = false)

Creates a new Markov chain.

addTransition(from, to)

chain.addTransition('a', 'b')
chain.addTransition('b', 3)
chain.addTransition(3, 'foobar')
chain.addTransition('foobar', 'a')

This methods adds a new transition to the internal transition memory. from and to are both converted to strings.

generateState(initialState = null)

chain.generateState()
chain.generateState('foobar')

This method will generate a new state based on the transition matrix calculated from the added states.

If initialState is supplied, it is used as the basis for the state generation.
If it is not supplied, the last generated state will be used.
If no state has been generated so far, a random one will be picked from the from states.

In the event that a new state is being generated and the initial state has no to states, behaviour changes based on isStrict:
If isStrict === false a random from state will be returned.
If isStrict === true an InvalidStateError will be thrown.

If this method is called while no states have been added, an EmptyChainError will be thrown.

transitionMatrix

If you want to access the transition matrix, you can do it like this:

chain.transitionMatrix

With the states added above, this would return the following structure:

{
  "a": {
    "b": 0.5,
    "c": 0.5
  },
  "b": {
    "a": 1
  },
  "c": {
    "a": 1
  }
}

The transition matrix is only calculated when the states have been modified (through the API) and it is then requested, making it a very efficient operation.

save()

const data = chain.save()

This will return a JSON string representing the current matrix (and its' current state).

It persists the added states as well as the last generated state. This means that you can easily load the data and continue adding states if needed.

#load()

const chain = MarkovChain.load(data)

This will create a new Markov chain based on the specified serialized data.