MCPcopy Create free account
hub / github.com/CyberPoint/libpgm / SampleAggregator

Class SampleAggregator

libpgm/sampleaggregator.py:31–108  ·  view source on GitHub ↗

This class is a machine for aggregating data from sample sequences. It contains the method *aggregate*.

Source from the content-addressed store, hash-verified

29
30
31class SampleAggregator(object):
32 '''
33 This class is a machine for aggregating data from sample sequences. It contains the method *aggregate*.
34
35 '''
36 def __init__(self):
37 self.seq = None
38 '''The sequence inputted.'''
39 self.avg = None
40 '''The average of all the entries in *seq*, represented as a dict where each vertex has an entry whose value is a dict of {key, value} pairs, where each key is a possible outcome of that vertex and its value is the approximate frequency.'''
41
42
43 def aggregate(self, samplerstatement):
44 '''
45 Generate a sequence of samples using *samplerstatement* and return the average of its results.
46
47 Arguments:
48 1. *samplerstatement* -- The statement of a function (with inputs) that would output a sequence of samples. For example: ``bn.randomsample(50)`` where ``bn`` is an instance of the :doc:`DiscreteBayesianNetwork <discretebayesiannetwork>` class.
49
50 This function stores the output of *samplerstatement* in the attribute *seq*, and then averages *seq* and stores the approximate distribution found in the attribute *avg*. It then returns *avg*.
51
52 Usage example: this would print the average of 10 data points::
53
54 import json
55
56 from libpgm.nodedata import NodeData
57 from libpgm.graphskeleton import GraphSkeleton
58 from libpgm.discretebayesiannetwork import DiscreteBayesianNetwork
59 from libpgm.sampleaggregator import SampleAggregator
60
61 # load nodedata and graphskeleton
62 nd = NodeData()
63 skel = GraphSkeleton()
64 nd.load("../tests/unittestdict.txt")
65 skel.load("../tests/unittestdict.txt")
66
67 # topologically order graphskeleton
68 skel.toporder()
69
70 # load bayesian network
71 bn = DiscreteBayesianNetwork(skel, nd)
72
73 # build aggregator
74 agg = SampleAggregator()
75
76 # average samples
77 result = agg.aggregate(bn.randomsample(10))
78
79 # output
80 print json.dumps(result, indent=2)
81
82 ''&#x27;
83
84 # get sequence
85 seq = samplerstatement
86
87 # denominator
88 denom = len(seq)

Callers 2

setUpMethod · 0.90
examples.pyFile · 0.90

Calls

no outgoing calls

Tested by 1

setUpMethod · 0.72