-
Notifications
You must be signed in to change notification settings - Fork 219
Probability Distribution -- Gamma Distribution #1099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Calluumm
wants to merge
48
commits into
fortran-lang:master
Choose a base branch
from
Calluumm:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 20 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
c2f7367
added gamma to specs
Calluumm bd43489
added gamma distribution documentation
Calluumm 1c47789
added gamma distribution + dependancy
Calluumm c53a8c9
Gamma distrbution function
Calluumm 7a651bf
added gamma test
Calluumm bd6d186
Gamma test
Calluumm 3b97e8d
Update doc/specs/stdlib_stats_distribution_gamma.md
Calluumm 6c52773
Update src/stats/stdlib_stats_distribution_gamma.fypp
Calluumm c97482f
Update src/stats/stdlib_stats_distribution_gamma.fypp
Calluumm 3338b91
Update doc/specs/stdlib_stats_distribution_gamma.md
Calluumm 12ee781
removed xdp override
Calluumm 56cbe8e
removed xdp override
Calluumm 021d83e
Update src/stats/stdlib_stats_distribution_gamma.fypp
Calluumm 8eebbcd
Update test/stats/test_distribution_gamma.fypp
Calluumm 54001ec
Changed to use regular gamma instead of lower
Calluumm 113f063
comment spelling mistake
Calluumm 9a36255
Used log gamma for numerical stability
Calluumm 4832880
Merge branch 'master' into master
Calluumm c3a6700
reworked qp skip
Calluumm 54f2d2b
Removed global flag
Calluumm fdf4708
Changed scale to rate to match docs
Calluumm a7d2b37
made log_gamma intrinsic
Calluumm 8f96166
fixed typo, removed qp at top, fixed pdf=0 logic
Calluumm 1d79b48
Changed to computing d/c locally
Calluumm bc91291
PDF returns NaN - stdlib standard
Calluumm c3ff88b
Removed tol, left over from old setup
Calluumm 5647f87
Elaborated on docs
Calluumm a957123
Fixed mathematical pdf error
Calluumm 71153c6
Fixed 0 result to be in line with other distrib functions
Calluumm 6b31513
same changes for cdf as pdf
Calluumm 87b20d1
return nan instead of error stop
Calluumm 29c1580
Added impure wrappers for the elemental functions
Calluumm bfb7793
Added a loc paramter to functions
Calluumm 91106f1
Updated stats for the loc parameter
Calluumm b4864b1
updated things to include loc
Calluumm 69f2cc8
Documentation overhaul
Calluumm 6343096
Switched to example version
Calluumm 2700e62
added gamma examples dir
Calluumm 94f992e
initial file commit
Calluumm 79a0708
reverted that
Calluumm 669e501
initial file path
Calluumm cef6c94
added gamma exaples
Calluumm 771b34e
removed initial
Calluumm 2db76d0
changed to bisets to match current prod
Calluumm df4f906
Updated example structure (rvs)
Calluumm 23d31c5
Updated example structure (pdf)
Calluumm 60df243
Updated gamma structure (cdf)
Calluumm e009726
stated loc, removed beta from rvs description and re-phrased
Calluumm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| --- | ||
|
|
||
| title: stats_distribution_gamma | ||
| --- | ||
|
|
||
| # Statistical Distributions -- Gamma Distribution Module | ||
|
|
||
| [TOC] | ||
|
|
||
| ## `rvs_gamma` - gamma distribution random variates | ||
|
|
||
| ### Status | ||
|
|
||
| Experimental | ||
|
|
||
| ### Description | ||
|
|
||
| With one argument for shape parameter, the function returns a random sample from the standard gamma distribution `Gam(shape)` with `rate = 1.0`. | ||
|
|
||
| With two arguments, the function returns a random sample from gamma distribution `Gam(shape, rate)`. | ||
|
|
||
| With three arguments, the function returns a rank one array of gamma distributed random variates. | ||
|
|
||
| For complex shape and rate parameters, the real and imaginary parts are sampled independently of each other. | ||
|
|
||
|
|
||
| ### Syntax | ||
|
|
||
| `result = [[stdlib_stats_distribution_gamma(module):rvs_gamma(interface)]](shape [, rate] [[, array_size]])` | ||
|
|
||
| ### Class | ||
|
|
||
| Function | ||
|
|
||
| ### Arguments | ||
|
|
||
| `shape` : has `intent(in)` and is a scalar of type `real` or `complex`. | ||
|
|
||
| `rate`: optional argument has `intent(in)` and is a scalar of type `real` or `complex`. | ||
|
|
||
| `array_size`: optional argument has `intent(in)` and is a scalar of type `integer` with default kind. | ||
|
|
||
| ### Return value | ||
|
|
||
| The result is a scalar or rank one array, with a size of `array_size`, and has the same type of `shape`. | ||
|
|
||
| ### Example | ||
|
|
||
| ```fortran | ||
| program demo_gamma_rvs | ||
| use stdlib_random, only : random_seed | ||
| use stdlib_stats_distribution_gamma, only: rgamma => rvs_gamma | ||
| implicit none | ||
| real :: g(2,3,4) | ||
| complex :: shape, rate | ||
| integer :: put, get | ||
| put = 1234567 | ||
| call random_seed(put, get) | ||
| print *, rgamma(2.0) | ||
| !single standard gamma random variate with shape of 2.0, rate=1.0 | ||
| ! 2.50538206 | ||
| print *, rgamma(3.0,2.0) !gamma random variate with shape=3.0, rate=2.0 | ||
| ! 1.30591583 | ||
| g(:,:,:) = 0.5 | ||
| print *, rgamma(g) | ||
| !a rank 3 array of 60 standard gamma random variates with shape=0.5 | ||
| ! 1.03841162 1.33044529 0.912742674 0.131288037 0.638593793 | ||
| ! 1.03565669E-02 0.624804378 1.12179172 4.91380468E-02 6.69969944E-03 | ||
| ! 6.67014271E-02 0.132111162 0.101102419 0.648416579 1.14922595 | ||
| ! 2.29003578E-02 1.85964716E-04 1.21213868E-02 1.69112933 | ||
| ! 7.30440915E-02 0.395139128 0.182758048 0.427981257 0.985665262 | ||
| print *, rgamma(0.5,1.0,10) | ||
| ! an array of 10 random variates with shape=0.5, rate=1.0 | ||
| ! 1.39297554E-04 0.296419382 0.352113068 2.80515051 3.65264394E-04 | ||
| ! 0.197743446 5.54569438E-02 9.30598825E-02 1.02596343 1.85311246 | ||
| shape = (3.0, 4.0) | ||
| rate = (2.0, 0.7) | ||
| print *, rgamma(shape, rate) | ||
| !single complex gamma random variate with real part of shape = 3.0, | ||
| !rate=2.0; imaginary part of shape=4.0, rate=0.7 | ||
| ! (0.826188326,3.54749799) | ||
| end program demo_gamma_rvs | ||
| ``` | ||
|
|
||
| ## `pdf_gamma` - gamma distribution probability density function | ||
|
|
||
| ### Status | ||
|
|
||
| Experimental | ||
|
|
||
| ### Description | ||
|
|
||
| The probability density function (pdf) of the single real variable gamma distribution: | ||
|
|
||
| $$ f(x)= \frac{rate^{shape}}{\Gamma (shape)}x^{shape-1}e^{-rate \times x} ,\quad x>0,\ shape>0,\ rate>0 $$ | ||
|
|
||
| For a complex variable (x + y i) with independent real x and imaginary y parts, the joint probability density function is the product of the corresponding marginal pdf of real and imaginary pdf (for more details, see | ||
| "Probability and Random Processes with Applications to Signal Processing and Communications", 2nd ed., Scott L. Miller and Donald Childers, 2012, p.197): | ||
|
|
||
| $$f(x+\mathit{i}y)=f(x)f(y)$$ | ||
|
|
||
| ### Syntax | ||
|
|
||
| `result = [[stdlib_stats_distribution_gamma(module):pdf_gamma(interface)]](x, shape, rate)` | ||
|
|
||
| ### Class | ||
|
|
||
| Elemental function | ||
|
|
||
| ### Arguments | ||
|
|
||
| `x`: has `intent(in)` and is a scalar of type `real` or `complex`. | ||
|
|
||
| `shape` has `intent(in)` and is a scalar of type `real` or `complex`. | ||
|
|
||
| `rate`: has `intent(in)` and is a scalar of type `real` or `complex`. | ||
|
|
||
| All arguments must have the same type. | ||
|
|
||
| ### Return value | ||
|
|
||
| The result is a scalar or an array, with a shape conformable to arguments, of type `real`. | ||
|
|
||
| ### Example | ||
|
|
||
| ```fortran | ||
| program demo_gamma_pdf | ||
| use stdlib_random, only : random_seed | ||
| use stdlib_stats_distribution_gamma, only: rgamma => rvs_gamma,& | ||
| gamma_pdf => pdf_gamma | ||
| implicit none | ||
| real :: x(2,3,4),g(2,3,4),s(2,3,4) | ||
| complex :: shape, rate | ||
| integer :: put, get | ||
| put = 1234567 | ||
| call random_seed(put, get) | ||
| print *, gamma_pdf(1.0, 1.0, 1.0) | ||
| !a probability density at 1.0 with shape=1.0, rate=1.0 | ||
| ! 0.367879450 | ||
| g(:,:,:) = 2.0 | ||
| s(:,:,:) = 1.0 | ||
| x = reshape(rgamma(2.0, 1.0, 24),[2,3,4]) ! gamma random variates array | ||
| print *, gamma_pdf(x,g,s) ! a rank 3 gamma probability density array | ||
| ! 0.204550430 0.320178866 0.274986655 0.348611295 0.101865448 | ||
| ! 0.102199331 0.358981341 0.223676488 0.254329354 0.356714427 | ||
| ! 0.267390072 0.305148095 0.367848188 7.26194456E-02 1.49471285E-02 | ||
| ! 0.246272027 0.360770017 0.339665830 0.101558588 0.358678699 | ||
| ! 0.224196941 0.359253854 7.56355673E-02 0.251869917 | ||
| shape = (1.0, 1.5) | ||
| rate = (1.0, 2.) | ||
| print *, gamma_pdf((1.5,1.0), shape, rate) | ||
| ! a complex gamma probability density function at (1.5,1.0) with real part | ||
| !of shape=1.0, rate=1.0 and imaginary part of shape=1.5, rate=2.0 | ||
| ! 9.63761061E-02 | ||
| end program demo_gamma_pdf | ||
| ``` | ||
|
|
||
| ## `cdf_gamma` - gamma distribution cumulative distribution function | ||
|
|
||
| ### Status | ||
|
|
||
| Experimental | ||
|
|
||
| ### Description | ||
|
|
||
| Cumulative distribution function (cdf) of the single real variable gamma distribution: | ||
|
|
||
| $$ F(x)= \frac{\gamma (shape, rate \times x)}{\Gamma (shape)},\quad x>0,\ shape>0,\ rate>0 $$ | ||
|
|
||
| For a complex variable (x + y i) with independent real x and imaginary y parts, the joint cumulative distribution function is the product of corresponding marginal cdf of real and imaginary cdf (for more details, see | ||
| "Probability and Random Processes with Applications to Signal Processing and Communications", 2nd ed., Scott L. Miller and Donald Childers, 2012, p.197): | ||
|
|
||
| $$F(x+\mathit{i}y)=F(x)F(y)$$ | ||
|
|
||
| ### Syntax | ||
|
|
||
| `result = [[stdlib_stats_distribution_gamma(module):cdf_gamma(interface)]](x, shape, rate)` | ||
|
|
||
| ### Class | ||
|
|
||
| Elemental function | ||
|
|
||
| ### Arguments | ||
|
|
||
| `x`: has `intent(in)` and is a scalar of type `real` or `complex`. | ||
|
|
||
| `shape`: has `intent(in)` and is a scalar of type `real` or `complex`. | ||
|
|
||
| `rate`: has `intent(in)` and is a scalar of type `real` or `complex`. | ||
|
|
||
| All arguments must have the same type. | ||
|
|
||
| ### Return value | ||
|
|
||
| The result is a scalar of type `real` with the same kind of input arguments. | ||
|
|
||
| ### Example | ||
|
|
||
| ```fortran | ||
| program demo_gamma_cdf | ||
| use stdlib_random, only : random_seed | ||
| use stdlib_stats_distribution_gamma, only: rgamma => rvs_gamma,& | ||
| gamma_cdf => cdf_gamma | ||
| implicit none | ||
| real :: x(2,3,4),g(2,3,4),s(2,3,4) | ||
| complex :: shape, rate | ||
| integer :: seed_put, seed_get | ||
| seed_put = 1234567 | ||
| call random_seed(seed_put, seed_get) | ||
| print *, gamma_cdf(1.0, 0.5,1.0) | ||
| ! a standard gamma cumulative at 1.0 with a shape=0.5, rate=1.0 | ||
| ! 0.842700839 | ||
| print *, gamma_cdf(2.0, 1.5,2.0) | ||
| ! a cumulative at 2.0 with a shape=1.5, rate=2.0 | ||
| ! 0.953988254 | ||
| g(:,:,:) = 1.0 | ||
| s(:,:,:) = 1.0 | ||
| x = reshape(rgamma(1.0, 1.0, 24),[2,3,4]) | ||
| !gamma random variates array with a shape=1.0, rate=1.0 | ||
| print *, gamma_cdf(x,g,s) ! a rank 3 standard gamma cumulative array | ||
| ! 0.710880339 0.472411335 0.578345954 0.383050948 0.870905757 | ||
| ! 0.870430350 0.170215249 0.677347481 0.620089889 0.161825046 | ||
| ! 4.17549349E-02 0.510665894 0.252201647 0.911497891 0.984424412 | ||
| ! 0.635621786 0.177783430 0.414842933 0.871342421 0.338317066 | ||
| ! 2.06879266E-02 0.335232288 0.907408893 0.624871135 | ||
| shape = (.7, 2.1) | ||
| rate = (0.5,1.0) | ||
| print *, gamma_cdf((0.5,0.5),shape,rate) | ||
| !complex gamma cumulative distribution at (0.5,0.5) with real part of | ||
| !shape=0.7,rate=0.5 and imaginary part of shape=2.1,rate=1.0 | ||
| ! 2.87349485E-02 | ||
| end program demo_gamma_cdf | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.