# HG changeset patch
# User Kwankyu Lee <ekwankyu@gmail.com>
# Date 1275105086 -32400
# Node ID 1636334cf6db2082da0aa02a9fb8afbb8158e6d5
# Parent 1451c00a8d44f78f01cb0dc22ebb7464cc6ad0e2
add F distribution
diff -r 1451c00a8d44 -r 1636334cf6db sage/gsl/gsl_random.pxi
|
a
|
b
|
|
| 111 | 111 | double gsl_ran_tdist ( gsl_rng * r, double nu) |
| 112 | 112 | double gsl_ran_tdist_pdf ( double x, double nu) |
| 113 | 113 | |
| | 114 | double gsl_ran_fdist ( gsl_rng * r, double a, double b) |
| | 115 | double gsl_ran_fdist_pdf ( double x, double a, double b) |
| | 116 | |
| 114 | 117 | double gsl_ran_laplace ( gsl_rng * r, double a) |
| 115 | 118 | double gsl_ran_laplace_pdf ( double x, double a) |
| 116 | 119 | |
| … |
… |
|
| 197 | 200 | double gsl_cdf_fdist_P ( double x, double nu1, double nu2) |
| 198 | 201 | double gsl_cdf_fdist_Q ( double x, double nu1, double nu2) |
| 199 | 202 | |
| | 203 | double gsl_cdf_fdist_Pinv(double x, double a, double b) |
| | 204 | double gsl_cdf_fdist_Qinv(double x, double a, double b) |
| | 205 | |
| 200 | 206 | double gsl_cdf_beta_P ( double x, double a, double b) |
| 201 | 207 | double gsl_cdf_beta_Q ( double x, double a, double b) |
| 202 | 208 | |
diff -r 1451c00a8d44 -r 1636334cf6db sage/gsl/probability_distribution.pyx
|
a
|
b
|
|
| 6 | 6 | - ``RealDistribution``: various real-valued probability distributions. |
| 7 | 7 | |
| 8 | 8 | - ``SphericalDistribution``: uniformly distributed points on the |
| 9 | | surface of an `$n-1$` sphere in `$n$` dimensional euclidean space. |
| | 9 | surface of an `n-1` sphere in `n` dimensional euclidean space. |
| 10 | 10 | |
| 11 | 11 | - ``GeneralDiscreteDistribution``: user-defined discrete distributions. |
| 12 | 12 | |
| … |
… |
|
| 19 | 19 | - Carlo Hamalainen (2008-08): full doctest coverage, more documentation, |
| 20 | 20 | GeneralDiscreteDistribution, misc fixes. |
| 21 | 21 | |
| | 22 | - Kwankyu Lee (2010-05-29): F-distribution support. |
| | 23 | |
| 22 | 24 | REFERENCES: |
| 23 | 25 | |
| 24 | 26 | GNU gsl library, General discrete distributions |
| … |
… |
|
| 28 | 30 | http://www.gnu.org/software/gsl/manual/html_node/Random-Number-Distributions.html |
| 29 | 31 | """ |
| 30 | 32 | |
| 31 | | |
| 32 | 33 | ############################################################################## |
| 33 | 34 | # Copyright (C) 2004, 2005, 2006 Joshua Kantor <kantor.jm@gmail.com> |
| 34 | 35 | # Distributed under the terms of the GNU General Public License (GPL) |
| … |
… |
|
| 46 | 47 | import integration |
| 47 | 48 | from sage.modules.free_module_element import vector |
| 48 | 49 | |
| 49 | | |
| 50 | 50 | #TODO: Add more distributions available in gsl |
| 51 | 51 | #available but not currently wrapped are exponential, laplace, cauchy, landau, gamma, |
| 52 | 52 | #gamma, beta logistic. |
| … |
… |
|
| 58 | 58 | lognormal |
| 59 | 59 | pareto |
| 60 | 60 | t |
| | 61 | F |
| 61 | 62 | chisquared |
| 62 | 63 | exppow |
| 63 | 64 | weibull |
| … |
… |
|
| 365 | 366 | sage: T.cum_distribution_function_inv(.5) |
| 366 | 367 | 2.0 |
| 367 | 368 | |
| 368 | | The student's t distribution has one parameter ``nu``:: |
| | 369 | The t-distribution has one parameter ``nu``:: |
| 369 | 370 | |
| 370 | 371 | sage: nu = 1 |
| 371 | 372 | sage: T = RealDistribution('t', nu) |
| … |
… |
|
| 377 | 378 | 0.75 |
| 378 | 379 | sage: T.cum_distribution_function_inv(.5) |
| 379 | 380 | 0.0 |
| | 381 | |
| | 382 | The F-distribution has two parameters ``nu1`` and ``nu2``:: |
| | 383 | |
| | 384 | sage: nu1 = 9; nu2 = 17 |
| | 385 | sage: F = RealDistribution('F', [nu1,nu2]) |
| | 386 | sage: F.get_random_element() # random |
| | 387 | 1.65211335491 |
| | 388 | sage: F.distribution_function(1) |
| | 389 | 0.669502550519 |
| | 390 | sage: F.cum_distribution_function(3.68) |
| | 391 | 0.98997177723 |
| | 392 | sage: F.cum_distribution_function_inv(0.99) |
| | 393 | 3.68224152405 |
| 380 | 394 | |
| 381 | | The chi squared distribution has one parameter ``nu``:: |
| | 395 | The chi-squared distribution has one parameter ``nu``:: |
| 382 | 396 | |
| 383 | 397 | sage: nu = 1 |
| 384 | 398 | sage: T = RealDistribution('chisquared', nu) |
| … |
… |
|
| 537 | 551 | |
| 538 | 552 | def get_random_element(self): |
| 539 | 553 | """ |
| 540 | | Get a random sample the probability distribution. |
| | 554 | Get a random sample from the probability distribution. |
| 541 | 555 | |
| 542 | 556 | EXAMPLE:: |
| 543 | 557 | |
| … |
… |
|
| 560 | 574 | result = gsl_ran_pareto(self.r, self.parameters[0], self.parameters[1]) |
| 561 | 575 | elif self.distribution_type == t: |
| 562 | 576 | result = gsl_ran_tdist(self.r, self.parameters[0]) |
| | 577 | elif self.distribution_type == F: |
| | 578 | result = gsl_ran_fdist(self.r, self.parameters[0], self.parameters[1]) |
| 563 | 579 | elif self.distribution_type == chisquared: |
| 564 | 580 | result = gsl_ran_chisq(self.r, self.parameters[0]) |
| 565 | 581 | elif self.distribution_type == exppow: |
| … |
… |
|
| 568 | 584 | result = gsl_ran_weibull(self.r, self.parameters[0], self.parameters[1]) |
| 569 | 585 | elif self.distribution_type == beta: |
| 570 | 586 | result = gsl_ran_beta(self.r, self.parameters[0], self.parameters[1]) |
| | 587 | else: |
| | 588 | raise TypeError, "Not a valid probability distribution" |
| 571 | 589 | |
| 572 | 590 | return sage.rings.real_double.RDF(result) |
| | 591 | |
| 573 | 592 | def set_distribution(self, name = 'uniform', parameters = []): |
| 574 | 593 | """ |
| 575 | 594 | This method can be called to change the current probability distribution. |
| … |
… |
|
| 629 | 648 | float(x) |
| 630 | 649 | except: |
| 631 | 650 | raise TypeError, "Lognormal distributions requires real parameters" |
| 632 | | |
| 633 | 651 | self.parameters = <double*>malloc(sizeof(double)*2) |
| 634 | 652 | self.parameters[0] = float(parameters[0]) |
| 635 | 653 | self.parameters[1] = float(parameters[1]) |
| … |
… |
|
| 642 | 660 | self.parameters = <double*>malloc(sizeof(double)) |
| 643 | 661 | self.parameters[0] = float(parameters) |
| 644 | 662 | self.distribution_type = t |
| 645 | | |
| | 663 | elif name == 'F': |
| | 664 | if len(parameters) != 2: |
| | 665 | raise TypeError, "F-distribution requires two real parameters" |
| | 666 | try: |
| | 667 | map(float, parameters) |
| | 668 | except: |
| | 669 | raise TypeError, "F-distribution requires real parameters" |
| | 670 | self.parameters = <double *>malloc(sizeof(double)*2) |
| | 671 | self.parameters[0] = float(parameters[0]) |
| | 672 | self.parameters[1] = float(parameters[1]) |
| | 673 | self.distribution_type = F |
| 646 | 674 | elif name == 'chisquared': |
| 647 | 675 | try: |
| 648 | 676 | float(parameters) |
| … |
… |
|
| 659 | 687 | float(x) |
| 660 | 688 | except: |
| 661 | 689 | raise TypeError, "exponential power distributions requires real parameters" |
| 662 | | |
| 663 | 690 | self.parameters = <double*>malloc(sizeof(double)*2) |
| 664 | 691 | self.parameters[0] = float(parameters[0]) |
| 665 | 692 | self.parameters[1] = float(parameters[1]) |
| … |
… |
|
| 671 | 698 | map(float, parameters) |
| 672 | 699 | except: |
| 673 | 700 | raise TypeError, "weibull distribution requires real parameters" |
| 674 | | |
| 675 | 701 | self.parameters = <double *>malloc(sizeof(double)*2) |
| 676 | 702 | self.parameters[0] = float(parameters[0]) |
| 677 | 703 | self.parameters[1] = float(parameters[1]) |
| 678 | 704 | self.distribution_type = weibull |
| 679 | | |
| 680 | 705 | elif name == 'beta': |
| 681 | 706 | if len(parameters) != 2: |
| 682 | 707 | raise TypeError, "beta distribution requires two real parameters" |
| … |
… |
|
| 684 | 709 | map(float, parameters) |
| 685 | 710 | except: |
| 686 | 711 | raise TypeError, "beta distribution requires real parameters" |
| 687 | | |
| 688 | 712 | self.parameters = <double *>malloc(sizeof(double)*2) |
| 689 | 713 | self.parameters[0] = float(parameters[0]) |
| 690 | 714 | self.parameters[1] = float(parameters[1]) |
| 691 | 715 | self.distribution_type = beta |
| 692 | | |
| 693 | | |
| 694 | 716 | else: |
| 695 | 717 | raise TypeError, "Not a valid probability distribution" |
| 696 | 718 | |
| 697 | 719 | self.name = name |
| 698 | 720 | |
| 699 | | # def _get_random_element_c(): |
| | 721 | #def _get_random_element_c(): |
| 700 | 722 | |
| 701 | | |
| 702 | 723 | def reset_distribution(self): |
| 703 | 724 | """ |
| 704 | 725 | This method resets the distribution. |
| … |
… |
|
| 748 | 769 | return sage.rings.real_double.RDF(gsl_ran_pareto_pdf(x, self.parameters[0], self.parameters[1])) |
| 749 | 770 | elif self.distribution_type == t: |
| 750 | 771 | return sage.rings.real_double.RDF(gsl_ran_tdist_pdf(x, self.parameters[0])) |
| | 772 | elif self.distribution_type == F: |
| | 773 | return sage.rings.real_double.RDF(gsl_ran_fdist_pdf(x, self.parameters[0], self.parameters[1])) |
| 751 | 774 | elif self.distribution_type == chisquared: |
| 752 | 775 | return sage.rings.real_double.RDF(gsl_ran_chisq_pdf(x, self.parameters[0])) |
| 753 | 776 | elif self.distribution_type == exppow: |
| … |
… |
|
| 756 | 779 | return sage.rings.real_double.RDF(gsl_ran_weibull_pdf(x, self.parameters[0], self.parameters[1])) |
| 757 | 780 | elif self.distribution_type == beta: |
| 758 | 781 | return sage.rings.real_double.RDF(gsl_ran_beta_pdf(x, self.parameters[0], self.parameters[1])) |
| 759 | | |
| | 782 | else: |
| | 783 | raise TypeError, "Not a valid probability distribution" |
| 760 | 784 | |
| 761 | 785 | def cum_distribution_function(self, x): |
| 762 | 786 | """ |
| … |
… |
|
| 781 | 805 | return sage.rings.real_double.RDF(gsl_cdf_pareto_P(x, self.parameters[0], self.parameters[1])) |
| 782 | 806 | elif self.distribution_type == t: |
| 783 | 807 | return sage.rings.real_double.RDF(gsl_cdf_tdist_P(x, self.parameters[0])) |
| | 808 | elif self.distribution_type == F: |
| | 809 | return sage.rings.real_double.RDF(gsl_cdf_fdist_P(x, self.parameters[0], self.parameters[1])) |
| 784 | 810 | elif self.distribution_type == chisquared: |
| 785 | 811 | return sage.rings.real_double.RDF(gsl_cdf_chisq_P(x, self.parameters[0])) |
| 786 | 812 | elif self.distribution_type == exppow: |
| … |
… |
|
| 789 | 815 | return sage.rings.real_double.RDF(gsl_cdf_weibull_P(x, self.parameters[0], self.parameters[1])) |
| 790 | 816 | elif self.distribution_type == beta: |
| 791 | 817 | return sage.rings.real_double.RDF(gsl_cdf_beta_P(x, self.parameters[0], self.parameters[1])) |
| | 818 | else: |
| | 819 | raise TypeError, "Not a valid probability distribution" |
| 792 | 820 | |
| 793 | | |
| 794 | 821 | def cum_distribution_function_inv(self, x): |
| 795 | 822 | """ |
| 796 | 823 | Evaluate the inverse of the cumulative distribution |
| … |
… |
|
| 814 | 841 | return sage.rings.real_double.RDF(gsl_cdf_pareto_Pinv(x, self.parameters[0], self.parameters[1])) |
| 815 | 842 | elif self.distribution_type == t: |
| 816 | 843 | return sage.rings.real_double.RDF(gsl_cdf_tdist_Pinv(x, self.parameters[0])) |
| | 844 | elif self.distribution_type == F: |
| | 845 | return sage.rings.real_double.RDF(gsl_cdf_fdist_Pinv(x, self.parameters[0], self.parameters[1])) |
| 817 | 846 | elif self.distribution_type == chisquared: |
| 818 | 847 | return sage.rings.real_double.RDF(gsl_cdf_chisq_Pinv(x, self.parameters[0])) |
| 819 | 848 | elif self.distribution_type == exppow: |
| … |
… |
|
| 823 | 852 | return sage.rings.real_double.RDF(gsl_cdf_weibull_Pinv(x, self.parameters[0], self.parameters[1])) |
| 824 | 853 | elif self.distribution_type == beta: |
| 825 | 854 | return sage.rings.real_double.RDF(gsl_cdf_beta_Pinv(x, self.parameters[0], self.parameters[1])) |
| | 855 | else: |
| | 856 | raise TypeError, "Not a valid probability distribution" |
| 826 | 857 | |
| 827 | 858 | def plot(self, *args, **kwds): |
| 828 | 859 | """ |