Debian Codegraph @

src/dr/evomodel/alloppnet/speciation/AlloppNetworkPrior.java

raw symbols SCIP
1 /*
2 * AlloppNetworkPrior.java
3 *
4 * Copyright (c) 2002-2017 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.evomodel.alloppnet.speciation;
27
28 import dr.inference.distribution.ParametricDistributionModel;
29 import dr.inference.model.Likelihood;
30 import dr.inference.model.Parameter;
31
32 import java.util.ArrayList;
33
34
35 /**
36 *
37 * Calculates prior likelihood for an allopolyploid network.
38 *
39 * @author Graham Jones
40 * Date: 01/07/2011
41 */
42
43
44
45
46
47 public class AlloppNetworkPrior extends Likelihood.Abstract {
48 AlloppSpeciesNetworkModel asnm;
49 AlloppNetworkPriorModel prior;
50 double numHybsLogL[];
51
52
53
54 public AlloppNetworkPrior(AlloppNetworkPriorModel prior, AlloppSpeciesNetworkModel asnm) {
55 super(prior);
56 this.asnm = asnm;
57 this.prior = prior;
58 asnm.setHybPopModel(prior.getHybridPopModel());
59
60 int ndips = asnm.getNofDiploids();
61 numHybsLogL = new double[asnm.maxNumberOfHybPopParameters()+1];
62 for (int h = 0; h < numHybsLogL.length; h++) {
63 numHybsLogL[h] = -h * Math.log(7.0*Math.sqrt(ndips));
64 // grjtodo-soon the form of the function is experimental
65 }
66
67
68 }
69
70
71
72 @Override
73 protected boolean getLikelihoodKnown() {
74 return false;
75 }
76
77 @Override
78 protected double calculateLogLikelihood() {
79 double llhood = 0.0;
80 //network topology and times prior
81 llhood += loglikelihoodEvents();
82 //System.out.print(llhood); System.out.print(" ");
83 llhood += loglikeNumHybridizations();
84 //System.out.print(llhood); System.out.print(" ");
85 // population prior for tips
86 Parameter tippvals = asnm.getTipPopValues();
87 ParametricDistributionModel tipmodel = prior.getTipPopModel();
88 for (int i = 0; i < tippvals.getDimension(); i++) {
89 llhood += tipmodel.logPdf(tippvals.getParameterValue(i));
90 }
91 // population prior for root ends
92 Parameter rootpvals = asnm.getRootPopValues();
93 ParametricDistributionModel rootmodel = prior.getRootPopModel();
94 for (int i = 0; i < rootpvals.getDimension(); i++) {
95 llhood += rootmodel.logPdf(rootpvals.getParameterValue(i));
96 }
97 // population prior for new hybrids
98 ParametricDistributionModel hybmodel = prior.getHybridPopModel();
99 for (int i = 0; i < asnm.getNumberOfTetraTrees(); i++) {
100 llhood += hybmodel.logPdf(asnm.getOneHybPopValue(i));
101 }
102 //System.out.println(llhood);
103 return llhood;
104 }
105
106
107
108
109
110 private double loglikeNumHybridizations() {
111 int nhybs = asnm.getNumberOfTetraTrees();
112 //System.out.print(nhybs); System.out.print(" ");
113 return numHybsLogL[nhybs];
114 }
115
116
117 /*
118 * Going backwards in time this gives probabilities to three types
119 * of events: diploid-diploid joins, tet-tet joins, and hybridization events.
120 */
121 private double loglikelihoodEvents() {
122
123 double lambda = prior.getRate().getParameterValue(0);
124
125 ArrayList<Double> heights = new ArrayList<Double>();
126
127 AlloppDiploidHistory adhist = asnm.getDiploidHistory();
128 adhist.collectInternalAndHybHeights(heights);
129 int nttrees = asnm.getNumberOfTetraTrees();
130 for (int tt = 0; tt < nttrees; tt++) {
131 AlloppLeggedTree ttree = asnm.getTetraploidTree(tt);
132 ttree.collectInternalHeights(heights);
133 }
134 double loglhood = 0.0;
135 for (double t : heights) {
136 loglhood += logexpPDF(t, lambda);
137 }
138 return loglhood;
139 }
140
141
142 private double logexpPDF(double x, double rate) {
143 return Math.log(rate) - rate*x;
144 }
145
146 }