Skip to content

Commit

Permalink
Update population function #458
Browse files Browse the repository at this point in the history
  • Loading branch information
yxu927 committed Apr 18, 2024
1 parent 28fa45d commit 54e72ca
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import java.util.TreeMap;

public class PopulationFunctionCoalescent extends TaxaConditionedTreeGenerator {
private Value<PopulationFunction> popFunc;
public Value<PopulationFunction> popFunc;

private final String popFuncParamName = "popFunc";
public final String popFuncParamName = "popFunc";

/**
* Constructs a coalescent model with specified population function, number of taxa, taxa object, and leaf node ages.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,14 @@ public Value<PopulationFunction> apply() {
return new Value<>(exponentialPopulation, this);
}

public Value<Double> getGrowthRate() {
return (Value<Double>) getParams().get(GROWTH_RATE_PARAM_NAME);
}


public Value<Double> getN0() {
return (Value<Double>) getParams().get(N0_PARAM_NAME);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.integration.IterativeLegendreGaussIntegrator;
import org.apache.commons.math3.analysis.integration.RombergIntegrator;
import org.apache.commons.math3.analysis.solvers.BrentSolver;
import org.apache.commons.math3.analysis.solvers.UnivariateSolver;
import org.apache.commons.math3.exception.NoBracketingException;
import org.apache.commons.math3.exception.TooManyEvaluationsException;

import java.io.FileWriter;
import java.io.IOException;
Expand Down Expand Up @@ -233,11 +237,22 @@ public double getInverseIntensity(double x) {
while (intensity < targetIntensity) {
time += deltaTime;
intensity = getIntensity(time);
}

double lowerBound = Math.max(0, time - deltaTime);
double upperBound = time;
UnivariateFunction function = t -> getIntensity(t) - x;
UnivariateSolver solver = new BrentSolver(1e-9, 1e-9);
try {
return solver.solve(100, function, lowerBound, upperBound);
} catch (NoBracketingException | TooManyEvaluationsException e) {
System.err.println("Solver failed: " + e.getMessage());
return Double.NaN;
}


// return time;
return Math.max(time, 0);
//return Math.max(time, 0);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,18 @@ public Value<PopulationFunction> apply() {

return new Value<>(logisticPopulation, this);
}
public Value<Double> getT50() {
return (Value<Double>) getParams().get(T50_PARAM_NAME);
}


public Value<Double> getNCarryingCapacity() {
return (Value<Double>) getParams().get(N_CARRYING_CAPACITY_PARAM_NAME);
}


public Value<Double> getB() {
return (Value<Double>) getParams().get(B_PARAM_NAME);
}
}

0 comments on commit 54e72ca

Please sign in to comment.