... |
... |
@@ -710,164 +710,187 @@
|
710 |
710 |
/// \todo Consider using the "ziggurat" method instead.
|
711 |
711 |
double gauss()
|
712 |
712 |
{
|
713 |
713 |
double V1,V2,S;
|
714 |
714 |
do {
|
715 |
715 |
V1=2*real<double>()-1;
|
716 |
716 |
V2=2*real<double>()-1;
|
717 |
717 |
S=V1*V1+V2*V2;
|
718 |
718 |
} while(S>=1);
|
719 |
719 |
return std::sqrt(-2*std::log(S)/S)*V1;
|
720 |
720 |
}
|
721 |
721 |
/// Gauss distribution with given mean and standard deviation
|
722 |
722 |
|
723 |
723 |
/// Gauss distribution with given mean and standard deviation.
|
724 |
724 |
/// \sa gauss()
|
725 |
725 |
double gauss(double mean,double std_dev)
|
726 |
726 |
{
|
727 |
727 |
return gauss()*std_dev+mean;
|
728 |
728 |
}
|
729 |
729 |
|
730 |
730 |
/// Exponential distribution with given mean
|
731 |
731 |
|
732 |
732 |
/// This function generates an exponential distribution random number
|
733 |
733 |
/// with mean <tt>1/lambda</tt>.
|
734 |
734 |
///
|
735 |
735 |
double exponential(double lambda=1.0)
|
736 |
736 |
{
|
737 |
737 |
return -std::log(1.0-real<double>())/lambda;
|
738 |
738 |
}
|
739 |
739 |
|
740 |
740 |
/// Gamma distribution with given integer shape
|
741 |
741 |
|
742 |
742 |
/// This function generates a gamma distribution random number.
|
743 |
743 |
///
|
744 |
744 |
///\param k shape parameter (<tt>k>0</tt> integer)
|
745 |
745 |
double gamma(int k)
|
746 |
746 |
{
|
747 |
747 |
double s = 0;
|
748 |
748 |
for(int i=0;i<k;i++) s-=std::log(1.0-real<double>());
|
749 |
749 |
return s;
|
750 |
750 |
}
|
751 |
751 |
|
752 |
752 |
/// Gamma distribution with given shape and scale parameter
|
753 |
753 |
|
754 |
754 |
/// This function generates a gamma distribution random number.
|
755 |
755 |
///
|
756 |
756 |
///\param k shape parameter (<tt>k>0</tt>)
|
757 |
757 |
///\param theta scale parameter
|
758 |
758 |
///
|
759 |
759 |
double gamma(double k,double theta=1.0)
|
760 |
760 |
{
|
761 |
761 |
double xi,nu;
|
762 |
762 |
const double delta = k-std::floor(k);
|
763 |
763 |
const double v0=E/(E-delta);
|
764 |
764 |
do {
|
765 |
765 |
double V0=1.0-real<double>();
|
766 |
766 |
double V1=1.0-real<double>();
|
767 |
767 |
double V2=1.0-real<double>();
|
768 |
768 |
if(V2<=v0)
|
769 |
769 |
{
|
770 |
770 |
xi=std::pow(V1,1.0/delta);
|
771 |
771 |
nu=V0*std::pow(xi,delta-1.0);
|
772 |
772 |
}
|
773 |
773 |
else
|
774 |
774 |
{
|
775 |
775 |
xi=1.0-std::log(V1);
|
776 |
776 |
nu=V0*std::exp(-xi);
|
777 |
777 |
}
|
778 |
778 |
} while(nu>std::pow(xi,delta-1.0)*std::exp(-xi));
|
779 |
779 |
return theta*(xi-gamma(int(std::floor(k))));
|
780 |
780 |
}
|
781 |
781 |
|
782 |
782 |
/// Weibull distribution
|
783 |
783 |
|
784 |
784 |
/// This function generates a Weibull distribution random number.
|
785 |
785 |
///
|
786 |
786 |
///\param k shape parameter (<tt>k>0</tt>)
|
787 |
787 |
///\param lambda scale parameter (<tt>lambda>0</tt>)
|
788 |
788 |
///
|
789 |
789 |
double weibull(double k,double lambda)
|
790 |
790 |
{
|
791 |
791 |
return lambda*pow(-std::log(1.0-real<double>()),1.0/k);
|
792 |
792 |
}
|
793 |
793 |
|
794 |
794 |
/// Pareto distribution
|
795 |
795 |
|
796 |
796 |
/// This function generates a Pareto distribution random number.
|
797 |
797 |
///
|
798 |
798 |
///\param k shape parameter (<tt>k>0</tt>)
|
799 |
799 |
///\param x_min location parameter (<tt>x_min>0</tt>)
|
800 |
800 |
///
|
801 |
801 |
double pareto(double k,double x_min)
|
802 |
802 |
{
|
803 |
803 |
return exponential(gamma(k,1.0/x_min));
|
804 |
804 |
}
|
805 |
805 |
|
|
806 |
/// Poisson distribution
|
|
807 |
|
|
808 |
/// This function generates a Poisson distribution random number with
|
|
809 |
/// parameter \c lambda.
|
|
810 |
///
|
|
811 |
/// The probability mass function of this distribusion is
|
|
812 |
/// \f[ \frac{e^{-\lambda}\lambda^k}{k!} \f]
|
|
813 |
/// \note The algorithm is taken from the book of Donald E. Knuth titled
|
|
814 |
/// ''Seminumerical Algorithms'' (1969). Its running time is linear in the
|
|
815 |
/// return value.
|
|
816 |
|
|
817 |
int poisson(double lambda)
|
|
818 |
{
|
|
819 |
const double l = std::exp(-lambda);
|
|
820 |
int k=0;
|
|
821 |
double p = 1.0;
|
|
822 |
do {
|
|
823 |
k++;
|
|
824 |
p*=real<double>();
|
|
825 |
} while (p>=l);
|
|
826 |
return k-1;
|
|
827 |
}
|
|
828 |
|
806 |
829 |
///@}
|
807 |
830 |
|
808 |
831 |
///\name Two dimensional distributions
|
809 |
832 |
///
|
810 |
833 |
|
811 |
834 |
///@{
|
812 |
835 |
|
813 |
836 |
/// Uniform distribution on the full unit circle
|
814 |
837 |
|
815 |
838 |
/// Uniform distribution on the full unit circle.
|
816 |
839 |
///
|
817 |
840 |
dim2::Point<double> disc()
|
818 |
841 |
{
|
819 |
842 |
double V1,V2;
|
820 |
843 |
do {
|
821 |
844 |
V1=2*real<double>()-1;
|
822 |
845 |
V2=2*real<double>()-1;
|
823 |
846 |
|
824 |
847 |
} while(V1*V1+V2*V2>=1);
|
825 |
848 |
return dim2::Point<double>(V1,V2);
|
826 |
849 |
}
|
827 |
850 |
/// A kind of two dimensional Gauss distribution
|
828 |
851 |
|
829 |
852 |
/// This function provides a turning symmetric two-dimensional distribution.
|
830 |
853 |
/// Both coordinates are of standard normal distribution, but they are not
|
831 |
854 |
/// independent.
|
832 |
855 |
///
|
833 |
856 |
/// \note The coordinates are the two random variables provided by
|
834 |
857 |
/// the Box-Muller method.
|
835 |
858 |
dim2::Point<double> gauss2()
|
836 |
859 |
{
|
837 |
860 |
double V1,V2,S;
|
838 |
861 |
do {
|
839 |
862 |
V1=2*real<double>()-1;
|
840 |
863 |
V2=2*real<double>()-1;
|
841 |
864 |
S=V1*V1+V2*V2;
|
842 |
865 |
} while(S>=1);
|
843 |
866 |
double W=std::sqrt(-2*std::log(S)/S);
|
844 |
867 |
return dim2::Point<double>(W*V1,W*V2);
|
845 |
868 |
}
|
846 |
869 |
/// A kind of two dimensional exponential distribution
|
847 |
870 |
|
848 |
871 |
/// This function provides a turning symmetric two-dimensional distribution.
|
849 |
872 |
/// The x-coordinate is of conditionally exponential distribution
|
850 |
873 |
/// with the condition that x is positive and y=0. If x is negative and
|
851 |
874 |
/// y=0 then, -x is of exponential distribution. The same is true for the
|
852 |
875 |
/// y-coordinate.
|
853 |
876 |
dim2::Point<double> exponential2()
|
854 |
877 |
{
|
855 |
878 |
double V1,V2,S;
|
856 |
879 |
do {
|
857 |
880 |
V1=2*real<double>()-1;
|
858 |
881 |
V2=2*real<double>()-1;
|
859 |
882 |
S=V1*V1+V2*V2;
|
860 |
883 |
} while(S>=1);
|
861 |
884 |
double W=-std::log(S)/S;
|
862 |
885 |
return dim2::Point<double>(W*V1,W*V2);
|
863 |
886 |
}
|
864 |
887 |
|
865 |
888 |
///@}
|
866 |
889 |
};
|
867 |
890 |
|
868 |
891 |
|
869 |
892 |
extern Random rnd;
|
870 |
893 |
|
871 |
894 |
}
|
872 |
895 |
|
873 |
896 |
#endif
|