Debian Codegraph @

src/dr/evolution/wrightfisher/GammaFitnessFunction.java

raw symbols SCIP
1 /*
2 * GammaFitnessFunction.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evolution.wrightfisher;
27
28 import dr.math.MathUtils;
29 import dr.math.distributions.GammaDistribution;
30
31 public class GammaFitnessFunction extends FitnessFunction {
32
33 /**
34 * selection coefficients are gamma distributed with parameters alpha and beta
35 */
36 public GammaFitnessFunction(int genomeLength, double alpha, double beta, int stateSize, boolean randomFittest) {
37
38 GammaDistribution gamma = new GammaDistribution(beta, alpha);
39
40 fitness = new double[genomeLength][stateSize];
41 fittest = new byte[genomeLength];
42 int fitpos = 0;
43 for (int i = 0; i < genomeLength; i++) {
44
45 if (randomFittest) {
46 fitpos = MathUtils.nextInt(stateSize);
47 }
48 fitness[i][fitpos] = 1.0;
49 fittest[i] = (byte) fitpos;
50 for (int j = 0; j < stateSize; j++) {
51
52 if (j != fitpos) {
53 double prob = Math.round(MathUtils.nextDouble() * 1000.0) / 1000.0;
54 while ((prob <= 0.0) || (prob >= 1.0)) {
55 prob = Math.round(MathUtils.nextDouble() * 1000.0) / 1000.0;
56 }
57 fitness[i][j] = Math.max(0.0, 1.0 - gamma.quantile(prob));
58 }
59 }
60 }
61 }
62
63 public final double getFitness(byte[] sequence) {
64
65 double totalFitness = 1.0;
66
67 for (int i = 0; i < sequence.length; i++) {
68 totalFitness *= fitness[i][sequence[i]];
69 }
70 return totalFitness;
71 }
72
73 /**
74 * @return the relative fitness increase of the new state at given position to the old state.
75 */
76 public double getFitnessFactor(int pos, byte newState, byte oldState) {
77 return fitness[pos][newState] / fitness[pos][oldState];
78 }
79
80 public final double[][] getFitnessTable() {
81
82 for (int j = 0; j < fitness[0].length; j++) {
83 for (int i = 0; i < fitness.length; i++) {
84 System.out.print((Math.round(fitness[i][j] * 1000.0) / 1000.0) + "\t");
85 }
86 System.out.println();
87 }
88
89 return fitness;
90 }
91
92 public void initializeToFittest(byte[] genome) {
93 System.arraycopy(fittest, 0, genome, 0, fittest.length);
94 }
95
96 double[][] fitness;
97 byte[] fittest = null;
98 }