Speeding up the calculation of a binomial sum

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












Is it possible to speed up this calculation?



A[j_, p_, n_] := Sum[Binomial[n, k] p^k (1 - p)^(n - k), k, 0, j]
Plot[A[j, 0.5, 12000], j, 0, 12000]


Thanks!










share|improve this question

























    up vote
    2
    down vote

    favorite












    Is it possible to speed up this calculation?



    A[j_, p_, n_] := Sum[Binomial[n, k] p^k (1 - p)^(n - k), k, 0, j]
    Plot[A[j, 0.5, 12000], j, 0, 12000]


    Thanks!










    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Is it possible to speed up this calculation?



      A[j_, p_, n_] := Sum[Binomial[n, k] p^k (1 - p)^(n - k), k, 0, j]
      Plot[A[j, 0.5, 12000], j, 0, 12000]


      Thanks!










      share|improve this question













      Is it possible to speed up this calculation?



      A[j_, p_, n_] := Sum[Binomial[n, k] p^k (1 - p)^(n - k), k, 0, j]
      Plot[A[j, 0.5, 12000], j, 0, 12000]


      Thanks!







      performance-tuning probability-or-statistics summation






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      GambitSquared

      1,010518




      1,010518




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          3
          down vote













          The expression inside the Sum is actually built into Mathematica as BernsteinBasis, which gets primarily used in the construction of Bézier curves. Applied to this situation, we have the formula



          A[j_, p_, n_] := Sum[BernsteinBasis[n, k, p], k, 0, j, Method -> "Procedural"]


          which ought to be very stable to evaluate. If needed, however, one can also implement the de Casteljau formula so that the Bernstein polynomials are recursively generated instead.



          (back to gedanken Mathematica, so untested)






          share|improve this answer



























            up vote
            2
            down vote













            The summation can be evaluated to a closed form, which can speed up the further usage:



            Sum[Binomial[n, k] p^k (1 - p)^(n - k), k, 0, j] // FullSimplify



            (1 - p)^n ((1/(1 - p))^n - (1 - p)^(-1 - j) p^(1 + j)
            Binomial[n, 1 + j] Hypergeometric2F1[1, 1 + j - n, 2 + j,
            p/(-1 + p)])






            share|improve this answer



























              up vote
              2
              down vote













              Mathematica complains due to the occurence of very small numbers that cannot be represented in machine precision. However, the built-in cummulative distribution function (CDF) of the BinomialDistribution is sufficiently robust against that (replacing very small numbers simply by 0.).



              A[j_, p_, n_] := CDF[BinomialDistribution[n, p], j]
              nn = 120;
              Plot[A[j, 0.5, nn], j, 0, nn]


              enter image description here



              nn = 12000;
              Plot[A[j, 0.5, nn], j, 0, nn]


              enter image description here






              share|improve this answer




















                Your Answer




                StackExchange.ifUsing("editor", function ()
                return StackExchange.using("mathjaxEditing", function ()
                StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
                StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
                );
                );
                , "mathjax-editing");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "387"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                convertImagesToLinks: false,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184002%2fspeeding-up-the-calculation-of-a-binomial-sum%23new-answer', 'question_page');

                );

                Post as a guest






























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                3
                down vote













                The expression inside the Sum is actually built into Mathematica as BernsteinBasis, which gets primarily used in the construction of Bézier curves. Applied to this situation, we have the formula



                A[j_, p_, n_] := Sum[BernsteinBasis[n, k, p], k, 0, j, Method -> "Procedural"]


                which ought to be very stable to evaluate. If needed, however, one can also implement the de Casteljau formula so that the Bernstein polynomials are recursively generated instead.



                (back to gedanken Mathematica, so untested)






                share|improve this answer
























                  up vote
                  3
                  down vote













                  The expression inside the Sum is actually built into Mathematica as BernsteinBasis, which gets primarily used in the construction of Bézier curves. Applied to this situation, we have the formula



                  A[j_, p_, n_] := Sum[BernsteinBasis[n, k, p], k, 0, j, Method -> "Procedural"]


                  which ought to be very stable to evaluate. If needed, however, one can also implement the de Casteljau formula so that the Bernstein polynomials are recursively generated instead.



                  (back to gedanken Mathematica, so untested)






                  share|improve this answer






















                    up vote
                    3
                    down vote










                    up vote
                    3
                    down vote









                    The expression inside the Sum is actually built into Mathematica as BernsteinBasis, which gets primarily used in the construction of Bézier curves. Applied to this situation, we have the formula



                    A[j_, p_, n_] := Sum[BernsteinBasis[n, k, p], k, 0, j, Method -> "Procedural"]


                    which ought to be very stable to evaluate. If needed, however, one can also implement the de Casteljau formula so that the Bernstein polynomials are recursively generated instead.



                    (back to gedanken Mathematica, so untested)






                    share|improve this answer












                    The expression inside the Sum is actually built into Mathematica as BernsteinBasis, which gets primarily used in the construction of Bézier curves. Applied to this situation, we have the formula



                    A[j_, p_, n_] := Sum[BernsteinBasis[n, k, p], k, 0, j, Method -> "Procedural"]


                    which ought to be very stable to evaluate. If needed, however, one can also implement the de Casteljau formula so that the Bernstein polynomials are recursively generated instead.



                    (back to gedanken Mathematica, so untested)







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 1 hour ago









                    J. M. is computer-less♦

                    94.8k10294454




                    94.8k10294454




















                        up vote
                        2
                        down vote













                        The summation can be evaluated to a closed form, which can speed up the further usage:



                        Sum[Binomial[n, k] p^k (1 - p)^(n - k), k, 0, j] // FullSimplify



                        (1 - p)^n ((1/(1 - p))^n - (1 - p)^(-1 - j) p^(1 + j)
                        Binomial[n, 1 + j] Hypergeometric2F1[1, 1 + j - n, 2 + j,
                        p/(-1 + p)])






                        share|improve this answer
























                          up vote
                          2
                          down vote













                          The summation can be evaluated to a closed form, which can speed up the further usage:



                          Sum[Binomial[n, k] p^k (1 - p)^(n - k), k, 0, j] // FullSimplify



                          (1 - p)^n ((1/(1 - p))^n - (1 - p)^(-1 - j) p^(1 + j)
                          Binomial[n, 1 + j] Hypergeometric2F1[1, 1 + j - n, 2 + j,
                          p/(-1 + p)])






                          share|improve this answer






















                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            The summation can be evaluated to a closed form, which can speed up the further usage:



                            Sum[Binomial[n, k] p^k (1 - p)^(n - k), k, 0, j] // FullSimplify



                            (1 - p)^n ((1/(1 - p))^n - (1 - p)^(-1 - j) p^(1 + j)
                            Binomial[n, 1 + j] Hypergeometric2F1[1, 1 + j - n, 2 + j,
                            p/(-1 + p)])






                            share|improve this answer












                            The summation can be evaluated to a closed form, which can speed up the further usage:



                            Sum[Binomial[n, k] p^k (1 - p)^(n - k), k, 0, j] // FullSimplify



                            (1 - p)^n ((1/(1 - p))^n - (1 - p)^(-1 - j) p^(1 + j)
                            Binomial[n, 1 + j] Hypergeometric2F1[1, 1 + j - n, 2 + j,
                            p/(-1 + p)])







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 1 hour ago









                            Αλέξανδρος Ζεγγ

                            2,6741826




                            2,6741826




















                                up vote
                                2
                                down vote













                                Mathematica complains due to the occurence of very small numbers that cannot be represented in machine precision. However, the built-in cummulative distribution function (CDF) of the BinomialDistribution is sufficiently robust against that (replacing very small numbers simply by 0.).



                                A[j_, p_, n_] := CDF[BinomialDistribution[n, p], j]
                                nn = 120;
                                Plot[A[j, 0.5, nn], j, 0, nn]


                                enter image description here



                                nn = 12000;
                                Plot[A[j, 0.5, nn], j, 0, nn]


                                enter image description here






                                share|improve this answer
























                                  up vote
                                  2
                                  down vote













                                  Mathematica complains due to the occurence of very small numbers that cannot be represented in machine precision. However, the built-in cummulative distribution function (CDF) of the BinomialDistribution is sufficiently robust against that (replacing very small numbers simply by 0.).



                                  A[j_, p_, n_] := CDF[BinomialDistribution[n, p], j]
                                  nn = 120;
                                  Plot[A[j, 0.5, nn], j, 0, nn]


                                  enter image description here



                                  nn = 12000;
                                  Plot[A[j, 0.5, nn], j, 0, nn]


                                  enter image description here






                                  share|improve this answer






















                                    up vote
                                    2
                                    down vote










                                    up vote
                                    2
                                    down vote









                                    Mathematica complains due to the occurence of very small numbers that cannot be represented in machine precision. However, the built-in cummulative distribution function (CDF) of the BinomialDistribution is sufficiently robust against that (replacing very small numbers simply by 0.).



                                    A[j_, p_, n_] := CDF[BinomialDistribution[n, p], j]
                                    nn = 120;
                                    Plot[A[j, 0.5, nn], j, 0, nn]


                                    enter image description here



                                    nn = 12000;
                                    Plot[A[j, 0.5, nn], j, 0, nn]


                                    enter image description here






                                    share|improve this answer












                                    Mathematica complains due to the occurence of very small numbers that cannot be represented in machine precision. However, the built-in cummulative distribution function (CDF) of the BinomialDistribution is sufficiently robust against that (replacing very small numbers simply by 0.).



                                    A[j_, p_, n_] := CDF[BinomialDistribution[n, p], j]
                                    nn = 120;
                                    Plot[A[j, 0.5, nn], j, 0, nn]


                                    enter image description here



                                    nn = 12000;
                                    Plot[A[j, 0.5, nn], j, 0, nn]


                                    enter image description here







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered 1 hour ago









                                    Henrik Schumacher

                                    42k260125




                                    42k260125



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184002%2fspeeding-up-the-calculation-of-a-binomial-sum%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Comments

                                        Popular posts from this blog

                                        White Anglo-Saxon Protestant

                                        BuddyTV

                                        Conflict (narrative)