Example #1
0
 @operator(
     value = {"percent_absolute_deviation"},
     content_type = IType.FLOAT,
     category = {IOperatorCategory.MAP_COMPARAISON},
     concept = {IConcept.STATISTIC})
 @doc(
     value =
         "percent absolute deviation indicator for 2 series of values: percent_absolute_deviation(list_vals_observe,list_vals_sim)",
     examples = {
       @example(
           value = "percent_absolute_deviation([200,300,150,150,200],[250,250,100,200,200])",
           isExecutable = false)
     })
 public static double percentAbsoluteDeviation(
     final IScope scope, final IList<Double> vals1, final IList<Double> vals2) {
   if (vals1 == null || vals2 == null) {
     return 1;
   }
   int nb = vals1.size();
   if (nb != vals2.size()) {
     return 0;
   }
   double sum = 0;
   double coeff = 0;
   for (int i = 0; i < nb; i++) {
     double val1 = Cast.asFloat(scope, vals1.get(i));
     double val2 = Cast.asFloat(scope, vals2.get(i));
     coeff += val1;
     sum += FastMath.abs(val1 - val2) * 100.0;
   }
   if (coeff == 0) {
     return 0;
   }
   return sum / coeff;
 }
Example #2
0
  private static Instances convertToInstances(
      final IScope scope,
      final IList<String> attributes,
      final IAddressableContainer<Integer, IAgent, Integer, IAgent> agents)
      throws GamaRuntimeException {
    FastVector attribs = new FastVector();
    for (String att : attributes) {
      attribs.addElement(new Attribute(att));
    }
    Instances dataset =
        new Instances(scope.getAgentScope().getName(), attribs, agents.length(scope));
    for (IAgent ag : agents.iterable(scope)) {

      int nb = attributes.size();
      double vals[] = new double[nb];
      for (int i = 0; i < nb; i++) {
        String attrib = attributes.get(i);
        Double var = Cast.asFloat(scope, ag.getDirectVarValue(scope, attrib));
        vals[i] = var;
      }
      Instance instance = new Instance(1, vals);
      dataset.add(instance);
    }
    return dataset;
  }
Example #3
0
  public void updateXValues(IScope scope, int chartCycle, int targetNb) {
    Object xval, xlab;
    if (this.useXSource || this.useXLabels) {

      if (this.useXSource) {
        xval = xsource.resolveAgainst(scope).value(scope);
      } else {
        xval = xlabels.resolveAgainst(scope).value(scope);
      }
      if (this.useXLabels) {
        xlab = xlabels.resolveAgainst(scope).value(scope);
      } else {
        xlab = xsource.resolveAgainst(scope).value(scope);
      }

      if (xval instanceof GamaList) {
        IList xv2 = Cast.asList(scope, xval);
        IList xl2 = Cast.asList(scope, xlab);

        if (this.useXSource && xv2.size() > 0 && xv2.get(0) instanceof Number) {
          XSeriesValues = new ArrayList<Double>();
          Xcategories = new ArrayList<String>();
          for (int i = 0; i < xv2.size(); i++) {
            XSeriesValues.add(new Double(Cast.asFloat(scope, xv2.get(i))));
            Xcategories.add(Cast.asString(scope, xl2.get(i)));
          }

        } else {
          if (xv2.size() > Xcategories.size()) {
            Xcategories = new ArrayList<String>();
            for (int i = 0; i < xv2.size(); i++) {
              if (i >= XSeriesValues.size()) {
                XSeriesValues.add(new Double(getXCycleOrPlusOneForBatch(scope, chartCycle)));
              }
              Xcategories.add(Cast.asString(scope, xl2.get(i)));
            }
          }
        }
        if (xv2.size() < targetNb) {
          throw GamaRuntimeException.error(
              "The x-serie length ("
                  + xv2.size()
                  + ") should NOT be shorter than any series length ("
                  + targetNb
                  + ") !",
              scope);
        }

      } else {
        if (this.useXSource && xval instanceof Number) {
          double dvalue = Cast.asFloat(scope, xval);
          String lvalue = Cast.asString(scope, xlab);
          XSeriesValues.add(new Double(dvalue));
          Xcategories.add(lvalue);
        }
        if (targetNb == -1 && !this.forceNoXAccumulate) targetNb = XSeriesValues.size() + 1;
        while (XSeriesValues.size() < targetNb) {
          XSeriesValues.add(new Double(getXCycleOrPlusOneForBatch(scope, chartCycle)));
          Xcategories.add(Cast.asString(scope, xlab));
        }
      }
    }

    if (!this.useXSource && !this.useXLabels) {
      if (targetNb == -1 && !this.forceNoXAccumulate && commonXindex >= XSeriesValues.size())
        targetNb = XSeriesValues.size() + 1;
      while (XSeriesValues.size() < targetNb) {
        addCommonXValue(scope, getXCycleOrPlusOneForBatch(scope, chartCycle));
      }
    }
  }
Example #4
0
  @operator(
      value = {"kappa"},
      content_type = IType.FLOAT,
      category = {IOperatorCategory.MAP_COMPARAISON},
      concept = {})
  @doc(
      value =
          "kappa indicator for 2 map comparisons: kappa(list_vals1,list_vals2,categories, weights). Reference: Cohen, J. A coefficient of agreement for nominal scales. Educ. Psychol. Meas. 1960, 20. ",
      examples = {
        @example(
            value =
                "kappa([cat1,cat1,cat2,cat3,cat2],[cat2,cat1,cat2,cat1,cat2],[cat1,cat2,cat3], [1.0, 2.0, 3.0, 1.0, 5.0])",
            isExecutable = false)
      })
  public static double kappa(
      final IScope scope,
      final IList<Object> vals1,
      final IList<Object> vals2,
      final List<Object> categories,
      final IList<Object> weights) {
    if (vals1 == null || vals2 == null) {
      return 1;
    }
    int nb = vals1.size();
    if (nb != vals2.size()) {
      return 0;
    }
    int nbCat = categories.size();
    double[] X = new double[nbCat];
    double[] Y = new double[nbCat];
    double[][] contigency = new double[nbCat][nbCat];
    for (int j = 0; j < nbCat; j++) {
      X[j] = 0;
      Y[j] = 0;
      for (int k = 0; k < nbCat; k++) {
        contigency[j][k] = 0;
      }
    }

    Map<Object, Integer> categoriesId = new TOrderedHashMap<Object, Integer>();
    for (int i = 0; i < nbCat; i++) {
      categoriesId.put(categories.get(i), i);
    }
    double total = 0;
    for (int i = 0; i < nb; i++) {
      double weight = weights == null ? 1.0 : Cast.asFloat(scope, weights.get(i));
      total += weight;
      Object val1 = vals1.get(i);
      Object val2 = vals2.get(i);
      int indexVal1 = categoriesId.get(val1);
      int indexVal2 = categoriesId.get(val2);
      X[indexVal1] += weight;
      Y[indexVal2] += weight;
      contigency[indexVal1][indexVal2] += weight;
    }
    for (int j = 0; j < nbCat; j++) {
      X[j] /= total;
      Y[j] /= total;
      for (int k = 0; k < nbCat; k++) {
        contigency[j][k] /= total;
      }
    }
    double po = 0;
    double pe = 0;
    for (int i = 0; i < nbCat; i++) {
      po += contigency[i][i];
      pe += X[i] * Y[i];
    }
    if (pe == 1) {
      return 1;
    }
    return (po - pe) / (1 - pe);
  }
Example #5
0
 @operator(
     value = {"kappa_sim"},
     content_type = IType.FLOAT,
     category = {IOperatorCategory.MAP_COMPARAISON},
     concept = {})
 @doc(
     value =
         "kappa simulation indicator for 2 map comparisons: kappa(list_valsInits,list_valsObs,list_valsSim, categories, weights). Reference: van Vliet, J., Bregt, A.K. & Hagen-Zanker, A. (2011). Revisiting Kappa to account for change in the accuracy assessment of land-use change models, Ecological Modelling 222(8)",
     examples = {
       @example(
           value =
               "kappa([cat1,cat1,cat2,cat2,cat2],[cat2,cat1,cat2,cat1,cat3],[cat2,cat1,cat2,cat3,cat3], [cat1,cat2,cat3],[1.0, 2.0, 3.0, 1.0, 5.0])",
           isExecutable = false)
     })
 public static double kappaSimulation(
     final IScope scope,
     final IList<Object> valsInit,
     final IList<Object> valsObs,
     final IList<Object> valsSim,
     final List<Object> categories,
     final IList<Object> weights) {
   if (valsInit == null || valsObs == null || valsSim == null) {
     return 1;
   }
   int nb = valsInit.size();
   if (nb != valsObs.size() || nb != valsSim.size()) {
     return 0;
   }
   int nbCat = categories.size();
   double[] O = new double[nbCat];
   double[][] contigency = new double[nbCat][nbCat];
   double[][] contigencyOA = new double[nbCat][nbCat];
   double[][] contigencyOS = new double[nbCat][nbCat];
   for (int j = 0; j < nbCat; j++) {
     O[j] = 0;
     for (int k = 0; k < nbCat; k++) {
       contigency[j][k] = 0;
       contigencyOA[j][k] = 0;
       contigencyOS[j][k] = 0;
     }
   }
   Map<Object, Integer> categoriesId = new TOrderedHashMap<Object, Integer>();
   for (int i = 0; i < nbCat; i++) {
     categoriesId.put(categories.get(i), i);
   }
   double total = 0;
   for (int i = 0; i < nb; i++) {
     double weight = weights == null ? 1.0 : Cast.asFloat(scope, weights.get(i));
     total += weight;
     Object val1 = valsObs.get(i);
     Object val2 = valsSim.get(i);
     Object valO = valsInit.get(i);
     int indexVal1 = categoriesId.get(val1);
     int indexVal2 = categoriesId.get(val2);
     int indexValO = categoriesId.get(valO);
     O[indexValO] += weight;
     contigency[indexVal1][indexVal2] += weight;
     contigencyOA[indexValO][indexVal1] += weight;
     contigencyOS[indexValO][indexVal2] += weight;
   }
   for (int j = 0; j < nbCat; j++) {
     for (int k = 0; k < nbCat; k++) {
       contigency[j][k] /= total;
       if (O[j] > 0) {
         contigencyOA[j][k] /= O[j];
         contigencyOS[j][k] /= O[j];
       }
     }
     O[j] /= total;
   }
   double po = 0;
   double pe = 0;
   for (int j = 0; j < nbCat; j++) {
     po += contigency[j][j];
     double sum = 0;
     for (int i = 0; i < nbCat; i++) {
       sum += contigencyOA[j][i] * contigencyOS[j][i];
     }
     pe += O[j] * sum;
   }
   if (pe == 1) {
     return 1;
   }
   return (po - pe) / (1 - pe);
 }