Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Distributions{
- static Scanner sc = new Scanner(System.in);
- static long fact(int n){
- long ans = 1;
- for(int i = 2; i <= n; i++)
- ans*=i;
- return ans;
- }
- static long comb(int n, int x){
- return fact(n)/(fact(x)*fact(n-x));
- }
- static double bernoulli(double p, int n, int x){
- return (comb(n, x)*Math.pow(p, x)*Math.pow(1-p, n-x));
- }
- static double poisson(int lamda, int x){
- return Math.exp(-lamda)*Math.pow(lamda, x)/fact(x);
- }
- static double uniform(int a, int b, int x){
- if(x>=a && x<=b)
- return Math.pow(a-b, -1);
- else return 0;
- }
- static double normal(double sigma, double mew, int x){
- return Math.pow(sigma*Math.sqrt(2*Math.PI), -1)*Math.exp(-(Math.pow(x-mew, 2))/(2*Math.pow(sigma, 2)));
- }
- static int takeInt(String prompt){
- System.out.print(prompt+": ");
- return sc.nextInt();
- }
- static double takeDouble(String prompt){
- System.out.print(prompt+": ");
- return sc.nextDouble();
- }
- public static void main(String[] args){
- int lambda, n, a, b; double p, sigma, mew;
- while(true){
- switch(takeInt("1.Discrete 2.Continous 3.exit")){
- case 1:
- switch(takeInt("1.Binomial 2.Poisson")){
- case 1:
- do{p = takeDouble("p");}while(p>1||p<0);
- do{n = takeInt("n");}while(n<1);
- for(int i = 0; i <= n; i++)
- System.out.println("p("+i+") = "+bernoulli(p, n, i));
- break;
- case 2:
- do{lambda = takeInt("lambda");}while(lambda<0);
- do{n = takeInt("n");}while(n<1);
- for(int i = 0; i <= n; i++)
- System.out.println("p("+i+") = "+poisson(lambda, i));
- break;
- default:
- System.out.println("Enter 1 or 2.");
- }
- break;
- case 2:
- switch(takeInt("1.Normal 2.Uniform")){
- case 1:
- do{sigma = takeDouble("sigma");}while(sigma<0);
- do{mew = takeDouble("mew");}while(mew<0);
- do{n = takeInt("n");}while(n<1);
- for(int i = 0; i <=n ; i++)
- System.out.println("p("+i+") = "+normal(sigma, mew, i));
- break;
- case 2:
- do{a = takeInt("a");}while(a<0);
- do{b = takeInt("b");}while(b<=a);
- for(int i = 0; i < b+6; i++)
- System.out.println("p("+i+") = "+uniform(a, b, i));
- break;
- default:
- System.out.println("Enter 1 or 2.");
- }
- break;
- case 3: return;
- default:
- System.out.println("Enter a number from 1 to 3.");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment