Expressions
Since version 9.0
Introduction
Bayes Server uses a subset of the C# language for its expressions. It is also 99% compatible with Java. The language is feature rich, including support for:
- Variables
- Multi-line statements
for
loopsif
statementsswitch
statements- Ternary operator ( e.g. b ? 1.0 : 0.0 )
- Dot notation ( e.g. c.States.Count )
- Read only object model to access information about variables, nodes, states etc...
- Blocks
{ }
var
keyword for implicit typing. ( e.g.var x = 3.0;
)break
statementscontinue
statements
Note that expressions must return a value using the return keyword and semi-colons are required (e.g. return 3.0;
)
Expressions are used in Function nodes and Table expressions.
Primitive types
The following primitive types are supported.
double
int
(32 bit)bool
string
Proxy object model
A read-only set of objects representing the Bayesian network are accessible. These objects are called Proxy objects, as they are not directly equivalent to the objects in the full API, however allow you to access a subset of read-only members.
For example, if you have an alias x
for a variable in your network you can use code such as x.States.Count
Properties of proxy objects
- They are read-only.
- They only have a subset of members that the full API does.
- They have a
Proxy
suffix to distinguish them from their counterparts in the full API.
Proxy objects:
Details of each proxy object can be found in the built-in objects section.
VariableProxy
StateProxy
IntervalProxy
NodeProxy
NodeVariablesProxy
StatesProxy
LinkProxy
NodeLinksProxy
Context object
All expressions have a built-in variable ctx
which is known as the Context Object. It has a common set of members available to all expression types, and then additional members specific to the expression type. For example:
- Code such as
ctx.Variable("variable name", true)
is available to all expression types - Code such as
ctx.CurrentState(alias)
is only available in Table expressions.
Aliases
In an expression there are 2 ways you can reference a variable.
- Using an expression alias.
- An alias can be assigned to any variable ( e.g.
x
) and referred to directly in an expression ( e.g.return x.States.Count;
)
- An alias can be assigned to any variable ( e.g.
- Via name from the context object
- e.g.
return ctx.Variable("X", true).States.Count;
- e.g.
Return types
When expressions are defined, they must indicate what type of value they will return. See the specific type of expression for the range of return types allowed.
Namespaces
Some built-in functions are contained within namespaces.
e.g. Bayes.Stats.Normal(x, mean, variance)
This is to avoid name clashes, as we will be adding to the available built-in functions, and do not want to break existing code.
Built-in classes
The following functions/fields are available in the expression library.
Math
Math
is a static class available in the global namespace. Provides constants and static methods for trigonometric, logarithmic, and other common mathematical functions.
public const double E // Represents the natural logarithmic base, specified by the constant, e.
public const double PI // Represents the ratio of the circumference of a circle to its diameter, specified by the constant, π.
public static double Pow(double x, double y) // Returns a specified number raised to the specified power.
public static double Max(double val1, double val2) // Returns the larger of two double-precision floating-point numbers.
public static double Min(double val1, double val2) // Returns the smaller of two double-precision floating-point numbers.
public static double Sqrt(double d) // Returns the square root of a specified number.
public static double Sin(double a) // Returns the sine of the specified angle (in radians).
public static double Sinh(double value) // Returns the hyperbolic sine of the specified angle (in radians).
public static double Asin(double d) // Returns the angle in radians whose sine is the specified number.
public static double Cos(double d) // Returns the cosine of the specified angle (in radians).
public static double Cosh(double value) // Returns the hyperbolic cosine of the specified angle in radians.
public static double Acos(double d) // Returns the angle in radians whose cosine is the specified number.
public static double Tan(double a) // Returns the tangent of the specified angle (in radians).
public static double Tanh(double value) // Returns the hyperbolic tangent of the specified angle (in radians).
public static double Atan(double d) // Returns the angle in radians whose tangent is the specified number.
public static double Atan2(double y, double x) // Returns the angle (in radians) whose tangent is the quotient of the y coordinate of a point and the x coordinate of a point in the Cartesian plane.
public static double Exp(double d) // Returns e raised to the specified power.
public static double Abs(double value) // Returns the absolute value of a double-precision floating-point number.
public static int Abs(int value) // Returns the absolute value of a 32-bit signed integer.
public static double Ceiling(double d) // Returns the smallest integral value that is greater than or equal to the specified double-precision floating-point number.
public static double Floor(double d) // Returns the largest integer less than or equal to the specified double-precision floating-point number.
public static double Log(double d) // Returns the natural (base e) logarithm of a specified number.
public static double Log(double d, double newBase) // Returns the logarithm of a specified number in a specified base.
Assert
Assert
is a static class available in the global namespace. A collection of helper classes to test various conditions within expressions. If the condition being tested is not met, an exception is thrown.
public static void Fail(string message) // Throws an assertion exception.
public static void IsTrue(bool condition, string message) // Tests whether the specified condition is true and throws an exception if the condition is false.
public static void IsFalse(bool condition, string message) // Tests whether the specified condition is false and throws an exception if the condition is true.
Beta
A Beta distribution over the interval [0,1].
public static double LogPdf(double x, double alpha, double beta) // Evaluates the natural logarithm of the density function.
public static double Pdf(double x, double alpha, double beta) // Evaluates the density function, with shape parameters alpha and beta.
Binomial
Binomial distribution over the integers [0,n]
public static double Pmf(int successCount, int trialCount, double probSuccess) // Evaluates the probability mass function of the Binomial distribution.
public static double LogPmf(int successCount, int trialCount, double probSuccess) // Evaluates the natural logarithm of the probability mass function of the Binomial distribution.
Gamma
A Gamma distribution on positive reals.
public static double Pdf(double x, double shape, double scale) // Evaluates the density function.
public static double LogPdf(double x, double shape, double scale) // Evaluates natural logarithm of the density function.
Normal
Represents a one-dimensional Normal/Gaussian distribution.
public static double Pdf(double x, double mean, double variance) // Evaluates a one-dimensional Normal/Gaussian density
public static double LogPdf(double x, double mean, double variance) // Evaluates the log of a one-dimensional Normal/Gaussian density.
Poisson
A Poisson distribution over the integers [0,infinity).
public static double Pmf(int value, double mean) // Evaluates the Probability mass function (Pmf) of a Poisson distribution.
public static double LogPmf(int value, double mean) // Evaluates the natural logarithm of the Probability mass function (Pmf) of a Poisson distribution.
Triangle
public static double LogPdf(double x, double left, double middle, double right) // Evaluates the natural logarithm of the probability density function of the Triangle distribution.
public static double Pdf(double x, double left, double middle, double right) // Evaluates the probability density function of the Triangle distribution.
TruncatedNormal
Distribution parameterized by a Normal/Gaussian and both a LowerBound and UpperBound. Between the bounds, the density is proportional to the Normal/Gaussian. Outside of the bounds, the density is zero.
public static double Pdf(double value, double mean, double variance, double lowerBound, double upperBound) // Evaluates the probability density function of the Truncated Normal/Gaussian distribution.
public static double LogPdf(double value, double mean, double variance, double lowerBound, double upperBound) // Evaluates the natural logarithm of the probability density function of the Truncated Normal/Gaussian distribution.
IntervalProxy
A state interval representing a range of continuous values.
public readonly double Minimum // The minimum value of the interval.
public readonly double Maximum // The maximum value of the interval.
public bool Contains(double value) // Determines whether a value falls within the interval.
NodeProxy
A node in the network.
public readonly string Name // The name of the node.
public readonly string Description // The node description.
public readonly NodeVariablesProxy Variables // The collection of variables belonging to the node.
public readonly NodeLinksProxy Links // The collection of both incoming and outgoing links for the node.
public readonly NodeLinksProxy LinksIn // The collection of incoming links for the node.
public readonly NodeLinksProxy LinksOut // The collection of outgoing links for the node.
LinkProxy
Represents a link in the network.
public readonly NodeProxy To // The child node of the directed link.
public readonly NodeProxy From // The parent node of the directed link.
public readonly string Description // Optional description for the link.
public readonly int TemporalOrder // The temporal order.
public readonly NoisyOrder NoisyOrder // When the child is noisy node, determines the kind of noisy relationship.
NodeVariablesProxy
The collection of nodes belonging to a variable.
public readonly int Count // The number of variables in the node.
public VariableProxy Get(int ix) // Gets the (zero based) i-th variable belonging to the node.
public VariableProxy Get(string name, bool throwIfNotFound) // Gets a variable by name, optionally throwing an exception if not found.
public VariableProxy First() //Gets the first variable, or raises an exception if the node has no variables.
public VariableProxy Last() //Gets the last variable, or raises an exception if the node has no variables.
NodeLinksProxy
A collection of links for a node.
public readonly int Count // The number of links in the collection.
public VariableProxy Get(int ix) // Gets the (zero based) i-th link in the collection.
StateProxy
Represents a state of a variable.
public readonly int Index // The zero based index of the state.
public readonly string Name // The name of the state.
public readonly string Description // The state description.
public readonly object Value // The state value (e.g. an interval).
public readonly VariableProxy Variable // The variable this state belongs to.
StatesProxy
The collection of States in a variable.
public readonly int Count // The number of states in the variable.
public StateProxy Get(int ix) // Gets the (zero based) i-th state belonging to the variable.
public StateProxy Get(string name, bool throwIfNotFound) // Gets a state by name, optionally throwing an exception if not found.
public StateProxy First() // Gets the first state.
public StateProxy Last() // Gets the last state.
VariableProxy
Represents a Variable in a network.
public readonly string Name // The name of the variable.
public readonly string Description // The variable description, if any.
public readonly string ExpressionAlias // The variable's expression alias, if any.
public readonly StatesProxy States // The collection of states of the variable.
public NodeProxy Node // The node this variable belongs to.
TableExpressionContext
A context object available in Table expressions.
public VariableProxy Variable(string name, bool throwIfNotFound) // Returns a variable in the network by name (case sensitive), optionally throwing an exception if not found.
public NodeProxy Node(string name, bool throwIfNotFound) // Returns a node in the network by name (case sensitive), optionally throwing an exception if not found.
public StateProxy CurrentState(VariableProxy variable) // Gets the current state of a variable, when generating a table from an expression.
public StateProxy CurrentState(VariableProxy variable, int time) // Gets the current state of a variable at a specific zero based time, when generating a table from an expression.
public Random Random // Since version 10. Returns an instance of the Random class, which is a random number generator.
FunctionExpressionContext
A context object available in Function expressions.
public VariableProxy Variable(string name, bool throwIfNotFound) // Returns a variable in the network by name (case sensitive), optionally throwing an exception if not found.
public NodeProxy Node(string name, bool throwIfNotFound) // Returns a node in the network by name (case sensitive), optionally throwing an exception if not found.
public double Mean(VariableProxy variable) // Gets the mean of a continuous query.
public double Mean(VariableProxy variable, int time) // Gets the mean of a continuous query, at a specified zero-based time.
public double Variance(VariableProxy variable) // Gets the variance of a continuous query.
public double Variance(VariableProxy variable, int time) // Gets the variance of a continuous query at a specific zero-based time.
public double TableValue(StateProxy state) // Gets the value of a discrete variable query.
public double TableValue(StateProxy state, int time) // Gets the value of a discrete variable query at a specified zero-based time.
public double LogLikelihood() // Returns the log-likelihood of the query.
public object FunctionValue(VariableProxy variable) // Retrieves the value of another function that has already been evaluated.
public Random Random // Since version 10. Returns an instance of the Random class, which is a random number generator.
Random
Since version 10. A random number generator available in both Function and Table expressions from the context object.
public double NextDouble() // Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.
public int NextInt(int minValue, int maxValue) // Returns a random integer that is greater than or equal to minValue, and less than maxValue.