Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 #include "problib/pdfs/Uniform.h"
00038 #include "problib/conversions.h"
00039 
00040 using namespace pbl;
00041 
00042 Uniform::Uniform(int dim) : PDF(dim, PDF::UNIFORM), uniform_probability_(0), size_is_set_(false) {
00043 }
00044 
00045 Uniform::Uniform(int dim, double density) : PDF(dim, PDF::UNIFORM), uniform_probability_(density), size_is_set_(false) {
00046 }
00047 
00048 Uniform::Uniform(pbl::Vector mean, pbl::Vector size) : PDF(mean.n_elem, PDF::UNIFORM), mean_(mean), size_(size), size_is_set_(true) {
00049     calculateUniformDensity();
00050 }
00051 
00052 Uniform::Uniform(const Uniform& orig) : PDF(orig), mean_(orig.mean_), size_(orig.size_),
00053     uniform_probability_(orig.uniform_probability_), size_is_set_(orig.size_is_set_) {
00054 }
00055 
00056 Uniform::~Uniform() {
00057 }
00058 
00059 Uniform& Uniform::operator=(const Uniform& other) {
00060     if (this != &other)  {
00061         mean_ = other.mean_;
00062         size_ = other.size_;
00063         size_is_set_ = other.size_is_set_;
00064         uniform_probability_ = other.uniform_probability_;
00065         dimensions_ = other.dimensions_;
00066     }
00067     return *this;
00068 }
00069 
00070 Uniform* Uniform::clone() const {
00071         return new Uniform(*this);
00072 }
00073 
00074 double Uniform::getLikelihood(const PDF& pdf) const {
00075         
00076 
00077     assert(dimensions() == pdf.dimensions());
00078 
00079     if (size_is_set_) {
00080 
00081         arma::vec my_min = mean_ - size_ / 2;
00082         arma::vec my_max = mean_ + size_ / 2;
00083 
00084         if (pdf.type() == PDF::UNIFORM) {
00085             const Uniform* U = PDFtoUniform(pdf);
00086 
00087             arma::vec other_min = U->mean_ - U->size_ / 2;
00088             arma::vec other_max = U->mean_ + U->size_ / 2;
00089 
00090             double overlapping_volume = 1;
00091             for(int i = 0; i < dimensions(); ++i) {
00092                 double diff = std::min(my_max(i), other_max(i)) - std::max(my_min(i), other_min(i));
00093                 if (diff <= 0) {
00094                     return 0;
00095                 }
00096                 overlapping_volume *= diff;
00097             }
00098 
00099             return overlapping_volume * uniform_probability_ * U->uniform_probability_;
00100         } else if (pdf.type() == PDF::HYBRID) {
00101             return pdf.getLikelihood(*this);
00102         } else {
00103             arma::vec other_mean;
00104             if (!pdf.getExpectedValue(other_mean)) {
00105                 std::cout << pdf.toString() << std::endl;
00106                 assert_msg(false, "Uniform likelihood calculation: cannot determine expected value of pdf.");
00107                 return 0;
00108             }
00109 
00110             for(int i = 0; i < dimensions(); ++i) {
00111                 if (other_mean(i) < my_min(i) || other_mean(i) > my_max(i)) {
00112                     return 0;
00113                 }
00114             }
00115 
00116             return uniform_probability_;
00117         }
00118     }
00119 
00120     return uniform_probability_;
00121 }
00122 
00123 void Uniform::setDensity(const double& density) {
00124         uniform_probability_ = density;
00125     size_is_set_ = false;
00126 }
00127 
00128 double Uniform::getDensity(const arma::vec& vec) const {
00129         return uniform_probability_;
00130 }
00131 
00132 double Uniform::getMaxDensity() const {
00133         return uniform_probability_;
00134 }
00135 
00136 void Uniform::setMean(const pbl::Vector mean) {
00137     mean_ = mean;
00138 }
00139 
00140 void Uniform::setSize(const pbl::Vector size) {
00141     size_ = size;
00142     calculateUniformDensity();
00143 }
00144 
00145 void Uniform::calculateUniformDensity() {
00146     double volume = 1;
00147     for(unsigned int i = 0; i < size_.n_elem; ++i) {
00148         volume *= size_(i);
00149     }
00150     uniform_probability_ = 1.0 / volume;
00151     size_is_set_ = true;
00152 }
00153 
00154 std::string Uniform::toString(const std::string& indent) const {
00155         std::stringstream s;
00156     s << "U(" << uniform_probability_;
00157 
00158     if (size_is_set_) {
00159         s << ", mean = " << mean_ << ", size = " << size_;
00160     }
00161 
00162     s << ")";
00163 
00164         return s.str();
00165 }