Skip to content

Commit

Permalink
Added operator for inferring negative diversification rates.
Browse files Browse the repository at this point in the history
  • Loading branch information
tgvaughan committed Oct 11, 2022
1 parent ec2caf6 commit 7489821
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/bdsky/JointDiversificationTurnoverOperator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package bdsky;

import beast.core.Description;
import beast.core.Input;
import beast.core.Operator;
import beast.core.parameter.RealParameter;
import beast.util.Randomizer;

@Description("Operator necessary for inferring negative diversification rates.")
public class JointDiversificationTurnoverOperator extends Operator {

public Input<RealParameter> divRateInput = new Input<>("netDiversification",
"Net diversification rate parameter.", Input.Validate.REQUIRED);

public Input<RealParameter> turnOverInput = new Input<>("turnOver",
"Turnover parameter.", Input.Validate.REQUIRED);

RealParameter divRate, turnOver;

@Override
public void initAndValidate() {
divRate = divRateInput.get();
turnOver = turnOverInput.get();

if (divRate.getDimension() != turnOver.getDimension())
throw new IllegalArgumentException("JointDiversificationTurnoverOperator " +
"requires that the dimension of diversification rate and " +
"turnover are identical.");
}

@Override
public double proposal() {

int idx = Randomizer.nextInt(divRate.getDimension());

double newDiv = -divRate.getValue(idx);
double newTurnOver = 1.0/turnOver.getValue(idx);

if (newDiv < divRate.getLower() || newDiv > divRate.getUpper()
|| newTurnOver < turnOver.getLower() || newTurnOver > turnOver.getUpper())
return Double.NEGATIVE_INFINITY;

divRate.setValue(idx, newDiv);
turnOver.setValue(idx, newTurnOver);

return 2*Math.log(newTurnOver);
}
}

0 comments on commit 7489821

Please sign in to comment.