File indexing completed on 2025-01-18 09:51:12
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef BOOST_RANDOM_STUDENT_T_DISTRIBUTION_HPP
0014 #define BOOST_RANDOM_STUDENT_T_DISTRIBUTION_HPP
0015
0016 #include <boost/config/no_tr1/cmath.hpp>
0017 #include <iosfwd>
0018 #include <boost/config.hpp>
0019 #include <boost/limits.hpp>
0020 #include <boost/random/detail/operators.hpp>
0021 #include <boost/random/chi_squared_distribution.hpp>
0022 #include <boost/random/normal_distribution.hpp>
0023
0024 namespace boost {
0025 namespace random {
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037 template<class RealType = double>
0038 class student_t_distribution {
0039 public:
0040 typedef RealType result_type;
0041 typedef RealType input_type;
0042
0043 class param_type {
0044 public:
0045 typedef student_t_distribution distribution_type;
0046
0047
0048
0049
0050
0051
0052 explicit param_type(RealType n_arg = RealType(1.0))
0053 : _n(n_arg)
0054 {}
0055
0056
0057 RealType n() const { return _n; }
0058
0059
0060 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
0061 { os << parm._n; return os; }
0062
0063
0064 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
0065 { is >> parm._n; return is; }
0066
0067
0068 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
0069 { return lhs._n == rhs._n; }
0070
0071
0072 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
0073
0074 private:
0075 RealType _n;
0076 };
0077
0078
0079
0080
0081
0082
0083 explicit student_t_distribution(RealType n_arg = RealType(1.0))
0084 : _normal(), _chi_squared(n_arg)
0085 {}
0086
0087 explicit student_t_distribution(const param_type& parm)
0088 : _normal(), _chi_squared(parm.n())
0089 {}
0090
0091
0092
0093
0094
0095 template<class URNG>
0096 RealType operator()(URNG& urng)
0097 {
0098 using std::sqrt;
0099 return _normal(urng) / sqrt(_chi_squared(urng) / n());
0100 }
0101
0102
0103
0104
0105
0106 template<class URNG>
0107 RealType operator()(URNG& urng, const param_type& parm) const
0108 {
0109 return student_t_distribution(parm)(urng);
0110 }
0111
0112
0113 RealType n() const { return _chi_squared.n(); }
0114
0115
0116 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
0117 { return -std::numeric_limits<RealType>::infinity(); }
0118
0119 RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
0120 { return std::numeric_limits<RealType>::infinity(); }
0121
0122
0123 param_type param() const { return param_type(n()); }
0124
0125 void param(const param_type& parm)
0126 {
0127 typedef chi_squared_distribution<RealType> chi_squared_type;
0128 typename chi_squared_type::param_type chi_squared_param(parm.n());
0129 _chi_squared.param(chi_squared_param);
0130 }
0131
0132
0133
0134
0135
0136 void reset()
0137 {
0138 _normal.reset();
0139 _chi_squared.reset();
0140 }
0141
0142
0143 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, student_t_distribution, td)
0144 {
0145 os << td.param();
0146 return os;
0147 }
0148
0149
0150 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, student_t_distribution, td)
0151 {
0152 param_type parm;
0153 if(is >> parm) {
0154 td.param(parm);
0155 }
0156 return is;
0157 }
0158
0159
0160
0161
0162
0163 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(student_t_distribution, lhs, rhs)
0164 { return lhs._normal == rhs._normal && lhs._chi_squared == rhs._chi_squared; }
0165
0166
0167
0168
0169
0170 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(student_t_distribution)
0171
0172 private:
0173 normal_distribution<RealType> _normal;
0174 chi_squared_distribution<RealType> _chi_squared;
0175 };
0176
0177 }
0178 }
0179
0180 #endif