| ... | ... |
@@ -819,48 +819,88 @@ |
| 819 | 819 |
/// Standard Gauss distribution |
| 820 | 820 |
|
| 821 | 821 |
/// Standard Gauss distribution. |
| 822 | 822 |
/// \note The Cartesian form of the Box-Muller |
| 823 | 823 |
/// transformation is used to generate a random normal distribution. |
| 824 | 824 |
double gauss() |
| 825 | 825 |
{
|
| 826 | 826 |
double V1,V2,S; |
| 827 | 827 |
do {
|
| 828 | 828 |
V1=2*real<double>()-1; |
| 829 | 829 |
V2=2*real<double>()-1; |
| 830 | 830 |
S=V1*V1+V2*V2; |
| 831 | 831 |
} while(S>=1); |
| 832 | 832 |
return std::sqrt(-2*std::log(S)/S)*V1; |
| 833 | 833 |
} |
| 834 | 834 |
/// Gauss distribution with given mean and standard deviation |
| 835 | 835 |
|
| 836 | 836 |
/// Gauss distribution with given mean and standard deviation. |
| 837 | 837 |
/// \sa gauss() |
| 838 | 838 |
double gauss(double mean,double std_dev) |
| 839 | 839 |
{
|
| 840 | 840 |
return gauss()*std_dev+mean; |
| 841 | 841 |
} |
| 842 | 842 |
|
| 843 |
/// Lognormal distribution |
|
| 844 |
|
|
| 845 |
/// Lognormal distribution. The parameters are the mean and the standard |
|
| 846 |
/// deviation of <tt>exp(X)</tt>. |
|
| 847 |
/// |
|
| 848 |
double lognormal(double n_mean,double n_std_dev) |
|
| 849 |
{
|
|
| 850 |
return std::exp(gauss(n_mean,n_std_dev)); |
|
| 851 |
} |
|
| 852 |
/// Lognormal distribution |
|
| 853 |
|
|
| 854 |
/// Lognormal distribution. The parameter is an <tt>std::pair</tt> of |
|
| 855 |
/// the mean and the standard deviation of <tt>exp(X)</tt>. |
|
| 856 |
/// |
|
| 857 |
double lognormal(const std::pair<double,double> ¶ms) |
|
| 858 |
{
|
|
| 859 |
return std::exp(gauss(params.first,params.second)); |
|
| 860 |
} |
|
| 861 |
/// Compute the lognormal parameters from mean and standard deviation |
|
| 862 |
|
|
| 863 |
/// This function computes the lognormal parameters from mean and |
|
| 864 |
/// standard deviation. The return value can direcly be passed to |
|
| 865 |
/// lognormal(). |
|
| 866 |
std::pair<double,double> lognormalParamsFromMD(double mean, |
|
| 867 |
double std_dev) |
|
| 868 |
{
|
|
| 869 |
double fr=std_dev/mean; |
|
| 870 |
fr*=fr; |
|
| 871 |
double lg=std::log(1+fr); |
|
| 872 |
return std::pair<double,double>(std::log(mean)-lg/2.0,std::sqrt(lg)); |
|
| 873 |
} |
|
| 874 |
/// Lognormal distribution with given mean and standard deviation |
|
| 875 |
|
|
| 876 |
/// Lognormal distribution with given mean and standard deviation. |
|
| 877 |
/// |
|
| 878 |
double lognormalMD(double mean,double std_dev) |
|
| 879 |
{
|
|
| 880 |
return lognormal(lognormalParamsFromMD(mean,std_dev)); |
|
| 881 |
} |
|
| 882 |
|
|
| 843 | 883 |
/// Exponential distribution with given mean |
| 844 | 884 |
|
| 845 | 885 |
/// This function generates an exponential distribution random number |
| 846 | 886 |
/// with mean <tt>1/lambda</tt>. |
| 847 | 887 |
/// |
| 848 | 888 |
double exponential(double lambda=1.0) |
| 849 | 889 |
{
|
| 850 | 890 |
return -std::log(1.0-real<double>())/lambda; |
| 851 | 891 |
} |
| 852 | 892 |
|
| 853 | 893 |
/// Gamma distribution with given integer shape |
| 854 | 894 |
|
| 855 | 895 |
/// This function generates a gamma distribution random number. |
| 856 | 896 |
/// |
| 857 | 897 |
///\param k shape parameter (<tt>k>0</tt> integer) |
| 858 | 898 |
double gamma(int k) |
| 859 | 899 |
{
|
| 860 | 900 |
double s = 0; |
| 861 | 901 |
for(int i=0;i<k;i++) s-=std::log(1.0-real<double>()); |
| 862 | 902 |
return s; |
| 863 | 903 |
} |
| 864 | 904 |
|
| 865 | 905 |
/// Gamma distribution with given shape and scale parameter |
| 866 | 906 |
|
0 comments (0 inline)