Polished EM code
[libdai.git] / include / dai / evidence.h
1 /* Copyright (C) 2009 Charles Vaske [cvaske at soe dot ucsc dot edu]
2 University of California Santa Cruz
3
4 This file is part of libDAI.
5
6 libDAI is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21
22 /// \file
23 /// \brief Defines classes Evidence and Observation
24 /// \todo Improve documentation
25
26
27 #ifndef __defined_libdai_evidence_h
28 #define __defined_libdai_evidence_h
29
30
31 #include <istream>
32 #include <dai/daialg.h>
33
34
35 namespace dai {
36
37
38 /// Stores observed values of a subset of variables
39 class Observation {
40 private:
41 std::map<Var, size_t> _obs;
42
43 public:
44 /// Default constructor
45 Observation() : _obs() {}
46 /// Get all observations
47 const std::map<Var, size_t>& observations() const { return _obs; }
48 /// Add an observation
49 void addObservation( Var node, size_t setting );
50 /// Clamp variables to observed values
51 void applyEvidence( InfAlg& alg ) const;
52 };
53
54
55 /// Stores multiple observations of variables
56 /** The Evidence class contains multiple samples, where a sample is the joint
57 * observation of the states of some variables.
58 */
59 class Evidence {
60 private:
61 std::vector<Observation> _samples;
62
63 public:
64 /// Default constructor
65 Evidence() : _samples() {}
66
67 /** Read in tab-data from a stream. Each line contains one sample, and
68 * the first line is a header line with names.
69 */
70 void addEvidenceTabFile(std::istream& is, std::map<std::string, Var>& varMap);
71
72 /** Read in tab-data from a stream. Each line contains one sample,
73 * and the first line is a header line with variable labels.
74 */
75 void addEvidenceTabFile(std::istream& is, FactorGraph& fg);
76
77 /// Returns total number of samples
78 size_t nrSamples() const { return _samples.size(); }
79
80 /// @name iterator interface
81 //@{
82 typedef std::vector<Observation>::iterator iterator;
83 typedef std::vector<Observation>::const_iterator const_iterator;
84
85 iterator begin() { return _samples.begin(); }
86 const_iterator begin() const { return _samples.begin(); }
87 iterator end() { return _samples.end(); }
88 const_iterator end() const { return _samples.end(); }
89 //@}
90 };
91
92
93 } // end of namespace dai
94
95
96 #endif