//Saddlepoint approximation of the ratio of two hypergeometric functions, with matrix arguments L and B (3x3). Vectors xL & xB contain the eigenvalues of L and B.
//Used for the ball & Binghams model, B has two non-zero eigenvalues that represent fanning indices.
c=0;//Signify that the approximation has failed. c should be >=0. How to treat these voxels???
if(isinf(c))//In this case again the approximation has failed
c=1e10;
returnc;
}
//Saddlepoint approximation of the ratio of two hypergeometric functions, one with matrix argument L and another with scalar argument k. Vector xL contains the eigenvalues of L.
//Used for the ball & Watsons model, L has up to two non-zero eigenvalues and k!=0 is the fanning index.
SortDescending(xL);//Not needed, performed by eigen-decomposition?
t1=find_t(xL);
R=1;K2=0;K3=0;K4=0;
for(inti=1;i<=3;i++){
tmp=xL(i)+t1;
tmp2=tmp*tmp;tmp3=tmp2*tmp;
R=-R*tmp;
K2=K2+0.5/tmp2;
K3=K3-1.0/tmp3;
K4=K4+3.0/(tmp3*tmp);
}
T1=K4/(8.0*K2*K2)-5.0*K3*K3/(24.0*K2*K2*K2);
Norm1=0.125/(K2*R);
}
//Approximate Denominator
R=sqrt(4*k*k+9-4*k);
Rf=R+3+2*k;
t2=-0.25*Rf;
tmp=k+t2;tmp2=tmp*tmp;tmp3=tmp2*tmp;
Bm=1.0/Rf;
K2=0.5/tmp2+1.0/(t2*t2);
K3=-1.0/tmp3-2.0/(t2*t2*t2);
K4=3.0/(tmp3*tmp)+6.0/(t2*t2*t2*t2);
T2=K4/(8.0*K2*K2)-5.0*K3*K3/(24.0*K2*K2*K2);
Norm2=Bm/R;
//Final Ratio
c=sqrt(Norm1/Norm2)*exp(-t1+T1+t2-T2);
returnc;
}
//Saddlepoint aproximation of the ratio ot two hypergeometric functions, one with matrix arguments L and the other with scalar argument k>0 in two steps:
//First denominator, then numerator. This allows them to be updated independently, used for the ball & Watsons model to compute the likelihood faster.
//This function returns values used in the denominator approximation.
ReturnMatrixapprox_denominatorW(constdoublek){
floatt2,R,Bm,Rf,K2,K3,K4,tmp,tmp2,tmp3,T2,Norm2;
ColumnVectorRes(3);
R=sqrt(4*k*k+9-4*k);
Rf=R+3+2*k;
t2=-0.25*Rf;
tmp=k+t2;tmp2=tmp*tmp;tmp3=tmp2*tmp;
Bm=1.0/Rf;
K2=0.5/tmp2+1.0/(t2*t2);
K3=-1.0/tmp3-2.0/(t2*t2*t2);
K4=3.0/(tmp3*tmp)+6.0/(t2*t2*t2*t2);
T2=K4/(8.0*K2*K2)-5.0*K3*K3/(24.0*K2*K2*K2);
Norm2=Bm/R;
Res<<t2<<T2<<Norm2;
Res.Release();
returnRes;
}
//Second step for saddlepoint approximation of the ratio of two hypergeometric functions, with matrix argument L and scalar argument k (xL has the eigenvalues of L).
//Assume that the denominator has already been approximated by the function above and the parameters are stored in denomvals.
//Here approximate the numerator and return the total ratio approximation.
Bingham and Watson Distributions and functions to approximate their normalizing constant
Stam Sotiropoulos - FMRIB Image Analysis Group
Copyright (C) 2011 University of Oxford */
/* CCOPYRIGHT */
#if !defined (Bingham_Watson_approx_h)
#define Bingham_Watson_approx_h
#include<iostream>
#include<fstream>
#include<iomanip>
#define WANT_STREAM
#define WANT_MATH
#include<string>
#include"miscmaths/miscmaths.h"
#include"miscmaths/nonlin.h"
#include"stdlib.h"
usingnamespaceNEWMAT;
usingnamespaceMISCMATHS;
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define INV3 0.333333333333333 // 1/3
#define SQRT3 1.732050807568877 //sqrt(3)
#define INV54 0.018518518518519 // 1/54
#define min3(a,b,c) (a < b ? min(a,c) : min(b,c) )
//Saddlepoint approximation of confluent hypergeometric function of a matrix argument B
//Vector x has the eigenvalues of B.
floathyp_Sapprox(ColumnVector&x);
//Saddlepoint approximation of confluent hypergeometric function of a matrix argument, with its eigenvalues being l1,l1,l2 or l1,l2,l2 with l1!=l2.
//Vector x has the three eigenvalues. This function can be also used to approximate a confluent hypergeometric function of a scalar argument k
//by providing x=[k 0 0].
floathyp_Sapprox_twoequal(ColumnVector&x);
//Saddlepoint approximation of the ratio of two hypergeometric functions, with matrix arguments L and B (3x3). Vectors xL & xB contain the eigenvalues of L and B.
//Saddlepoint approximation of the ratio of two hypergeometric functions, one with matrix argument L and another with scalar argument k. Vector xL contains the eigenvalues of L.
//Used for the ball & Watsons model.
floathyp_SratioW(ColumnVector&xL,constdoublek);
//Saddlepoint aproximation of the ratio ot two hypergeometric functions, one with matrix arguments L and the other with scalar argument k in two steps:
//First denominator, then numerator. This allows them to be updated independently, used for the ball & Watsons model to compute the likelihood faster.
//This function returns values used in the denominator approximation.
ReturnMatrixapprox_denominatorW(constdoublek);
//Second step for saddlepoint approximation of the ratio of two hypergeometric functions, with matrix argument L and scalar argument k (xL has the eigenvalues of L).
//Assume that the denominator has already been approximated by the function above and the parameters are stored in denomvals.
//Here approximate the numerator and return the total ratio approximation.