简体   繁体   中英

Octave: “'ncx2cdf' undefined” error

我正在尝试从八度运行M文件,但出现此错误:

Octave evaluation error: 'ncx2cdf' undefined     

Apparently the non-central chi-square distribution is simply defined as 1 minus the Marcum Q function . The signal package at octave-forge provides an implementation for this function (seemingly compatible with matlab).

Therefore you could presumably write your own ncx2cdf function simply as follows:

function Out = myncx2cdf (X, V, Delta)
  Out = 1 - marcumq (sqrt (Delta), sqrt (X), V/2);
end

Confirmed in matlab:

>> X = randi(100, [1,20]); V = 4; Delta = 10;
>> ncx2cdf(X, V, Delta)                          
ans =
    1.0000    0.9410    0.9999    1.0000    1.0000    1.0000    1.0000    0.5549    0.6093    0.9410    1.0000    0.9410    1.0000    0.9279    1.0000    0.9920    0.8183    0.9410    1.0000    0.9997
>> 1 - marcumq(sqrt(Delta), sqrt(X), V/2)
ans =
    1.0000    0.9410    0.9999    1.0000    1.0000    1.0000    1.0000    0.5549    0.6093    0.9410    1.0000    0.9410    1.0000    0.9279    1.0000    0.9920    0.8183    0.9410    1.0000    0.9997

Octave session for the same X, V, and Delta:

octave:34> pkg load signal
octave:35> 1 - marcumq(sqrt(Delta), sqrt(X), V/2)
ans =
   1.00000   0.94105   0.99988   1.00000   1.00000   1.00000   0.99996   0.55492   0.60929   0.94105   1.00000   0.94105   1.00000   0.92793   1.00000   0.99203   0.81831   0.94105   1.00000   0.99972

Note that the degrees of freedom parameter V is restricted to even values with this implementation; if you'd like to use odd degrees of freedom too, eg 5, this could be interpolated from the result for V=4 and V=6 (this seems to work well in practice).

here is a easy implementation of non-central chi square distribution:

function f = ncx2pdf(x, n, lambda, term = 32)
  f = exp(-lambda/2) * arrayfun(@(x) sum_expression([0:term],x,n,lambda), x);
  function t = sum_expression(j,v,n,l)
    # j is vector, v is scalar.
    numerator = (l/2).^j .* v.^(n/2+j-1) * exp(-v/2);
    denominator = factorial(j) .* 2.^(n/2+j) .* gamma(n/2+j);
    t = sum(numerator ./ denominator);
  end
end

here is the function file , put it in your octave path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM