ttestp.m

来自「偏最小二乘算法在MATLAB中的实现」· M 代码 · 共 63 行

M
63
字号
function y = ttestp(x,a,z) 
%TTESTP Evaluates t-distribution and its inverse
% The function will return either the probabilities given 
% the t statistic and the degrees of freedom or the 
% t statistic given the probability and degrees of freedom.  
% 
% The I/O format is: y = ttestp(x,a,z) 
%
% where 
%	x = either the statistic (t) or the probability at which
%	    the function is to be evaluated.
%	a = degrees of freedom
%	z = flag for determining how the function is evaluated
%	  = 1 percentage from t-test, given t statistic and D.F.
%	  = 2 t from inverse t-test given probability and D.F.

%  Based on a public domain stats toolbox
%  Modified by B.M. Wise December 1994
aa = a * 0.5;
if z == 1
  xx = a / (a + x^2);
  bb = 0.50;
  tmp = beta(xx,aa,bb);
  y = tmp * 0.50;
elseif z == 2
  ic = 1;
  xl = 0.0;
  xr = 1.0;
  fxl = -x*2;
  fxr = 1.0 - (x*2);
  if fxl * fxr > 0
    error('probability not in the range(0,1) ')
  else
    while ic < 30
	  xx = (xl + xr) * 0.5;
   	  p1 = beta(xx,aa,0.5);
	  fcs = p1 - (x*2);
	  if fcs * fxl > 0
	    xl = xx;
	    fxl = fcs;
	  else
	    xr = xx;
	    fxr = fcs;
	  end
	  xrmxl = xr - xl;
	  if xrmxl <= 0.0001 | abs(fcs) <= 1E-4
	    break
	  else
	    ic = ic + 1;
   	  end
    end
  end
  if ic == 30
    error(' failed to converge ')
  end
  tmp = xx;
  y = sqrt((a - a * tmp) / tmp);
else
  error('z must be 1 or 2')
  return
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?