From 1aef4543ccb3a6e0bc1e51a903898ff06e2bfd04 Mon Sep 17 00:00:00 2001 From: Raj Bhattacharjea Date: Fri, 13 Jun 2008 17:19:53 -0700 Subject: [PATCH] bug 181: Normal Variable Infinite Value & Bounds --- src/core/random-variable.cc | 26 ++++++++++++++++++++++---- src/core/random-variable.h | 26 +++++++++++++++++++++----- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/core/random-variable.cc b/src/core/random-variable.cc index 336a2a119..778cb2e45 100644 --- a/src/core/random-variable.cc +++ b/src/core/random-variable.cc @@ -1043,11 +1043,21 @@ double NormalVariableImpl::GetValue() { // Got good pair double y = sqrt((-2 * log(w))/w); m_next = m_mean + v2 * y * sqrt(m_variance); - if (fabs(m_next) > m_bound) m_next = m_bound * (m_next)/fabs(m_next); - m_nextValid = true; + //if next is in bounds, it is valid + m_nextValid = fabs(m_next-m_mean) <= m_bound; double x1 = m_mean + v1 * y * sqrt(m_variance); - if (fabs(x1) > m_bound) x1 = m_bound * (x1)/fabs(x1); - return x1; + //if x1 is in bounds, return it + if (fabs(x1-m_mean) <= m_bound) + { + return x1; + } + //otherwise try and return m_next if it is valid + else if (m_nextValid) + { + m_nextValid = false; + return m_next; + } + //otherwise, just run this loop again } } } @@ -1094,10 +1104,18 @@ double NormalVariableImpl::GetSingleValue(double m, double v, double b) NormalVariable::NormalVariable() : RandomVariable (NormalVariableImpl ()) {} +NormalVariable::NormalVariable(double m, double v) + : RandomVariable (NormalVariableImpl (m, v)) +{} NormalVariable::NormalVariable(double m, double v, double b) : RandomVariable (NormalVariableImpl (m, v, b)) {} double +NormalVariable::GetSingleValue(double m, double v) +{ + return NormalVariableImpl::GetSingleValue (m, v); +} +double NormalVariable::GetSingleValue(double m, double v, double b) { return NormalVariableImpl::GetSingleValue (m, v, b); diff --git a/src/core/random-variable.h b/src/core/random-variable.h index 4abb36bdb..a311a2bbc 100644 --- a/src/core/random-variable.h +++ b/src/core/random-variable.h @@ -490,27 +490,43 @@ public: class NormalVariable : public RandomVariable { public: - static const double INFINITE_VALUE; /** * Constructs an normal random variable with a mean * value of 0 and variance of 1. */ NormalVariable(); + /** + * \brief Construct a normal random variable with specified mean and variance. + * \param m Mean value + * \param v Variance + */ + NormalVariable(double m, double v); + /** * \brief Construct a normal random variable with specified mean and variance * \param m Mean value * \param v Variance - * \param b Bound. The NormalVariable is bounded within +-bound. + * \param b Bound. The NormalVariable is bounded symetrically about the mean + * [mean-bound,mean+bound] */ - NormalVariable(double m, double v, double b = INFINITE_VALUE); + NormalVariable(double m, double v, double b); + /** * \param m Mean value * \param v Variance - * \param b Bound. The NormalVariable is bounded within +-bound. + * \return A random number from a distribution specified by m, and v. + */ + static double GetSingleValue(double m, double v); + + /** + * \param m Mean value + * \param v Variance + * \param b Bound. The NormalVariable is bounded symetrically about the mean + * [mean-bound,mean+bound] * \return A random number from a distribution specified by m,v, and b. */ - static double GetSingleValue(double m, double v, double b = INFINITE_VALUE); + static double GetSingleValue(double m, double v, double b); }; /**