1 /* Copyright (C) 2009 Charles Vaske [cvaske at soe dot ucsc dot edu]
2 University of California Santa Cruz
4 This file is part of libDAI.
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.
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.
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
23 /// \brief Defines classes Evidence and Observation
24 /// \todo Describe tabular data file format
27 #ifndef __defined_libdai_evidence_h
28 #define __defined_libdai_evidence_h
32 #include <dai/daialg.h>
38 /// Stores observed values of a subset of variables
41 /// Used to store the state of some variables
42 std::map
<Var
, size_t> _obs
;
45 /// Default constructor
46 Observation() : _obs() {}
48 /// Get all observations
49 const std::map
<Var
, size_t>& observations() const { return _obs
; }
51 /// Add an observation
52 void addObservation( Var node
, size_t setting
);
54 /// Clamp variables in the graphical model to their observed values
55 void applyEvidence( InfAlg
& alg
) const;
59 /// Stores multiple joint observations of sets of variables.
60 /** The Evidence class stores multiple samples, where each sample is the joint
61 * observation of the states of some variables.
65 /// Each sample is the joint observation of the states of some variables
66 std::vector
<Observation
> _samples
;
69 /// Default constructor
70 Evidence() : _samples() {}
72 /// Constructor with existing samples
73 Evidence(std::vector
<Observation
>& samples
) : _samples(samples
) {}
75 /// Read in tabular data from a stream.
76 /** Each line contains one sample, and the first line is a header line with names.
78 void addEvidenceTabFile( std::istream
& is
, std::map
<std::string
, Var
> &varMap
);
81 /// Read in tabular data from a stream.
82 /** Each line contains one sample, and the first line is a header line with
83 * variable labels which should correspond with a subset of the variables in fg.
85 void addEvidenceTabFile( std::istream
& is
, FactorGraph
& fg
);
87 /// Returns number of stored samples
88 size_t nrSamples() const { return _samples
.size(); }
90 /// @name Iterator interface
92 /// Iterator over the elements
93 typedef std::vector
<Observation
>::iterator iterator
;
94 /// Constant iterator over the elements
95 typedef std::vector
<Observation
>::const_iterator const_iterator
;
97 /// Returns iterator that points to the first element
98 iterator
begin() { return _samples
.begin(); }
99 /// Returns constant iterator that points to the first element
100 const_iterator
begin() const { return _samples
.begin(); }
101 /// Returns iterator that points beyond the last element
102 iterator
end() { return _samples
.end(); }
103 /// Returns constant iterator that points beyond the last element
104 const_iterator
end() const { return _samples
.end(); }
109 } // end of namespace dai