Difference between revisions of "Meta-Inference"
Line 47: | Line 47: | ||
= Treatment of Handles in Local variables = | = Treatment of Handles in Local variables = | ||
− | + | There are four constructs for declaring local variables (excluding local indexes) within an expression: | |
+ | :[[Var]] x := ...; | ||
+ | :[[For]] x := ...; | ||
+ | :[[MetaVar]] x := ...; | ||
+ | :[[LocalAlias]] x := ...; | ||
− | + | When a [[handle]] is assigned to a local variable, the local variable may be treated either as a holder of a handle object, or as an alias to the object pointed to by the handle. The last two constructs ([[MetaVar]] and [[LocalAlias]]) make this distinction explicitly. When ''x'' is declared using [[MetaVar]], it acts as a holder of a handle object, while when it is declared using [[LocalAlias]], it acts as an alias of the object. When you are creating meta-inference algorithms, it is advisable to use these two constructs, rather than [[Var]] and [[For]], when holding handles. You can assign [[handle]]s to locals using [[Var]] and [[For]], but the semantics is a hybrid between [[MetaVar]] and [[LocalAlias]]. It is simply easier to learn and understand if you use [[MetaVar]] and [[LocalAlias]] exclusively. | |
− | + | When using [[LocalAlias]], the local variable acts as an exact alias for the object pointed to by the handle. For example, the following are exactly equivalent: | |
− | + | :[[LocalAlias]] x := [[Handle]](Va1) Do x^2 | |
− | + | :Va1^2 | |
− | + | In general, if you take an expression of the form: | |
− | + | :[[LocalAlias]] x := [[Handle]](y) Do «body» | |
− | [[For..Do | + | and then take only the «body» part and replace every occurrence of ''x'' in «body» with y, the resulting expression will be exactly equivalent. |
− | + | ||
+ | When ''x'' is declared using [[LocalAlias]], ''x'' must be the alias of exactly one object. Hence, if you assign a list of handles to ''x'', the [[LocalAlias]] construct loops over the [[handle]]s, evaluating «body» each time. For example: | ||
+ | |||
+ | :[[LocalAlias]] x := [[HandleList]](Va1,Va2,Va3) Do [[Size]](x) | ||
+ | |||
+ | Evaluates <code>[ [[Size]](Va1), [[Size]](Va2), [[Size]](Va3) ]</code>. | ||
+ | |||
+ | In other cases, you may want to manipulate a hold a [[handle]]s in the local variable, and treat it as a [[handle]] object. In these cases, use [[MetaVar]]. | ||
+ | |||
+ | Consider the following set of three variables: | ||
+ | |||
+ | :Variable A := 5 | ||
+ | :Variable B := [[MetaVar]] x := [[Handle]](A) Do x | ||
+ | :Variable C := [[LocalAlias]] x := [[Handle]](A) Do x | ||
+ | |||
+ | The result of ''B'' is a [[handle]] to ''A''. The result of ''C'' is 5. | ||
+ | |||
+ | As another example, consider the following expression: | ||
+ | |||
+ | :[[MetaVar]] x := [[Handle]](A); | ||
+ | :[[LocalAlias]] y := [[Handle]](B); | ||
+ | :x := [[Handle]](C); | ||
+ | :y := [[Handle]](D); | ||
+ | :... | ||
+ | |||
+ | Finally, consider these: | ||
+ | :Variable A := 3.1 | ||
+ | :[[LocalAlias]] x := [[Handle]](A) Do [[TypeOf]](x) → "Number" | ||
+ | :[[MetaVar]] x := [[Handle]](A) Do [[TypeOf]](x) → "Variable" | ||
+ | |||
+ | When this expression is in a [[User-Defined Function]] that is called from a [[Scripting Guide|button script]], once the code shown is evaluated, ''x'' holds a handle to ''C'' (and the variable ''C'' is unchanged), while ''y'' is still an alias of ''B'' by ''B'' is now defined as ''D''. | ||
+ | |||
+ | You can loop over a collection of handles using [[MetaVar]] by declaring dimensionality, using an empty list of indexes to list over atomic elements. For example: | ||
+ | |||
+ | :[[MetaVar]] x[] := [[ListOfHandles]](Va1,Va2,Va3) Do «body» | ||
+ | |||
+ | will evaluate «body» three times with ''x'' holding a [[handle]] to ''Va1'', then a [[handle]] to ''Va2'', and finally a [[handle]] to ''Va3''. | ||
+ | |||
+ | Sometimes you may have a [[MetaVar]] containing a [[handle]] or a list of [[handle]]s, and you'd like to create an alias to that object. This is often useful when you want to use the object as an index. You can do this by assigning the [[MetaVar]] to a [[LocalAlias]]. | ||
+ | |||
+ | :[[Var]] s := A { hold the sum } | ||
+ | :[[MetaVar]] inds := [[IndexesOf]](A); { This is a list of handles to the indexes of ''A''} | ||
+ | :[[LocalAlias]] J := inds; { This iterates over the indexes } | ||
+ | :s := [[Sum]](s,J) | ||
+ | |||
+ | You can convert between [[MetaVar]] and [[LocalAlias]] semantics using the [[Handle]](..) and [[Evaluate]](..) functions. For example, if you have a local variable, ''x'', declared using [[LocalAlias]], and you find yourself needing the [[handle]] instead, use [[Handle]](x). Conversely, if you have a local declared using [[MetaVar]] and you which you has the value of the underlying object, then use [[Evaluate]](x). The one exception is if you need to use it as an index, in which case you should declare another local using [[LocalAlias]] as shown in the previous example. | ||
+ | |||
+ | When defining a [[User-Defined Function]], various [[Function Parameter Qualifiers]] can also be used to control whether the local variable acts according to the [[MetaVar]] or the [[LocalAlias]] semantics. When you declare a variable using the [[Function Parameter Qualifiers#Handle|Handle]] qualifier, this specifies that the data type of the value held is a [[Handle]] -- endowing the local variable with [[MetaVar]] semantics. When you use the [[Function Parameter Qualifiers#Variable|Variable]] or [[Function Parameter Qualifiers#Object|Object]] qualifier, then the parameter acts as an alias. | ||
= Evaluation = | = Evaluation = |
Revision as of 17:07, 28 January 2011
(TBD: Under construction) (This is advanced reference that is beyond the scope of the user guide)
Introduction
Meta-Inference refers to logic in your model that reasons about (or manipulates) the model itself. For example, an expression that identifies all Chance-node ancestors of variable X is performing meta-inference.
Meta-inference is an advanced topic, appropriate only for the more experienced Analytica modelers. However, the ability to create meta-inferential algorithms opens many doors for expanding the capabilities of Analytica itself. For example, in the past, users of Analytica implemented meta-inferential algorithms including the following:
- Providing an advanced "Find object" form, that locates objects matching rich search criteria.
- Changing the colors or sizes of nodes in influence diagrams to reflect the results of sensitivity analyses.
- Changing the colors of nodes to reflect a special color scheme.
- Implementing specialized forms of sensitivity analyses.
- Running stochastic simulations in batches, so that huge models or huge sample sizes can be utilized within the constraints of available memory.
- Collecting time/memory profiling information after demanding computations.
Other examples of potential meta-inferential algorithms could also include:
- Triangulation and propagation algorithms to compute discrete posterior probabilities in Markov field models such as Bayesian networks.
- Algorithms to automatically re-arrange (layout) variables in an influence diagram.
Objects and Handles
Meta-inference involves reasoning about Analytica objects, including the various nodes that appear on diagrams (Variables, indexes, buttons, etc), and the attributes that constitute the properties of those objects. The fundamental unit of representation is a handle to an object, which we refer to as a handle (or varTerm).
While there is at most one instantiation of any given object, there may be any number of handles that refer to that object. There are many ways in which you might obtain a handle to an object from an Analytica expression. The most straightforward is by using the Handle function. If Va1 is the identifier of an object, then
Handle(Va1)
returns a handle to that object. This handle unambiguously refers to that particular object. Below several other methods for obtaining handles are identified. You can also obtain a handle for a global object using the HandleFromIdentifier function.
Lists and Indexes of Handles
Often, meta-inference algorithms manipulate lists of handles, or utilize indexes where the index elements are handles.
A global index can be defined as a list of identifiers. You can do this by selecting "List" from the definition type pulldown, and entering identifers of existing objects as the elements of the index. When you do this, the IndexValue of the index consists of handles to objects. If you define Index In1 in this fashion and evaluate IndexValue(In1), you will obtain a list of handles. Each of the element objects will be evaluated when your index is evaluated, so these must be well-defined variables.
A local index can be defined as a list of handles using the MetaIndex..Do construct. For example, the following defines an index, I, as the objects contained in Module Mo1, and computes an array based on this index containing all the descriptions of those objects:
MetaIndex I := Contains of Mo1; Description of I
When you are performing meta-inference, you should generally use MetaIndex..Do rather than Index..Do. While both can be used, they differ in how the local index behaves when used in a Subscript operation. MetaIndex..Do can be used without the elements being evaluated, and hence is what is generally desired.
The Display of Handles
When a Handle is displayed in a result table, its title is shown in the table cell, unless Show By Identifiers is on, in which case its identifier is displayed. Show by identifier is toggled via the Object menu, or by pressing CTRL-Y.
If you double click on a cell containing a handle, Analytica jumps to the object window for that object.
Treatment of Handles in Local variables
There are four constructs for declaring local variables (excluding local indexes) within an expression:
- Var x := ...;
- For x := ...;
- MetaVar x := ...;
- LocalAlias x := ...;
When a handle is assigned to a local variable, the local variable may be treated either as a holder of a handle object, or as an alias to the object pointed to by the handle. The last two constructs (MetaVar and LocalAlias) make this distinction explicitly. When x is declared using MetaVar, it acts as a holder of a handle object, while when it is declared using LocalAlias, it acts as an alias of the object. When you are creating meta-inference algorithms, it is advisable to use these two constructs, rather than Var and For, when holding handles. You can assign handles to locals using Var and For, but the semantics is a hybrid between MetaVar and LocalAlias. It is simply easier to learn and understand if you use MetaVar and LocalAlias exclusively.
When using LocalAlias, the local variable acts as an exact alias for the object pointed to by the handle. For example, the following are exactly equivalent:
- LocalAlias x := Handle(Va1) Do x^2
- Va1^2
In general, if you take an expression of the form:
- LocalAlias x := Handle(y) Do «body»
and then take only the «body» part and replace every occurrence of x in «body» with y, the resulting expression will be exactly equivalent.
When x is declared using LocalAlias, x must be the alias of exactly one object. Hence, if you assign a list of handles to x, the LocalAlias construct loops over the handles, evaluating «body» each time. For example:
- LocalAlias x := HandleList(Va1,Va2,Va3) Do Size(x)
Evaluates [ Size(Va1), Size(Va2), Size(Va3) ]
.
In other cases, you may want to manipulate a hold a handles in the local variable, and treat it as a handle object. In these cases, use MetaVar.
Consider the following set of three variables:
- Variable A := 5
- Variable B := MetaVar x := Handle(A) Do x
- Variable C := LocalAlias x := Handle(A) Do x
The result of B is a handle to A. The result of C is 5.
As another example, consider the following expression:
Finally, consider these:
- Variable A := 3.1
- LocalAlias x := Handle(A) Do TypeOf(x) → "Number"
- MetaVar x := Handle(A) Do TypeOf(x) → "Variable"
When this expression is in a User-Defined Function that is called from a button script, once the code shown is evaluated, x holds a handle to C (and the variable C is unchanged), while y is still an alias of B by B is now defined as D.
You can loop over a collection of handles using MetaVar by declaring dimensionality, using an empty list of indexes to list over atomic elements. For example:
- MetaVar x[] := ListOfHandles(Va1,Va2,Va3) Do «body»
will evaluate «body» three times with x holding a handle to Va1, then a handle to Va2, and finally a handle to Va3.
Sometimes you may have a MetaVar containing a handle or a list of handles, and you'd like to create an alias to that object. This is often useful when you want to use the object as an index. You can do this by assigning the MetaVar to a LocalAlias.
- Var s := A { hold the sum }
- MetaVar inds := IndexesOf(A); { This is a list of handles to the indexes of A}
- LocalAlias J := inds; { This iterates over the indexes }
- s := Sum(s,J)
You can convert between MetaVar and LocalAlias semantics using the Handle(..) and Evaluate(..) functions. For example, if you have a local variable, x, declared using LocalAlias, and you find yourself needing the handle instead, use Handle(x). Conversely, if you have a local declared using MetaVar and you which you has the value of the underlying object, then use Evaluate(x). The one exception is if you need to use it as an index, in which case you should declare another local using LocalAlias as shown in the previous example.
When defining a User-Defined Function, various Function Parameter Qualifiers can also be used to control whether the local variable acts according to the MetaVar or the LocalAlias semantics. When you declare a variable using the Handle qualifier, this specifies that the data type of the value held is a Handle -- endowing the local variable with MetaVar semantics. When you use the Variable or Object qualifier, then the parameter acts as an alias.
Evaluation
Subscripting
Accessing attributes
Notable attributes
Contains, IsIn, Outputs, Inputs, DisplayOutputs, DisplayInputs, Identifier, Class, nodeSize, nodeLocation, nodeColor, nodeFontColor.
Manipulating Local Indexes
Parameters and Local Variables holding varTerms
Assignment
Looping over Handles
See Also
- category:Meta-Inference Functions
- Handle function
- HandleFromIdentifier
- IndexesOf
- Variable parameter qualifier
- MetaIndex..Do
- Attrib of Obj
Enable comment auto-refresher