Java programming example of recursive and non-recursive code with two distributions, two Recursion

Source: Internet
Author: User

Java programming example of recursive and non-recursive code with two distributions, two Recursion

The main content of this article is recursive and non-Recursive Implementation of the binary distribution of Java programming.

Source:

Exercise 27: return (1.1-p) * binomial (N-1, k, p) + p * binomial (N-1, k-1, k-1, p );
How does the recursive formula calculate the number of recursive calls?

Two distributions:

Definition: n Independent discrete probability distributions of the number of successful times k in/non-test. The probability of each successful experiment is p, which is recorded as B (n, p, k ).

Probability formula: P (ε = K) = C (n, k) * p ^ k * (1-p) ^ (n-k)

Where C (n, k) = (n-k )! /(K! * (N-k )!), Recorded as EZ ~ B (n, p), expect: E ε = np, variance: D ε = npq, where q = 1-p.

There is a recursive formula in probability statistics:

This is the source of recursion in the question.

The recursive formula comes from C (n, k) = C (n-1, k) + C (n-1, k-1 ). The actual scenario is to select k from n people. How many combinations are there? Place n people by 1 ~ N is sorted in order. If the k-th individual is not selected, select k from the n-th individual. The k-th individual is selected, you need to select the K-1 from the remaining n-1 individuals.

Recursive Implementation of two item distribution in the book:

Public static double binomial (int N, int k, double p) {COUNT ++; // records the number of recursive calls if (N = 0 & k = 0) {return 1.0;} if (N <0 | k <0) {return 0.0;} return (1.0-p) * binomial (N-1, k, p) + p * binomial (N-1, k-1, p );}

Experiment results:

N k p call times 10 5 0.25 246720 10 0.25 243553830 15 0.25 2440764535

From the results, we can see that the number of calls required for this recursive method is a geometric disaster, and n to 50 is not enough.

Improved Recursive Implementation of two distributions:

Private static long COUNT = 0; private static double [] [] M; private static double binomial (int N, int k, double p) {COUNT ++; if (N = 0 & k = 0) {return 1.0;} if (N <0 | k <0) {return 0.0 ;} if (M [N] [k] =-1) {// Save the calculation result and use the calculated result directly, no need to recursively calculate M [N] [k] = (1.0-p) * binomial (N-1, k, p) + p * binomial (N-1, k-1, p);} return M [N] [k];} public static double Binomial (int N, int k, double p) {M = new double [N + 1] [k + 1]; for (int I = 0; I <= N; I ++) {for (int j = 0; j <= k; j ++) {M [I] [j] =-1 ;}return binomial (N, k, p );}

Experiment results:

N k p call times 10 5 0.25 10120 10 0.25 15 45230 0.25 25 120350 0.25 50 3204100 0.25

The experiment results show that the number of calls is greatly reduced, and the algorithm can be used.

Non-Recursive Implementation of Two-item distribution:

In fact, it is faster to directly calculate the combination number and factorial without recursion.

// Calculate the combination number public static double combination (double N, double k) {double min = k; double max = N-k; double t = 0; double NN = 1; double kk = 1; if (min> max) {t = min; min = max; max = t;} while (N> max) {// you do not need to calculate NN = NN * N; N --;} while (min> 0) {// calculate the factorial kk = kk * min; min --;} return NN/kk ;} // calculate the public static double binomial (int N, int k, double p) {double a = 1; double B = 1; double c = combination (N, k); while (N-k)> 0) {// calculate the (N-k) Power of (1-p) a = a * (1-p); N --;} while (k> 0) {// calculate the k power of p B = B * p; k --;} return c * a * B ;}

Experiment results:

N k p binary distribution value: 10, 5, 0.25 0.05839920043945312520, 10, 0.25 0.00992227527967770650, 25, 0.25 8.44919466990417e-5

Compared with the previous algorithm, the calculation result is correct and the running speed is very fast.

Summary

The above is all about the recursive and non-Recursive Implementation code examples of the two distributions of Java programming, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.