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

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

本文整理了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

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

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

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

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

  1. /**
  2. * Method to set the class variable.
  3. * @param className String with the name of the class variable
  4. */
  5. public BayesianLinearRegression setClassName(String className){
  6. setClassVar(vars.getVariableByName(className));
  7. return this;
  8. }

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

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

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

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

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

  1. /**
  2. * Returns the class variable.
  3. * @param modelContext a {@link moa.core.InstancesHeader} object.
  4. * @param atts a set of of {@link Attributes}.
  5. * @return a {@link Variable} object that represents the class variable.
  6. */
  7. public static Variable getClassVariable(InstancesHeader modelContext, Attributes atts){
  8. Variables variables = new Variables(atts);
  9. String className = modelContext.classAttribute().name();
  10. return variables.getVariableByName(className);
  11. }

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

  1. public static void main(String[] args) throws Exception {
  2. //We first load the WasteIncinerator bayesian network which has multinomial and Gaussian variables.
  3. BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("./networks/simulated/WasteIncinerator.bn");
  4. //We recover the relevant variables for this example: Mout which is normally distributed, and W which is multinomial.
  5. Variable varMout = bn.getVariables().getVariableByName("Mout");
  6. Variable varW = bn.getVariables().getVariableByName("W");
  7. //Set the evidence.
  8. Assignment assignment = new HashMapAssignment(1);
  9. assignment.setValue(varW,0);
  10. //Then we query the posterior of
  11. System.out.println("P(Mout|W=0) = " + InferenceEngine.getPosterior(varMout, bn, assignment));
  12. //Or some more refined queries
  13. 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 ));
  14. }

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

  1. /**
  2. * Returns the class variable.
  3. * @param modelContext a {@link weka.core.Instances} object.
  4. * @param atts a set of of {@link Attributes}.
  5. * @return a {@link Variable} object that represents the class variable.
  6. */
  7. public static Variable getClassVariable(Instances modelContext, Attributes atts){
  8. Variables variables = new Variables(atts);
  9. String className = modelContext.classAttribute().name();
  10. return variables.getVariableByName(className);
  11. }

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

  1. public <E extends UnivariateDistribution> E getPosteriorDistribution(String varName) {
  2. if (learningAlgorithm !=null){
  3. return (E)this.learningAlgorithm.getLearntBayesianNetwork()
  4. .getConditionalDistribution(dag.getVariables().getVariableByName(varName));
  5. } else if (learningAlgorithmFlink != null ){
  6. return (E)this.learningAlgorithmFlink.getLearntBayesianNetwork()
  7. .getConditionalDistribution(dag.getVariables().getVariableByName(varName));
  8. }
  9. return null;
  10. }

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

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

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

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

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

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

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

  1. /**
  2. * Returns the Bayesian network at Time 0.
  3. * @return a {@link BayesianNetwork} object.
  4. */
  5. public BayesianNetwork toBayesianNetworkTime0(){
  6. DAG dagTime0 = this.getDynamicDAG().toDAGTime0();
  7. BayesianNetwork bnTime0 = new BayesianNetwork(dagTime0);
  8. for (Variable dynamicVar : this.getDynamicVariables()) {
  9. Variable staticVar = dagTime0.getVariables().getVariableByName(dynamicVar.getName());
  10. ConditionalDistribution deepCopy = Serialization.deepCopy(this.getConditionalDistributionTime0(dynamicVar));
  11. deepCopy.setVar(staticVar);
  12. List<Variable> newParents = deepCopy.getConditioningVariables().stream().map(var -> dagTime0.getVariables().getVariableByName(var.getName())).collect(Collectors.toList());
  13. deepCopy.setConditioningVariables(newParents);
  14. bnTime0.setConditionalDistribution(staticVar,deepCopy);
  15. }
  16. return bnTime0;
  17. }

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

  1. /**
  2. * Returns the Bayesian network at Time T.
  3. * @return a {@link BayesianNetwork} object.
  4. */
  5. public BayesianNetwork toBayesianNetworkTimeT(){
  6. DAG dagTimeT = this.getDynamicDAG().toDAGTimeT();
  7. BayesianNetwork bnTimeT = new BayesianNetwork(dagTimeT);
  8. for (Variable dynamicVar : this.getDynamicVariables()) {
  9. Variable staticVar = dagTimeT.getVariables().getVariableByName(dynamicVar.getName());
  10. ConditionalDistribution deepCopy = Serialization.deepCopy(this.getConditionalDistributionTimeT(dynamicVar));
  11. deepCopy.setVar(staticVar);
  12. List<Variable> newParents = deepCopy.getConditioningVariables().stream().map(var -> dagTimeT.getVariables().getVariableByName(var.getName())).collect(Collectors.toList());
  13. deepCopy.setConditioningVariables(newParents);
  14. bnTimeT.setConditionalDistribution(staticVar,deepCopy);
  15. }
  16. return bnTimeT;
  17. }

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

  1. public static void main(String[] args) throws IOException, ClassNotFoundException {
  2. BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("networks/simulated/exampleBN.bn");
  3. Variables variables = bn.getVariables();
  4. //Variabeles of interest
  5. Variable varTarget = variables.getVariableByName("LatentVar1");
  6. Variable varObserved = null;
  7. //we set the evidence
  8. Assignment assignment = new HashMapAssignment(2);
  9. varObserved = variables.getVariableByName("Income");
  10. assignment.setValue(varObserved,0.0);
  11. //we set the algorithm
  12. InferenceAlgorithm infer = new VMP(); //new HuginInference(); new ImportanceSampling();
  13. infer.setModel(bn);
  14. infer.setEvidence(assignment);
  15. //query
  16. infer.runInference();
  17. Distribution p = infer.getPosterior(varTarget);
  18. System.out.println("P(LatentVar1|Income=0.0) = "+p);
  19. //Or some more refined queries
  20. 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 ));
  21. }

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

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

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

  1. public static BayesianNetwork createBN(int nVars) throws Exception {
  2. Variables dynamicVariables = new Variables();
  3. Variable classVar = dynamicVariables.newMultinomialVariable("C", 2);
  4. for (int i = 0; i < nVars; i++) {
  5. dynamicVariables.newGaussianVariable("A" + i);
  6. }
  7. DAG dag = new DAG(dynamicVariables);
  8. for (int i = 0; i < nVars; i++) {
  9. dag.getParentSet(dynamicVariables.getVariableByName("A" + i)).addParent(classVar);
  10. }
  11. dag.setName("dbn1");
  12. BayesianNetwork bn = new BayesianNetwork(dag);
  13. bn.randomInitialization(new Random(1));
  14. return bn;
  15. }

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

  1. public static void main(String[] args) throws Exception {
  2. //Load the learnt model
  3. BayesianNetwork fireDetector = BayesianNetworkLoader.loadFromFile("./models/LearntFireDetectorModel.bn");
  4. //Access the variable of interest.
  5. Variable fire = fireDetector.getVariables().getVariableByName("Fire");
  6. Variable temperature = fireDetector.getVariables().getVariableByName("Temperature");
  7. Variable smoke = fireDetector.getVariables().getVariableByName("Smoke");
  8. //Modify the parameters of the model according to our prior knowledge.
  9. Multinomial fireprob = fireDetector.getConditionalDistribution(fire);
  10. fireprob.setProbabilities(new double[]{0.999, 0.001});
  11. Normal_MultinomialParents tempprob = fireDetector.getConditionalDistribution(temperature);
  12. tempprob.getNormal(1).setMean(tempprob.getNormal(0).getMean()+10);
  13. tempprob.getNormal(1).setVariance(tempprob.getNormal(0).getVariance());
  14. Multinomial_MultinomialParents smokeProb = fireDetector.getConditionalDistribution(smoke);
  15. smokeProb.getMultinomial(1).setProbabilities(new double[]{0.001, 0.999});
  16. //Print the model
  17. System.out.println(fireDetector);
  18. //Save to disk the new model
  19. BayesianNetworkWriter.save(fireDetector,"./models/FireDetectorModel.bn");
  20. }
  21. }

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

  1. public <E extends UnivariateDistribution> E getParameterPosteriorTimeT(Variable parameter) {
  2. if (parameter.isParameterVariable()) {
  3. Variable newVar =this.svbTimeT.getPlateuStructure().getEFLearningBN().getParametersVariables().getVariableByName(parameter.getName());
  4. return this.svbTimeT.getParameterPosterior(newVar);
  5. }else {
  6. Variable newVar =this.dagTimeT.getVariables().getVariableByName(parameter.getName());
  7. return this.svbTimeT.getParameterPosterior(newVar);
  8. }
  9. }

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

  1. public <E extends UnivariateDistribution> E getParameterPosteriorTime0(Variable parameter) {
  2. if (parameter.isParameterVariable()) {
  3. Variable newVar =this.parallelVBTime0.getSVB().getPlateuStructure().getEFLearningBN().getParametersVariables().getVariableByName(parameter.getName());
  4. return this.parallelVBTime0.getParameterPosterior(newVar);
  5. }else {
  6. Variable newVar =this.dagTime0.getVariables().getVariableByName(parameter.getName());
  7. return this.parallelVBTime0.getParameterPosterior(newVar);
  8. }
  9. }

相关文章