function value = gennch ( df, xnonc ) %*****************************************************************************80 % %% GENNCH generates a noncentral Chi-Square random deviate. % % Discussion: % % This procedure generates a random deviate from the distribution of a % noncentral chisquare with DF degrees of freedom and noncentrality parameter % XNONC. % % It uses the fact that the noncentral chisquare is the sum of a chisquare % deviate with DF-1 degrees of freedom plus the square of a normal % deviate with mean XNONC and standard deviation 1. % % A subtle ambiguity arises in the original formulation: % % value = genchi ( arg1 ) + ( gennor ( arg2, arg3 ) ) ^ 2; % % because the compiler is free to invoke either genchi or gennor % first, both of which alter the random number generator state, % resulting in two distinct possible results. % % Licensing: % % This code is distributed under the GNU LGPL license. % % Modified: % % 01 April 2013 % % Author: % % Original FORTRAN77 version by Barry Brown, James Lovato. % MATLAB version by John Burkardt. % % Parameters: % % Input, real DF, the degrees of freedom. % 1.0 < DF. % % Input, real XNONC, the noncentrality parameter. % 0.0 <= XNONC. % % Output, real VALUE, a random deviate from the distribution. % if ( df <= 1.0 ) fprintf ( 1, ' \n' ); fprintf ( 1, 'GENNCH - Fatal error!\n' ); fprintf ( 1, ' DF <= 1.\n' ); error ( 'GENNCH - Fatal error!\n' ); end if ( xnonc < 0.0 ) fprintf ( 1, ' \n' ); fprintf ( 1, 'GENNCH - Fatal error!\n' ); fprintf ( 1, ' XNONC < 0.0.\n' ); error ( 'GENNCH - Fatal error!\n' ); end arg1 = df - 1.0; arg2 = sqrt ( xnonc ); arg3 = 1.0; t1 = genchi ( arg1 ); t2 = gennor ( arg2, arg3 ); value = t1 + t2 * t2; return end