# Bayesian network Continuous variables inputs predict discrete probabilities outcome

• ### Question

• I’m new to infer.net (mostly an R guy) but like the capabilities I’ve seen so far. Trying to translate a process of creating a Bayesian network I did in bnlearn using Infer.net to assess probabilities of diabetes with setting various evidence. I’ve limited the variables to weight, total cholesterol, blood pressure, and of course a discrete variables whether the patient has diabetes or not.

Having issues on translating my understanding over, c# code attached below.

Any help would be appreciated.

Regards,

Josh

double[] WeightData = new double[] { 87.4, 17, 72.3, 39.8, 116.8, 97.6, 86.7, 9.4, 26, 79.1};
double[] TotChoData = new double[] { 3.49, 4.65, 4.97, 4.16, 5.22, 4.14, 6.7, 4.81, 4.14, 4.71};
double[] BPSysAve = new double[] { 113, 132, 109, 93, 150, 104, 112, 109, 108, 139};
//int[] DiabetesData = new int[] { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0};
bool[] DiabetesData = new bool[] { false, false, false, false, true, false, false, false, false, false};
public class WetGlassSprinklerRainModel
{
// Primary random variables
public VariableArray<double> WeightData;
public VariableArray<double> TotChoData;
public VariableArray<double> BPSysAveData;
public VariableArray<int> DiabetesData;
public Variable<int> NumberOfExamples;

// Random variables representing the parameters of the distributions
// of the primary random variables. For child variables, these are
// in the form of conditional probability tables (CPTs)
public Variable<Vector> ProbWeight;
public VariableArray<Vector> CPTTotCho;
public VariableArray<Vector> CPTBPSysAve;
public VariableArray<VariableArray<Vector>, Vector[][]> CPTDiabetes;

// Prior distributions for the probability and CPT variables.
// The prior distributions are formulated as Infer.NET variables
// so that they can be set at runtime without recompiling the model
public Variable<Dirichlet> ProbWeightPrior;
public VariableArray<Dirichlet> CPTTotChoPrior;
public VariableArray<Dirichlet> CPTBPSysAvePrior;
public VariableArray<VariableArray<Dirichlet>, Dirichlet[][]> CPTDiabetesPrior;

// Posterior distributions for the probability and CPT variables.
public Dirichlet ProbWeightPosterior;
public Dirichlet[] CPTTotChoPosterior;
public Dirichlet[] CPTBPSysAvePosterior;
public Dirichlet[][] CPTDiabetesPosterior;

// Inference engine
public InferenceEngine Engine = new InferenceEngine();

public WetGlassSprinklerRainModel()
{
// Set up the ranges
NumberOfExamples = Variable.New<int>().Named("NofE");
Range N = new Range(NumberOfExamples).Named("N");

// Although all the variables in this example have just 2 states (true/false),
// the example is formulated in a way that shows how to extend to multiple states
Range C = new Range(2).Named("C");
Range S = new Range(2).Named("S");
Range R = new Range(2).Named("R");
Range W = new Range(2).Named("W");

// Define the priors and the parameters
ProbWeightPrior = Variable.New<Dirichlet>().Named("ProbWeightPrior");
ProbWeight = Variable<Vector>.Random(ProbWeightPrior).Named("ProbWeight");
ProbWeight.SetValueRange(C);
// Sprinkler probability table conditioned on cloudiness
CPTTotChoPrior = Variable.Array<Dirichlet>(C).Named("CPTTotChoPrior");
CPTTotCho = Variable.Array<Vector>(C).Named("CPTTotCho");
CPTTotCho[C] = Variable<Vector>.Random(CPTTotChoPrior[C]);
CPTTotCho.SetValueRange(S);
// Rain probability table conditioned on cloudiness
CPTBPSysAvePrior = Variable.Array<Dirichlet>(C).Named("CPTBPSysAvePrior");
CPTBPSysAve = Variable.Array<Vector>(C).Named("CPTBPSysAve"); ;
CPTBPSysAve[C] = Variable<Vector>.Random(CPTBPSysAvePrior[C]);
CPTBPSysAve.SetValueRange(R);
// Wet grass probability table conditioned on sprinkler and rain
CPTDiabetesPrior = Variable.Array(Variable.Array<Dirichlet>(R), S).Named("CPTDiabetesPrior");
CPTDiabetes = Variable.Array(Variable.Array<Vector>(R), S).Named("CPTDiabetes");
CPTDiabetes[S][R] = Variable<Vector>.Random(CPTDiabetesPrior[S][R]);
CPTDiabetes.SetValueRange(W);

// Define the primary variables
WeightData = Variable.Array<double>(N).Named("Weight");
WeightData[N] = Variable.Discrete(ProbWeight).ForEach(N);
WeightData[N] = Variable.Random<double, Gaussian>(ProbWeight).ForEach(N);
TotChoData = AddChildFromOneParent(WeightData, CPTTotCho).Named("TotCho");
BPSysAveData = AddChildFromOneParent(WeightData, CPTBPSysAve).Named("BPSysAve");
DiabetesData = AddChildFromTwoParents(TotChoData, BPSysAveData, CPTDiabetes).Named("Diabetes");

}
/// <summary>
/// Helper method to add a child from one parent
/// </summary>
/// <param name="parent">Parent (a variable array over a range of examples)</param>
/// <param name="cpt">Conditional probability table</param>
/// <returns></returns>
public static VariableArray<int> AddChildFromOneParent(
VariableArray<int> parent,
VariableArray<Vector> cpt)
{
var n = parent.Range;
var child = Variable.Array<int>(n);
using (Variable.ForEach(n))
using (Variable.Switch(parent[n]))
child[n] = Variable.Discrete(cpt[parent[n]]);
return child;
}

/// <summary>
/// Helper method to add a child from two parents
/// </summary>
/// <param name="parent1">First parent (a variable array over a range of examples)</param>
/// <param name="parent2">Second parent (a variable array over the same range)</param>
/// <param name="cpt">Conditional probability table</param>
/// <returns></returns>
public static VariableArray<int> AddChildFromTwoParents(
VariableArray<int> parent1,
VariableArray<int> parent2,
VariableArray<VariableArray<Vector>, Vector[][]> cpt)
{
var n = parent1.Range;
var child = Variable.Array<int>(n);
using (Variable.ForEach(n))
using (Variable.Switch(parent1[n]))
using (Variable.Switch(parent2[n]))
child[n] = Variable.Discrete(cpt[parent1[n]][parent2[n]]);
return child;
}

}

}

}

Tuesday, November 24, 2015 7:07 PM

### All replies

• Sorry, what exactly is your question?
Thursday, November 26, 2015 4:09 PM