docs: public FactorGraph marginalize(Collection<Integer> varNumsToEliminate) {
      FactorGraph currentFactorGraph = this;
      for (Integer eliminatedVariableIndex : varNumsToEliminate) {
        // Each iteration marginalizes out a single variable from
        // currentFactorGraph,
        // aggregating intermediate results in nextFactorGraph.
        FactorGraph nextFactorGraph = new FactorGraph();
  
        // Copy the variables in currentFactorGraph to nextFactorGraph
        for (String variableName : currentFactorGraph.getVariables().getVariableNamesArray()) {
          int varIndex = currentFactorGraph.getVariables().getVariableByName(variableName);
          if (varIndex != eliminatedVariableIndex) {
            nextFactorGraph = nextFactorGraph.addVariableWithIndex(variableName,
                currentFactorGraph.variables.getVariable(varIndex), varIndex);
          }
        }
  
        // Identify the factors which contain the variable, which must
        // be
        // multiplied together. All other factors can be immediately
        // copied into
        // the next factor graph.
        List<Factor> factorsToMultiply = Lists.newArrayList();
        String mulName = null;
        List<Factor> currentFactors = currentFactorGraph.getFactors();
        List<String> currentFactorNames = currentFactorGraph.getFactorNames();
        for (int i = 0; i < currentFactors.size(); i++) {
          Factor factor = currentFactors.get(i);
          if (factor.getVars().contains(eliminatedVariableIndex)) {
            factorsToMultiply.add(factor);
            mulName = currentFactorNames.get(i);
          } else {
            // No variable in factor is being eliminated, so we don't
            // have to
            // modify it.
            nextFactorGraph = nextFactorGraph.addFactor(currentFactorNames.get(i), factor);
          }
        }
  
        if (factorsToMultiply.size() > 0) {
          // If the variable is present, eliminate it!
          Factor productFactor = Factors.product(factorsToMultiply);
          nextFactorGraph = nextFactorGraph.addFactor(mulName,
              productFactor.marginalize(eliminatedVariableIndex));
        }
  
        currentFactorGraph = nextFactorGraph;
      }
      return currentFactorGraph;
    }