Debian Codegraph @

src/dr/evolution/wrightfisher/GammaInvFitnessFunction.java

raw symbols SCIP
1 /*
2 * GammaInvFitnessFunction.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 GammaInvFitnessFunction extends FitnessFunction {
32
33 /**
34 * selection coefficients are gamma distributed with parameters alpha and beta
35 */
36 public GammaInvFitnessFunction(int genomeLength, double alpha, double beta, double pInv, 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
54 if (MathUtils.nextDouble() > pInv) {
55
56 double prob = Math.round(MathUtils.nextDouble() * 1000.0) / 1000.0;
57 while ((prob <= 0.0) || (prob >= 1.0)) {
58 prob = Math.round(MathUtils.nextDouble() * 1000.0) / 1000.0;
59 }
60 fitness[i][j] = Math.max(0.0, 1.0 - gamma.quantile(prob));
61 } else {
62 fitness[i][j] = 0.0;
63 }
64 }
65 }
66 }
67 }
68
69 public final double getFitness(byte[] sequence) {
70
71 double totalFitness = 1.0;
72
73 for (int i = 0; i < sequence.length; i++) {
74 totalFitness *= fitness[i][sequence[i]];
75 }
76 return totalFitness;
77 }
78
79 /**
80 * @return the relative fitness increase of the new state at given position to the old state.
81 */
82 public double getFitnessFactor(int pos, byte newState, byte oldState) {
83 return fitness[pos][newState] / fitness[pos][oldState];
84 }
85
86 public final double[][] getFitnessTable() {
87
88 for (int j = 0; j < fitness[0].length; j++) {
89 for (int i = 0; i < fitness.length; i++) {
90 System.out.print((Math.round(fitness[i][j] * 1000.0) / 1000.0) + "\t");
91 }
92 System.out.println();
93 }
94
95 return fitness;
96 }
97
98 public void initializeToFittest(byte[] genome) {
99 System.arraycopy(fittest, 0, genome, 0, fittest.length);
100 }
101
102 double[][] fitness;
103 byte[] fittest = null;
104 }