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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9501e7b169bda69caf717e26a63ba141fbfcb413607f8888cd62a66eaf3ab78c
4
- data.tar.gz: 4ca8a547e3f71d4b5690a73ae4430700a7115f97bbee974974da094701e192b3
3
+ metadata.gz: 9f17178cdfa472aea14e7769f1831f1903ceee2613b6e7b71e1135990dc974ab
4
+ data.tar.gz: c55cb95134d4dbb479e89839be7a4634b6bc6a8be52c5c066f1cc1ad21c584a7
5
5
  SHA512:
6
- metadata.gz: 11c61de5da370ee1a5b75014c1d74644d8b38cb45a5daa81e2292efac233beaf35ee85059074b09296d7d3ce842e67353921fbcf54f92109c5ace3b09459159a
7
- data.tar.gz: 9abb2ee0bcd8661546b6ca3e27f9a3d3f96a2679c5b6b60e06ea7e26b759542f043f39a8fa7dc1de8270032b9485fc531b23a0895ca2001119a357de13797216
6
+ metadata.gz: fda426d9dd319b2ad8300a8ba59acae511740cc271729f40b36ce7a7765d8019a901d9d83a2e955ccd964901ee604a3ad96fba092ce41394da5980a77d1bb198
7
+ data.tar.gz: 03d4beb66b11ba684007dd49f67ef2ae085ecd5c6e5ca9b8b63327eee109ada3b1f389731543fa6d970bff110230231bc1533022640367a906f57dec1c41bc6d
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.0.3
1
+ 3.1.0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.1.0] - 2021-12-18
3
+ ## [0.0.2] - 2021-12-28
4
+
5
+ - README, CI/CD for Ruby 2.6, 2.7, 3.1 added
6
+
7
+ ## [0.0.1] - 2021-12-28
4
8
 
5
9
  - Initial release
10
+
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bayesnet (0.0.1)
4
+ bayesnet (0.0.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,8 +1,70 @@
1
1
  # Bayesnet
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/bayesnet`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ This gem provides an API for building a Bayesian network and executing some queries against it.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
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
+ ![model](./doc/morning-mood-model.png "Morning Mood Model")
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/main/CHANGELOG.md"
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
@@ -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
- vars = @vars.except(*evidence.keys)
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] }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bayesnet
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
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.1
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/main/CHANGELOG.md
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.2.32
118
+ rubygems_version: 3.3.3
118
119
  signing_key:
119
120
  specification_version: 4
120
121
  summary: Byesian network DSL + inference API