# A primary question (Migrated from community.research.microsoft.com) • ### Question

• Ali Yaghoubi posted on 02-28-2011 7:07 AM

Hi,

I am a beginner at Infer.Net.

Would you please tell me what exactly this line means?

means[k] = Variable.VectorGaussianFromMeanAndPrecision(new Vector(0.0,0.0),

PositiveDefiniteMatrix.IdentityScaledBy(2,0.01)).ForEach(k);

I mean what new Vector does here?

and this:

``` Variable<Vector> weights = Variable.Dirichlet(k, new double[] { 1, 1 }); Thanks, ali ```

Friday, June 3, 2011 6:22 PM

• John Guiver replied on 02-28-2011 9:02 AM

Hi Ali

Thanks for the question. Because Infer.NET is written as an API it is difficult when you are starting out to figure out exactly what’s going on – especially if you are fairly new to .NET and to C#.

When you ask about new Vector, are you asking about new or Vector? If the former, this is basic C# constructor syntax and I refer you to any primer on C#. If the latter, Vector is an Infer.NET class representing arrays of double values which you can read about in the section on vectors and matrices in the user guide or in the Maths section of the API document. Similarly, PositiveDefiniteMatrix is a positive definite matrix class which you can also read about there.

Actually, the Vector class no longer has a constructor which takes a list of values as you show in the example code. The equivalent for recent versions of Infer.NET is Vector.FromArray(0.0,0.0)which constructs an instance of type Vector with two elements both of which are equal to 0.0.

Variable<T> where T is some type (such as double or Vector ) is an Infer.NET type. The angle bracket is C# syntax for generic types which you can read about in a C# book. Variable<T> represents a random variable over the domain T. So Variable<Vector> represents a random variable over a vector domain - i.e. the random variate is of type Vector. You can read about Variable<T> here (and also in the model section of the API documentation).

Similarly VariableArray<T> is an Infer.NET type representing a variable over the domain T which sits in a graphical model plate. The indexing for a plate uses an Infer.NET type Range. Infer.NET overrides the standard C# indexing to allow an instance of type VariableArray<T> to be indexed by an instance of type Range. (Other more complex types of plate are also possible).

Variable is an Infer.NET class whose main purpose is to provide static methods representing different ways of creating random variables (see a C# book for static). You can think of the class as a factory for creating random variables. Almost all the code to build your model will involve calls to methods on this class.

In the line of code you refer to, the static method VectorGaussianFromMeanAndPrecision is invoked to provide a statistical definition for 'means'. This method returns a random variable of type Vector whose prior distribution is a multivariate Gaussian distribution (VectorGaussian)with the given mean vector and positive definite matrix precision. 'means' has previously been defined as a VariableArray<Vector> and 'k' has been defined as a Range. 'means[k]' is expecting a stream of variables tagged with the range k, and this can be achieved by calling ForEach(k) method on the returned random variable as described here.

So the sequence of events that occurs when this line of code is executed is as follows:

1.  A dense vector of 2 elements is created (Vector.FromArray(0.0,0.0))

2.  A positive definite matrix is created (PositiveDefiniteMatrix.IdentityScaledBy(2,0.01))

3.  A random variable with the given prior is formed (Variable.VectorGaussianFromMeanAndPrecision(...))

4.  Multiple identically-defined copies of the random variable are created  (.ForEach(k))

5.  Which are then assigned to the elements of 'means' (means[k] = ...)

Note that no model compilation or inference happen at this point. Executing this line of code merely adds to the definition of the model. You can think of the execution of each line of modeling code as adding an addidional variable and factor to a factor graph.

The second line you mention is similar but more straightforward. Here we are defining a scalar random variable 'weights' to have a Dirichlet prior of the given specification. The variate type of a Dirichlet-distributed variable is a Vector, so ‘weights’ is of type Variable<Vector>).See the section on Variable types and their distributions in the user guide.

John

Friday, June 3, 2011 6:22 PM
{"ft_lang_label":"__label__en","ft_lang_prob":0.88846296,"math_prob":0.8475579,"size":3900,"snap":"2022-40-2023-06","text_gpt3_token_len":883,"char_repetition_ratio":0.13578029,"word_repetition_ratio":0.012598425,"special_character_ratio":0.21230769,"punctuation_ratio":0.101583116,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9900214,"pos_list":[0,1,2,3,4,5,6,7,8],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-09-28T06:53:44Z\",\"WARC-Record-ID\":\"<urn:uuid:f8ca0021-67ce-4588-87f5-060fba8762be>\",\"Content-Length\":\"62451\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2514e096-ba24-4d19-b192-85bbc3794679>\",\"WARC-Concurrent-To\":\"<urn:uuid:10c002c1-c57b-4170-ad06-6c57f4130d1d>\",\"WARC-IP-Address\":\"13.107.213.40\",\"WARC-Target-URI\":\"https://social.microsoft.com/Forums/en-US/1399d3a1-a157-4595-9b2e-018939811149/a-primary-question-migrated-from-communityresearchmicrosoftcom?forum=infer.net\",\"WARC-Payload-Digest\":\"sha1:B2ROEF73RSZPP6UNJ5IN3T4ECNGDD5DS\",\"WARC-Block-Digest\":\"sha1:CDZTYKPYATUCRY2VRUGXNYJQH6Y4FU77\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030335124.77_warc_CC-MAIN-20220928051515-20220928081515-00296.warc.gz\"}"}