integrate() gives totally wrong number

后端 未结 4 1188
傲寒
傲寒 2021-01-12 00:02

integrate() gives horribly wrong answer:

integrate(function (x) dnorm(x, -5, 0.07), -Inf, Inf, subdivisions = 10000L)
# 2.127372e-23 with absolute error <         


        
4条回答
  •  说谎
    说谎 (楼主)
    2021-01-12 00:38

    Interesting workaround: not too surprisingly, integrate does well when the values sampled (on (-Inf,Inf), no less) are closer to the "center" of the data. You can reduce this by using your function but hinting at a center:

    Without adjustment:

    t(sapply(-10:10, function(i) integrate(function (x) dnorm(x, i, 0.07), -Inf, Inf, subdivisions = 10000L)))
    #       value        abs.error    subdivisions message call      
    #  [1,] 0            0            1            "OK"    Expression
    #  [2,] 1            4.611403e-05 10           "OK"    Expression
    #  [3,] 6.619713e-19 1.212066e-18 2            "OK"    Expression
    #  [4,] 7.344551e-71 0            2            "OK"    Expression
    #  [5,] 3.389557e-06 6.086176e-06 3            "OK"    Expression
    #  [6,] 2.127372e-23 3.849798e-23 2            "OK"    Expression
    #  [7,] 1            3.483439e-05 8            "OK"    Expression
    #  [8,] 1            6.338078e-07 11           "OK"    Expression
    #  [9,] 1            3.408389e-06 7            "OK"    Expression
    # [10,] 1            6.414833e-07 8            "OK"    Expression
    # [11,] 1            7.578907e-06 3            "OK"    Expression
    # [12,] 1            6.414833e-07 8            "OK"    Expression
    # [13,] 1            3.408389e-06 7            "OK"    Expression
    # [14,] 1            6.338078e-07 11           "OK"    Expression
    # [15,] 1            3.483439e-05 8            "OK"    Expression
    # [16,] 2.127372e-23 3.849798e-23 2            "OK"    Expression
    # [17,] 3.389557e-06 6.086176e-06 3            "OK"    Expression
    # [18,] 7.344551e-71 0            2            "OK"    Expression
    # [19,] 6.619713e-19 1.212066e-18 2            "OK"    Expression
    # [20,] 1            4.611403e-05 10           "OK"    Expression
    # [21,] 0            0            1            "OK"    Expression
    

    If we add a "centering" hint, though, we get more consistent results:

    t(sapply(-10:10, function(i) integrate(function (x, offset) dnorm(x + offset, i, 0.07), -Inf, Inf, subdivisions = 10000L, offset = i)))
    #       value abs.error    subdivisions message call      
    #  [1,] 1     7.578907e-06 3            "OK"    Expression
    #  [2,] 1     7.578907e-06 3            "OK"    Expression
    #  [3,] 1     7.578907e-06 3            "OK"    Expression
    #  [4,] 1     7.578907e-06 3            "OK"    Expression
    #  [5,] 1     7.578907e-06 3            "OK"    Expression
    #  [6,] 1     7.578907e-06 3            "OK"    Expression
    #  [7,] 1     7.578907e-06 3            "OK"    Expression
    #  [8,] 1     7.578907e-06 3            "OK"    Expression
    #  [9,] 1     7.578907e-06 3            "OK"    Expression
    # [10,] 1     7.578907e-06 3            "OK"    Expression
    # [11,] 1     7.578907e-06 3            "OK"    Expression
    # [12,] 1     7.578907e-06 3            "OK"    Expression
    # [13,] 1     7.578907e-06 3            "OK"    Expression
    # [14,] 1     7.578907e-06 3            "OK"    Expression
    # [15,] 1     7.578907e-06 3            "OK"    Expression
    # [16,] 1     7.578907e-06 3            "OK"    Expression
    # [17,] 1     7.578907e-06 3            "OK"    Expression
    # [18,] 1     7.578907e-06 3            "OK"    Expression
    # [19,] 1     7.578907e-06 3            "OK"    Expression
    # [20,] 1     7.578907e-06 3            "OK"    Expression
    # [21,] 1     7.578907e-06 3            "OK"    Expression
    

    I recognize this is mitigation for heuristics, presumes knowing something about your distribution before integration, and is not a perfect "generic" solution. Just offering another perspective.

提交回复
热议问题