Problem with BesselK function

0 downloads 0 Views 736KB Size Report
Attaching package: 'GeneralizedHyperbolic'. The following objects are masked from 'package:ghyp': dghyp, dgig, pghyp, pgig, qghyp, qgig, qqghyp, rghyp, rgig.
Problem with BesselK function The modified Bessel function of the second kind Kn(z) in Mathematica have a small error from its asymptotic expansion calculated number for large z, in the scale of 1.0e-16 relative error. But this small error bring about a completely wrong results for Hyperbolic Distribution Kurtosis, or the central moment of the 4th order, give a negative number that is impossible from the definition. Since the asymptotic series expansion converged to a positive number when take 3 or 4 terms, we may believe that the function Kn(z) output is wrong:

Matlab 2014b cannot calculate to a nonzero number:

>> format longG >> besselk(3,2.0645855712472382e+8) ans = 0 >> besselk(3.0,2.0645855712472382e+8) ans = 0 >> besselk(3.0,2.0645855712472382e+4) ans = 0 >> besselk(3.0,2.0645855712472382e+1) ans = 3.66460995946465e-10

R also give negative Kurtosis: > require(Bessel) Loading required package: Bessel Loading required package: Rmpfr Loading required package: gmp Attaching package: ‘gmp’ The following objects are masked from ‘package:base’: %*%, apply, crossprod, matrix, tcrossprod C code of R package 'Rmpfr': GMP using 64 bits per limb Attaching package: ‘Rmpfr’ The following objects are masked from ‘package:stats’: dbinom, dnorm, dpois, pnorm The following objects are masked from ‘package:base’: cbind, pmax, pmin, rbind Warning messages: 1: package ‘Bessel’ was built under R version 3.3.3 2: package ‘Rmpfr’ was built under R version 3.3.3 3: package ‘gmp’ was built under R version 3.3.3 > options(digits=22) > BesselK(2.0645855712472382e+8,3.0) [1] 0 Warning message: In BesselK(206458557.124724, 3) : 'zbesk(2.06459e+08 + 0i, nu=3)' large arguments -> precision loss (of at least half machine accuracy) > require(GeneralizedHyperbolic) Loading required package: GeneralizedHyperbolic Loading required package: DistributionUtils Loading required package: RUnit Attaching package: ‘GeneralizedHyperbolic’ The following objects are masked from ‘package:ghyp’: dghyp, dgig, pghyp, pgig, qghyp, qgig, qqghyp, rghyp, rgig Warning messages: 1: package ‘GeneralizedHyperbolic’ was built under R version 3.3.3 2: package ‘DistributionUtils’ was built under R version 3.3.3 3: package ‘RUnit’ was built under R version 3.3.3 > ghypKurt(mu = 0, delta = 1, alpha = 5.858607053496991e+08, beta = 5.482769649208547e+08, lambda = 1) [1] -12.342003342145707 > ghypKurt(mu = 0, delta = 1, alpha = 5.858607053496991e+08, beta = 5.482769649208547e+08, lambda = 0) [1] -59.052022605760158

SymPy seems is better than Mathematica: F:\work\Frank\tasks\RMSELTsimulationtoolandmasterinfo>ipython Python 2.7.11 |Anaconda 2.1.0 (64-bit)| (default, Feb 16 2016, 09:58:36) [MSC v.1500 64 bit (AMD64)] Type "copyright", "credits" or "license" for more information. IPython 4.2.0 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]: from sympy import * In [2]: mpmath.mp.dps = 44 In [3]: besselk(3.0,2.0645855712472382e+8) Out[3]: 6.91303332162685e-89663817 In [4]: besselk(3.0,2.06458557124723820000000000000000000000000e+8) Out[4]: 6.91303332162685e-89663817 In [5]: mp.dps = 44 --------------------------------------------------------------------------NameError Traceback (most recent call last) in () ----> 1 mp.dps = 44 NameError: name 'mp' is not defined In [6]: from mpmath import * --------------------------------------------------------------------------ImportError Traceback (most recent call last) in () ----> 1 from mpmath import * ImportError: No module named mpmath In [7]: In [7]: import sympy.mpmath as mpmath In [8]: from mpmath import * --------------------------------------------------------------------------ImportError Traceback (most recent call last) in () ----> 1 from mpmath import * ImportError: No module named mpmath In [9]: from sympy.mpmath import * In [10]: mp.dps = 44 In [11]: besselk(3.0,2.0645855712472382e+8) Out[11]: mpf('6.9130333216268486296841113758195263986018867734e-89663817')