All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.ac.sussex.gdsc.smlm.fitting.nonlinear.MleBaseFunctionSolver Maven / Gradle / Ivy

Go to download

Genome Damage and Stability Centre SMLM Package Software for single molecule localisation microscopy (SMLM)

The newest version!
/*-
 * #%L
 * Genome Damage and Stability Centre SMLM Package
 *
 * Software for single molecule localisation microscopy (SMLM)
 * %%
 * Copyright (C) 2011 - 2023 Alex Herbert
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

package uk.ac.sussex.gdsc.smlm.fitting.nonlinear;

import uk.ac.sussex.gdsc.smlm.fitting.FunctionSolverType;
import uk.ac.sussex.gdsc.smlm.fitting.MleFunctionSolver;
import uk.ac.sussex.gdsc.smlm.function.ChiSquaredDistributionTable;
import uk.ac.sussex.gdsc.smlm.function.GradientFunction;

/**
 * Abstract class with utility methods for the {@link MleFunctionSolver} interface.
 */
public abstract class MleBaseFunctionSolver extends BaseFunctionSolver
    implements MleFunctionSolver {
  /** The log-likelihood ratio. */
  protected double llr = Double.NaN;

  /**
   * Default constructor.
   *
   * @param function the function
   * @throws NullPointerException if the function is null
   */
  public MleBaseFunctionSolver(GradientFunction function) {
    super(FunctionSolverType.MLE, function);
  }

  @Override
  protected void preProcess() {
    llr = Double.NaN;
  }

  @Override
  public double getLogLikelihood() {
    return value;
  }

  @Override
  public double getLogLikelihoodRatio() {
    if (Double.isNaN(llr) && lastY != null) {
      // From https://en.wikipedia.org/wiki/Likelihood-ratio_test#Use:
      // LLR = 2 * [ ln(likelihood for alternative model) - ln(likelihood for null model)]
      // The model with more parameters (here alternative) will always fit at least as well—
      // i.e., have the same or greater log-likelihood—than the model with fewer parameters
      // (here null)

      final double llAlternative = computeObservedLogLikelihood(lastY, lastA);
      final double llNull = getLogLikelihood();

      // The alternative should always fit better (higher value) than the null model
      if (llAlternative < llNull) {
        llr = 0;
      } else {
        llr = 2 * (llAlternative - llNull);
      }
    }
    return llr;
  }

  /**
   * Compute the observed log likelihood (i.e. the log-likelihood with y as the function value).
   *
   * @param y the y
   * @param a the a
   * @return the observed log likelihood
   */
  protected abstract double computeObservedLogLikelihood(double[] y, double[] a);

  @Override
  public double getQ() {
    return ChiSquaredDistributionTable.computeQValue(getLogLikelihoodRatio(),
        getNumberOfFittedPoints() - getNumberOfFittedParameters());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy