README.md (4038B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Variance 22 23 > [Binomial][binomial-distribution] distribution [variance][variance]. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 The [variance][variance] for a [binomial][binomial-distribution] random variable is 30 31 <!-- <equation class="equation" label="eq:binomial_variance" align="center" raw="\operatorname{Var}\left[ X \right] = n p (1-p)" alt="Variance for a binomial distribution."> --> 32 33 <div class="equation" align="center" data-raw-text="\operatorname{Var}\left[ X \right] = n p (1-p)" data-equation="eq:binomial_variance"> 34 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@51534079fef45e990850102147e8945fb023d1d0/lib/node_modules/@stdlib/stats/base/dists/binomial/variance/docs/img/equation_binomial_variance.svg" alt="Variance for a binomial distribution."> 35 <br> 36 </div> 37 38 <!-- </equation> --> 39 40 where `n` is the number of trials and `p` is the success probability. 41 42 </section> 43 44 <!-- /.intro --> 45 46 <!-- Package usage documentation. --> 47 48 <section class="usage"> 49 50 ## Usage 51 52 ```javascript 53 var variance = require( '@stdlib/stats/base/dists/binomial/variance' ); 54 ``` 55 56 #### variance( n, p ) 57 58 Returns the [variance][variance] of a [binomial][binomial-distribution] distribution with number of trials `n` and success probability `p`. 59 60 ```javascript 61 var v = variance( 20, 0.1 ); 62 // returns 1.8 63 64 v = variance( 50, 0.5 ); 65 // returns 12.5 66 ``` 67 68 If provided `NaN` as any argument, the function returns `NaN`. 69 70 ```javascript 71 var v = variance( NaN, 0.5 ); 72 // returns NaN 73 74 v = variance( 20, NaN ); 75 // returns NaN 76 ``` 77 78 If provided a number of trials `n` which is not a nonnegative integer, the function returns `NaN`. 79 80 ```javascript 81 var v = variance( 1.5, 0.5 ); 82 // returns NaN 83 84 v = variance( -2.0, 0.5 ); 85 // returns NaN 86 ``` 87 88 If provided a success probability `p` outside of `[0,1]`, the function returns `NaN`. 89 90 ```javascript 91 var v = variance( 20, -1.0 ); 92 // returns NaN 93 94 v = variance( 20, 1.5 ); 95 // returns NaN 96 ``` 97 98 </section> 99 100 <!-- /.usage --> 101 102 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 103 104 <section class="notes"> 105 106 </section> 107 108 <!-- /.notes --> 109 110 <!-- Package usage examples. --> 111 112 <section class="examples"> 113 114 ## Examples 115 116 <!-- eslint no-undef: "error" --> 117 118 ```javascript 119 var randu = require( '@stdlib/random/base/randu' ); 120 var round = require( '@stdlib/math/base/special/round' ); 121 var variance = require( '@stdlib/stats/base/dists/binomial/variance' ); 122 123 var v; 124 var i; 125 var n; 126 var p; 127 128 for ( i = 0; i < 10; i++ ) { 129 n = round( randu() * 100.0 ); 130 p = randu(); 131 v = variance( n, p ); 132 console.log( 'n: %d, p: %d, Var(X;n,p): %d', n, p.toFixed( 4 ), v.toFixed( 4 ) ); 133 } 134 ``` 135 136 </section> 137 138 <!-- /.examples --> 139 140 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 141 142 <section class="references"> 143 144 </section> 145 146 <!-- /.references --> 147 148 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 149 150 <section class="links"> 151 152 [binomial-distribution]: https://en.wikipedia.org/wiki/Binomial_distribution 153 154 [variance]: https://en.wikipedia.org/wiki/Variance 155 156 </section> 157 158 <!-- /.links -->