Skip to content

Commit

Permalink
Causalbuilder tests extended #23
Browse files Browse the repository at this point in the history
  • Loading branch information
rcabanasdepaz committed Aug 2, 2021
1 parent dd5b6d1 commit 77c914c
Showing 1 changed file with 67 additions and 7 deletions.
74 changes: 67 additions & 7 deletions src/test/java/ch/idsia/credici/model/CausalBuilderTest.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
package ch.idsia.credici.model;

import ch.idsia.credici.factor.EquationBuilder;
import ch.idsia.credici.model.builder.CausalBuilder;
import ch.idsia.crema.factor.bayesian.BayesianFactor;
import ch.idsia.crema.model.graphical.SparseDirectedAcyclicGraph;
import ch.idsia.crema.model.graphical.specialized.BayesianNetwork;
import ch.idsia.crema.utility.RandomUtil;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;

public class CausalBuilderTest {

BayesianNetwork bnet;
int x, y;


@Before
public void init(){
bnet = new BayesianNetwork();
int y = bnet.addVariable(2);
int x = bnet.addVariable(2);
y = bnet.addVariable(2);
x = bnet.addVariable(2);

bnet.setFactor(y, new BayesianFactor(bnet.getDomain(y), new double[]{0.3,0.7}));
bnet.setFactor(x, new BayesianFactor(bnet.getDomain(x,y), new double[]{0.6,0.5, 0.5,0.5}));
Expand Down Expand Up @@ -51,16 +54,73 @@ public void buildMarkovianEqless() {
@Test
public void builWithCausalDAG() {

}
StructuralCausalModel model = CausalBuilder.of(bnet).build();


SparseDirectedAcyclicGraph causalDAG = new SparseDirectedAcyclicGraph();
causalDAG.addVariable(y);
causalDAG.addVariable(x);

int u = causalDAG.addVariable();

causalDAG.addLink(y,x);
causalDAG.addLink(u,x);
causalDAG.addLink(u,y);


RandomUtil.setRandomSeed(1);
StructuralCausalModel m =
CausalBuilder.of(bnet)
.setCausalDAG(causalDAG)
.setFillRandomEquations(true)
.setFillRandomExogenousFactors(4)
.setExoVarSizes(new int[]{4}).build();


assertArrayEquals(
((BayesianFactor)m.getEmpiricalMap().values().toArray()[0]).getData(),
new double[]{0.806, 0.0, 0.0, 0.194},
0.00001);

@Test
public void builFromEquations() {

}

@Test
public void buildFromExoVarSizes() {
public void buildEquations() {


StructuralCausalModel model = CausalBuilder.of(bnet).build();
int ux = model.getExogenousParents(x)[0];
int uy = model.getExogenousParents(y)[0];


/* u0 u1 u2 u4
x0 0 0 0 1
y0 x1 1 1 1 0
---------------------
x0 1 0 1 0
y1 x1 0 1 0 1
*/

BayesianFactor eq = null;
eq = EquationBuilder.of(model).fromVector(x, 1,0, 1,1, 1,0, 0,1);

assertArrayEquals(
eq.filter(x,0).filter(y, 0).getData(),
new double[]{0.0, 0.0, 0.0, 1.0},
0.000001
);

assertArrayEquals(
eq.filter(x,0).filter(y, 1).getData(),
new double[]{1.0, 0.0, 1.0, 0.0},
0.000001
);



}


}

0 comments on commit 77c914c

Please sign in to comment.