Skip to content
Snippets Groups Projects
melhlprfns.cc 22.3 KiB
Newer Older
/*  MELODIC - Multivariate exploratory linear optimized decomposition into 
              independent components
    
    melhlprfns.cc - misc functions

    Christian F. Beckmann, FMRIB Image Analysis Group
    
    Copyright (C) 1999-2007 University of Oxford */

/*  CCOPYRIGHT  */

#include "melhlprfns.h"
#include "libprob.h"
#include "miscmaths/miscprob.h"
Christian Beckmann's avatar
Christian Beckmann committed
#include "miscmaths/t2z.h"
#include "miscmaths/f2z.h"

namespace Melodic{

  void update_mask(volume<float>& mask, Matrix& Data)
  {
    Matrix DStDev=stdev(Data);
    volume4D<float> tmpMask, RawData;
    tmpMask.setmatrix(DStDev,mask);
    RawData.setmatrix(Data,mask);

    float tMmax;
    volume<float> tmpMask2;
    tmpMask2 = tmpMask[0];

    tMmax = tmpMask2.max();
    double st_mean = DStDev.Sum()/DStDev.Ncols();
    double st_std  = stdev(DStDev.t()).AsScalar();
      
    mask = binarise(tmpMask2,(float) max((float) st_mean-3*st_std,(float) 0.01*st_mean),tMmax);

    Data = RawData.matrix(mask);
  }

  void del_vols(volume4D<float>& in, int howmany)
  {
    for(int ctr=1; ctr<=howmany; ctr++){
	in.deletevolume(ctr);
    }    
  }

  Matrix calc_FFT(const Matrix& Mat, const bool logpwr)
  {
    Matrix res;
      for(int ctr=1; ctr <= Mat.Ncols(); ctr++){
	      ColumnVector tmpCol;  
	      tmpCol=Mat.Column(ctr);
	      ColumnVector FtmpCol_real;
	      ColumnVector FtmpCol_imag;
	      ColumnVector tmpPow;
	      if(tmpCol.Nrows()%2 != 0){
	        Matrix empty(1,1); empty=0;
	        tmpCol &= empty;
	      }
	      RealFFT(tmpCol,FtmpCol_real,FtmpCol_imag);
	      tmpPow = pow(FtmpCol_real,2)+pow(FtmpCol_imag,2);
	      tmpPow = tmpPow.Rows(2,tmpPow.Nrows());
	      if(logpwr) tmpPow = log(tmpPow);
	      if(res.Storage()==0){res= tmpPow;}else{res|=tmpPow;}
      }
      return res;
  }  //Matrix calc_FFT()

  Matrix smoothColumns(const Matrix& inp)
  {
    Matrix temp(inp);
    int ctr1 = temp.Nrows();
    Matrix temp2(temp);
    temp2=0;
    
    temp = temp.Row(4) & temp.Row(3) & temp.Row(2) & temp & temp.Row(ctr1-1) 
      & temp.Row(ctr1-2) &temp.Row(ctr1-3);
    
    double kern[] ={0.0045 , 0.055, 0.25, 0.4, 0.25, 0.055, 0.0045};
    double fac = 0.9090909;

    
    for(int cc=1;cc<=temp2.Ncols();cc++){
      for(int cr=1;cr<=temp2.Nrows();cr++){
	temp2(cr,cc) = fac*( kern[0] * temp(cr,cc) + kern[1] * temp(cr+1,cc) + 
			     kern[2] * temp(cr+2,cc) + kern[3] * temp(cr+3,cc) + 
			     kern[4] * temp(cr+4,cc) + kern[5] * temp(cr+5,cc) + 
			     kern[6] * temp(cr+6,cc));
      }
    }
    return temp2;
  }  //Matrix smoothColumns()

  Matrix convert_to_pbsc(Matrix& inp)
  {
    Matrix meanimg;
    meanimg = mean(inp);
    float eps = 0.00001;

    for(int ctr=1; ctr <= inp.Ncols(); ctr++){
      if(meanimg(1,ctr) < eps) 
	meanimg(1,ctr) = eps;
    }

    for(int ctr=1; ctr <= inp.Nrows(); ctr++){
      Matrix tmp;
      tmp << inp.Row(ctr);
      inp.Row(ctr) << 100 * SP((tmp - meanimg),pow(meanimg,-1));   
    }

    inp = remmean(inp);
    return meanimg;
  }  //void convert_to_pbsc   

  RowVector varnorm(Matrix& in, int dim, float level)
  {
    Matrix Corr;
    Corr = calc_corr(in);
    RowVector out;
    out = varnorm(in,Corr,dim,level);
    return out;
  }  //RowVector varnorm

  RowVector varnorm(Matrix& in, Matrix& Corr, int dim, float level)
  { 
    Matrix tmpE, white, dewhite;
Christian Beckmann's avatar
Christian Beckmann committed
    RowVector tmpD, tmpD2;

    std_pca(in, Corr, tmpE, tmpD);
    calc_white(tmpE,tmpD, dim, white, dewhite);
    
    Matrix ws = white * in;
    for(int ctr1 = 1; ctr1<=ws.Ncols(); ctr1++)
      for(int ctr2 = 1; ctr2<=ws.Nrows(); ctr2++)
	if(std::abs(ws(ctr2,ctr1)) < level)
	  ws(ctr2,ctr1)=0;
    tmpD = stdev(in - dewhite*ws);
Christian Beckmann's avatar
Christian Beckmann committed
    for(int ctr1 = 1; ctr1<=tmpD.Ncols(); ctr1++)
      if(tmpD(ctr1) < 0.01){
	tmpD(ctr1) = 1.0;
	in.Column(ctr1) = 0.0*in.Column(ctr1);
      }
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 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
    in = SP(in,pow(ones(in.Nrows(),1) * tmpD,-1));
    return tmpD;
  }  //RowVector varnorm

  Matrix SP2(const Matrix& in, const Matrix& weights, bool econ)
  {
    Matrix Res;
    Res = in;
    if(econ){
      ColumnVector tmp;
      for(int ctr=1; ctr <= in.Ncols(); ctr++){
	tmp = in.Column(ctr);
	tmp = tmp * weights(1,ctr);
	Res.Column(ctr) = tmp;
      }
    }
    else{
      Res = ones(in.Nrows(),1)*weights.Row(1);
      Res = SP(in,Res);
    }
    return Res;
  }  //Matrix SP

  Matrix calc_corr(const Matrix& in, bool econ)
  {
    Matrix Res;
    Res = zeros(in.Nrows(),in.Nrows());
    if(econ){
      ColumnVector tmp;
      for(int ctr=1; ctr <= in.Ncols(); ctr++){
	tmp = in.Column(ctr);
	tmp = tmp - mean(tmp).AsScalar();
	Res += (tmp * tmp.t()) / in.Ncols();
      }
    }
    else
      Res = cov(in.t());
    return Res;
  }  //Matrix calc_corr

  Matrix calc_corr(const Matrix& in, const Matrix& weights, bool econ)
  {
    Matrix Res;
    Res = zeros(in.Nrows(),in.Nrows());
    Matrix localweights;
    if(weights.Storage() == 0)
      localweights = ones(1,in.Ncols());
    else
      localweights = weights;
    if(econ){
      ColumnVector tmp;
      for(int ctr=1; ctr <= in.Ncols(); ctr++){
	tmp = in.Column(ctr);
	tmp = tmp - mean(tmp).AsScalar();
	tmp = tmp * localweights(1,ctr);
	Res += (tmp * tmp.t()) / in.Ncols();
      }
    }
    else{
      Res = SP2(in,localweights);
      Res = calc_corr(Res, 0); 
    }
    return Res;
  }  //Matrix calc_corr

  float calc_white(const Matrix& tmpE, const RowVector& tmpD, const RowVector& PercEV, int dim, Matrix& white, Matrix& dewhite)
  {
    Matrix RE;
    DiagonalMatrix RD;
    int N = tmpE.Ncols();
    dim = std::min(dim,N);
    RE = tmpE.Columns(N-dim+1,N);
    RD << abs(diag(tmpD.t()));
    RD << RD.SymSubMatrix(N-dim+1,N);    

    float res = 1.0;    
    white = sqrt(abs(RD.i()))*RE.t();
    dewhite = RE*sqrt(RD);

    if(dim < N)
      res = PercEV(dim);
    return res;
  }  //Matrix calc_white

  void calc_white(const Matrix& tmpE, const RowVector& tmpD, int dim, Matrix& white, Matrix& dewhite)
  {
    RowVector tmp(tmpE.Ncols());
    float tmp2;
    tmp2 = calc_white(tmpE,tmpD, tmp, dim, white, dewhite); 
  }  //Matrix calc_white

  void calc_white(const Matrix& Corr, int dim, Matrix& white, Matrix& dewhite)
  {
    Matrix RE;
    DiagonalMatrix RD;
    SymmetricMatrix tmp;
    RowVector tmp2;
    tmp << Corr;
    EigenValues(tmp,RD,RE);
    tmp2 = diag(RD).t();
    calc_white(RE,tmp2, dim, white, dewhite); 
  }  //Matrix calc_white
  
  void std_pca(const Matrix& Mat, const Matrix& weights, Matrix& Corr, Matrix& evecs, RowVector& evals)
  {
    if(weights.Storage()>0)
      Corr << calc_corr(Mat, weights);
    else
      Corr << calc_corr(Mat);
    SymmetricMatrix tmp;
    tmp << Corr;
    DiagonalMatrix tmpD;
    EigenValues(tmp,tmpD,evecs);
    evals = tmpD.AsRow();
  }  //void std_pca

  void std_pca(const Matrix& Mat, Matrix& Corr, Matrix& evecs, RowVector& evals)
  {
    Matrix weights;
    std_pca(Mat,weights,Corr,evecs,evals);
  }  //void std_pca

  void em_pca(const Matrix& Mat, Matrix& evecs, RowVector& evals, int num_pc, int iter)
  {
    Matrix guess;
    guess = normrnd(Mat.Nrows(),num_pc);
    em_pca(Mat,guess,evecs,evals,num_pc,iter);
  }  //void em_pca

  void em_pca(const Matrix& Mat, Matrix& guess, Matrix& evecs, RowVector& evals, int num_pc, int iter)
  {
    Matrix C;
    if(guess.Ncols() < num_pc){
      C=normrnd(Mat.Nrows(),num_pc);
      C.Columns(1,guess.Ncols()) = guess;
    }
    else
      C = guess;

    Matrix tmp, tmp2;
    for(int ctr=1; ctr <= iter; ctr++){
      // E-Step
      tmp = C.t()*C;
      tmp = tmp.i();
      tmp = tmp * C.t();
      tmp = tmp * Mat;
      // M-Step
      tmp2 = tmp * tmp.t();
      tmp2 = tmp2.i();
      tmp2 = Mat*tmp.t()*tmp2;
      C = tmp2;
    }
    
    symm_orth(C);
    Matrix Evc, tmpC;
    RowVector Evl;
    tmp = C.t() * Mat;
    std_pca(tmp,tmpC,Evc,Evl);
    evals = Evl;
    evecs = C * Evc;
  }  //void em_pca

  void rankapprox(const Matrix& Mat, Matrix& cols, Matrix& rows, int dim)
  { 
    Matrix Corr, Evecs, tmpWM, tmpDWM, tmp;
    RowVector Evals;
    std_pca(Mat.t(), Corr, Evecs, Evals);
    calc_white(Corr, dim, tmpWM, tmpDWM);
    tmp = tmpWM * Mat.t();
    cols = tmp.t();
    rows << tmpDWM;
  } // rankapprox

  void krfact(const Matrix& Mat, Matrix& cols, Matrix& rows)
  {
    Matrix out;
    for(int ctr1 = 1; ctr1 <= Mat.Ncols(); ctr1++)
      {
	Matrix tmpVals(cols.Nrows(),rows.Nrows());
	for(int ctr2 = 1; ctr2 <= rows.Nrows(); ctr2++)
	  tmpVals.Column(ctr2) << Mat.SubMatrix(cols.Nrows() * (ctr2 - 1) + 1,cols.Nrows()*ctr2 ,ctr1,ctr1);
	
	Matrix tmpcols, tmprows;
	rankapprox(tmpVals, tmpcols, tmprows);
	cols.Column(ctr1) = tmpcols;
	rows.Column(ctr1) = tmprows;
      }
  } // krfact

  void krfact(const Matrix& Mat, int colnum, Matrix& cols, Matrix& rows)
  {
    cols = zeros(colnum,Mat.Ncols());
    rows = zeros(int(Mat.Nrows() / colnum),Mat.Ncols());
    krfact(Mat,cols,rows);
  } // krfact

  Matrix krprod(const Matrix& cols, const Matrix& rows)
  {
    Matrix out;
    out = zeros(cols.Nrows()*rows.Nrows(),cols.Ncols());
    for(int ctr1 = 1; ctr1 <= cols.Ncols(); ctr1++)
      for(int ctr2 = 1; ctr2 <= rows.Nrows(); ctr2++)
	{
	  out.SubMatrix(cols.Nrows()*(ctr2-1)+1,cols.Nrows()*ctr2,ctr1,ctr1) << cols.Column(ctr1) * rows(ctr2,ctr1);
	}
    return out;
  } // krprod

  Matrix krapprox(const Matrix& Mat, int size_cols, int dim)
  {
    Matrix out, cols, rows;
    out = zeros(Mat.Nrows(), Mat.Ncols());
    cols = zeros(size_cols,Mat.Ncols());
    rows = zeros(int(Mat.Nrows() / size_cols), Mat.Ncols());
    krfact(Mat,cols,rows);
    out = krprod(cols, rows);
    return out;
  } // krapprox

  void adj_eigspec(const RowVector& in, RowVector& out1, RowVector& out2, RowVector& out3, int& out4, int num_vox, float resels)
  {
    RowVector AdjEV;
    AdjEV << in.AsRow();
    AdjEV = AdjEV.Columns(3,AdjEV.Ncols());
    AdjEV = AdjEV.Reverse();

    RowVector CircleLaw;
    int NumVox = (int) floor(num_vox/(2.5*resels));

    CircleLaw = Feta(int(AdjEV.Ncols()), NumVox);

    for(int ctr=1;ctr<=CircleLaw.Ncols(); ctr++){
      if(CircleLaw(ctr)<5*10e-10){CircleLaw(ctr) = 5*10e-10;}
    } 

    /*    float slope;
    slope = CircleLaw.Columns(int(AdjEV.Ncols()/4),AdjEV.Ncols() - 
			      int(AdjEV.Ncols()/4)).Sum() /  
      AdjEV.Columns(int(AdjEV.Ncols()/4),AdjEV.Ncols() - 
      int(AdjEV.Ncols()/4)).Sum();*/

    RowVector PercEV(AdjEV);
    PercEV = cumsum(AdjEV / sum(AdjEV,2).AsScalar());

    AdjEV << SP(AdjEV,pow(CircleLaw.Columns(1,AdjEV.Ncols()),-1));

    SortDescending(AdjEV);
    int maxEV = 1;
    float threshold = 0.98;
    for(int ctr_i = 1; ctr_i < PercEV.Ncols(); ctr_i++){ 
      if((PercEV(ctr_i)<threshold)&&(PercEV(ctr_i+1)>=threshold)){maxEV=ctr_i;}
    }

    if(maxEV<3){maxEV=PercEV.Ncols()/2;}
    RowVector NewEV;
    Matrix temp1;
    temp1 = abs(AdjEV);
    NewEV << temp1.SubMatrix(1,1,1,maxEV);

    AdjEV = (AdjEV - min(AdjEV).AsScalar())/(max(AdjEV).AsScalar() - min(AdjEV).AsScalar());

    out1 = AdjEV;
    out2 = PercEV;
    out3 = NewEV;
    out4 = maxEV;
  }  //adj_eigspec

 void adj_eigspec(const RowVector& in, RowVector& out1, RowVector& out2)
  {
    RowVector AdjEV, PercEV;
    AdjEV = in.Reverse();
    SortDescending(AdjEV);
  
    PercEV = cumsum(AdjEV / sum(AdjEV,2).AsScalar());
    AdjEV = (AdjEV - min(AdjEV).AsScalar())/(max(AdjEV).AsScalar() - min(AdjEV).AsScalar());
    out1 = AdjEV;
    out2 = PercEV;
  }  //adj_eigspec

  RowVector Feta(int n1, int n2)
  {
    float nu = (float) n1/n2; 
    float bm = pow((1-sqrt(nu)),2.0);
    float bp = pow((1+sqrt(nu)),2.0);

    float lrange = 0.9*bm;
    float urange = 1.1*bp;

    RowVector eta(30*n1);
    float rangestepsize = (urange - lrange) / eta.Ncols(); 
    for(int ctr_i = 0; ctr_i < eta.Ncols(); ctr_i++){ 
      eta(ctr_i+1) = lrange + rangestepsize * (ctr_i);
    }

    RowVector teta(10*n1);
    teta = 0;
    float stepsize = (bp - bm) / teta.Ncols();
    for(int ctr_i = 0; ctr_i < teta.Ncols(); ctr_i++){ 
      teta(ctr_i+1) = stepsize*(ctr_i);
    }  
    
    RowVector feta(teta);
    feta = SP(pow(2*M_PI*nu*(teta + bm),-1), pow(SP(teta, bp-bm-teta),0.5));
   
    teta = teta + bm;

    RowVector claw(eta);
    claw = 0;
    for(int ctr_i = 1; ctr_i <= eta.Ncols(); ctr_i++){
      double tmpval = 0.0;
      for(int ctr_j = 1; ctr_j <= teta.Ncols(); ctr_j++){
	if(( double(teta(ctr_j))/double(eta(ctr_i)) )<1)
	  tmpval += feta(ctr_j);
      }
      claw(ctr_i) = n1*(1-stepsize*tmpval);
    }
    
    RowVector Res(n1); //invert the CDF
    for(int ctr_i = 1; ctr_i < eta.Ncols(); ctr_i++){
      if(floor(claw(ctr_i))>floor(claw(ctr_i+1))){
	Res(int(floor(claw(ctr_i)))) = eta(ctr_i);
      }
    }
 
    return Res;
  }  //RowVector Feta

  RowVector cumsum(const RowVector& Inp)
  {
    UpperTriangularMatrix UT(Inp.Ncols());
    UT=1.0;
    RowVector Res;
    Res = Inp * UT;
    return Res;
  }  //RowVector cumsum

  int ppca_dim(const Matrix& in, const Matrix& weights, ColumnVector& PPCA, RowVector& AdjEV, RowVector& PercEV, Matrix& Corr, Matrix& tmpE, RowVector &tmpD, float resels, string which)
  {   
    std_pca(in,weights,Corr,tmpE,tmpD);

    int maxEV = 1;
    RowVector NewEV;
    adj_eigspec(tmpD.AsRow(),AdjEV,PercEV,NewEV,maxEV,in.Ncols(),resels);
    
    int res;
    PPCA = ppca_select(ppca_est(NewEV, in.Ncols(),resels), res, maxEV, which);
    return res;
  }  //int ppca_dim

  int ppca_dim(const Matrix& in, const Matrix& weights, ColumnVector& PPCA, RowVector& AdjEV, RowVector& PercEV, float resels, string which)
  {   
    RowVector tmpD;
    Matrix tmpE;
    Matrix Corr;

    int res = ppca_dim(in, weights, PPCA, AdjEV, PercEV, Corr, tmpE, tmpD, resels, which);
    return res;
  }  //int ppca_dim


  int ppca_dim(const Matrix& in, const Matrix& weights, float resels, string which)
  {
    ColumnVector PPCA;
    RowVector AdjEV, PercEV;
    int res = ppca_dim(in,weights,PPCA,AdjEV,PercEV,resels,which);
    return res;
  }  //int ppca_dim

  ColumnVector ppca_select(const Matrix& PPCAest, int& dim, int maxEV, string which)
  {
    RowVector estimators(5);
    estimators = 1.0;
    
    Matrix PPCA2(PPCAest);
    for(int ctr=1; ctr<=PPCA2.Ncols(); ctr++){
      PPCA2.Column(ctr) = (PPCA2.Column(ctr) - 
			   min(PPCA2.Column(ctr)).AsScalar()) / 
	( max(PPCA2.Column(ctr)).AsScalar() - 
	  min(PPCA2.Column(ctr)).AsScalar());
    }
    
    int ctr_i = 1;
    while((ctr_i< PPCAest.Nrows()-1)&&
	  (PPCAest(ctr_i,2) < PPCAest(ctr_i+1,2))&&(ctr_i<maxEV))
      {estimators(1)=ctr_i+1;ctr_i++;}
    ctr_i = 1;
    while((ctr_i< PPCAest.Nrows()-1)&&
	  (PPCAest(ctr_i,3) < PPCAest(ctr_i+1,3))&&(ctr_i<maxEV))
      {estimators(2)=ctr_i+1;ctr_i++;}
    ctr_i = 1;
    while((ctr_i< PPCAest.Nrows()-1)&&
	  (PPCAest(ctr_i,4) < PPCAest(ctr_i+1,4))&&(ctr_i<maxEV))
      {estimators(3)=ctr_i+1;ctr_i++;}
    ctr_i = 1;
    while((ctr_i< PPCAest.Nrows()-1)&&
	  (PPCAest(ctr_i,5) < PPCAest(ctr_i+1,5))&&(ctr_i<maxEV))
      {estimators(4)=ctr_i+1;ctr_i++;}
    ctr_i = 1;
    while((ctr_i< PPCAest.Nrows()-1)&&
	  (PPCAest(ctr_i,6) < PPCAest(ctr_i+1,6))&&(ctr_i<maxEV))
      {estimators(5)=ctr_i+1;ctr_i++;}

    int res = 0;
    ColumnVector PPCA;
 
    if(which == string("lap")){
      res = int(estimators(1));
      PPCA << PPCA2.Column(2);
    }

    if(which == string("bic")){
      res = int(estimators(2));
      PPCA << PPCA2.Column(2);
    }
    if(which == string("mdl")){
      res = int(estimators(3));
      PPCA << PPCA2.Column(4);
    }
    if(which == string("aic")){
      res = int(estimators(5));
      PPCA << PPCA2.Column(6);
    }
    if(res==0){//median estimator
      PPCA = PPCA2.Column(2);

      for(int ctr=1; ctr<=PPCA2.Nrows(); ctr++){ 
	RowVector tmp = PPCA2.SubMatrix(ctr,ctr,2,6);
	PPCA(ctr) = float(tmp.Sum()/5);
      }

      ctr_i = 1; 
      while((PPCA(ctr_i) < PPCA(ctr_i+1))&&(ctr_i<maxEV)){
	res=ctr_i+1;ctr_i++;
      }
    }

    dim = res;
    return PPCA;
  }  //RowVector ppca_select

  Matrix ppca_est(const RowVector& eigenvalues, const int N1, const float N2)
  { 
    Matrix Res;
    Res = ppca_est(eigenvalues, (int) floor(N1/(2.5*N2)));
    return Res;
  }  //Matrix ppca_est

  Matrix ppca_est(const RowVector& eigenvalues, const int N)
  {
    RowVector logLambda(eigenvalues);
    logLambda = log(eigenvalues);

    int d = logLambda.Ncols();

    RowVector k(d);
    for(int ctr_i = 1; ctr_i <=d; ctr_i++){
      k(ctr_i)=ctr_i;
    }
   
    RowVector m(d);
    m=d*k-0.5*SP(k,k+1); 

    RowVector loggam(d);
    loggam = 0.5*k.Reverse();
    for(int ctr_i = 1; ctr_i <=d; ctr_i++){
      loggam(ctr_i)=lgam(loggam(ctr_i));
    }
    loggam = cumsum(loggam); 

    RowVector l_probU(d);
    l_probU = -log(2)*k + loggam - cumsum(0.5*log(M_PI)*k.Reverse());

    RowVector tmp1;
    tmp1 = -cumsum(eigenvalues).Reverse()+sum(eigenvalues,2).AsScalar();
    tmp1(1) = 0.95*tmp1(2);
    tmp1=tmp1.Reverse();

    RowVector tmp2;
    tmp2 = -cumsum(logLambda).Reverse()+sum(logLambda,2).AsScalar();
    tmp2(1)=tmp2(2);
    tmp2=tmp2.Reverse();

    RowVector tmp3;
    tmp3 = d-k;
    tmp3(d) = 1.0;

    RowVector tmp4;
    tmp4 = SP(tmp1,pow(tmp3,-1));    
    for(int ctr_i = 1; ctr_i <=d; ctr_i++){
      if(tmp4(ctr_i)<0.01){tmp4(ctr_i)=0.01;}
      if(tmp3(ctr_i)<0.01){tmp3(ctr_i)=0.01;}
      if(tmp1(ctr_i)<0.01){tmp1(ctr_i)=0.01;}
    }

    RowVector l_nu;
    l_nu = SP(-N/2*(d-k),log(tmp4));
    l_nu(d) = 0;

    RowVector l_lam;
    l_lam = -(N/2)*cumsum(logLambda);

    RowVector l_lhood;
    l_lhood = SP(pow(tmp3,-1),tmp2)-log(SP(pow(tmp3,-1),tmp1));

    Matrix t1,t2, t3;
    UpperTriangularMatrix triu(d);
    triu = 1.0;
    for(int ctr_i = 1; ctr_i <= triu.Ncols(); ctr_i++){
      triu(ctr_i,ctr_i)=0.0;
    }
    t1 = (ones(d,1) * eigenvalues);
    t1 = SP(triu,t1.t() - t1);
    t2 = pow(tmp4,-1).t()*ones(1,d);
    t3 = ones(d,1)*pow(eigenvalues,-1);
    t2 = SP(triu, t2.t()-t3.t());
    for(int ctr_i = 1; ctr_i <= t1.Ncols(); ctr_i++){
      for(int ctr_j = 1; ctr_j <= t1.Nrows(); ctr_j++){
	if(t1(ctr_j,ctr_i)<=0){t1(ctr_j,ctr_i)=1;}
      } 
    }
    for(int ctr_i = 1; ctr_i <= t2.Ncols(); ctr_i++){
      for(int ctr_j = 1; ctr_j <= t2.Nrows(); ctr_j++){
	if(t2(ctr_j,ctr_i)<=0){t2(ctr_j,ctr_i)=1;}
      }
    } 
    t1 = cumsum(sum(log(t1),2).AsRow());
    t2 = cumsum(sum(log(t2),2).AsRow());

    RowVector l_Az(d);
    l_Az << (t1+t2);

    RowVector l_lap;
    l_lap = l_probU + l_nu +l_Az + l_lam + 0.5*log(2*M_PI)*(m+k)-0.5*log(N)*k;
 
    RowVector l_BIC;
    l_BIC = l_lam + l_nu - 0.5*log(N)*(m+k);

    RowVector l_RRN;
    l_RRN = -0.5*N*SP(k,log(SP(cumsum(eigenvalues),pow(k,-1))))+l_nu;

    RowVector l_AIC;
    l_AIC = -2*N*SP(tmp3,l_lhood)+ 2*(1+d*k+0.5*(k-1));
    l_AIC = -l_AIC;

    RowVector l_MDL;
    l_MDL = -N*SP(tmp3,l_lhood)+ 0.5*(1+d*k+0.5*(k-1))*log(N);
    l_MDL = -l_MDL;

    Matrix Res;

    Res = eigenvalues.t();
    Res |= l_lap.t();
    Res |= l_BIC.t();
    Res |= l_MDL.t();
    Res |= l_RRN.t();
    Res |= l_AIC.t();
    
   
    return Res;
  }  //Matrix ppca_est

  ColumnVector acf(const ColumnVector& in, int order)
  {
    double meanval;
    meanval = mean(in).AsScalar();
    int tpoints = in.Nrows();
    
    ColumnVector y, res;
    Matrix X, tmp;

    y = in.Rows(order+1,tpoints) - meanval;
    X = zeros(order + 1, order);
    for(int ctr1 = 1; ctr1 <= order; ctr1++)
      X.Column(ctr1) = in.Rows(order + 1 - ctr1, tpoints - ctr1) - meanval;
    tmp = X.t()*X;
    tmp = tmp.i();
    tmp = tmp * X.t();
    res << tmp * y;
    return res;
  }  //ColumnVector acf

  ColumnVector pacf(const ColumnVector& in, int maxorder)
  {
    int tpoint = in.Nrows();
    ColumnVector res;
    res = acf(in, maxorder);
    for(int ctr1 = 1; ctr1 <= maxorder; ctr1++)
      if ( res.Column(ctr1).AsScalar() <  (1/tpoint) + 2/(float)std::pow(tpoint,0.5)) 
	res.Column(ctr1) = 0;
    return res;
  }  //ColumnVector pacf
  
  Matrix est_ar(const Matrix& Mat, int maxorder)
  {
    Matrix res;
    res = zeros(maxorder, Mat.Ncols());
    ColumnVector tmp;
    for (int ctr = 1; ctr <= Mat.Ncols(); ctr++){
      tmp = pacf(Mat.Column(ctr));
      res.Column(ctr) = tmp;
    }
    return res;
  }  //Matrix est_ar

  ColumnVector gen_ar(const ColumnVector& in, int maxorder)
  {
    float sdev;
    sdev = stdev(in).AsScalar();
    ColumnVector x, arcoeff, scaled;
    scaled = in / sdev;
    arcoeff = pacf( scaled, maxorder);
    x = normrnd(in.Nrows(),1).AsColumn() * sdev;
    for(int ctr1=2; ctr1 <= in.Nrows(); ctr1++)
      for(int ctr2 = 1; ctr2 <= maxorder; ctr2++)
	x(ctr1) = arcoeff(ctr2) * x(std::max(1,int(ctr1-ctr2))) + x(ctr1);
    return x;
  }  //ColumnVector gen_ar

  Matrix gen_ar(const Matrix& in, int maxorder)
  {
    Matrix res;
    res = in;
    ColumnVector tmp;
    for(int ctr=1; ctr <= in.Ncols(); ctr++){
      tmp = in.Column(ctr);
      res.Column(ctr) = gen_ar(tmp, maxorder);
    } 
    return res;
  }  //Matrix gen_ar

  Matrix gen_arCorr(const Matrix& in, int maxorder)
  {
    Matrix res;
    res = zeros(in.Nrows(), in.Nrows());
    ColumnVector tmp;
    for(int ctr=1; ctr<= in.Ncols(); ctr++){
      tmp = in.Column(ctr);
      tmp = gen_ar(tmp, maxorder);
      res += tmp * tmp.t();
    }
    return res;
  }  //Matrix gen_arCorr

Christian Beckmann's avatar
Christian Beckmann committed
	Matrix basicGLM::olsfit(const Matrix& data, const Matrix& design, 
		const Matrix& contrasts, int DOFadjust)
	{
		beta = zeros(design.Ncols(),1); 
		residu = zeros(1); sigsq = -1.0*ones(1); varcb = -1.0*ones(1); 
		t = zeros(1); z = zeros(1); p=-1.0*ones(1);
		dof = (int)-1; cbeta = -1.0*ones(1); 

		if(data.Nrows()==design.Nrows()){
			Matrix dat = remmean(data,1);
			Matrix pinvdes = pinv(design);
			
			beta = pinvdes * dat;
			residu = dat - design*beta;
			Matrix R = Identity(design.Nrows()) - design * pinvdes;
			Matrix R2 = R*R;
			float tR = R.Trace();
			sigsq = sum(SP(residu,residu))/tR;
			
			dof = (int)(tR*tR/R2.Trace() - DOFadjust);
			
			float fact = float(design.Nrows() - 1 - design.Ncols()) / design.Ncols();
			f_fmf = SP(var(design*beta),pow(var(residu),-1))* fact;
			pf_fmf = f_fmf.Row(1); pf_fmf &= pf_fmf;
			for(int ctr1=1;ctr1<=f_fmf.Ncols();ctr1++)
				pf_fmf(1,ctr1) = 1.0-MISCMATHS::fdtr(design.Ncols(),
				int(design.Nrows() -1 -design.Ncols()),f_fmf.Column(ctr1).AsScalar());
			pf_fmf.Row(2) = pf_fmf.Row(1) * pf_fmf.Ncols();
				
			if(contrasts.Storage()>0 && contrasts.Nrows()==beta.Ncols()){
				cbeta = contrasts.t()*beta;
				Matrix tmp = contrasts.t()*pinvdes*pinvdes.t()*contrasts;
				varcb = diag(tmp)*sigsq;
				t = SP(cbeta,pow(varcb,-0.5));
				z = t; p=t; 
				for(int ctr1=1;ctr1<=t.Ncols();ctr1++){
					ColumnVector tmp = t.Column(ctr1);
					T2z::ComputeZStats(varcb.Column(ctr1),cbeta.Column(ctr1),dof, tmp);
					z.Column(ctr1) << tmp;
					T2z::ComputePs(varcb.Column(ctr1),cbeta.Column(ctr1),dof, tmp);
					p.Column(ctr1) << exp(tmp);
				}
			}
		}	
		return beta;
	}