First working version of EM
[libdai.git] / include / dai / evidence.h
1 /*
2 Copyright 2009 Charles Vaske <cvaske@soe.ucsc.edu>
3 University of California Santa Cruz
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef __defined_libdai_evidence_h
20 #define __defined_libdai_evidence_h
21
22 #include <istream>
23
24 #include <dai/daialg.h>
25
26 namespace dai {
27
28 /// Store joint observations on a graphical model.
29 class SampleData {
30 private:
31 std::string _name;
32 std::map<Var, size_t> _obs;
33 public:
34 /// Construct an empty object
35 SampleData() : _name(), _obs() {}
36 /// Set the name of the sample
37 void name(const std::string& name) { _name = name; }
38 /// Get the name of the sample
39 const std::string& name() const { return _name; }
40 /// Read from the observation map
41 const std::map<Var, size_t>& observations() const { return _obs; }
42 /// Add an observation
43 void addObservation(Var node, size_t setting);
44 /// Add evidence by clamping variables to observed values.
45 void applyEvidence(InfAlg& alg) const;
46 };
47
48 /// Store observations from a graphical model.
49 class Evidence {
50 private:
51 std::map< std::string, SampleData > _samples;
52 public:
53 /// Start with empty obects, then fill with calls to addEvidenceTabFile()
54 Evidence() : _samples() {}
55
56 /** Read in tab-data from a stream. Each line contains one sample, and
57 * the first line is a header line with names. The first column contains
58 * names for each of the samples.
59 */
60 void addEvidenceTabFile(std::istream& is,
61 std::map< std::string, Var >& varMap);
62
63 /** Read in tab-data from a stream. Each line contains one sample,
64 * and the first line is a header line with variable IDs. The first
65 * column contains names for each of the samples.
66 */
67 void addEvidenceTabFile(std::istream& is, FactorGraph& fg);
68
69 /// Total number of samples in this evidence file
70 size_t nrSamples() const { return _samples.size(); }
71
72 /// @name iterator interface
73 //@{
74 typedef std::map< std::string, SampleData >::iterator iterator;
75 typedef std::map< std::string, SampleData >::const_iterator const_iterator;
76 iterator begin() { return _samples.begin(); }
77 const_iterator begin() const { return _samples.begin(); }
78 iterator end() { return _samples.end(); }
79 const_iterator end() const { return _samples.end(); }
80 //@}
81
82 };
83
84 }
85
86 #endif