bayesnet 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/CHANGELOG.md +6 -1
- data/Gemfile.lock +1 -1
- data/README.md +64 -5
- data/bayesnet.gemspec +1 -1
- data/doc/morning-mood-model.png +0 -0
- data/lib/bayesnet/factor.rb +4 -2
- data/lib/bayesnet/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f17178cdfa472aea14e7769f1831f1903ceee2613b6e7b71e1135990dc974ab
|
4
|
+
data.tar.gz: c55cb95134d4dbb479e89839be7a4634b6bc6a8be52c5c066f1cc1ad21c584a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fda426d9dd319b2ad8300a8ba59acae511740cc271729f40b36ce7a7765d8019a901d9d83a2e955ccd964901ee604a3ad96fba092ce41394da5980a77d1bb198
|
7
|
+
data.tar.gz: 03d4beb66b11ba684007dd49f67ef2ae085ecd5c6e5ca9b8b63327eee109ada3b1f389731543fa6d970bff110230231bc1533022640367a906f57dec1c41bc6d
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
3.0
|
1
|
+
3.1.0
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,70 @@
|
|
1
1
|
# Bayesnet
|
2
2
|
|
3
|
-
|
3
|
+
This gem provides an API for building a Bayesian network and executing some queries against it.
|
4
4
|
|
5
|
-
|
5
|
+
### Example:
|
6
|
+
|
7
|
+
Someone has decided to study how some sleep hours followed by a cup of coffee are affecting a morning mood.
|
8
|
+
Here is the graphical model:
|
9
|
+
|
10
|
+

|
11
|
+
|
12
|
+
`Sleep hours` could have values `:six, :seven, :eight`. `Mood` could be `:bad, :moderate, :good` and variable
|
13
|
+
`Coffee` could take values `:yes, :no`. Here is how accumulated statistics could be compiled in a Bayesian network and then defined in the code:
|
14
|
+
|
15
|
+
```
|
16
|
+
net = Bayesnet.define do
|
17
|
+
node :coffee do
|
18
|
+
values yes: 0.7, no: 0.3
|
19
|
+
end
|
20
|
+
|
21
|
+
node :sleep_hours do
|
22
|
+
values six: 0.1, seven: 0.3, eight: 0.6
|
23
|
+
end
|
24
|
+
|
25
|
+
node :mood, parents: [:coffee, :sleep_hours] do
|
26
|
+
values [:good, :moderate, :bad] do
|
27
|
+
distributions do
|
28
|
+
as [0.8, 0.1, 0.1], given: [:yes, :eight]
|
29
|
+
as [0.6, 0.2, 0.2], given: [:yes, :seven]
|
30
|
+
as [0.4, 0.4, 0.2], given: [:yes, :six]
|
31
|
+
as [0.7, 0.2, 0.1], given: [:no, :eight]
|
32
|
+
as [0.5, 0.3, 0.2], given: [:no, :seven]
|
33
|
+
as [0.3, 0.4, 0.3], given: [:no, :six]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
Above, must be read as:
|
41
|
+
someone could be observed having morning coffee in 70% of all cases and when that person has a morning coffee after sleeping 8 hours, chances to find her in
|
42
|
+
- `:good` mood - 80%
|
43
|
+
- `:moderate` mood - 10%
|
44
|
+
- `:bad` mood - 10%
|
45
|
+
|
46
|
+
i.e. `[0.8, 0.1, 0.1]` is a conditional distribution.
|
47
|
+
|
48
|
+
Given the model above the following queries could be executed:
|
49
|
+
|
50
|
+
#### Given sleep time is six hours and mood is good, what chances cup of coffee has been consumed?
|
51
|
+
```
|
52
|
+
net.chances({coffee: :yes}, evidence: {mood: :good, sleep_hours: :six}) # 0.757
|
53
|
+
```
|
54
|
+
|
55
|
+
#### Given sleep time is six hours and mood is good, what is the most likely value for a `:coffee` variable?
|
56
|
+
```
|
57
|
+
net.most_likely_value(:coffee, evidence: {mood: :good, sleep_hours: :six}) # :yes
|
58
|
+
```
|
59
|
+
|
60
|
+
#### A broader question than the two above: Given sleep time is six hours and mood is good, what is the distribution for a `:coffee` variable?
|
61
|
+
```
|
62
|
+
net.distribution(over: [:coffee], evidence: {mood: :good, sleep_hours: :six}) # [:yes, 0.757], [:no, 0.243]
|
63
|
+
```
|
64
|
+
|
65
|
+
The inference is based on summing over joint distribution, i.e. it is the simplest and
|
66
|
+
most expensive way to calculate it. No optimization is implemented in this version; the code
|
67
|
+
is more a proof of API.
|
6
68
|
|
7
69
|
## Installation
|
8
70
|
|
@@ -20,9 +82,6 @@ Or install it yourself as:
|
|
20
82
|
|
21
83
|
$ gem install bayesnet
|
22
84
|
|
23
|
-
## Usage
|
24
|
-
|
25
|
-
TODO: Write usage instructions here
|
26
85
|
|
27
86
|
## Development
|
28
87
|
|
data/bayesnet.gemspec
CHANGED
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
|
|
15
15
|
|
16
16
|
spec.metadata["homepage_uri"] = spec.homepage
|
17
17
|
spec.metadata["source_code_uri"] = spec.homepage
|
18
|
-
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/
|
18
|
+
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/master/CHANGELOG.md"
|
19
19
|
|
20
20
|
# Specify which files should be added to the gem when it is released.
|
21
21
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
Binary file
|
data/lib/bayesnet/factor.rb
CHANGED
@@ -15,7 +15,7 @@ module Bayesnet
|
|
15
15
|
# Specifies function values for args. Latest args is an function value, all previous are argument values
|
16
16
|
def val(*args)
|
17
17
|
args = args[0] if args.size == 1 && args[0].is_a?(Array)
|
18
|
-
@vals[args[..-2]] = args[-1]
|
18
|
+
@vals[args[0..-2]] = args[-1]
|
19
19
|
end
|
20
20
|
|
21
21
|
def var_names
|
@@ -52,7 +52,9 @@ module Bayesnet
|
|
52
52
|
end
|
53
53
|
|
54
54
|
def limit_by(evidence)
|
55
|
-
|
55
|
+
# todo: use Hash#except when Ruby 2.6 support no longer needed
|
56
|
+
evidence_keys_set = evidence.keys.to_set
|
57
|
+
vars = @vars.reject { |k, _| evidence_keys_set.include?(k) }
|
56
58
|
|
57
59
|
evidence_vals = evidence.values
|
58
60
|
indices = evidence.keys.map { |k| index_by_var_name[k] }
|
data/lib/bayesnet/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bayesnet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aleksandr Furmanov
|
@@ -85,6 +85,7 @@ files:
|
|
85
85
|
- bayesnet.gemspec
|
86
86
|
- bin/console
|
87
87
|
- bin/setup
|
88
|
+
- doc/morning-mood-model.png
|
88
89
|
- lib/bayesnet.rb
|
89
90
|
- lib/bayesnet/dsl.rb
|
90
91
|
- lib/bayesnet/error.rb
|
@@ -98,7 +99,7 @@ licenses:
|
|
98
99
|
metadata:
|
99
100
|
homepage_uri: https://github.com/afurmanov/bayesnet
|
100
101
|
source_code_uri: https://github.com/afurmanov/bayesnet
|
101
|
-
changelog_uri: https://github.com/afurmanov/bayesnet/blob/
|
102
|
+
changelog_uri: https://github.com/afurmanov/bayesnet/blob/master/CHANGELOG.md
|
102
103
|
post_install_message:
|
103
104
|
rdoc_options: []
|
104
105
|
require_paths:
|
@@ -114,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
114
115
|
- !ruby/object:Gem::Version
|
115
116
|
version: '0'
|
116
117
|
requirements: []
|
117
|
-
rubygems_version: 3.
|
118
|
+
rubygems_version: 3.3.3
|
118
119
|
signing_key:
|
119
120
|
specification_version: 4
|
120
121
|
summary: Byesian network DSL + inference API
|