1 /* This file is part of libDAI - http://www.libdai.org/
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.
7 * Copyright (C) 2009 Charles Vaske [cvaske at soe dot ucsc dot edu]
8 * Copyright (C) 2009 University of California, Santa Cruz
13 /// \brief Defines classes Evidence and Observation
14 /// \todo Describe tabular data file format
17 #ifndef __defined_libdai_evidence_h
18 #define __defined_libdai_evidence_h
22 #include <dai/daialg.h>
28 /// Stores observed values of a subset of variables
31 /// Used to store the state of some variables
32 std::map
<Var
, size_t> _obs
;
35 /// Default constructor
36 Observation() : _obs() {}
38 /// Get all observations
39 const std::map
<Var
, size_t>& observations() const { return _obs
; }
41 /// Add an observation
42 void addObservation( Var node
, size_t setting
);
44 /// Clamp variables in the graphical model to their observed values
45 void applyEvidence( InfAlg
& alg
) const;
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.
55 /// Each sample is the joint observation of the states of some variables
56 std::vector
<Observation
> _samples
;
59 /// Default constructor
60 Evidence() : _samples() {}
62 /// Construct from existing samples
63 Evidence( std::vector
<Observation
> &samples
) : _samples(samples
) {}
65 /// Read in tabular data from a stream.
66 /** Each line contains one sample, and the first line is a header line with names.
68 void addEvidenceTabFile( std::istream
& is
, std::map
<std::string
, Var
> &varMap
);
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.
74 void addEvidenceTabFile( std::istream
& is
, FactorGraph
& fg
);
76 /// Returns number of stored samples
77 size_t nrSamples() const { return _samples
.size(); }
79 /// @name Iterator interface
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
;
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(); }
98 } // end of namespace dai