Skip to content

wo/webppl-rsa

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WebPPL-RSA

This package contains a few helper functions to model speakers and hearers in the RSA framework.

Functions

Agent(params:dict) → {Agent}

Defines a representation of an agent (a speaker or hearer).

params is an associative array with the following keys:

  • credence: a Distribution or a dictionary of states and their probabilities
  • options (optional): a list of possible acts/utterances
  • utility (optional): a function that takes an act and a state and returns a number
  • alpha (optional): if set, controls the agent’s softmax rationality; if unset, the agent maximizes expected utility
  • kinematics (optional): a function that specifies how the agent updates its credence in response to an input
  • inferParams (optional): optional update parameters: ‘inferMethod’, ‘new_p’

see choice and learn.

Examples:

var speaker = Agent({
   credence: { 'rain': 0.2, 'sun': 0.8 },
`  options: ['A', 'B', 'C'],
   utility: function(act, state) { return act == 'A' ? (state == 'rain' ? 1 : 0) : 0; }
});
viz(choice(speaker));
var hearer = Agent({
    credence: { 'rain': 0.2, 'sun': 0.8 },
    kinematics: function(utterance) {
        return function(state) {
            return state == utterance;
         };
    }
});
viz(learn(hearer, 'rain'));

bestOption(agent:Agent, threshold:Number, alpha:Number) → {Object}

Returns the uniquely best option for an agent if there is one, else false.

This function computes a softmax distribution over the agent’s options and checks if the best option is at least threshold more likely than the second best.

cell(question:Function, state:Object, states:Array) → {Array}

Returns all elements of states for which the output of question is equal to state.

choice(agent:Agent) → {Distribution}

Computes a distribution over the agent’s options based on their credences and utilities.

If the agent is an expected utility mazimizer, the distribution is a uniform distribution over all options that maximize EU.

Credence(dict) → {Distribution}

Converts a dictionary into a Categorical distribution.

dict is an associative array {key1: prob1, key2: prob2, …}. The returned distribution is a Categorical distribution over the keys in dict.

Cross(args:Array|dict) → {dict}

Defines an algebra of states by combining the dimensions of its arguments.

Examples:

var weight = [{ weight: 1 }, { weight: 2 }, { weight: 3 }];
var colour = [{ colour: 'red' }, { colour: 'green' }, { colour: 'blue' }];
// Think of these as discrete random variables.
var states = Cross(weight, colour);
// [ { weight: 1, colour: 'red' }, { weight: 1, colour: 'green' }, ... ]
var states = Cross({ weight: [1,2,3], colour: ['red','green','blue'] });
// [ { weight: 1, colour: 'red' }, { weight: 1, colour: 'green' }, ... ]
var states = Cross('sunny', 'cold');
// [ { 'sunny': false, 'cold': false }, { 'sunny': false, 'cold': true }, ... ] 

evaluate(function:Function, arg:Object) → {Object}

Applies the function to the argument and returns the result.

Indifferent(list) → {Distribution}

Converts a list into a flat Categorical distribution over its elements.

join(dict) → {Distribution}

Returns the joint distribution of several distributions, treated as independent.

Example:

var d1 = Credence({ 'rain': 0.2, 'sun': 0.8 });
var d2 = Credence({ 'cold': 0.3, 'warm': 0.7 });
var d = join({ 'weather': d1, 'temperature': d2 });

keys(dict) → {list}

Returns the keys of a dictionary as a list.

learn(agent:Agent, input:Object) → {Distribution}

Updates an agent’s credence by some input in accordance with their kinematics function. Returns the posterior distribution.

numWords(string) → {number}

Returns the number of words in a string.

Partition(list:Array, function:Function) → {list}

Partitions an array into an array of arrays based on the return value of a function.

prob(dist:Distribution, event:Object) → {number}

Returns the probability of an event under a distribution. The event can be either a state, an array of states, a function state → bool, or a value of a multidimensional state, e.g. { ‘a’: 7 }.

showKinematics(agent:Agent, inputs:Array) → {}

Prints the agent’s doxastic response to each element in the given list.

showChoices(agentFn:Function, params1:Array, params2:Array) → {}

Prints the choice dispositions for a family of agents.

showBestOption(agentFn:Function, params1:Array, params2:Array) → {}

Prints the best option for a family of agents.

showDecisionMatrix(Agent) → {}

Prints the agent’s decision matrix.

update(dist:Distribution, event:Object, params:Dict) → {Distribution}

Conditionalizes a distribution on an event. The event can be either a state, an array of states, a function state → bool, or a value of a multidimensional state, e.g. { ‘a’: 7 }. The optional params argument can specify:

  • new_p: the new probability of the event, trigger an update by Jeffrey conditioning,
  • inferMethod: the method passed to the WebPPL ‘infer’ function; defaults to ‘enumerate’.

About

A webppl package for Rational Speech Act models

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published