aecddb6d413c00b9532d4586574171712ed20974
[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 classes Evidence and Observation
14 * \todo Describe tabular data file format
15 * \todo Improve documentation
16 * \author Charles Vaske
17 */
18
19
20 #ifndef __defined_libdai_evidence_h
21 #define __defined_libdai_evidence_h
22
23
24 #include <istream>
25 #include <dai/daialg.h>
26
27
28 namespace dai {
29
30
31 /// Stores observed values of a subset of variables
32 /** \author Charles Vaske
33 */
34 class Observation {
35 private:
36 /// Used to store the state of some variables
37 std::map<Var, size_t> _obs;
38
39 public:
40 /// Default constructor
41 Observation() : _obs() {}
42
43 /// Get all observations
44 const std::map<Var, size_t>& observations() const { return _obs; }
45
46 /// Add an observation
47 void addObservation( Var node, size_t setting );
48
49 /// Clamp variables in the graphical model to their observed values
50 void applyEvidence( InfAlg& alg ) const;
51 };
52
53
54 /// Stores multiple joint observations of sets of variables.
55 /** The Evidence class stores multiple samples, where each sample is the joint
56 * observation of the states of some variables.
57 *
58 * \author Charles Vaske
59 */
60 class Evidence {
61 private:
62 /// Each sample is the joint observation of the states of some variables
63 std::vector<Observation> _samples;
64
65 public:
66 /// Default constructor
67 Evidence() : _samples() {}
68
69 /// Construct from existing samples
70 Evidence( std::vector<Observation> &samples ) : _samples(samples) {}
71
72 /// Read in tabular data from a stream.
73 /** Each line contains one sample, and the first line is a header line with names.
74 */
75 void addEvidenceTabFile( std::istream& is, std::map<std::string, Var> &varMap );
76
77 /// Read in tabular data from a stream.
78 /** Each line contains one sample, and the first line is a header line with
79 * variable labels which should correspond with a subset of the variables in fg.
80 */
81 void addEvidenceTabFile( std::istream& is, FactorGraph& fg );
82
83 /// Returns number of stored samples
84 size_t nrSamples() const { return _samples.size(); }
85
86 /// \name Iterator interface
87 //@{
88 /// Iterator over the elements
89 typedef std::vector<Observation>::iterator iterator;
90 /// Constant iterator over the elements
91 typedef std::vector<Observation>::const_iterator const_iterator;
92
93 /// Returns iterator that points to the first element
94 iterator begin() { return _samples.begin(); }
95 /// Returns constant iterator that points to the first element
96 const_iterator begin() const { return _samples.begin(); }
97 /// Returns iterator that points beyond the last element
98 iterator end() { return _samples.end(); }
99 /// Returns constant iterator that points beyond the last element
100 const_iterator end() const { return _samples.end(); }
101 //@}
102 };
103
104
105 } // end of namespace dai
106
107
108 #endif