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