eu.amidst.core.variables.Variables.getVariableByName()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(13.5k)|赞(0)|评价(0)|浏览(140)

本文整理了Java中eu.amidst.core.variables.Variables.getVariableByName()方法的一些代码示例,展示了Variables.getVariableByName()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Variables.getVariableByName()方法的具体详情如下:
包路径:eu.amidst.core.variables.Variables
类名称:Variables
方法名:getVariableByName

Variables.getVariableByName介绍

暂无

代码示例

代码示例来源:origin: amidst/toolbox

public ParallelVBTranslate(DAG dag, List<String> latentNames, List<String> latentInterfaceNames, List<String> noLatentVariablesName) {
  latentVariables = latentNames.stream().map(name -> dag.getVariables().getVariableByName(name)).collect(Collectors.toList());
  latentInterfaceVariables = latentInterfaceNames.stream().map(name -> dag.getVariables().getVariableByName(name)).collect(Collectors.toList());
  allVariables = noLatentVariablesName.stream().map(name -> dag.getVariables().getVariableByName(name)).collect(Collectors.toList());
}

代码示例来源:origin: amidst/toolbox

/**
 * Method to set the class variable. Note that it should be multinomial
 * @param className String with the name of the class variable
 * @throws WrongConfigurationException is thrown when the variable is not a multinomial.
 */
public T setClassName(String className) throws WrongConfigurationException {
  setClassVar(vars.getVariableByName(className));
  return ((T) this);
}

代码示例来源:origin: amidst/toolbox

/**
 * Method to set the class variable.
 * @param className String with the name of the class variable
 */
public BayesianLinearRegression setClassName(String className){
  setClassVar(vars.getVariableByName(className));
  return this;
}

代码示例来源:origin: amidst/toolbox

private void updateTime0(DataFlink<DynamicDataInstance> data){
  DataFlink<DataInstance> newdata = DataFlinkConverter.convertToStatic(data);
  this.parallelVBTime0.updateModel(newdata);
  List<Variable> vars = this.latentVariablesNames
                  .stream()
                  .map(name -> this.dagTime0.getVariables().getVariableByName(name))
                  .collect(Collectors.toList());
  this.dataPosteriorDataSet = this.parallelVBTime0.computePosteriorAssignment(newdata, vars);
}

代码示例来源:origin: amidst/toolbox

@Override
  public void open(Configuration parameters) throws Exception {
    super.open(parameters);
    svb = Serialization.deserializeObject(parameters.getBytes(eu.amidst.flinklink.core.learning.parametric.ParallelVB.SVB, null));
    svb.initLearning();
    List<String> variableNames = Serialization.deserializeObject(parameters.getBytes(LATENT_VARIABLE_NAMES, null));
    List<String> interfaceVariablenames = Serialization.deserializeObject(parameters.getBytes(LATENT_INTERFACE_VARIABLE_NAMES, null));
    latentVariables = variableNames.stream().map(name -> svb.getDAG().getVariables().getVariableByName(name)).collect(Collectors.toList());
    latentInterfaceVariables = interfaceVariablenames.stream().map(name -> svb.getDAG().getVariables().getVariableByName(name)).collect(Collectors.toList());
  }
}

代码示例来源:origin: amidst/toolbox

/**
 * Returns the class variable.
 * @param modelContext a {@link moa.core.InstancesHeader} object.
 * @param atts a set of of {@link Attributes}.
 * @return a {@link Variable} object that represents the class variable.
 */
public static Variable getClassVariable(InstancesHeader modelContext, Attributes atts){
  Variables variables = new Variables(atts);
  String className = modelContext.classAttribute().name();
  return variables.getVariableByName(className);
}

代码示例来源:origin: amidst/toolbox

public static void main(String[] args) throws Exception {
  //We first load the WasteIncinerator bayesian network which has multinomial and Gaussian variables.
  BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("./networks/simulated/WasteIncinerator.bn");
  //We recover the relevant variables for this example: Mout which is normally distributed, and W which is multinomial.
  Variable varMout = bn.getVariables().getVariableByName("Mout");
  Variable varW = bn.getVariables().getVariableByName("W");
  //Set the evidence.
  Assignment assignment = new HashMapAssignment(1);
  assignment.setValue(varW,0);
  //Then we query the posterior of
  System.out.println("P(Mout|W=0) = " + InferenceEngine.getPosterior(varMout, bn, assignment));
  //Or some more refined queries
  System.out.println("P(0.7<Mout<6.59 | W=0) = " + InferenceEngine.getExpectedValue(varMout, bn, v -> (0.7 < v && v < 6.59) ? 1.0 : 0.0 ));
}

代码示例来源:origin: amidst/toolbox

/**
 * Returns the class variable.
 * @param modelContext a {@link weka.core.Instances} object.
 * @param atts a set of of {@link Attributes}.
 * @return a {@link Variable} object that represents the class variable.
 */
public static Variable getClassVariable(Instances modelContext, Attributes atts){
  Variables variables = new Variables(atts);
  String className = modelContext.classAttribute().name();
  return variables.getVariableByName(className);
}

代码示例来源:origin: amidst/toolbox

public <E extends UnivariateDistribution> E getPosteriorDistribution(String varName) {
  if (learningAlgorithm !=null){
     return (E)this.learningAlgorithm.getLearntBayesianNetwork()
         .getConditionalDistribution(dag.getVariables().getVariableByName(varName));
  } else if (learningAlgorithmFlink != null ){
    return (E)this.learningAlgorithmFlink.getLearntBayesianNetwork()
        .getConditionalDistribution(dag.getVariables().getVariableByName(varName));
  }
  return null;
}

代码示例来源:origin: amidst/toolbox

public static void generateData(int seed, double tempMean, String outputFile) throws Exception {
  BayesianNetwork network = createFireDetectorModel(tempMean);
  BayesianNetworkSampler sampler = new BayesianNetworkSampler(network);
  sampler.setSeed(seed);
  sampler.setLatentVar(network.getVariables().getVariableByName("Temperature"));
  sampler.setLatentVar(network.getVariables().getVariableByName("Smoke"));
  DataStream<DataInstance> dataStream = sampler.sampleToDataStream(1000);
  DataStreamWriter.writeDataToFile(dataStream, outputFile);
}
public static void main(String[] args) throws Exception {

代码示例来源:origin: amidst/toolbox

@Override
public void mapPartition(Iterable<DynamicDataInstance> values, Collector<DynamicDataInstance> out) throws Exception {
  values.forEach(d -> {
    HashMapAssignment pastAssignment = new HashMapAssignment();
    for (Attribute att : d.getAttributes().getListOfNonSpecialAttributes()){
      pastAssignment.setValue(bn.getVariables().getVariableByName(att.getName()+ DynamicVariables.INTERFACE_SUFFIX),d.getValue(att));
    }
    Assignment assignment = sample(bn, causalOrder, random, pastAssignment);
    hiddenVars.keySet().stream().forEach(var -> assignment.setValue(bn.getVariables().getVariableByName(var.getName()),Utils.missingValue()));
    marVars.entrySet().forEach(e -> {
      if (random.nextDouble()<e.getValue())
        assignment.setValue(bn.getVariables().getVariableByName(e.getKey().getName()),Utils.missingValue());
    });
    DataInstance dataInstanceNew = new DataStreamFromStreamOfAssignments.DataInstanceFromAssignment(assignment,attributes, bn.getVariables().getListOfVariables());
    DynamicDataInstanceWrapper wrapper = new DynamicDataInstanceWrapper(dataInstanceNew,attributes,d.getSequenceID(), d.getTimeID()+1);
    out.collect(wrapper);
  });
}

代码示例来源:origin: amidst/toolbox

public static void generateData() throws Exception {
  BayesianNetwork network = createFireDetectorModel();
  BayesianNetworkSampler sampler = new BayesianNetworkSampler(network);
  sampler.setSeed(0);
  sampler.setLatentVar(network.getVariables().getVariableByName("Temperature"));
  sampler.setLatentVar(network.getVariables().getVariableByName("Smoke"));
  DataStream<DataInstance> dataStream = sampler.sampleToDataStream(1000);
  DataStreamWriter.writeDataToFile(dataStream, "./datasets/sensorReadings.arff");
}
public static void main(String[] args) throws Exception {

代码示例来源:origin: amidst/toolbox

/**
 * Returns the Bayesian network at Time 0.
 * @return a {@link BayesianNetwork} object.
 */
public BayesianNetwork toBayesianNetworkTime0(){
  DAG dagTime0 = this.getDynamicDAG().toDAGTime0();
  BayesianNetwork bnTime0 = new BayesianNetwork(dagTime0);
  for (Variable dynamicVar : this.getDynamicVariables()) {
    Variable staticVar = dagTime0.getVariables().getVariableByName(dynamicVar.getName());
    ConditionalDistribution deepCopy = Serialization.deepCopy(this.getConditionalDistributionTime0(dynamicVar));
    deepCopy.setVar(staticVar);
    List<Variable> newParents = deepCopy.getConditioningVariables().stream().map(var -> dagTime0.getVariables().getVariableByName(var.getName())).collect(Collectors.toList());
    deepCopy.setConditioningVariables(newParents);
    bnTime0.setConditionalDistribution(staticVar,deepCopy);
  }
  return bnTime0;
}

代码示例来源:origin: amidst/toolbox

/**
 * Returns the Bayesian network at Time T.
 * @return a {@link BayesianNetwork} object.
 */
public BayesianNetwork toBayesianNetworkTimeT(){
  DAG dagTimeT = this.getDynamicDAG().toDAGTimeT();
  BayesianNetwork bnTimeT = new BayesianNetwork(dagTimeT);
  for (Variable dynamicVar : this.getDynamicVariables()) {
    Variable staticVar = dagTimeT.getVariables().getVariableByName(dynamicVar.getName());
    ConditionalDistribution deepCopy = Serialization.deepCopy(this.getConditionalDistributionTimeT(dynamicVar));
    deepCopy.setVar(staticVar);
    List<Variable> newParents = deepCopy.getConditioningVariables().stream().map(var -> dagTimeT.getVariables().getVariableByName(var.getName())).collect(Collectors.toList());
    deepCopy.setConditioningVariables(newParents);
    bnTimeT.setConditionalDistribution(staticVar,deepCopy);
  }
  return bnTimeT;
}

代码示例来源:origin: amidst/toolbox

public static void main(String[] args) throws IOException, ClassNotFoundException {
  BayesianNetwork bn  = BayesianNetworkLoader.loadFromFile("networks/simulated/exampleBN.bn");
  Variables variables = bn.getVariables();
  //Variabeles of interest
  Variable varTarget = variables.getVariableByName("LatentVar1");
  Variable varObserved = null;
  //we set the evidence
  Assignment assignment = new HashMapAssignment(2);
  varObserved = variables.getVariableByName("Income");
  assignment.setValue(varObserved,0.0);
  //we set the algorithm
  InferenceAlgorithm infer = new VMP(); //new HuginInference(); new ImportanceSampling();
  infer.setModel(bn);
  infer.setEvidence(assignment);
  //query
  infer.runInference();
  Distribution p = infer.getPosterior(varTarget);
  System.out.println("P(LatentVar1|Income=0.0) = "+p);
  //Or some more refined queries
  System.out.println("P(0.7<LatentVar1<6.59 |Income=0.0) = " + infer.getExpectedValue(varTarget, v -> (0.7 < v && v < 6.59) ? 1.0 : 0.0 ));
}

代码示例来源:origin: amidst/toolbox

public static void generateBigData(double tempMean, int nRep) throws Exception {
  BayesianNetwork network = createBigFireDetectorModel(tempMean, nRep);
  eu.amidst.flinklink.core.utils.BayesianNetworkSampler sampler = new eu.amidst.flinklink.core.utils.BayesianNetworkSampler(network);
  sampler.setSeed(0);
  sampler.setLatentVar(network.getVariables().getVariableByName("TemperatureBuilding"));
  for (int i = 0; i < nRep; i++) {
    sampler.setLatentVar(network.getVariables().getVariableByName("Temperature_"+i));
    sampler.setLatentVar(network.getVariables().getVariableByName("Smoke_"+i));
  }
  ExecutionEnvironment env = ExecutionEnvironment.createLocalEnvironment();
  env.setParallelism(10);
  env.getConfig().disableSysoutLogging();
  DataFlink<DataInstance> data = sampler.sampleToDataFlink(env,10000);
  DataFlinkWriter.writeDataToARFFFolder(data, "./dataSets/BigSensorReadings.arff");
}

代码示例来源:origin: amidst/toolbox

public static BayesianNetwork createBN(int nVars) throws Exception {
  Variables dynamicVariables = new Variables();
  Variable classVar = dynamicVariables.newMultinomialVariable("C", 2);
  for (int i = 0; i < nVars; i++) {
    dynamicVariables.newGaussianVariable("A" + i);
  }
  DAG dag = new DAG(dynamicVariables);
  for (int i = 0; i < nVars; i++) {
    dag.getParentSet(dynamicVariables.getVariableByName("A" + i)).addParent(classVar);
  }
  dag.setName("dbn1");
  BayesianNetwork bn = new BayesianNetwork(dag);
  bn.randomInitialization(new Random(1));
  return bn;
}

代码示例来源:origin: amidst/toolbox

public static void main(String[] args) throws Exception {
    //Load the learnt model
    BayesianNetwork fireDetector = BayesianNetworkLoader.loadFromFile("./models/LearntFireDetectorModel.bn");

    //Access the variable of interest.
    Variable fire = fireDetector.getVariables().getVariableByName("Fire");
    Variable temperature = fireDetector.getVariables().getVariableByName("Temperature");
    Variable smoke = fireDetector.getVariables().getVariableByName("Smoke");

    //Modify the parameters of the model according to our prior knowledge.
    Multinomial fireprob = fireDetector.getConditionalDistribution(fire);
    fireprob.setProbabilities(new double[]{0.999, 0.001});

    Normal_MultinomialParents tempprob = fireDetector.getConditionalDistribution(temperature);
    tempprob.getNormal(1).setMean(tempprob.getNormal(0).getMean()+10);
    tempprob.getNormal(1).setVariance(tempprob.getNormal(0).getVariance());

    Multinomial_MultinomialParents smokeProb = fireDetector.getConditionalDistribution(smoke);
    smokeProb.getMultinomial(1).setProbabilities(new double[]{0.001, 0.999});

    //Print the model
    System.out.println(fireDetector);

    //Save to disk the new model
    BayesianNetworkWriter.save(fireDetector,"./models/FireDetectorModel.bn");

  }
}

代码示例来源:origin: amidst/toolbox

public <E extends UnivariateDistribution> E getParameterPosteriorTimeT(Variable parameter) {
  if (parameter.isParameterVariable()) {
    Variable newVar =this.svbTimeT.getPlateuStructure().getEFLearningBN().getParametersVariables().getVariableByName(parameter.getName());
    return this.svbTimeT.getParameterPosterior(newVar);
  }else {
    Variable newVar =this.dagTimeT.getVariables().getVariableByName(parameter.getName());
    return this.svbTimeT.getParameterPosterior(newVar);
  }
}

代码示例来源:origin: amidst/toolbox

public <E extends UnivariateDistribution> E getParameterPosteriorTime0(Variable parameter) {
  if (parameter.isParameterVariable()) {
    Variable newVar =this.parallelVBTime0.getSVB().getPlateuStructure().getEFLearningBN().getParametersVariables().getVariableByName(parameter.getName());
    return this.parallelVBTime0.getParameterPosterior(newVar);
  }else {
    Variable newVar =this.dagTime0.getVariables().getVariableByName(parameter.getName());
    return this.parallelVBTime0.getParameterPosterior(newVar);
  }
}

相关文章