2012-11-16 48 views
4

如何修改Infer.NETTutorial 4: Bayes Point Machine以包含更多結果?如何在Infer.NET的BPM中包含更多結果?

例如,如何添加willRent並獲取willBuy和willRent的單獨概率?

 double[] incomes = { 63, 16, 28, 55, 22, 20 }; 
     double[] ages = { 38, 23, 40, 27, 18, 40 }; 
     bool[] willBuy = { true, false, true, true, false, false }; 
     bool[] willRent = { false, false, true, false, true, false }; 

編輯 - 下面是複製/粘貼格式的例子:

static void Main() 
{ 
    double[] incomes = { 63, 16, 28, 55, 22, 20 }; 
    double[] ages = { 38, 23, 40, 27, 18, 40 }; 
    bool[] willBuy = { true, false, true, true, false, false }; 

    // Create x vector, augmented by 1 
    Vector[] xdata = new Vector[incomes.Length]; 
    for (int i = 0; i < xdata.Length; i++) 
     xdata[i] = Vector.FromArray(incomes[i], ages[i], 1); 
    VariableArray<Vector> x = Variable.Observed(xdata); 

    // Create target y 
    VariableArray<bool> y = Variable.Observed(willBuy, x.Range); 

    Variable<Vector> w = Variable.Random(new VectorGaussian(Vector.Zero(3), PositiveDefiniteMatrix.Identity(3))); 
    Range j = y.Range; 
    double noise = 0.1; 
    y[j] = Variable.GaussianFromMeanAndVariance(Variable.InnerProduct(w, x[j]), noise) > 0; 

    InferenceEngine engine = new InferenceEngine(new ExpectationPropagation()); 
    VectorGaussian wPosterior = engine.Infer<VectorGaussian>(w); 
    Console.WriteLine("Dist over w=\n" + wPosterior); 

    double[] incomesTest = { 58, 18, 22 }; 
    double[] agesTest = { 36, 24, 37 }; 
    VariableArray<bool> ytest = Variable.Array<bool>(new Range(agesTest.Length)); 
    BayesPointMachine(incomesTest, agesTest, Variable.Random(wPosterior), ytest); 
    Console.WriteLine("output=\n" + engine.Infer(ytest)); 

    Console.ReadKey(); 
} 

static void BayesPointMachine(double[] incomes,double[] ages,Variable<Vector> w,VariableArray<bool> y) 
{ 
    // Create x vector, augmented by 1 
    Range j = y.Range; 
    Vector[] xdata = new Vector[incomes.Length]; 
    for (int i = 0; i < xdata.Length; i++) 
     xdata[i] = Vector.FromArray(incomes[i], ages[i], 1); 
    VariableArray<Vector> x = Variable.Observed(xdata, j); 

    // Bayes Point Machine 
    double noise = 0.1; 
    y[j] = Variable.GaussianFromMeanAndVariance(Variable.InnerProduct(w, x[j]), noise) > 0; 
} 

回答

1

你有4個變量在這裏。

    Will Buy? 
       ^ ^
       /  \ 
       Age  Income 
       \  /
        v  v 
        Will Rent? 

鑑於(Age,Income)Will BuyWill Rent是獨立的變量,即,conditional independent

所以,你可以只構建兩個獨立的貝葉斯點機:

  • 一個用於AgeIncomeWill Rent
  • 一個用於Age,IncomeWill Buy
+0

隨着我的數據集,它已經需要很長時間才能完成一次傳球。我會計算許多結果的概率。爲每一個結果做手術是不現實的。我試圖一次性得到所有結果的概率。 – Manuel

+1

如果你的'結果'在同一組變量(例如'age'和'income')中都是有條件獨立的,你怎麼可能通過將它們包含在同一個圖形模型中來提高你的運行時間性能? – greeness

相關問題