locked
Realisation from Variable<Gaussian> to Variable<double> RRS feed

  • Question

  • Hi

    Suppose that we have two variables specifying the mean and variance of a normal distribution.

    int K = 5; Range rK = new Range(K); VariableArray<Gaussian> myMeanPrior = Variable.Array<Gaussian>(rK); VariableArray<Gaussian> myVarPrior = Variable.Array<Gamma>(K); myMeanPrior.ObservedValue = Enumerable.Repeat(Gaussian.FromMeanAndVariance(0,100),K).ToArray(); myVarPrior.ObservedValue = Enumerable.Repeat(Gamma.FromShapeAndScale(100,0.4),K).ToArray(); VariableArray<double> myMean; VariableArray<double> myVar;


    The parameters of the pirori are random variables which have been observed. Now I wish to assign observed values to the mean and the variance according to the observed parameters. Is there any way to use the pirori to specify the observed values of the mean and variance?

    For example, to create a Gaussian distribution using the observed apriori like the following:

    Gaussian[] myGausian = Gaussian(myMeanPriori);
    Gamma[] myGamma = Gamma(myVarPriori);
    myMean.ObservedValue = myGaussian.sample();
    myVar.ObservedValue = myGamma.sample();

    I know there are some syntax error in the above code block. But I hope I express my idea clearly in the above code block. Is there any way to achieve it in a syntax correct way?

    This link (https://social.microsoft.com/Forums/en-US/fe7b5fa2-3503-4c32-8ea3-1ee797adb5a9/how-to-generate-sample-data-from-a-set-of-bernoulli-variables?forum=infer.net) says Infer.NET doesn't support sampling data from a generative model. My question is related to this one but not the same.

    Thanks

    Monday, June 1, 2015 5:51 PM

Answers

  • If I understand the question correctly (which I'm not entirely sure about), then you're after the Random factor. If you define the variables this way instead of using FromMeanAndVariance/FromShapeAndScale, then you can observe the prior at runtime. A usage example can be found here.

    -Y-

    Thanks for your suggestion.

    After reading the example you mentioned, I realize that the following code can achieve my purpose.

    `myMeanPrior` is a random variable array statistically defined by a Gaussian. The distributions of the random variables are specified by

    myMeanPrior.ObservedValue = Enumerable.Repeat(Gaussian.FromMeanAndVariance(0,100),K).ToArray();

    Hence, in terms of syntax, the observed values of myMeanPrior are several Gaussian distributions. And we can `Sample()` from these distributions.

    myMean.ObservedValue[K] = myMeanPrior.ObservedValue[K].Sample();
    Since I'm new to Infer.NET and C#, I'm not sure if the usage of index above is correct. Anyway we can use a loop to replace it.


    • Marked as answer by VS2015 Monday, June 8, 2015 3:46 PM
    Tuesday, June 2, 2015 5:54 PM
    • I still don't understand what you want to achieve. Could you provide some more detail on the model? What do you need to infer?
    • In your code above, the mean is observed, so you're not trying to learn it. But then why do you need a prior on it?
    • Why do you sample at all? By doing this, you lose all of the uncertainty that is available to you. You can observe the prior of the mean, but why observe the mean itself?
    • You're mixing Infer.NET modelling code with the rest of your C# code. Try to isolate the definition of the model in one routine, and make sure this is the only routine which uses the Infer.NET modelling API.
    • Take a look at the Learning a Gaussian example.

    -Y-

    • Marked as answer by VS2015 Monday, June 8, 2015 3:46 PM
    Tuesday, June 2, 2015 11:51 PM
    • I still don't understand what you want to achieve. Could you provide some more detail on the model? What do you need to infer?
    • In your code above, the mean is observed, so you're not trying to learn it. But then why do you need a prior on it?
    • Why do you sample at all? By doing this, you lose all of the uncertainty that is available to you. You can observe the prior of the mean, but why observe the mean itself?
    • You're mixing Infer.NET modelling code with the rest of your C# code. Try to isolate the definition of the model in one routine, and make sure this is the only routine which uses the Infer.NET modelling API.
    • Take a look at the Learning a Gaussian example.

    -Y-

    This is only part of a program. Hidden variables are generated by Gaussian(myMean, myVar). And myMean has it prior distribution myMeanPrior. Observed values are affected by the hidden variables.

    To generate data, we need to specify the Gaussian to get the hidden variables, and use the hidden variables to get the observations. Hence we need specified values for myMean and myVar.

    For inference, we have observed data and a prior. We're trying to infer the hidden variables and the parameters. In this case, we need to observe the prior of the mean.

    I think previously I didn't figure out how to use InferenceEngine.Infer() method. I tried to output myMena.ObservedValue before observing it, so I got null exception. The assignment can solve this problem. But now I get a better understanding of the method, I find that we don't need to observe the mean for inference.

    Since I'm new to both C# and Infer.NET, I can not tell which is which. So I'm not sure which part should be isolated.

    • Marked as answer by VS2015 Monday, June 8, 2015 3:46 PM
    Wednesday, June 3, 2015 8:37 PM

All replies

  • If I understand the question correctly (which I'm not entirely sure about), then you're after the Random factor. If you define the variables this way instead of using FromMeanAndVariance/FromShapeAndScale, then you can observe the prior at runtime. A usage example can be found here.

    -Y-

    Monday, June 1, 2015 6:12 PM
  • If I understand the question correctly (which I'm not entirely sure about), then you're after the Random factor. If you define the variables this way instead of using FromMeanAndVariance/FromShapeAndScale, then you can observe the prior at runtime. A usage example can be found here.

    -Y-

    Thanks for your suggestion.

    After reading the example you mentioned, I realize that the following code can achieve my purpose.

    `myMeanPrior` is a random variable array statistically defined by a Gaussian. The distributions of the random variables are specified by

    myMeanPrior.ObservedValue = Enumerable.Repeat(Gaussian.FromMeanAndVariance(0,100),K).ToArray();

    Hence, in terms of syntax, the observed values of myMeanPrior are several Gaussian distributions. And we can `Sample()` from these distributions.

    myMean.ObservedValue[K] = myMeanPrior.ObservedValue[K].Sample();
    Since I'm new to Infer.NET and C#, I'm not sure if the usage of index above is correct. Anyway we can use a loop to replace it.


    • Marked as answer by VS2015 Monday, June 8, 2015 3:46 PM
    Tuesday, June 2, 2015 5:54 PM
    • I still don't understand what you want to achieve. Could you provide some more detail on the model? What do you need to infer?
    • In your code above, the mean is observed, so you're not trying to learn it. But then why do you need a prior on it?
    • Why do you sample at all? By doing this, you lose all of the uncertainty that is available to you. You can observe the prior of the mean, but why observe the mean itself?
    • You're mixing Infer.NET modelling code with the rest of your C# code. Try to isolate the definition of the model in one routine, and make sure this is the only routine which uses the Infer.NET modelling API.
    • Take a look at the Learning a Gaussian example.

    -Y-

    • Marked as answer by VS2015 Monday, June 8, 2015 3:46 PM
    Tuesday, June 2, 2015 11:51 PM
    • I still don't understand what you want to achieve. Could you provide some more detail on the model? What do you need to infer?
    • In your code above, the mean is observed, so you're not trying to learn it. But then why do you need a prior on it?
    • Why do you sample at all? By doing this, you lose all of the uncertainty that is available to you. You can observe the prior of the mean, but why observe the mean itself?
    • You're mixing Infer.NET modelling code with the rest of your C# code. Try to isolate the definition of the model in one routine, and make sure this is the only routine which uses the Infer.NET modelling API.
    • Take a look at the Learning a Gaussian example.

    -Y-

    This is only part of a program. Hidden variables are generated by Gaussian(myMean, myVar). And myMean has it prior distribution myMeanPrior. Observed values are affected by the hidden variables.

    To generate data, we need to specify the Gaussian to get the hidden variables, and use the hidden variables to get the observations. Hence we need specified values for myMean and myVar.

    For inference, we have observed data and a prior. We're trying to infer the hidden variables and the parameters. In this case, we need to observe the prior of the mean.

    I think previously I didn't figure out how to use InferenceEngine.Infer() method. I tried to output myMena.ObservedValue before observing it, so I got null exception. The assignment can solve this problem. But now I get a better understanding of the method, I find that we don't need to observe the mean for inference.

    Since I'm new to both C# and Infer.NET, I can not tell which is which. So I'm not sure which part should be isolated.

    • Marked as answer by VS2015 Monday, June 8, 2015 3:46 PM
    Wednesday, June 3, 2015 8:37 PM