forked from xiaoming-liu/stairway-plot-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSFS_log_likelihood_problem_no_dim_penalty_fold.java
405 lines (377 loc) · 12.1 KB
/
SFS_log_likelihood_problem_no_dim_penalty_fold.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/**
*
* Copyright (c) @author Xiaoming Liu, Ph.D.
* Associate Professor,
* USF Genomics,
* College of Public Health,
* University of South Florida at Tampa
*
* This source code is distributed under the Artistic License 2.0
*
* The license can be found at
* https://opensource.org/licenses/Artistic-2.0
*/
import java.util.*;
import swarmops.Problem;
import swarmops.Tools;
public class SFS_log_likelihood_problem_no_dim_penalty_fold extends Problem{
int n;
double L;
double[][] logpara;
double[] logfac;
double[] logexi;
double[] exi;
double[] eta;
boolean precomputesReady=false;
boolean expectedXiReady=false;
double maxfit;
double[] lowerBound;
double[] upperBound;
String[] parameterName;
boolean[] obs;
int nobs;
boolean setdimpenalty=false;
double dimpenalty=0;
boolean setautocorr=false;
double pautocorr=1;
int[] groupSplitPoints;
double[][] groupThetaXiMatrix;
int[] groupThetaXiMatrixMax;
double[] etaLogFactorial;
public SFS_log_likelihood_problem_no_dim_penalty_fold(int N){
super();
n=N;
logfac= new double[n+1];
logfac[0]=0;
for(int i=1;i<=n;i++){
logfac[i]=logfac[i-1]+Math.log(1.0*i);
}
logpara=new double[n][];
for(int i=1;i<=n-1;i++){
logpara[i]=new double[n-i+1+1];
}
for(int i=1;i<=n-1;i++){
for(int k=2;k<=n-i+1;k++) logpara[i][k]=logfac[n-i-1]+logfac[n-k]-logfac[n-i-k+1]-logfac[n-1];
}
logexi=new double[n];
exi=new double[n];
eta=new double[n/2+1];
groupSplitPoints=new int[n];
for(int i=0;i<n;i++)groupSplitPoints[i]=i+2;//default group: each theta(i) as a group
obs=new boolean[n/2+1];
for(int i=0;i<=n/2;i++) obs[i]=true;//default observed eta: all eta
nobs=n/2+1;
}
public double[] getLogExi(double[] groupThetas){
if(!expectedXiReady) calculateExpectedXi(groupThetas);
return logexi;
}
public boolean getInitialize2(double[] Theta){
double totale=0;
for(int i=1;i<=n-1;i++){
double exii=0;
for(int k=2;k<=n-i+1;k++) exii+=Math.exp(logpara[i][k])*Theta[k];
totale+=exii;
if (exii==0) exii=Double.MIN_VALUE;
exi[i]=exii;
logexi[i]=Math.log(exii);
}
if(1-totale>0){
exi[0]=(1-totale);
logexi[0]=Math.log(exi[0]);
expectedXiReady=true;
return true;
}
else {
exi[0]=Double.MIN_VALUE;
logexi[0]=Math.log(exi[0]);
return false;
}
}
public double preFunction(int n, int i, int j) {
if (j==2) return 1.0;
return Math.exp(logfac[n-j+1]+logfac[n-i-1]-logfac[n-i-j+1]-logfac[n-1]);
}
public void doPrecomputes() {
groupThetaXiMatrixMax = new int[n];
groupThetaXiMatrix = new double[n][];
for (int i=1;i<=n-1;i++) {
int jmax = n-i+1;
int ngroups = groupSplitPoints.length-1;
int igmax;
for(igmax=0; igmax<ngroups; igmax++) {
int j1=groupSplitPoints[igmax+1];
if (j1 > jmax) break;
}
groupThetaXiMatrixMax[i] = igmax;
groupThetaXiMatrix[i] = new double[igmax+1];
for(int ig=0; ig<=igmax; ig++) {
int j0=groupSplitPoints[ig];
int j1=groupSplitPoints[ig+1];
double pre0 = preFunction(n,i,j0);
double pre1 = ig==igmax ? 0.0 : preFunction(n,i,j1);
groupThetaXiMatrix[i][ig] = (pre0-pre1)/i;
}
}
etaLogFactorial = new double[n];
for(int i=1;i<=n/2;i++) etaLogFactorial[i] = gammln(eta[i]+1.0);
precomputesReady = true;
}
public boolean calculateExpectedXi(double[] groupThetas){
if (!precomputesReady) doPrecomputes();
//calculate
double totale=0;
for(int i=1;i<=n-1;i++){
int jmax = n-i+1;
double exii=0;
for(int ig=0; ig<=groupThetaXiMatrixMax[i]; ig++) {
exii += groupThetaXiMatrix[i][ig] * groupThetas[ig];
}
totale+=exii;
if (exii==0) exii=Double.MIN_VALUE;
exi[i]=exii;
logexi[i]=Math.log(exii);
}
if(1-totale>0){
exi[0]=(1-totale);
logexi[0]=Math.log(exi[0]);
expectedXiReady=true;
return true;
}
else {
//System.out.println("1-sum(theta)>1");
exi[0]=Double.MIN_VALUE;
logexi[0]=Math.log(exi[0]);
return false;
}
}
public void setData(double[] Eta){
for(int i=0;i<=n/2;i++){
eta[i]=Eta[i];
}
double totall=0;
L=0;
for(int i=0;i<=n/2;i++){
L+=eta[i];
}
double eother=eta[0]/L;
double oother=eta[0];
for(int i=1;i<=n/2;i++){
if(eta[i]>0&&obs[i])totall+=Math.log(eta[i]/L)*eta[i]-gammln(eta[i]+1.0);
else{
eother+=eta[i]/L;
oother+=eta[i];
}
}
totall+=Math.log(eother)*oother-gammln(oother+1.0);
totall+=gammln(L+1.0);//add constant
maxfit=totall;//no panelty
precomputesReady = false;
}
public double getLogLikelihood(double[] groupThetas){
if (!precomputesReady) doPrecomputes();
calculateExpectedXi(groupThetas);
double totall=0;
double eother=exi[0];
double oother=eta[0];
for(int i=1;i<=n/2;i++){
if(i!=n-i){
if(obs[i]) totall+=Math.log(exi[i]+exi[n-i])*eta[i]-etaLogFactorial[i];
else{
eother+=exi[i]+exi[n-i];
oother+=eta[i];
}
}
else{//when i*2=n
if(obs[i]) totall+=logexi[i]*eta[i]-etaLogFactorial[i];
else{
eother+=exi[i];
oother+=eta[i];
}
}
}
totall+=Math.log(eother)*oother-gammln(oother+1.0);
totall+=gammln(L+1.0);//add constant
double adjustlogL=totall;//no panelty
if(setdimpenalty)adjustlogL=totall-dimpenalty;
return adjustlogL;
}
/**
*
* @param exi, expected freq of \xi, from \xi_0 to \xi_n-1
* @param Eta, observed counts of \eta from \eta_0 to \eta_n/2
* @return logL
*/
public double getLogLikelihood(double[] exi,double[] Eta){
n=exi.length;
for(int i=0;i<=n-1;i++)logexi[i]=Math.log(exi[i]);
L=0;
for(int i=0;i<=n/2;i++){
eta[i]=Eta[i];
L+=eta[i];
}
double totall=0;
double eother=Math.exp(logexi[0]);
double oother=eta[0];
for(int i=1;i<=n/2;i++){
if(i!=n-i){
if(obs[i]) totall+=Math.log(Math.exp(logexi[i])+Math.exp(logexi[n-i]))*eta[i]-gammln(eta[i]+1.0);
else{
eother+=Math.exp(logexi[i])+Math.exp(logexi[n-i]);
oother+=eta[i];
}
}
else{//when i*2=n
if(obs[i]) totall+=logexi[i]*eta[i]-gammln(eta[i]+1.0);
else{
eother+=Math.exp(logexi[i]);
oother+=eta[i];
}
}
}
totall+=Math.log(eother)*oother-gammln(oother+1.0);
totall+=gammln(L+1.0);//add constant
double adjustlogL=totall;//no panelty
if(setdimpenalty)adjustlogL=totall-dimpenalty;
return adjustlogL;
}
public void setDimPenalty(double p){
dimpenalty=p;
setdimpenalty=true;
}
public void setAutoCorr(double p){
setautocorr=true;
pautocorr=p;
}
public void setThetaGroup(int[][] Group){
groupSplitPoints = new int[Group.length+1];
for(int i=0;i<Group.length;i++)groupSplitPoints[i]=Group[i][0];
int ilast=Group.length-1;
int jlast=Group[ilast].length-1;
groupSplitPoints[ilast+1]=Group[ilast][jlast]+1;
precomputesReady = false;
}
/**
* set whether xi(i) is used for analysis
* @param Obs
*/
public void setObsEta(boolean[] Obs){
obs=(boolean[])Obs.clone();
nobs=1;
for(int i=1;i<=n/2;i++) if(obs[i])nobs++;
precomputesReady = false;
}
public String getName() {
return "SFS_log_likelihood_problem";
}
public int getDimensionality() {
return groupSplitPoints.length-1;
}
public double[] getLowerBound() {
lowerBound = new double[getDimensionality()];
Arrays.fill(lowerBound,0);
/
return lowerBound;
}
public double[] getUpperBound() {
upperBound = new double[getDimensionality()];
Arrays.fill(upperBound,0.2);
return upperBound;
}
@Override
public double[] getLowerInit() {
return getLowerBound();
}
@Override
public double[] getUpperInit() {
return getUpperBound();
}
public double getMinFitness() {
return -maxfit;
}
@Override
public double getAcceptableFitness() {
return -maxfit;
}
@Override
public String[] getParameterName() {
parameterName =new String[getDimensionality()];
for(int i=0;i<n-1;i++) parameterName[i]="theta"+(i+2);
return parameterName;
}
@Override
public double fitness(double[] x) {
assert x != null && x.length == getDimensionality();
double[] groupThetas = x;
return -getLogLikelihood(groupThetas);
}
@Override
public boolean enforceConstraints(double[] x) {
// Enforce boundaries.
Tools.bound(x, getLowerBound(), getUpperBound());
// Return feasibility.
return isFeasible(x);
}
@Override
public boolean isFeasible(double[] x) {//isFeasible come first before geting fitness
assert x != null && x.length == getDimensionality();
boolean feasible=true;
double[] groupThetas = x;
for (int ig=0; ig<groupThetas.length; ig++) if (groupThetas[ig]==0) return false;
if(setautocorr){
double p2=(1-pautocorr)/2;
//assume theta' drawn from an exponential distribution with mean theta, from Bayesian skyline plot
for (int ig=1; ig<groupThetas.length-0; ig++) {
double lambda=1/groupThetas[ig-1];
double cdf=1-Math.exp(-lambda*groupThetas[ig]);
if(cdf<p2||cdf>1-p2) return false;
}
for (int ig=0; ig<groupThetas.length-1; ig++) {
double lambda=1/groupThetas[ig+1];
double cdf=1-Math.exp(-lambda*groupThetas[ig]);
if(cdf<p2||cdf>1-p2) return false;
}
}
double k=0;
for (int ig=0; ig<groupThetas.length; ig++) {
int i0 = groupSplitPoints[ig];
int i1 = groupSplitPoints[ig+1];
for (int i=i0; i<i1; i++) k += groupThetas[ig]/(i-1);
}
if(k>1)feasible=false;
return feasible;
}
/**
* Compute LnGamma(x)
*/
private static double gammln(double xx)
{
int j;
double temp;
double cof[] = new double[7];
double stp, half, one, fpf, x, tmp, ser;
cof[1] = 76.18009172947146;
cof[2] = -86.50532032941677;
cof[3] = 24.01409824083091;
cof[4] = -1.231739572450155;
cof[5] = 0.001208650973866179;
cof[6] = -0.000005395239384953;
stp = 2.5066282746310005;
half = 0.5;
one = 1.0;
fpf = 5.5;
x = xx ;
tmp = x + fpf;
tmp = (x + half) * Math.log(tmp) - tmp;
ser = 1.000000000190015;
for (j = 1; j <= 6; j++)
{
x = x + one;
ser = ser + cof[j] / x;
}
temp = tmp + Math.log(stp * ser/xx);
return temp;
}
public static void main(String[] args){
}
}