nodeDirichletMixingTopics = new Node(ef_learningmodel.getDistribution(dirichletMixingTopics)); tmpNodes.add(nodeDirichletMixingTopics); tmpNodes = new ArrayList<>(); nodeDirichletMixingTopics = new Node(ef_learningmodel.getDistribution(dirichletMixingTopics)); tmpNodes.add(nodeDirichletMixingTopics); nodeTopic = new Node(ef_learningmodel.getDistribution(topicIndicator)); nodeWord = new Node(ef_learningmodel.getDistribution(word)); nodeWord.setAssignment(data.get(i)); nodeWord.getSufficientStatistics().multiplyBy(data.get(i).getValue(wordCountAtt)); nodeTopic.setParents(Arrays.asList(nodeDirichletMixingTopics)); nodeTopic.setChildren(Arrays.asList(nodeWord)); nodeDirichletMixingTopics.getChildren().add(nodeTopic); nodeWord.setParents(p); nonReplictedNode.setChildren(wordNodes);
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; }
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()); } } }
/** * 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); }
/** * Sets the evidence at time T. * @param data a {@code List} of {@link DynamicDataInstance} objects. */ public void setEvidenceTimeT(List<DynamicDataInstance> data) { if (data.size()>nRepetitions) throw new IllegalArgumentException("The size of the data is bigger than the number of repetitions"); this.cloneNodesTimeT.forEach( node -> node.setAssignment(data.get(0))); for (int i = 0; i < nRepetitions && i<data.size(); i++) { final int slice = i; this.plateuNodesTimeT.get(i).forEach(node -> { node.setAssignment(data.get(slice)); node.setActive(true); //No barrent nodes if (!node.isObserved() && node.getChildren().isEmpty()) node.setActive(false); }); } for (int i = data.size(); i < nRepetitions; i++) { this.plateuNodesTimeT.get(i).forEach(node -> { node.setAssignment(null); node.setActive(false); }); } }
if (!node.isActive() || node.isObserved()) continue; Optional<Message<NaturalParameters>> message = node.getChildren() .stream() .filter(children -> children.isActive()) .map(children -> this.vmp.newMessageToParent(children, node)) .reduce(Message::combineNonStateless); .filter(node -> node.isActive() && !node.isObserved()) .forEach(node -> { Message<NaturalParameters> selfMessage = this.vmp.newSelfMessage(node); Optional<Message<NaturalParameters>> message = node.getChildren() .stream() .filter(children -> children.isActive()) .map(children -> this.vmp.newMessageToParent(children, node)) .reduce(Message::combineNonStateless);
if (!node.isActive() || node.isObserved() || plateuStructure.isNonReplicatedVar(node.getMainVariable())) continue; Optional<Message<NaturalParameters>> message = node.getChildren() .stream() .filter(children -> children.isActive()) .map(children -> newMessageToParent(children, node)) .reduce(Message::combineNonStateless); done &= node.isDone(); if (!node.isActive() || node.isObserved() || plateuStructure.isReplicatedVar(node.getMainVariable())) continue; Optional<Message<NaturalParameters>> message = node.getChildren() .stream() .filter(children -> children.isActive()) .map(children -> newMessageToParent(children, node)) .reduce(Message::combineNonStateless);
.filter(dist -> dist.getVariable().isParameterVariable()) .map(dist -> { Node node = new Node(dist); parametersToNodeTimeT.put(dist.getVariable(), node); return node; Variable temporalClone = this.dbnModel.getDynamicVariables().getInterfaceVariable(dist.getVariable()); EF_UnivariateDistribution uni = temporalClone.getDistributionType().newUnivariateDistribution().toEFUnivariateDistribution(); Node node = new Node(uni); node.setActive(false); cloneVariablesToNode.put(temporalClone, node); return node; .filter(dist -> !dist.getVariable().isParameterVariable()) .map(dist -> { Node node = new Node(dist, dist.getVariable().getName()+"_Slice_"+slice); map.put(dist.getVariable(), node); return node; for (Node node : plateuNodesTimeT.get(i)) { final int slice = i; node.setParents(node.getPDist().getConditioningVariables().stream().map(var -> this.getNodeOfVarTimeT(var, slice)).collect(Collectors.toList())); node.getPDist().getConditioningVariables().stream().filter(var -> var.isInterfaceVariable()).forEach(var -> node.setVariableToNodeParent(var, this.getNodeOfVarTimeT(var, slice))); node.getPDist().getConditioningVariables().stream().forEach(var -> this.getNodeOfVarTimeT(var, slice).getChildren().add(node));
/** * 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); }); }
UnivariateDistribution dist = dataPosteriorAssignment.getPosterior().getPosterior(latentVariable); Variable interfaceVariable = this.svb.getDAG().getVariables().getVariableByName(latentVariable.getName() + DynamicVariables.INTERFACE_SUFFIX); this.svb.getPlateuStructure().getNodeOfVar(latentVariable, i).setPDist(dist.toEFUnivariateDistribution().deepCopy(interfaceVariable)); this.svb.getPlateuStructure().getNodeOfVar(latentVariable, i).setAssignment(null); svb.getPlateuStructure().getNonReplictedNodes().forEach(node -> node.setActive(false)); SVB.BatchOutput outElbo = svb.updateModelOnBatchParallel(dataBatch); .filter(node -> this.idenitifableModelling.isActiveAtEpoch(node.getMainVariable(), superstep)) .forEach(node -> node.setActive(true)); svb.getPlateuStructure().getNonReplictedNodes().forEach(node -> node.setActive(true));
.filter(dist -> dist.getVariable().isParameterVariable()) .map(dist -> { Node node = new Node(dist); parametersToNodeTime0.put(dist.getVariable(), node); return node; .filter(dist -> !dist.getVariable().isParameterVariable()) .map(dist -> { Node node = new Node(dist); variablesToNodeTime0.put(dist.getVariable(), node); return node; node.setParents(node.getPDist().getConditioningVariables().stream().map(var -> this.getNodeOfVarTime0(var)).collect(Collectors.toList())); node.getPDist().getConditioningVariables().stream().forEach(var -> this.getNodeOfVarTime0(var).getChildren().add(node));
/** * {@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; } }
svb.getPlateuStructure().getNonReplictedNodes().forEach(node -> node.setActive(false)); svb.setOutput(false); SVB.BatchOutput outElbo = svb.updateModelOnBatchParallel(dataBatch); .getNonReplictedNodes() .forEach(node -> node.setActive(this.idenitifableModelling.isActiveAtEpoch(node.getMainVariable(), superstep)) );
/** * Sets the evidence for this PlateuStructure. * * @param data a {@code List} of {@link DataInstance}. */ public void setEvidence(List<? extends DataInstance> data) { this.data = data; this.replicateModelForDocs(); //And reset the Q's of the new replicated nodes. this.getReplicatedNodes().filter(node -> !node.isObserved()).forEach(node -> { node.resetQDist(this.vmp.getRandom()); }); }
@Override public void flatMap(Batch<DataPosteriorAssignment> data, Collector<DataPosteriorAssignment> out) { for (int i = 0; i < data.getElements().size(); i++) { for (Variable latentVariable : latentInterfaceVariables) { DataPosteriorAssignment dataPosteriorAssignment = data.getElements().get(i); if (!dataPosteriorAssignment.isObserved(latentVariable)){ UnivariateDistribution dist = dataPosteriorAssignment.getPosterior().getPosterior(latentVariable); Variable interfaceVariable = this.svb.getDAG().getVariables().getVariableByName(latentVariable.getName() + DynamicVariables.INTERFACE_SUFFIX); this.svb.getPlateuStructure().getNodeOfVar(latentVariable, i).setPDist(dist.toEFUnivariateDistribution().deepCopy(interfaceVariable)); this.svb.getPlateuStructure().getNodeOfVar(latentVariable, i).setAssignment(null); } } } DataOnMemory<DataInstance> dataBatch = new DataOnMemoryListContainer<DataInstance>( attributes, data.getElements().stream() .map(d -> new DataInstanceFromAssignment(d.getPosterior().getId(), d.getAssignment(), attributes, variables)) .collect(Collectors.toList()) ); List<DataPosteriorAssignment> posteriorAssignments = svb.computePosteriorAssignment(dataBatch, latentVariables); for (DataPosteriorAssignment posterior: posteriorAssignments){ out.collect(posterior); } }
Map<Variable, MomentParameters> momentParents = node.getMomentParents(); kl_q_p0[count] = local_kl((EF_Dirichlet)node.getQDist(),(EF_Dirichlet)node.getPDist()); count++; Map<Variable, MomentParameters> momentParents = node.getMomentParents(); kl_q_pt_1[count] = local_kl((EF_Dirichlet)node.getQDist(),(EF_Dirichlet)node.getPDist()); count++;
/** * {@inheritDoc} */ @Override public void setModel(DynamicBayesianNetwork model_) { model = model_; ef_model = new EF_DynamicBayesianNetwork(this.model); this.vmpTime0.setEFModel(ef_model.getBayesianNetworkTime0()); nodesTimeT = this.ef_model.getBayesianNetworkTimeT().getDistributionList() .stream() .map(dist -> new Node(dist)) .collect(Collectors.toList()); nodesClone = this.ef_model.getBayesianNetworkTime0().getDistributionList() .stream() .map(dist -> { Variable temporalClone = this.model.getDynamicVariables().getInterfaceVariable(dist.getVariable()); EF_UnivariateDistribution uni = temporalClone.getDistributionType().newUnivariateDistribution().toEFUnivariateDistribution(); Node node = new Node(uni); node.setActive(false); return node; }) .collect(Collectors.toList()); List<Node> allNodes = new ArrayList(); allNodes.addAll(nodesTimeT); allNodes.addAll(nodesClone); this.vmpTimeT.setNodes(allNodes); this.vmpTimeT.updateChildrenAndParents(); }
/** * Resets the exponential family distributions of all nodes for the {@link VMP} object of this PlateuStructure. */ public void resetQs() { //And reset the Q's of the new replicated nodes. this.getNonReplictedNodes().filter(node -> node.isActive()).forEach(node -> { node.resetQDist(this.vmp.getRandom()); }); }
for (Node node : this.plateuStructure.getNonReplictedNodes().collect(Collectors.toList())) { EF_Dirichlet dirichletQ = (EF_Dirichlet)node.getQDist(); EF_Dirichlet dirichletP = (EF_Dirichlet)node.getPDist(); double[] localKL = computeLocalKLDirichletBinary(dirichletQ,dirichletP); for (Node node : this.plateuStructure.getNonReplictedNodes().collect(Collectors.toList())) { EF_Dirichlet dirichletQ = (EF_Dirichlet)node.getQDist(); EF_Dirichlet dirichletP = (EF_Dirichlet)node.getPDist(); double[] localKL = computeLocalKLDirichletBinary(dirichletQ,dirichletP);
count[0]++) .filter(node -> this.idenitifableModelling.isActiveAtEpoch(node.getMainVariable(), superstep-1)) .forEach(node -> updatedPosterior.setVectorByPosition(count[0] - 1, newVector.getVectorByPosition(count[0]-1))