public void setConstraint(Node node){ for ( Constraint constraint : this.constraintMap.get(node.getMainVariable())) { Optional<Node> optional = node.getParents().stream().filter(nodeParent -> eu.amidst.core.constraints.Constraints.match(node.getPDist(), nodeParent.getMainVariable(), constraint)).findFirst(); if (!optional.isPresent()) throw new IllegalStateException("No constraint for the given parameter"); Node nodeParent = optional.get(); nodeParent.setActive(false); eu.amidst.core.constraints.Constraints.fixValue(nodeParent.getQDist(), constraint.getValue()); } } }
/** * Runs inference at time T. */ public void runInferenceTimeT() { this.vmpTimeT.runInference(); this.plateuNodesTimeT.get(this.nRepetitions-1).stream().filter(node -> !node.isObserved() && !node.getMainVariable().isParameterVariable()).forEach(node -> { Variable temporalClone = this.dbnModel.getDynamicVariables().getInterfaceVariable(node.getMainVariable()); moveNodeQDist(this.getNodeOfVarTimeT(temporalClone,0), node); }); }
/** * Runs inference at time 0. */ public void runInferenceTime0() { this.vmpTime0.runInference(); this.vmpTime0.getNodes().stream().filter(node -> !node.isObserved() && !node.getMainVariable().isParameterVariable()).forEach(node -> { Variable temporalClone = this.dbnModel.getDynamicVariables().getInterfaceVariable(node.getMainVariable()); moveNodeQDist(this.getNodeOfVarTimeT(temporalClone,0), node); }); }
/** * Moves the exponential family distributions. * @param toTemporalCloneNode a {@link Node} object. * @param fromNode a {@link Node} object. */ private static void moveNodeQDist(Node toTemporalCloneNode, Node fromNode){ EF_UnivariateDistribution uni = fromNode.getQDist().deepCopy(toTemporalCloneNode.getMainVariable()); toTemporalCloneNode.setPDist(uni); toTemporalCloneNode.setQDist(uni); }
private static void moveNodeQDist(Node toTemporalCloneNode, Node fromNode){ EF_UnivariateDistribution uni = fromNode.getQDist().deepCopy(toTemporalCloneNode.getMainVariable()); toTemporalCloneNode.setPDist(uni); toTemporalCloneNode.setQDist(uni); }
private double computeELBO() { double elbo = this.vmp.getNodes().parallelStream().filter(node -> node.isActive() && !node.isObserved()).mapToDouble(node -> this.vmp.computeELBO(node)).sum(); elbo += this.vmp.getNodes() .parallelStream() .filter(node -> node.isActive() && node.isObserved()).mapToDouble(node -> { EF_BaseDistribution_MultinomialParents base = (EF_BaseDistribution_MultinomialParents) node.getPDist(); Variable topicVariable = (Variable) base.getMultinomialParents().get(0); Map<Variable, MomentParameters> momentParents = node.getMomentParents(); double localELBO = 0; MomentParameters topicMoments = momentParents.get(topicVariable); int wordIndex = (int) node.getAssignment().getValue(node.getMainVariable())%node.getMainVariable().getNumberOfStates(); for (int i = 0; i < topicMoments.size(); i++) { EF_SparseMultinomial_Dirichlet dist = (EF_SparseMultinomial_Dirichlet)base.getBaseEFConditionalDistribution(i); MomentParameters dirichletMoments = momentParents.get(dist.getDirichletVariable()); localELBO += node.getSufficientStatistics().get(wordIndex)*dirichletMoments.get(wordIndex)*topicMoments.get(i); } return localELBO; }).sum(); return elbo; }
/** * {@inheritDoc} */ @Override public <E extends UnivariateDistribution> E getPredictivePosterior(Variable var, int nTimesAhead) { if (timeID==-1){ this.vmpTime0.setEvidence(null); this.vmpTime0.runInference(); this.vmpTime0.getNodes().stream().filter(node -> !node.isObserved()).forEach(node -> { Variable temporalClone = this.model.getDynamicVariables().getInterfaceVariable(node.getMainVariable()); moveNodeQDist(this.vmpTimeT.getNodeOfVar(temporalClone), node); }); this.moveWindow(nTimesAhead-1); E resultQ = this.getFilteredPosterior(var); this.vmpTime0.resetQs(); this.vmpTimeT.resetQs(); return resultQ; }else { Map<Variable, EF_UnivariateDistribution> map = new HashMap<>(); //Create at copy of Qs this.vmpTimeT.getNodes().stream().filter(node -> !node.isObserved()).forEach(node -> map.put(node.getMainVariable(), node.getQDist().deepCopy())); this.moveWindow(nTimesAhead); E resultQ = this.getFilteredPosterior(var); //Come to the original state map.entrySet().forEach(e -> this.vmpTimeT.getNodeOfVar(e.getKey()).setQDist(e.getValue())); return resultQ; } }
if (!node.isActive() || node.isObserved() || plateuStructure.isNonReplicatedVar(node.getMainVariable())) continue; if (!node.isActive() || node.isObserved() || plateuStructure.isReplicatedVar(node.getMainVariable())) continue;
int wordIndex = (int)node.getAssignment().getValue(node.getMainVariable());
this.timeID=0; this.vmpTime0.getNodes().stream().filter(node -> !node.isObserved()).forEach(node -> { Variable temporalClone = this.model.getDynamicVariables().getInterfaceVariable(node.getMainVariable()); moveNodeQDist(this.vmpTimeT.getNodeOfVar(temporalClone), node); }); .filter(node -> !node.isObserved()) .forEach(node -> { Variable temporalClone = this.model.getDynamicVariables().getInterfaceVariable(node.getMainVariable()); moveNodeQDist(this.vmpTimeT.getNodeOfVar(temporalClone), node); }); this.vmpTimeT.runInference(); this.vmpTimeT.getNodes().stream() .filter(node -> !node.getMainVariable().isInterfaceVariable()) .filter(node -> !node.isObserved()) .forEach(node -> { Variable temporalClone = this.model.getDynamicVariables().getInterfaceVariable(node.getMainVariable()); moveNodeQDist(this.vmpTimeT.getNodeOfVar(temporalClone), node); });
/** * Moves the window ahead for a given number of time steps. * @param nsteps an {@link int} that represents a given number of time steps. */ private void moveWindow(int nsteps){ //The first step we need to manually move the evidence from master to clone variables. HashMapDynamicAssignment newassignment = null; if (this.assignment!=null) { newassignment=new HashMapDynamicAssignment(this.model.getNumberOfDynamicVars()); for (Variable var : this.model.getDynamicVariables()) { newassignment.setValue(this.model.getDynamicVariables().getInterfaceVariable(var), this.assignment.getValue(var)); newassignment.setValue(var, Utils.missingValue()); } } for (int i = 0; i < nsteps; i++) { this.vmpTimeT.setEvidence(newassignment); this.vmpTimeT.runInference(); this.vmpTimeT.getNodes().stream() .filter(node -> !node.getMainVariable().isInterfaceVariable()) .filter(node -> !node.isObserved()) .forEach(node -> { Variable temporalClone = this.model.getDynamicVariables().getInterfaceVariable(node.getMainVariable()); moveNodeQDist(this.vmpTimeT.getNodeOfVar(temporalClone), node); }); newassignment=null; } }
.getNonReplictedNodes() .forEach(node -> node.setActive(this.idenitifableModelling.isActiveAtEpoch(node.getMainVariable(), superstep)) );
.getNonReplictedNodes() .forEach(node -> node.setActive(this.idenitifableModelling.isActiveAtEpoch(node.getMainVariable(), superstep)) );
.filter(node -> this.idenitifableModelling.isActiveAtEpoch(node.getMainVariable(), superstep)) .forEach(node -> node.setActive(true));
.filter(node -> this.idenitifableModelling.isActiveAtEpoch(node.getMainVariable(), superstep)) .forEach(node -> node.setActive(true));
count[0]++) .filter(node -> this.idenitifableModelling.isActiveAtEpoch(node.getMainVariable(), superstep-1)) .forEach(node -> updatedPosterior.setVectorByPosition(count[0] - 1, newVector.getVectorByPosition(count[0]-1))