-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFit_Lambda_Script.m
More file actions
654 lines (537 loc) · 16.4 KB
/
Copy pathFit_Lambda_Script.m
File metadata and controls
654 lines (537 loc) · 16.4 KB
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
%Here we start with all of the data and fit all lambda values
%Pilot Diet data, N=12 subjects, length 740 TR
%Camcan Lifespan data, N=636, length 258 TR
%Here we do calculations with the raw lambda value (as opposed to
%Lambda=(lambda-lambda_c)/lambda_c).
%% Binarizing the Data
load('Diet_Data.mat'); %load variable Diet_Data
load('Flip_Ind.mat'); %load variable Flip_Ind
%The first 16 entries here are the ROI we decided to flip the signs of for
Flip=ones(1,498); %1 if we don't flip, -1 if we flip. See Methods and XYZ.m
for i=1:15
Flip(Flip_Ind(i))=-1;
end
Bglu=zeros(739,12);
Bket=zeros(739,12);
for i=1:12
yy=Diet_Data{i,1};
yyn=Isingify2(length(yy(:,1)),498,yy); %Isingify2.m binarizes input data using BDM
Bglu(:,i)=sum(Flip.*yyn,2)/498;
yy=Diet_Data{i,2};
yyn=Isingify2(length(yy(:,1)),498,yy);
Bket(:,i)=sum(Flip.*yyn,2)/498;
end
load('Age_Data.mat'); %Loads Age_Data
BAge=zeros(257,636);
for i=1:636
yy=Age_Data{i};
yyn=Isingify2(length(yy(:,1)),498,yy);
BAge(:,i)=sum(Flip.*yyn,2)/498;
end
%%
%Here we fit Lambda (Lam) using the probability distribution Eq 5
lamglu=zeros(1,12);
lamket=zeros(1,12);
lamage=zeros(1,636);
%This finds the unique value of lambda that best fits the sufficient
%statistic, mean(s^2), for each subject
for t=1:12
m2=mean(Bglu(:,t).^2);
k=0:1:498;
v=(2*k-498)/498; %all values of s
vv=v.^2;
nck=zeros(1,499);
for i=1:499
nck(i)=nchoosek(498,k(i));
end
f1=@(lambda) sum((vv).*nck.*exp(lambda.*vv*498^2));
f2=@(lambda) sum(nck.*exp(lambda.*vv*498^2)); %ML estimation
f3=@(lambda) f1(lambda)./f2(lambda)-m2;
options = optimset('TolX',1*10^-10);
lamglu(t)=fzero(f3,.00001,options);
end
for t=1:12
m2=mean(Bket(:,t).^2);
k=0:1:498;
v=(2*k-498)/498;
vv=v.^2;
nck=zeros(1,499);
for i=1:499
nck(i)=nchoosek(498,k(i));
end
f1=@(lambda) sum((vv).*nck.*exp(lambda.*vv*498^2));
f2=@(lambda) sum(nck.*exp(lambda.*vv*498^2));
f3=@(lambda) f1(lambda)./f2(lambda)-m2;
options = optimset('TolX',1*10^-10);
lamket(t)=fzero(f3,.00001,options);
end
for t=1:636
m2=mean(BAge(:,t).^2);
k=0:1:498;
v=(2*k-498)/498;
vv=v.^2;
nck=zeros(1,499);
for i=1:499
nck(i)=nchoosek(498,k(i));
end
f1=@(lambda) sum((vv).*nck.*exp(lambda.*vv*498^2));
f2=@(lambda) sum(nck.*exp(lambda.*vv*498^2));
f3=@(lambda) f1(lambda)./f2(lambda)-m2;
options = optimset('TolX',1*10^-10);
lamage(t)=fzero(f3,.00001,options);
end
%Outputs up to this point are ML lambda values for all subjects
%Statistics based on ML estimates
load('Sub_Ages.mat'); %Loads Sub_Ages
pdiet=signrank(lamglu,lamket,'tail','left'); %Wilcoxon Sign-rank for diets
[~,page]=corr(Sub_Ages,lamage','Type','Spearman'); %Spearman-Rank Test for age
%statistics were calculated using the raw lamda. Using
%Lambda=(lambda-lambda_c)/(lambda_c) instead does not change the pvalues or
%test outcomes.
%% Find Errorbars for Each Lambda fit
x=.0006:.00001:.0016; %Essentially our uniform prior distribution, other lambda values are unphysical
%Calculating the log-likelihood ratio of the ML fit from the data for each
%subject to all of the lambda values in x
LLglur=zeros(12,length(x));
LLketr=zeros(12,length(x));
LLager=zeros(636,length(x));
for i=1:636
va=BAge(:,i);
Qa=sum(va.^2);
for j=1:length(x)
LLager(i,j)=(x(j)-lamage(i))*Qa*498^2-length(va)*(log(f2(x(j)))-log(f2(lamage(i))));
end
end
for i=1:12
vglu=Bglu(:,i);
vket=Bket(:,i);
Qsglu=sum(vglu.^2);
Qsket=sum(vket.^2);
for j=1:length(x)
LLglur(i,j)=(x(j)-lamglu(i))*Qsglu*498^2-length(vglu)*(log(f2(x(j)))-log(f2(lamglu(i))));
LLketr(i,j)=(x(j)-lamket(i))*Qsket*498^2-length(vket)*(log(f2(x(j)))-log(f2(lamket(i))));
end
end
%Calculating the actual errorbars
Errtopg=zeros(1,12);
Errbotg=zeros(1,12);
Errtopk=zeros(1,12);
Errbotk=zeros(1,12);
Errtopage=zeros(1,636);
Errbotage=zeros(1,636);
thr=log(.01); %LL ratio is .01, that means that the ends of our confidence interval are predicted
%to be 100-fold less likely than the ML estimate.
%for each subject, finds the index (for the top and the bottom) of x, and corresponding values of lambda
%where the likelihood ratio is .01. This is done by looping through x and
%stopping at the lowest and highest values in x that satify LL(x(i))>.01
for i=1:636
xx=x-lamage(i);
qp=find(xx>0);
qm=find(xx<0);
ind=0;
ird=1;
while ind==0
if LLager(i,qp(ird))<=thr
Errtopage(i)=x(qp(ird));
ind=1;
elseif ird>length(qp)
ind=1;
else
ird=ird+1;
end
end
ind=0;
ird=1;
while ind==0
if LLager(i,qm(length(qm)-ird+1))<=thr
Errbotage(i)=x(qm(length(qm)-ird+1));
ind=1;
elseif ird>length(qm)
ind=1;
else
ird=ird+1;
end
end
end
for i=1:12
xx=x-lamglu(i);
qp=find(xx>0);
qm=find(xx<0);
ind=0;
ird=1;
while ind==0
if LLglur(i,qp(ird))<=thr
Errtopg(i)=x(qp(ird));
ind=1;
elseif ird>length(qp)
ind=1;
else
ird=ird+1;
end
end
ind=0;
ird=1;
while ind==0
if LLglur(i,qm(length(qm)-ird+1))<=thr
Errbotg(i)=x(qm(length(qm)-ird+1));
ind=1;
elseif ird>length(qm)
ind=1;
else
ird=ird+1;
end
end
end
for i=1:12
xx=x-lamket(i);
qp=find(xx>0);
qm=find(xx<0);
ind=0;
ird=1;
while ind==0
if LLketr(i,qp(ird))<=thr
Errtopk(i)=x(qp(ird));
ind=1;
elseif ird>length(qp)
ind=1;
else
ird=ird+1;
end
end
ind=0;
ird=1;
while ind==0
if LLketr(i,qm(length(qm)-ird+1))<=thr
Errbotk(i)=x(qm(length(qm)-ird+1));
ind=1;
elseif ird>length(qm)
ind=1;
else
ird=ird+1;
end
end
end
%output Error bars for all Lambda fits
%% Find probability distribution of Ising model
k=0:1:498;
v=(2*k-498)/498;
vv=v.^2;
nck=zeros(1,499);
for i=1:499
nck(i)=nchoosek(498,k(i));
end
x=.0007:.00001:.0012;
f1=@(lambda) sum(nck.*exp(lambda.*vv*498^2));
y=zeros(499,length(x));
for j=1:499
for i=1:length(x)
y(j,i)=x(i)*((2*(j-1)-498)^2)-log(f1(x(i)));
y(j,i)=y(j,i)+log(nchoosek(498,498+1-j));
end
end
P=sum(exp(y),1);
PP=log(P);
Prob_Dist=y-PP; %normalization correction just in case
Prob_Dist=exp(Prob_Dist); %This gives the probabilities of each synchrony for different values of lambda
% Calculating the 2nd and 4th moments
% from data- these were all concatenated in Supplementary Fig. ABC
K2g=mean(Bglu.^2,1);
K4g=var(Bglu.^2,1,1);
K2k=mean(Bket.^2,1);
K4k=var(Bket.^2,1,1);
K2a=mean(BAge.^2,1);
K4a=var(BAge.^2,1,1);
%from the model
K2mod=sum(vv'.*Prob_Dist,1);
K4mod=sum(vv'.^2.*Prob_Dist,1)-K2mod.^2;
%% Calculating the means and error bars from binning each subject.
% For the plots we used the rescaled Lambda, so we will compute that
% here
lam_crit=1.004*10^(-3);
Lamglu=(lamglu-lam_crit)/lam_crit; %Capital L refers to the rescaled values
Lamket=(lamket-lam_crit)/lam_crit;
Lamage=(lamage-lam_crit)/lam_crit;
% Age groups
Edges=[18,32,46,60,74,88]; %The plotted age groups (5 total) are the midpoints between these edges
Q25e=zeros(1,5);
Q75e=zeros(1,5);
Qmed=zeros(1,5);
for i=1:5
q=find(Sub_Ages>Edges(i)&Sub_Ages<Edges(i+1)); %calculates the medians and quartiles (error bars) of each age group
% Supplementary Fig Z
sss=Lamage(q);
Qmed(i)=median(sss);
Q25e(i)=Qmed(i)-quantile(sss,.25);
Q75e(i)=quantile(sss,.75)-Qmed(i);
end
% Diet
%Here we worked with difLam=Lamket-Lamglu
difLam=Lamket-Lamglu;
%Next we have to propagate the error bars (from our posterior
%distribution to find the error bars of difLam for each subject
% For simplicity we just approximate the error as being symmetric.
Errg=(Errtopg-Errbotg)/2;
Errk=(Errtopk-Errbotk)/2;
difErr=sqrt(Errg.^2+Errk.^2)/(lam_crit); %standard error propagation for differences
%rescaling was because of rescaling lambda->Lambda
%difErr gives the errorbars in Fig 5, where the
%subjects were ordered by difLam.
%% Find Int and Seg and find their ratios vs Lambda
All_Age_data=zeros(257*636,498);
for i=1:636
yy=Age_Data{i};
yyn=Isingify2(length(yy(:,1)),498,yy);
All_Age_data(257*(i-1)+1:257*(i-1)+257,:)=Flip.*yyn;
end
BAge_reshaped=reshape(BAge,[257*636,1]);
%Find Int and Seg and normalize them
Ind_Seg=All_Age_data((abs(BAge_reshaped)==0),:);
Seg=corrcoef(Ind_Seg);
Seg=Seg-eye(498,498);
Seg=Seg/sqrt(sum(sum(Seg.^2)));
Ind_Int=All_Age_data((abs(BAge_reshaped)>=1/2),:);
Int=corrcoef(Ind_Int);
Int=Int-eye(498,498);
Int=Int/sqrt(sum(sum(Int.^2)));
%Find P_Seg and P_Int for each subject
pseg=zeros(1,636);
pint=zeros(1,636);
cov_Seg_Int=sum(sum(Seg.*Int));
for i=1:636
yy=Age_Data{i};
yyn=Flip.*Isingify2(length(yy(:,1)),498,yy);
Ma=cov(yyn);
Ma=Ma-diag(diag(Ma));
Ma=Ma/sqrt((sum(sum(Ma.^2))));
pinti=sum(sum(Ma.*Int));
psegi=sum(sum(Ma.*Seg));
pint(i)=(pinti-psegi*cov_Seg_Int)/(1-cov_Seg_Int^2);
pseg(i)=(psegi-pinti*cov_Seg_Int)/(1-cov_Seg_Int^2);
end
pintd=zeros(1,24);
psegd=zeros(1,24);
for i=1:12
yy=Diet_Data{i,1};
yyn=Flip.*Isingify2(length(yy(:,1)),498,yy);
Ma=cov(yyn);
Ma=Ma-diag(diag(Ma));
Ma=Ma/sqrt(sum(sum(Ma.^2)));
pinti=sum(sum(Ma.*Int));
psegi=sum(sum(Ma.*Seg));
pintd(i)=(pinti-psegi*cov_Seg_Int)/(1-cov_Seg_Int^2);
psegd(i)=(psegi-pinti*cov_Seg_Int)/(1-cov_Seg_Int^2);
yy=Diet_Data{i,2};
yyn=Flip.*Isingify2(length(yy(:,1)),498,yy);
Ma=cov(yyn);
Ma=Ma-diag(diag(Ma));
Ma=Ma/sqrt(sum(sum(Ma.^2)));
pinti=sum(sum(Ma.*Int));
psegi=sum(sum(Ma.*Seg));
pintd(i+12)=(pinti-psegi*cov_Seg_Int)/(1-cov_Seg_Int^2);
psegd(i+12)=(psegi-pinti*cov_Seg_Int)/(1-cov_Seg_Int^2);
end
Lam_All=[Lamage,Lamglu,Lamket];
pint=[pint,pintd];
pseg=[pseg,psegd];
pint=pint./(pint+pseg);
pseg=1-pint;
%% Plot typical fits (for lambda above, below, and at the critical point)
load('lamket.mat')
load('lamglu.mat')
load('lamage.mat')
% Age 32
figure
annotation('textarrow','Position',[.0675,0.085,0,.15],'String',' P(s)','HeadWidth',5,'HeadLength',5,'HorizontalAlignment','left','VerticalAlignment','cap','TextRotation',90,'Textmargin',10)
annotation('doublearrow','Position',[.0250,0.085,.075,0],'Head1Width',5,'Head2Width',5,'Head1Length',5,'Head2Length',5)
annotation('textbox',[.0475,0.055,0,.05],'String','s','FitBoxToText','on','EdgeColor','none')
Lamplot=lamage(120);
Q=120;
pfit=zeros(1,499);
k=0:1:498;
v=(2*k-498)/498; %all values of s
vv=v.^2;
nck=zeros(1,499);
for i=1:499
nck(i)=nchoosek(498,k(i));
pfit(i)=nck(i)*exp(Lamplot*vv(i)*498^2);
end
countsp=round(pfit);
[countsd,Edges]=histcounts(BAge(:,Q));
countspnew=zeros(1,length(Edges)-1);
for i=1:length(countspnew)
k=ceil(Edges(i)*498);
if mod(k,2)==1
k=k+1;
end
while k<= floor(Edges(i+1)*498)
countspnew(i)=countspnew(i)+countsp((k+500)/2);
k=k+2;
end
end
Edges1=Edges;
countspnew1=countspnew;
subplot(2,3,1)
hold on
histogram('BinEdges',Edges1,'BinCounts',countspnew1,'normalization','probability','DisplayStyle','stairs');
histogram(BAge(:,Q),'normalization','probability','DisplayStyle','stairs')
xlabel('Age 32')
% Age 48
Lamplot=lamage(250);
Q=250;
pfit=zeros(1,499);
k=0:1:498;
v=(2*k-498)/498; %all values of s
vv=v.^2;
nck=zeros(1,499);
for i=1:499
nck(i)=nchoosek(498,k(i));
pfit(i)=nck(i)*exp(Lamplot*vv(i)*498^2);
end
countsp=round(pfit);
[countsd,Edges]=histcounts(BAge(:,Q));
countspnew=zeros(1,length(Edges)-1);
for i=1:length(countspnew)
k=ceil(Edges(i)*498);
if mod(k,2)==1
k=k+1;
end
while k<= floor(Edges(i+1)*498)
countspnew(i)=countspnew(i)+countsp((k+500)/2);
k=k+2;
end
end
Edges2=Edges;
countspnew2=countspnew;
subplot(2,3,2)
hold on
histogram('BinEdges',Edges2,'BinCounts',countspnew2,'normalization','probability','DisplayStyle','stairs');
histogram(BAge(:,Q),'normalization','probability','DisplayStyle','stairs')
xlabel('Age 48')
% Age 63
Lamplot=lamage(450);
Q=450;
pfit=zeros(1,499);
k=0:1:498;
v=(2*k-498)/498; %all values of s
vv=v.^2;
nck=zeros(1,499);
for i=1:499
nck(i)=nchoosek(498,k(i));
pfit(i)=nck(i)*exp(Lamplot*vv(i)*498^2);
end
countsp=round(pfit);
[countsd,Edges]=histcounts(BAge(:,Q));
countspnew=zeros(1,length(Edges)-1);
for i=1:length(countspnew)
k=ceil(Edges(i)*498);
if mod(k,2)==1
k=k+1;
end
while k<= floor(Edges(i+1)*498)
countspnew(i)=countspnew(i)+countsp((k+500)/2);
k=k+2;
end
end
Edges3=Edges;
countspnew3=countspnew;
subplot(2,3,4)
hold on
histogram('BinEdges',Edges3,'BinCounts',countspnew3,'normalization','probability','DisplayStyle','stairs');
histogram(BAge(:,Q),'normalization','probability','DisplayStyle','stairs')
xlabel('Age 63')
% Age 81
Lamplot=lamage(600);
Q=600;
pfit=zeros(1,499);
k=0:1:498;
v=(2*k-498)/498; %all values of s
vv=v.^2;
nck=zeros(1,499);
for i=1:499
nck(i)=nchoosek(498,k(i));
pfit(i)=nck(i)*exp(Lamplot*vv(i)*498^2);
end
countsp=round(pfit);
[countsd,Edges]=histcounts(BAge(:,Q));
countspnew=zeros(1,length(Edges)-1);
for i=1:length(countspnew)
k=ceil(Edges(i)*498);
if mod(k,2)==1
k=k+1;
end
while k<= floor(Edges(i+1)*498)
countspnew(i)=countspnew(i)+countsp((k+500)/2);
k=k+2;
end
end
Edges4=Edges;
countspnew4=countspnew;
subplot(2,3,5)
hold on
histogram('BinEdges',Edges4,'BinCounts',countspnew4,'normalization','probability','DisplayStyle','stairs');
histogram(BAge(:,Q),'normalization','probability','DisplayStyle','stairs')
xlabel('Age 81')
% Glucose
Q=10;
Lamplot=lamglu(Q);
pfit=zeros(1,499);
k=0:1:498;
v=(2*k-498)/498; %all values of s
vv=v.^2;
nck=zeros(1,499);
for i=1:499
nck(i)=nchoosek(498,k(i));
pfit(i)=nck(i)*exp(Lamplot*vv(i)*498^2);
end
countsp=round(pfit);
[countsd,Edges]=histcounts(Bglu(:,Q));
countspnew=zeros(1,length(Edges)-1);
for i=1:length(countspnew)
k=ceil(Edges(i)*498);
if mod(k,2)==1
k=k+1;
end
while k<= floor(Edges(i+1)*498)
countspnew(i)=countspnew(i)+countsp((k+500)/2);
k=k+2;
end
end
Edges5=Edges;
countspnew5=countspnew;
subplot(2,3,3)
hold on
histogram('BinEdges',Edges5,'BinCounts',countspnew5,'normalization','probability','DisplayStyle','stairs');
histogram(Bglu(:,Q),'normalization','probability','DisplayStyle','stairs')
xlabel('Glucose')
% Ketones
Q=10;
Lamplot=lamket(Q);
pfit=zeros(1,499);
k=0:1:498;
v=(2*k-498)/498; %all values of s
vv=v.^2;
nck=zeros(1,499);
for i=1:499
nck(i)=nchoosek(498,k(i));
pfit(i)=nck(i)*exp(Lamplot*vv(i)*498^2);
end
countsp=round(pfit);
[countsd,Edges]=histcounts(Bket(:,Q));
countspnew=zeros(1,length(Edges)-1);
for i=1:length(countspnew)
k=ceil(Edges(i)*498);
if mod(k,2)==1
k=k+1;
end
while k<= floor(Edges(i+1)*498)
countspnew(i)=countspnew(i)+countsp((k+500)/2);
k=k+2;
end
end
Edges6=Edges;
countspnew6=countspnew;
subplot(2,3,6)
hold on
histogram('BinEdges',Edges6,'BinCounts',countspnew6,'normalization','probability','DisplayStyle','stairs');
histogram(Bket(:,Q),'normalization','probability','DisplayStyle','stairs')
xlabel('Ketones')