/* * $Id: example0b.cpp,v 1.1 2007/08/15 19:44:55 brook Exp $ */ /* * Copyright (c) 2007 Brook Milligan. * Distributed under the Boost Software License, Version 1.0. (See * accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) */ /* * The purpose of this example is to illustrate a solution to a * typical likelihood problem using the library. It was created as an * example for the library documentation, and included here to ensure * that it compiles correctly. Note that the actual implementations * of the classes and functions is meaningless, as they are not * relevant to illustrating the structure of the solution encapsulated * within the main() function. */ #include #include #include #include #include #include class GHCN_record { /* ... */ }; typedef std::vector GHCN_data_set; class GlobalClimateModel { public: virtual ~GlobalClimateModel () { } virtual boost::probability probability (const GHCN_record&) const = 0; }; class GlobalClimateModelFactory { public: GlobalClimateModel* operator () (const char*) const; }; // read GHCN data set std::istream& operator >> (std::istream&, GHCN_data_set&); // output a likelihood std::ostream& operator << (std::ostream&, const boost::likelihood&); int main () { typedef boost::probabilities::linear_domain linear_domain; GlobalClimateModelFactory climate_model_factory; GlobalClimateModel* climate_model = climate_model_factory("GCM"); GHCN_data_set data; std::cin >> data; assert (data.size() == 590543); // XXX - illustrate large size of dataset boost::log_likelihood lnL; for (GHCN_data_set::const_iterator i = data.begin(); i != data.end(); ++i) { GHCN_record record = *i; boost::probability p (climate_model->probability(record)); lnL *= p; } std::cout << "likelihood: " << boost::probabilities::domain_cast(lnL) << std::endl; return 0; } class GCM : public GlobalClimateModel { public: GCM () : GlobalClimateModel() { } virtual ~GCM () { } virtual boost::probability probability (const GHCN_record&) const { return boost::probability(1); } }; GlobalClimateModel* GlobalClimateModelFactory::operator () (const char*) const { return new GCM; } std::istream& operator >> (std::istream& s, GHCN_data_set& d) { d = GHCN_data_set (590543); return s; } std::ostream& operator << (std::ostream& s, const boost::likelihood& l) { typedef boost::probabilities::linear_domain linear_domain; return s << boost::probabilities::value_cast(l); }