public double[] distributionForInstance (Instance instance) throws Exception {

    if (!getDoNotReplaceMissingValues()) {
      m_ReplaceMissingValues.input(instance);
      m_ReplaceMissingValues.batchFinished();
      instance = m_ReplaceMissingValues.output();
    }

    if (getConvertNominalToBinary() 
        && m_NominalToBinary != null) {
      m_NominalToBinary.input(instance);
      m_NominalToBinary.batchFinished();
      instance = m_NominalToBinary.output();
    }

    if (m_Filter != null) {
      m_Filter.input(instance);
      m_Filter.batchFinished();
      instance = m_Filter.output();
    }

    Object x = instanceToArray(instance);
    double v;
    double[] result = new double[instance.numClasses()];
    if (m_ProbabilityEstimates) {
      if (m_SVMType != SVMTYPE_L2_LR) {
        throw new WekaException("probability estimation is currently only " +
            "supported for L2-regularized logistic regression");
      }

      int[] labels = (int[])invokeMethod(m_Model, "getLabels", null, null);
      double[] prob_estimates = new double[instance.numClasses()];

      v = ((Integer) invokeMethod(
            Class.forName(CLASS_LINEAR).newInstance(),
            "predictProbability",
            new Class[]{
              Class.forName(CLASS_MODEL),
        Array.newInstance(Class.forName(CLASS_FEATURENODE), Array.getLength(x)).getClass(),
        Array.newInstance(Double.TYPE, prob_estimates.length).getClass()},
        new Object[]{ m_Model, x, prob_estimates})).doubleValue();

      // Return order of probabilities to canonical weka attribute order
      for (int k = 0; k < prob_estimates.length; k++) {
        result[labels[k]] = prob_estimates[k];
      }
    }
    else {
      v = ((Integer) invokeMethod(
            Class.forName(CLASS_LINEAR).newInstance(),
            "predict",
            new Class[]{
              Class.forName(CLASS_MODEL),
        Array.newInstance(Class.forName(CLASS_FEATURENODE), Array.getLength(x)).getClass()},
        new Object[]{
          m_Model,
        x})).doubleValue();

      assert (instance.classAttribute().isNominal());
      result[(int) v] = 1;
    }

    return result;
  }