Adds a key / value pair to a Distribution. -Will add to the source distribution by default, unless the distribution -only has normalized values.
-Key to be added.
-Value of the key to add.
-Adds an object of values to a Distribution. -Will add to the source distribution by default, unless the distribution -only has normalized values.
-An object containing additions.
-Creates a clone of a Distribution instance.
-If true this will strip out the source.
-Picks one more values from a Distribution without exclusion. -If you just need to pick one value, you should use pickOne instead.
-The number of picks to make (default 1).
-A mask containing keys in the distribution that should be ignored.
-If true picks are considered exclusive and are removed.
-Picks a single value from a Distribution. -If you are picking multiple values, use pick instead.
-A mask containing keys in the distribution that should be ignored.
-Removes a key or array of keys from a Distribution and renormalizes.
-Key or Keys to be removed.
-Serializes a Distribution instance into a data transfer object.
-If true this will strip out the source.
-Applies a DTO's data to the source and normal distributions.
-A Distribution data transfer object.
-Adds a key / value pair to a Distribution. -Will add to the source distribution by default, unless the distribution -only has normalized values.
-A Distribution data transfer object.
-Key to be added.
-Value of the key to add.
-Adds a key / value pair to a normal distribution and renormalizes it. -If a source distribution exists, it will be recalculated based off of the -new normal distribution.
-A Distribution data transfer object.
-Key to be added.
-Value of the key to add.
-Adds an object of values to a normal Distribution and renormalizes it. -If a source distribution exists, it will be recalculated by scaling the -new normal distribution to fit its sum.
-A Distribution data transfer object.
-An object containing additions.
-Adds a key / value pair to a distribution's source and renormalizes it. -If a source distribution exists, it will be recalculated based off of the -new normal distribution.
-A Distribution data transfer object.
-Key to be added.
-Value of the key to add.
-Adds an object of values to a Distribution's source and renormalizes it.
-A Distribution data transfer object.
-An object containing additions.
-Adds an object of values to a Distribution. -Will add to the source distribution by default, unless the distribution -only has normalized values.
-A Distribution data transfer object.
-An object containing additions.
-Creates a clone of a Distribution DTO.
-A Distribution data transfer object.
-If true this will strip out the source.
-Initializes a new DistributionSourceDTO.
-An optional source of values to generate the distribution from.
-Picks one more values from a Distribution without exclusion. -If you just need to pick one value, you should use pickOne instead.
-A Distribution data transfer object.
-The number of picks to make (default 1).
-A mask containing keys in the distribution that should be ignored.
-If true picks are considered exclusive and are removed.
-A Random engine. This is created if not provided.
-Picks a single value from a Distribution. -If you are picking multiple values, use pick instead.
-A Distribution data transfer object.
-A mask containing keys in the distribution that should be ignored.
-A Random engine. This is created if not provided.
-Removes a key or array of keys from a Distribution and renormalizes.
-A Distribution data transfer object.
-Key or Keys to be removed.
-Generated using TypeDoc
Adds an edge from a gram to the items before and after it in the sequence.
-The id of a gram, or the gram sequence.
-The id of the previous gram in the sequence.
-The id of the next gram in the sequence.
-The order of the Gram we're adding.
-Adds or inserts a Sequence into a Markov Chain DTO.
-The sequence to be added.
-Determines how sequences should be inserted. If false, delimiters will be - prepended and appended to the sequences. - "start" or setting true will only prepend the start delimiter, while - "end" will append the end delimiter. "middle" will not add any delimiters.
-Adds or inserts a list of Sequences into a Markov Chain DTO.
-The sequences to be added.
-Determines how sequences should be inserted. If false, delimiters will be - prepended and appended to the sequences. - "start" or setting true will only prepend the start delimiter, while - "end" will append the end delimiter. "middle" will not add any delimiters.
-Analyze's a sequences sources and sinks. Generates a number of samples from a given gram sequence, and gives the resulting -distribution of where the generated sequences terminated both backwards (sources) and forwards (sinks).
-Ceates a clone of the Markov Chain.
-If true this will strip out the sequences, removing the chain's source data.
-Finds the valid gram of the highest valid order in a sequence.
-An array containing the Gram sequence.
-The highest order to look for.
-The direction we are looking for sequences in. - "next" will look for grams at the end of the sequence. - "last" will look for grams at the beginning of the sequence.
-Generates a sequence from a Markov Chain.
-Returns the corresponding Gram from a sequence.
-An array containing the Gram sequence.
-Returns the id of a Gram from its sequence.
-An array containing the Gram sequence.
-Picks the last random value from a Markov Chain given a sequence.
-The starting Gram sequence. If this isn't supplied this defaults to the start.
-A mask containing keys in the chain that should be ignored.
-Picks the next random value from a Markov Chain given a sequence.
-The starting Gram sequence. If this isn't supplied this defaults to the start.
-A mask containing keys in the chain that should be ignored.
-Picks the next or last random value from a Markov Chain.
-The starting Gram sequence. If this isn't supplied this defaults to the start.
-If true states that come after the gram will be picked. - If false states that came before the gram will be picked.
-A mask containing keys in the chain that should be ignored.
-Serializes a Markov Chain instance into a DTO.
-If true this will strip out the sequences, removing the chain's source data.
-Updates a Markov Chain's members from a DTO.
-Adds an edge from a gram to the items before and after it in the sequence.
-A Markov Chain data transfer object.
-The id of a gram, or the gram sequence.
-The id of the previous gram in the sequence.
-The id of the next gram in the sequence.
-The order of the Gram we're adding.
-Adds or inserts a Sequence into a Markov Chain DTO.
-A Markov Chain data transfer object.
-The sequence to be added.
-Determines how sequences should be inserted. If false, delimiters will be - prepended and appended to the sequences. - "start" or setting true will only prepend the start delimiter, while - "end" will append the end delimiter. "middle" will not add any delimiters.
-Adds or inserts a list of Sequences into a Markov Chain DTO.
-A Markov Chain data transfer object.
-The sequences to be added.
-Determines how sequences should be inserted. If false, delimiters will be - prepended and appended to the sequences. - "start" or setting true will only prepend the start delimiter, while - "end" will append the end delimiter. "middle" will not add any delimiters.
-Analyze's a sequences sources and sinks. Generates a number of samples from a given gram sequence, and gives the resulting -distribution of where the generated sequences terminated both backwards (sources) and forwards (sinks).
-Create a deep copy of a Markov Chain DTO.
-Markov DTO to clone.
-If true this will strip out the sequences, removing the chain's source data.
-Finds the valid gram of the highest valid order in a sequence.
-A Markov Chain data transfer object.
-An array containing the Gram sequence.
-The highest order to look for.
-The direction we are looking for sequences in. - "next" will look for grams at the end of the sequence. - "last" will look for grams at the beginning of the sequence.
-Generates a sequence from a Markov Chain.
-Returns the corresponding Gram from a sequence.
-A Markov Chain data transfer object.
-An array containing the Gram sequence.
-Returns the id of a Gram from its sequence.
-A Markov Chain data transfer object.
-An array containing the Gram sequence.
-Utility function to find a sequence given an order and a direction. -This returns an array containing the first or last elements of an array equal to the order.
-An array containing the Gram sequence.
-The length of the array to return.
-If true, will find elements at the end. If false will find elements at the beginning.
-Picks the last random value from a Markov Chain given a sequence.
-A Markov Chain data transfer object.
-The starting Gram sequence. If this isn't supplied this defaults to the start.
-A mask containing keys in the chain that should be ignored.
-A Random engine.
-Creates a new Markov Chain data transfer object.
-An optional array of sequences to generate the grams from.
-The maximum gram size of the markov chain.
-Determines how sequences should be inserted. If false, delimiters will be - prepended and appended to the sequences. "start" or setting true will only prepend the start delimiter, while - "end" will append the end delimiter. "middle" will not add any delimiters.
-If true this will strip out the sequences, removing the chain's source data.
-Picks the next random value from a Markov Chain given a sequence.
-A Markov Chain data transfer object.
-The starting Gram sequence. If this isn't supplied this defaults to the start.
-A mask containing keys in the chain that should be ignored.
-A Random engine.
-Picks the next or last random value from a Markov Chain.
-A Markov Chain data transfer object.
-The starting Gram sequence. If this isn't supplied this defaults to the start.
-If true states that come after the gram will be picked. - If false states that came before the gram will be picked.
-A mask containing keys in the chain that should be ignored.
-A Random engine.
-Makes a random pick from the next or last state of a given Gram.
-The starting Gram sequence. If this isn't supplied this defaults to the start.
-If true states that come after the gram will be picked. - If false states that came before the gram will be picked.
-A mask containing keys in the chain that should be ignored.
-A Random engine.
-Generated using TypeDoc
Picks a weighted key from a normalized object, ignoring masked values.
-An object containing normalized number values.
-Array of keys to be ignored while evaluating.
-Generated using TypeDoc
acausal is a Typescript module that makes it easy to create, edit and generate pseudo random data from Weighted Random Distributions and Markov Chains.
-Design Philosophy
-Basic Examples:
-import { MarkovChain, Distribution, Random } from 'acausal';
// Random Rarity Distribution
const dist = new Distribution({ seed: 1 });
dist.add('Green', 10); // Common
dist.add('Blue', 5); // Uncommon
dist.add('Purple', 1); // Rare
dist.pick(10);
/* Results in:
[
'Green', 'Green', 'Green', 'Blue', 'Green',
'Blue', 'Purple', 'Green', 'Green', 'Green'
]
*/
// Markov Chain Name Generator
const mc = new MarkovChain({ seed: 1 });
mc.addSequence('alice'.split(''));
mc.addSequence('bob'.split(''));
mc.addSequence('erwin'.split(''));
console.log(mc.generate({ order: 1 }));
/* Results in:
[ 'a', 'l', 'i', 'n' ]
*/
// Random Numbers
const rand = new Random({ seed: 1 });
rand.integer(1, 6); // Roll 1d6
// Results in: 6
-
-
-
- Run:
-npm install -s acausal
-
-
-
- acausal is also implemented in Golang. You can find the module here:
-A Random Distribution is a simple model which can simulate picks from a weighted distribution of items.
-Distributions can be used to model random draws from a discrete collection of items, where each item has a different probability of appearing.
-Example Use Cases:
-Distribution Quickstart Example - Card Deck:
-import { Distribution } from 'acausal';
// Create a Deck of Cards
const suits = ['♣️', '♦️', '♥️', '♠️'];
const ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
// Combine the Suits and Ranks
const cards = suits.reduce((last, suit) => {
return [...last, ...ranks.map(rank => `${rank}${suit}`)];
}, []);
/* Should result in:
[
'A♣️', '2♣️', '3♣️', '4♣️', '5♣️', '6♣️', '7♣️',
'8♣️', '9♣️', '10♣️', 'J♣️', 'Q♣️', 'K♣️', 'A♦️',
'2♦️', '3♦️', '4♦️', '5♦️', '6♦️', '7♦️', '8♦️',
'9♦️', '10♦️', 'J♦️', 'Q♦️', 'K♦️', 'A♥️', '2♥️',
'3♥️', '4♥️', '5♥️', '6♥️', '7♥️', '8♥️', '9♥️',
'10♥️', 'J♥️', 'Q♥️', 'K♥️', 'A♠️', '2♠️', '3♠️',
'4♠️', '5♠️', '6♠️', '7♠️', '8♠️', '9♠️', '10♠️',
'J♠️', 'Q♠️', 'K♠️'
]
*/
// Create weighted source data for the Distribution
const src = cards.reduce((last, card) => ({ ...last, [card]: 1 }), {});
/* Should result in:
{
'A♣️': 1,
'2♣️': 1,
'3♣️': 1,
...
'J♠️': 1,
'Q♠️': 1,
'K♠️': 1,
}
*/
// Create the Distribution from the deck.
const deck = new Distribution({
seed: 23, // Random Seed - if this is empty it will be generated.
source: src, // The weighted source to generate the normalized Distribution from.
});
// Add in 2 Jokers
deck.add('🃏', 2);
// Generate 4 picks from the deck without replacement.
const picks = deck.pick(4, undefined, true);
console.log(picks);
/* Should print:
[ 'J♣️', '10♠️', '3♦️', '9♣️' ]
*/
-
-You can learn more about how to use Random Distributions with acausal in the Random Distribution Quickstart.
- - -A Markov Chain is a mathematical model of a system in which the future state of the system depends only on its present state.
-Markov Chains are usually generated by building a statistical model off of sample data, such as a list of names, which can then be used to output sequences which resemble the sampled data. A useful property of this process is that sample data can be "mixed" together like paint to achieve a desired result.
-For example, if you wanted to generate names which sounded like a mix of Irish and Japanese, you could generate a Markov Chain from a sample of Irish and Japanese names and the resulting model would be able to output names that mixed the two.
-Markov Chain Quickstart Example - Name Generator:
-import { MarkovChain } from 'acausal';
// Sample Data
const jpNames = ['honoka', 'akari', 'himari', 'mei', 'ema'];
const ieNames = ['grace', 'fiadh', 'emily', 'sophie', 'ava'];
const names = [...jpNames, ...ieNames];
// Prepare Data Source - the class expects an array of arrays.
const src = names.map(name => name.split(''));
/* Should result in:
[
[ 'h', 'o', 'n', 'o', 'k', 'a' ],
[ 'a', 'k', 'a', 'r', 'i' ],
[ 'h', 'i', 'm', 'a', 'r', 'i' ],
[ 'm', 'e', 'i' ],
[ 'e', 'm', 'a' ],
[ 'g', 'r', 'a', 'c', 'e' ],
[ 'f', 'i', 'a', 'd', 'h' ],
[ 'e', 'm', 'i', 'l', 'y' ],
[ 's', 'o', 'p', 'h', 'i', 'e' ],
[ 'a', 'v', 'a' ]
]
*/
// Create the Markov Chain from the source data.
const chain = new MarkovChain({
seed: 33, // Random Seed - if this is empty it will be generated.
maxOrder: 2, // Maximum Order - Chain will generate orders up to this value.
sequences: src, // Source data, expects an array of arrays.
});
// Generate 5 picks.
for (let i = 0; i < 3; i += 1) {
const pick = chain.generate({
min: 4, // Min Picks - This will force the model to pick at least 4 times.
max: 10, // Max Picks - Stops generation after 10 picks if no end has been reached.
order: 2, // Order - The largest gram size used to calculate the next pick.
strict: false // Strict Order - Dynamically adjusts order up or down each pick if false.
});
console.log(pick.join(''));
}
/* Should print:
sophimari
emari
hie
*/
-
-You can learn more about how to use Markov Chains with acausal in the Markov Chain Quickstart
- - -For documentation of underlying classes and functions, please see the API documentation.
-Generated using TypeDoc
Generated using TypeDoc
Generated using TypeDoc
Generated using TypeDoc
Generated using TypeDoc
Generated using TypeDoc
Generated using TypeDoc
Generated using TypeDoc
Generated using TypeDoc
Generated using TypeDoc
Generated using TypeDoc
Generated using TypeDoc
Generated using TypeDoc
Generated using TypeDoc
Generated using TypeDoc
Generated using TypeDoc
| From \ To | + {uniqueStates!.map(target => ( +{formatState(target)} | + ))} +
|---|---|
| {formatGramId(sourceId)} | + {matrix![rowIdx]!.map((prob, colIdx) => ( +0 ? `${ACCENT} ${prob * 0.85})` : undefined, + color: + prob > 0.5 + ? 'var(--sl-color-black)' + : prob > 0 + ? 'var(--sl-color-white)' + : 'var(--sl-color-gray-4)', + fontWeight: prob > 0.5 ? 600 : 400, + }} + > + {prob > 0 ? prob.toFixed(2) : '\u2014'} + | + ))} +
Class
- -