187300d9ad62a82a435bad9783f886e55edd6a2c
[libdai.git] / include / dai / evidence.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2009 Charles Vaske [cvaske at soe dot ucsc dot edu]
8 * Copyright (C) 2009 University of California, Santa Cruz
9 */
10
11
12 /// \file
13 /// \brief Defines class Evidence, which stores multiple observations of joint states of variables
14
15
16 #ifndef __defined_libdai_evidence_h
17 #define __defined_libdai_evidence_h
18
19
20 #include <istream>
21 #include <dai/daialg.h>
22
23
24 namespace dai {
25
26
27 /// Stores joint state of a set of variables
28 typedef std::map<Var, size_t> Observation;
29
30
31 /// Stores a data set consisting of multiple samples, where each sample is the observed joint state of some variables.
32 /** \note Each sample can describe the joint state of a different set of variables,
33 * in order to be able to deal with missing data.
34 *
35 * \author Charles Vaske
36 */
37 class Evidence {
38 private:
39 /// Each sample is an observed joint state of some variables
40 std::vector<Observation> _samples;
41
42 public:
43 /// Default constructor
44 Evidence() : _samples() {}
45
46 /// Construct from \a samples
47 Evidence( std::vector<Observation> &samples ) : _samples(samples) {}
48
49 /// Read in tabular data from a stream and add the read samples to \c *this.
50 /** \param is Input stream in .tab file format, describing joint observations of variables in \a fg
51 * \param fg Factor graph describing the corresponding variables
52 * \see \ref fileformats-evidence
53 */
54 void addEvidenceTabFile( std::istream& is, FactorGraph& fg );
55
56 /// Returns number of stored samples
57 size_t nrSamples() const { return _samples.size(); }
58
59 /// \name Iterator interface
60 //@{
61 /// Iterator over the samples
62 typedef std::vector<Observation>::iterator iterator;
63 /// Constant iterator over the samples
64 typedef std::vector<Observation>::const_iterator const_iterator;
65
66 /// Returns iterator that points to the first sample
67 iterator begin() { return _samples.begin(); }
68 /// Returns constant iterator that points to the first sample
69 const_iterator begin() const { return _samples.begin(); }
70 /// Returns iterator that points beyond the last sample
71 iterator end() { return _samples.end(); }
72 /// Returns constant iterator that points beyond the last sample
73 const_iterator end() const { return _samples.end(); }
74 //@}
75
76 private:
77 /// Read in tabular data from a stream and add the read samples to \c *this.
78 void addEvidenceTabFile( std::istream& is, std::map<std::string, Var> &varMap );
79 };
80
81
82 } // end of namespace dai
83
84
85 #endif