compiled-knowledge 4.0.0a20__cp312-cp312-musllinux_1_2_x86_64.whl

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.

Potentially problematic release.


This version of compiled-knowledge might be problematic. Click here for more details.

Files changed (178) hide show
  1. ck/__init__.py +0 -0
  2. ck/circuit/__init__.py +17 -0
  3. ck/circuit/_circuit_cy.c +37520 -0
  4. ck/circuit/_circuit_cy.cpython-312-x86_64-linux-musl.so +0 -0
  5. ck/circuit/_circuit_cy.pxd +32 -0
  6. ck/circuit/_circuit_cy.pyx +768 -0
  7. ck/circuit/_circuit_py.py +836 -0
  8. ck/circuit/tmp_const.py +74 -0
  9. ck/circuit_compiler/__init__.py +2 -0
  10. ck/circuit_compiler/circuit_compiler.py +26 -0
  11. ck/circuit_compiler/cython_vm_compiler/__init__.py +1 -0
  12. ck/circuit_compiler/cython_vm_compiler/_compiler.c +19821 -0
  13. ck/circuit_compiler/cython_vm_compiler/_compiler.cpython-312-x86_64-linux-musl.so +0 -0
  14. ck/circuit_compiler/cython_vm_compiler/_compiler.pyx +380 -0
  15. ck/circuit_compiler/cython_vm_compiler/cython_vm_compiler.py +121 -0
  16. ck/circuit_compiler/interpret_compiler.py +223 -0
  17. ck/circuit_compiler/llvm_compiler.py +388 -0
  18. ck/circuit_compiler/llvm_vm_compiler.py +546 -0
  19. ck/circuit_compiler/named_circuit_compilers.py +57 -0
  20. ck/circuit_compiler/support/__init__.py +0 -0
  21. ck/circuit_compiler/support/circuit_analyser/__init__.py +13 -0
  22. ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_cy.c +10615 -0
  23. ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_cy.cpython-312-x86_64-linux-musl.so +0 -0
  24. ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_cy.pyx +98 -0
  25. ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_py.py +93 -0
  26. ck/circuit_compiler/support/input_vars.py +148 -0
  27. ck/circuit_compiler/support/llvm_ir_function.py +234 -0
  28. ck/example/__init__.py +53 -0
  29. ck/example/alarm.py +366 -0
  30. ck/example/asia.py +28 -0
  31. ck/example/binary_clique.py +32 -0
  32. ck/example/bow_tie.py +33 -0
  33. ck/example/cancer.py +37 -0
  34. ck/example/chain.py +38 -0
  35. ck/example/child.py +199 -0
  36. ck/example/clique.py +33 -0
  37. ck/example/cnf_pgm.py +39 -0
  38. ck/example/diamond_square.py +68 -0
  39. ck/example/earthquake.py +36 -0
  40. ck/example/empty.py +10 -0
  41. ck/example/hailfinder.py +539 -0
  42. ck/example/hepar2.py +628 -0
  43. ck/example/insurance.py +504 -0
  44. ck/example/loop.py +40 -0
  45. ck/example/mildew.py +38161 -0
  46. ck/example/munin.py +22982 -0
  47. ck/example/pathfinder.py +53747 -0
  48. ck/example/rain.py +39 -0
  49. ck/example/rectangle.py +161 -0
  50. ck/example/run.py +30 -0
  51. ck/example/sachs.py +129 -0
  52. ck/example/sprinkler.py +30 -0
  53. ck/example/star.py +44 -0
  54. ck/example/stress.py +64 -0
  55. ck/example/student.py +43 -0
  56. ck/example/survey.py +46 -0
  57. ck/example/triangle_square.py +54 -0
  58. ck/example/truss.py +49 -0
  59. ck/in_out/__init__.py +3 -0
  60. ck/in_out/parse_ace_lmap.py +216 -0
  61. ck/in_out/parse_ace_nnf.py +322 -0
  62. ck/in_out/parse_net.py +480 -0
  63. ck/in_out/parser_utils.py +185 -0
  64. ck/in_out/pgm_pickle.py +42 -0
  65. ck/in_out/pgm_python.py +268 -0
  66. ck/in_out/render_bugs.py +111 -0
  67. ck/in_out/render_net.py +177 -0
  68. ck/in_out/render_pomegranate.py +184 -0
  69. ck/pgm.py +3475 -0
  70. ck/pgm_circuit/__init__.py +1 -0
  71. ck/pgm_circuit/marginals_program.py +352 -0
  72. ck/pgm_circuit/mpe_program.py +237 -0
  73. ck/pgm_circuit/pgm_circuit.py +79 -0
  74. ck/pgm_circuit/program_with_slotmap.py +236 -0
  75. ck/pgm_circuit/slot_map.py +35 -0
  76. ck/pgm_circuit/support/__init__.py +0 -0
  77. ck/pgm_circuit/support/compile_circuit.py +83 -0
  78. ck/pgm_circuit/target_marginals_program.py +103 -0
  79. ck/pgm_circuit/wmc_program.py +323 -0
  80. ck/pgm_compiler/__init__.py +2 -0
  81. ck/pgm_compiler/ace/__init__.py +1 -0
  82. ck/pgm_compiler/ace/ace.py +299 -0
  83. ck/pgm_compiler/factor_elimination.py +395 -0
  84. ck/pgm_compiler/named_pgm_compilers.py +63 -0
  85. ck/pgm_compiler/pgm_compiler.py +19 -0
  86. ck/pgm_compiler/recursive_conditioning.py +231 -0
  87. ck/pgm_compiler/support/__init__.py +0 -0
  88. ck/pgm_compiler/support/circuit_table/__init__.py +17 -0
  89. ck/pgm_compiler/support/circuit_table/_circuit_table_cy.c +16393 -0
  90. ck/pgm_compiler/support/circuit_table/_circuit_table_cy.cpython-312-x86_64-linux-musl.so +0 -0
  91. ck/pgm_compiler/support/circuit_table/_circuit_table_cy.pyx +332 -0
  92. ck/pgm_compiler/support/circuit_table/_circuit_table_py.py +304 -0
  93. ck/pgm_compiler/support/clusters.py +568 -0
  94. ck/pgm_compiler/support/factor_tables.py +406 -0
  95. ck/pgm_compiler/support/join_tree.py +332 -0
  96. ck/pgm_compiler/support/named_compiler_maker.py +43 -0
  97. ck/pgm_compiler/variable_elimination.py +91 -0
  98. ck/probability/__init__.py +0 -0
  99. ck/probability/empirical_probability_space.py +50 -0
  100. ck/probability/pgm_probability_space.py +32 -0
  101. ck/probability/probability_space.py +622 -0
  102. ck/program/__init__.py +3 -0
  103. ck/program/program.py +137 -0
  104. ck/program/program_buffer.py +180 -0
  105. ck/program/raw_program.py +67 -0
  106. ck/sampling/__init__.py +0 -0
  107. ck/sampling/forward_sampler.py +211 -0
  108. ck/sampling/marginals_direct_sampler.py +113 -0
  109. ck/sampling/sampler.py +62 -0
  110. ck/sampling/sampler_support.py +232 -0
  111. ck/sampling/uniform_sampler.py +72 -0
  112. ck/sampling/wmc_direct_sampler.py +171 -0
  113. ck/sampling/wmc_gibbs_sampler.py +153 -0
  114. ck/sampling/wmc_metropolis_sampler.py +165 -0
  115. ck/sampling/wmc_rejection_sampler.py +115 -0
  116. ck/utils/__init__.py +0 -0
  117. ck/utils/iter_extras.py +163 -0
  118. ck/utils/local_config.py +270 -0
  119. ck/utils/map_list.py +128 -0
  120. ck/utils/map_set.py +128 -0
  121. ck/utils/np_extras.py +51 -0
  122. ck/utils/random_extras.py +64 -0
  123. ck/utils/tmp_dir.py +94 -0
  124. ck_demos/__init__.py +0 -0
  125. ck_demos/ace/__init__.py +0 -0
  126. ck_demos/ace/copy_ace_to_ck.py +15 -0
  127. ck_demos/ace/demo_ace.py +49 -0
  128. ck_demos/all_demos.py +88 -0
  129. ck_demos/circuit/__init__.py +0 -0
  130. ck_demos/circuit/demo_circuit_dump.py +22 -0
  131. ck_demos/circuit/demo_derivatives.py +43 -0
  132. ck_demos/circuit_compiler/__init__.py +0 -0
  133. ck_demos/circuit_compiler/compare_circuit_compilers.py +32 -0
  134. ck_demos/circuit_compiler/show_llvm_program.py +26 -0
  135. ck_demos/pgm/__init__.py +0 -0
  136. ck_demos/pgm/demo_pgm_dump.py +18 -0
  137. ck_demos/pgm/demo_pgm_dump_stress.py +18 -0
  138. ck_demos/pgm/demo_pgm_string_rendering.py +15 -0
  139. ck_demos/pgm/show_examples.py +25 -0
  140. ck_demos/pgm_compiler/__init__.py +0 -0
  141. ck_demos/pgm_compiler/compare_pgm_compilers.py +63 -0
  142. ck_demos/pgm_compiler/demo_compiler_dump.py +60 -0
  143. ck_demos/pgm_compiler/demo_factor_elimination.py +47 -0
  144. ck_demos/pgm_compiler/demo_join_tree.py +25 -0
  145. ck_demos/pgm_compiler/demo_marginals_program.py +53 -0
  146. ck_demos/pgm_compiler/demo_mpe_program.py +55 -0
  147. ck_demos/pgm_compiler/demo_pgm_compiler.py +38 -0
  148. ck_demos/pgm_compiler/demo_recursive_conditioning.py +33 -0
  149. ck_demos/pgm_compiler/demo_variable_elimination.py +33 -0
  150. ck_demos/pgm_compiler/demo_wmc_program.py +29 -0
  151. ck_demos/pgm_compiler/time_fe_compiler.py +93 -0
  152. ck_demos/pgm_inference/__init__.py +0 -0
  153. ck_demos/pgm_inference/demo_inferencing_basic.py +188 -0
  154. ck_demos/pgm_inference/demo_inferencing_mpe_cancer.py +45 -0
  155. ck_demos/pgm_inference/demo_inferencing_wmc_and_mpe_sprinkler.py +154 -0
  156. ck_demos/pgm_inference/demo_inferencing_wmc_student.py +110 -0
  157. ck_demos/programs/__init__.py +0 -0
  158. ck_demos/programs/demo_program_buffer.py +24 -0
  159. ck_demos/programs/demo_program_multi.py +24 -0
  160. ck_demos/programs/demo_program_none.py +19 -0
  161. ck_demos/programs/demo_program_single.py +23 -0
  162. ck_demos/programs/demo_raw_program_interpreted.py +21 -0
  163. ck_demos/programs/demo_raw_program_llvm.py +21 -0
  164. ck_demos/sampling/__init__.py +0 -0
  165. ck_demos/sampling/check_sampler.py +71 -0
  166. ck_demos/sampling/demo_marginal_direct_sampler.py +40 -0
  167. ck_demos/sampling/demo_uniform_sampler.py +38 -0
  168. ck_demos/sampling/demo_wmc_direct_sampler.py +40 -0
  169. ck_demos/utils/__init__.py +0 -0
  170. ck_demos/utils/compare.py +120 -0
  171. ck_demos/utils/convert_network.py +45 -0
  172. ck_demos/utils/sample_model.py +216 -0
  173. ck_demos/utils/stop_watch.py +384 -0
  174. compiled_knowledge-4.0.0a20.dist-info/METADATA +50 -0
  175. compiled_knowledge-4.0.0a20.dist-info/RECORD +178 -0
  176. compiled_knowledge-4.0.0a20.dist-info/WHEEL +5 -0
  177. compiled_knowledge-4.0.0a20.dist-info/licenses/LICENSE.txt +21 -0
  178. compiled_knowledge-4.0.0a20.dist-info/top_level.txt +2 -0
ck/example/rain.py ADDED
@@ -0,0 +1,39 @@
1
+ from ck.pgm import PGM
2
+
3
+
4
+ class Rain(PGM):
5
+ """
6
+ This PGM is the pedagogical 'Rain' Bayesian network.
7
+ See Adnan Darwiche, 2009, Modeling and Reasoning with Bayesian Networks, p127.
8
+ """
9
+
10
+ def __init__(self):
11
+ super().__init__(self.__class__.__name__)
12
+
13
+ a = self.new_rv('Winter', (True, False))
14
+ b = self.new_rv('Sprinkler', (True, False))
15
+ c = self.new_rv('Rain', (True, False))
16
+ d = self.new_rv('Wet Grass', (True, False))
17
+ e = self.new_rv('Slippery Road', (True, False))
18
+
19
+ f_a = self.new_factor(a).set_cpt()
20
+ f_ba = self.new_factor(b, a).set_cpt()
21
+ f_ca = self.new_factor(c, a).set_cpt()
22
+ f_dbc = self.new_factor(d, b, c).set_cpt()
23
+ f_ec = self.new_factor(e, c).set_cpt()
24
+
25
+ f_a.set_cpd((), (0.6, 0.4))
26
+
27
+ f_ba.set_cpd(0, (0.2, 0.8))
28
+ f_ba.set_cpd(1, (0.75, 0.25))
29
+
30
+ f_ca.set_cpd(0, (0.8, 0.2))
31
+ f_ca.set_cpd(1, (0.1, 0.9))
32
+
33
+ f_dbc.set_cpd((0, 0), (0.95, 0.05))
34
+ f_dbc.set_cpd((0, 1), (0.9, 0.1))
35
+ f_dbc.set_cpd((1, 0), (0.8, 0.2))
36
+ f_dbc.set_cpd((1, 1), (0, 1))
37
+
38
+ f_ec.set_cpd(0, (0.7, 0.3))
39
+ f_ec.set_cpd(1, (0, 1))
@@ -0,0 +1,161 @@
1
+ import random as _random
2
+ from typing import Callable
3
+
4
+ import numpy as _np
5
+
6
+ from ck.pgm import PGM
7
+
8
+
9
+ class Rectangle(PGM):
10
+ """
11
+ This PGM is an interpretation of the 'rectangle' Bayesian network as discussed
12
+ in "An Advance on Variable Elimination with Applications to Tensor-Based Computation",
13
+ Adnan Darwiche, ECAI-2020.
14
+ """
15
+
16
+ def __init__(
17
+ self,
18
+ n: int = 8,
19
+ duplicate_functions: int = 1,
20
+ extend_label: bool = False,
21
+ random_seed: int = 129837697,
22
+ ):
23
+ """
24
+ Args:
25
+ n: is the image size (n * n pixels).
26
+ extend_label: if true extends the rectangle label to 'tall', 'wide', 'square', 'none'.
27
+ duplicate_functions: is the multiplier for duplicates of the functional CPTs.
28
+ Its minimum is 1 (no duplication) and maximum is n (fully duplicated as per Darwiche 2020).
29
+ If 'duplicate_functions' is outside this range, it will be clipped to the extreme (1 or n).
30
+ If 'duplicate_functions' is 'full' then it will be set to the maximum, n.
31
+ """
32
+ params = (n, duplicate_functions, extend_label)
33
+ super().__init__(f'{self.__class__.__name__}({",".join(str(param) for param in params)})')
34
+
35
+ duplicate_functions = n if duplicate_functions == 'full' else duplicate_functions
36
+ duplicate_functions = max(1, min(duplicate_functions, n)) # clip value
37
+ start_duplicate_functions = n - duplicate_functions + 1
38
+
39
+ name = f'rectangle_{n}x{n}_dup({duplicate_functions})'
40
+ if extend_label:
41
+ name += '(extend_label)'
42
+
43
+ random_stream = _random.Random(random_seed).random
44
+ rand_iter = iter(random_stream, None)
45
+
46
+ rect_row = self.new_rv('rect_row', n)
47
+ rect_col = self.new_rv('rect_col', n)
48
+ rect_height = self.new_rv('rect_height', n)
49
+ rect_width = self.new_rv('rect_width', n)
50
+ if extend_label:
51
+ rect_label = self.new_rv('label', ['tall', 'wide', 'square', 'none'])
52
+ else:
53
+ rect_label = self.new_rv('label', ['tall', 'wide'])
54
+ rows = [self.new_rv(f'row_{i}', 2) for i in range(n)]
55
+ cols = [self.new_rv(f'col_{i}', 2) for i in range(n)]
56
+ dup_rows = [[]] * n
57
+ dup_cols = [[]] * n
58
+ for k in range(n):
59
+ if k < start_duplicate_functions:
60
+ dup_rows[k] = rows
61
+ dup_cols[k] = cols
62
+ else:
63
+ # Each rows[i] and cols[i] RV will be the child of a functional CPT.
64
+ # and there children will be all pixs[i,j]
65
+ # In Darwiche 2020, functional CPTs get duplicated for each child
66
+ dup_rows[k] = [self.new_rv(f'row_{i}_dup_{k}', 2) for i in range(n)]
67
+ dup_cols[k] = [self.new_rv(f'col_{i}_dup_{k}', 2) for i in range(n)]
68
+
69
+ pixs = [[self.new_rv(f'pix_{i},{j}', 2) for j in range(n)] for i in range(n)]
70
+
71
+ self.new_factor(rect_row).set_dense().set_iter(rand_iter)
72
+ self.new_factor(rect_col).set_dense().set_iter(rand_iter)
73
+
74
+ f_height = self.new_factor(rect_height, rect_row).set_sparse()
75
+ for row in range(len(rect_row)):
76
+ max_height = n - row
77
+ if max_height > 0:
78
+ cpd = _random_cpd(max_height - 1, 0, random_stream) # -1 as height cannot be zero
79
+ for height in range(1, max_height):
80
+ f_height[height, row] = cpd.item(height - 1)
81
+
82
+ f_width = self.new_factor(rect_width, rect_col).set_sparse()
83
+ for col in range(len(rect_col)):
84
+ max_width = n - col
85
+ if max_width > 0:
86
+ cpd = _random_cpd(max_width - 1, 0, random_stream) # -1 as width cannot be zero
87
+ for width in range(1, max_width):
88
+ f_width[width, col] = cpd.item(width - 1)
89
+
90
+ # functional relationship: rect_label <- rect_height, rect_width
91
+ f_label = self.new_factor(rect_label, rect_height, rect_width).set_sparse()
92
+ if extend_label:
93
+ for height in range(len(rect_height)):
94
+ for width in range(len(rect_width)):
95
+ if height == 0 or width == 0:
96
+ label = 3 # none
97
+ elif height > width:
98
+ label = 0 # tall
99
+ elif height < width:
100
+ label = 1 # wide
101
+ else:
102
+ label = 2 # square
103
+ f_label[label, height, width] = 1
104
+ else:
105
+ for height in range(len(rect_height)):
106
+ for width in range(len(rect_width)):
107
+ label = 0 if height > width else 1
108
+ f_label[label, height, width] = 1
109
+
110
+ # functional relationship: rows[i] <- rect_row, rect_height
111
+ f_row = []
112
+ for i in range(n):
113
+ f_row_i = self.new_factor(rows[i], rect_row, rect_height).set_sparse()
114
+ f_row.append(f_row_i)
115
+ for row in range(len(rect_row)):
116
+ for height in range(len(rect_height)):
117
+ if row <= i < row + height:
118
+ f_row_i[1, row, height] = 1
119
+ else:
120
+ f_row_i[0, row, height] = 1
121
+
122
+ # functional relationship: cols[i] <- rect_col, rect_width
123
+ f_col = []
124
+ for i in range(n):
125
+ f_col_i = self.new_factor(cols[i], rect_col, rect_width).set_sparse()
126
+ f_col.append(f_col_i)
127
+ for col in range(len(rect_col)):
128
+ for width in range(len(rect_width)):
129
+ if col <= i < col + width:
130
+ f_col_i[1, col, width] = 1
131
+ else:
132
+ f_col_i[0, col, width] = 1
133
+
134
+ # patch in the potential functions for the duplicate factors
135
+ for k in range(start_duplicate_functions, n):
136
+ for i in range(n):
137
+ self.new_factor(dup_rows[k][i], rect_row, rect_height).function = f_row[i]
138
+ self.new_factor(dup_cols[k][i], rect_col, rect_width).function = f_col[i]
139
+
140
+ # connect rows and cols to pixels
141
+ for i in range(n):
142
+ for j in range(n):
143
+ self.new_factor(pixs[i][j], dup_rows[j][i], dup_cols[i][j]).set_dense().set_iter(rand_iter)
144
+
145
+
146
+ def _random_cpd(size: int, sparsity: int, random_stream: Callable[[], float]):
147
+ cpd = _np.zeros(size)
148
+ if sparsity <= 0:
149
+ for i in range(len(cpd)):
150
+ cpd[i] = 0.0000001 + random_stream()
151
+ cpd /= _np.sum(cpd)
152
+ else:
153
+ for i in range(len(cpd)):
154
+ if random_stream() <= sparsity:
155
+ cpd[i] = 0
156
+ else:
157
+ cpd[i] = 0.0000001 + random_stream()
158
+ sum_value = _np.sum(cpd)
159
+ if sum_value > 0:
160
+ cpd /= sum_value
161
+ return cpd
ck/example/run.py ADDED
@@ -0,0 +1,30 @@
1
+ import random as _random
2
+
3
+ from ck.pgm import PGM
4
+
5
+
6
+ class Run(PGM):
7
+ """
8
+ This PGM is the 'Run' Bayesian network.
9
+
10
+ The Run Bayesian network is a sequence of random variables, x0, x1, x2, ... .
11
+ The parents of each random variable are all the random variable
12
+ earlier in the sequence.
13
+ """
14
+
15
+ def __init__(
16
+ self,
17
+ vars_per_run: int = 4,
18
+ states_per_var: int = 2,
19
+ sparsity: int = 0,
20
+ random_seed: int = 123456,
21
+ ):
22
+ params = (vars_per_run, states_per_var, sparsity)
23
+ super().__init__(f'{self.__class__.__name__}({",".join(str(param) for param in params)})')
24
+
25
+ random_stream = _random.Random(random_seed).random
26
+ for i in range(vars_per_run):
27
+ self.new_rv(f'x{vars_per_run - i}', states_per_var)
28
+ factor = self.new_factor(*tuple(reversed(self.rvs)))
29
+ cpt = factor.set_cpt()
30
+ cpt.set_random(random_stream, sparsity)
ck/example/sachs.py ADDED
@@ -0,0 +1,129 @@
1
+ from ck.pgm import PGM
2
+
3
+
4
+ class Sachs(PGM):
5
+
6
+ def __init__(self):
7
+ super().__init__(self.__class__.__name__)
8
+
9
+ pgm_rv0 = self.new_rv('Akt', ('1', '2', '3'))
10
+ pgm_rv1 = self.new_rv('Erk', ('1', '2', '3'))
11
+ pgm_rv2 = self.new_rv('Jnk', ('1', '2', '3'))
12
+ pgm_rv3 = self.new_rv('Mek', ('1', '2', '3'))
13
+ pgm_rv4 = self.new_rv('P38', ('1', '2', '3'))
14
+ pgm_rv5 = self.new_rv('PIP2', ('1', '2', '3'))
15
+ pgm_rv6 = self.new_rv('PIP3', ('1', '2', '3'))
16
+ pgm_rv7 = self.new_rv('PKA', ('1', '2', '3'))
17
+ pgm_rv8 = self.new_rv('PKC', ('1', '2', '3'))
18
+ pgm_rv9 = self.new_rv('Plcg', ('1', '2', '3'))
19
+ pgm_rv10 = self.new_rv('Raf', ('1', '2', '3'))
20
+ pgm_factor0 = self.new_factor(pgm_rv0, pgm_rv1, pgm_rv7)
21
+ pgm_factor1 = self.new_factor(pgm_rv1, pgm_rv3, pgm_rv7)
22
+ pgm_factor2 = self.new_factor(pgm_rv2, pgm_rv7, pgm_rv8)
23
+ pgm_factor3 = self.new_factor(pgm_rv3, pgm_rv7, pgm_rv8, pgm_rv10)
24
+ pgm_factor4 = self.new_factor(pgm_rv4, pgm_rv7, pgm_rv8)
25
+ pgm_factor5 = self.new_factor(pgm_rv5, pgm_rv6, pgm_rv9)
26
+ pgm_factor6 = self.new_factor(pgm_rv6, pgm_rv9)
27
+ pgm_factor7 = self.new_factor(pgm_rv7, pgm_rv8)
28
+ pgm_factor8 = self.new_factor(pgm_rv8)
29
+ pgm_factor9 = self.new_factor(pgm_rv9)
30
+ pgm_factor10 = self.new_factor(pgm_rv10, pgm_rv7, pgm_rv8)
31
+
32
+ pgm_function0 = pgm_factor0.set_dense()
33
+ pgm_function0.set_flat(
34
+ 0.6722222, 0.6204819, 0.97674419, 0.3349515, 0.8214285714,
35
+ 0.94852941, 0.0, 0.177083333, 0.1702128, 0.3277778,
36
+ 0.3795181, 0.02325581, 0.6650485, 0.1781954887, 0.05147059,
37
+ 0.1182573, 0.813802083, 0.8297872, 0.0, 0.0,
38
+ 0.0, 0.0, 0.0003759398, 0.0, 0.8817427,
39
+ 0.009114583, 0.0
40
+ )
41
+
42
+ pgm_function1 = pgm_factor1.set_dense()
43
+ pgm_function1.set_flat(
44
+ 0.85066667, 0.1177011, 0.07401033, 0.3870968, 0.04893754,
45
+ 0.0, 0.00862069, 0.0, 0.0, 0.13866667,
46
+ 0.691954, 0.70051635, 0.483871, 0.72826787, 0.1,
47
+ 0.18793103, 0.75, 0.0, 0.01066667, 0.1903448,
48
+ 0.22547332, 0.1290323, 0.22279459, 0.9, 0.80344828,
49
+ 0.25, 1.0
50
+ )
51
+
52
+ pgm_function2 = pgm_factor2.set_dense()
53
+ pgm_function2.set_flat(
54
+ 0.2899207, 0.5796178, 0.0, 0.5767013, 0.6129167,
55
+ 0.04462475, 0.996261682, 0.8636364, 0.1538462, 0.2457531,
56
+ 0.4203822, 1.0, 0.4232987, 0.3870833, 0.93509128,
57
+ 0.003738318, 0.1363636, 0.8461538, 0.4643262, 0.0,
58
+ 0.0, 0.0, 0.0, 0.02028398, 0.0,
59
+ 0.0, 0.0
60
+ )
61
+
62
+ pgm_function3 = pgm_factor3.set_dense()
63
+ pgm_function3.set_flat(
64
+ 0.7454545, 0.3846154, 0.26217765, 0.7068966, 0.2692308,
65
+ 0.85106383, 0.8571429, 0.0, 0.3333333, 0.757732,
66
+ 0.343173432, 0.86538462, 0.714826, 0.274655355, 0.2814815,
67
+ 0.8256881, 0.1052632, 0.3333333, 0.997782705, 1.0,
68
+ 0.9375, 0.96969697, 0.8571429, 0.5, 0.7272727,
69
+ 0.0, 0.3333333, 0.2545455, 0.1230769, 0.001432665,
70
+ 0.2931034, 0.7307692, 0.10638298, 0.1428571, 1.0,
71
+ 0.3333333, 0.242268, 0.649446494, 0.10096154, 0.285174,
72
+ 0.720042418, 0.5851852, 0.1743119, 0.8947368, 0.3333333,
73
+ 0.002217295, 0.0, 0.0, 0.03030303, 0.1428571,
74
+ 0.5, 0.2727273, 1.0, 0.3333333, 0.0,
75
+ 0.4923077, 0.736389685, 0.0, 0.0, 0.04255319,
76
+ 0.0, 0.0, 0.3333333, 0.0, 0.007380074,
77
+ 0.03365385, 0.0, 0.005302227, 0.1333333, 0.0,
78
+ 0.0, 0.3333333, 0.0, 0.0, 0.0625,
79
+ 0.0, 0.0, 0.0, 0.0, 0.0,
80
+ 0.3333333
81
+ )
82
+
83
+ pgm_function4 = pgm_factor4.set_dense()
84
+ pgm_function4.set_flat(
85
+ 0.30690827, 0.656051, 0.875, 0.919261822, 0.815,
86
+ 0.803245436, 0.80747664, 0.3863636, 0.7692308, 0.06455266,
87
+ 0.343949, 0.125, 0.078431373, 0.185, 0.192697769,
88
+ 0.09158879, 0.1590909, 0.2307692, 0.62853907, 0.0,
89
+ 0.0, 0.002306805, 0.0, 0.004056795, 0.10093458,
90
+ 0.4545455, 0.0
91
+ )
92
+
93
+ pgm_function5 = pgm_factor5.set_dense()
94
+ pgm_function5.set_flat(
95
+ 0.996868476, 1.0, 0.2217573, 0.98674822, 0.95789474,
96
+ 0.0766129, 0.872442019, 0.521875, 0.02597403, 0.003131524,
97
+ 0.0, 0.4937238, 0.01325178, 0.04210526, 0.391129,
98
+ 0.12005457, 0.4625, 0.05194805, 0.0, 0.0,
99
+ 0.2845188, 0.0, 0.0, 0.5322581, 0.007503411,
100
+ 0.015625, 0.92207792
101
+ )
102
+
103
+ pgm_function6 = pgm_factor6.set_dense()
104
+ pgm_function6.set_flat(
105
+ 0.2184223, 0.07777778, 0.4237589, 0.4473324, 0.21111111,
106
+ 0.4397163, 0.3342453, 0.71111111, 0.1365248
107
+ )
108
+
109
+ pgm_function7 = pgm_factor7.set_dense()
110
+ pgm_function7.set_flat(
111
+ 0.3864333, 0.0603614, 0.0155642, 0.3794311, 0.92272203,
112
+ 0.95914397, 0.2341357, 0.01691657, 0.02529183
113
+ )
114
+
115
+ pgm_function8 = pgm_factor8.set_dense()
116
+ pgm_function8.set_flat(0.42314815, 0.48166667, 0.09518519)
117
+
118
+ pgm_function9 = pgm_factor9.set_dense()
119
+ pgm_function9.set_flat(0.81222222, 0.08333333, 0.10444444)
120
+
121
+ pgm_function10 = pgm_factor10.set_dense()
122
+ pgm_function10.set_flat(
123
+ 0.06228766, 0.3694268, 0.875, 0.4475202, 0.5508333,
124
+ 0.8843813, 0.84299065, 0.75, 0.8461538, 0.14722537,
125
+ 0.3312102, 0.125, 0.3125721, 0.3929167, 0.1156187,
126
+ 0.1271028, 0.15909091, 0.1538462, 0.79048698, 0.2993631,
127
+ 0.0, 0.2399077, 0.05625, 0.0, 0.02990654,
128
+ 0.09090909, 0.0
129
+ )
@@ -0,0 +1,30 @@
1
+ from ck.pgm import PGM
2
+
3
+
4
+ class Sprinkler(PGM):
5
+ """
6
+ This PGM is the well known, pedagogical 'Sprinkler' Bayesian network.
7
+ """
8
+
9
+ def __init__(self):
10
+ super().__init__(self.__class__.__name__)
11
+
12
+ rain = self.new_rv('rain', ['not raining', 'raining'])
13
+ sprinkler = self.new_rv('sprinkler', ['off', 'on'])
14
+ grass = self.new_rv('grass', ['dry', 'damp', 'wet'])
15
+
16
+ f_g = self.new_factor(grass, rain, sprinkler)
17
+ f_r = self.new_factor(rain)
18
+ f_s = self.new_factor(sprinkler)
19
+
20
+ f_r.set_dense().set_flat(0.8, 0.2)
21
+ f_s.set_dense().set_flat(0.9, 0.1)
22
+ # fmt: off
23
+ f_g.set_dense().set_flat(
24
+ # not raining, raining # rain
25
+ # off, on, off, on # sprinkler
26
+ 0.90, 0.01, 0.02, 0.01, # grass dry
27
+ 0.09, 0.01, 0.08, 0.04, # grass damp
28
+ 0.01, 0.98, 0.90, 0.95, # grass wet
29
+ )
30
+ # fmt: on
ck/example/star.py ADDED
@@ -0,0 +1,44 @@
1
+ import random as _random
2
+
3
+ from ck.pgm import PGM
4
+
5
+
6
+ class Star(PGM):
7
+ """
8
+ This PGM is the 'Star' factor graph.
9
+
10
+ The Star factor graph is where the first random variable, x0, is
11
+ connected to each of the other random variables, x1, x2, ..., via a binary factor.
12
+ If include_unaries then, also includes one unary factor per random variable.
13
+ """
14
+
15
+ def __init__(
16
+ self,
17
+ num_of_arms: int = 3,
18
+ length_of_arms: int = 2,
19
+ states_per_var: int = 2,
20
+ include_unaries: bool = True,
21
+ random_seed: int = 123456,
22
+ ):
23
+ params = (num_of_arms, length_of_arms, states_per_var, include_unaries)
24
+ super().__init__(f'{self.__class__.__name__}({",".join(str(param) for param in params)})')
25
+
26
+ random_stream = _random.Random(random_seed).random
27
+ rand_iter = iter(random_stream, None)
28
+
29
+ x0 = self.new_rv('x0', states_per_var)
30
+ arms = [
31
+ [self.new_rv(f'x{arm}_{i}', states_per_var) for i in range(length_of_arms)]
32
+ for arm in range(num_of_arms)
33
+ ]
34
+
35
+ for arm in arms:
36
+ self.new_factor(x0, arm[0]).set_dense().set_iter(rand_iter)
37
+ for i in range(1, len(arm)):
38
+ self.new_factor(arm[i - 1], arm[i]).set_dense().set_iter(rand_iter)
39
+
40
+ if include_unaries:
41
+ self.new_factor(x0).set_dense().set_iter(rand_iter)
42
+ for arm in arms:
43
+ for rv in arm:
44
+ self.new_factor(rv).set_dense().set_iter(rand_iter)
ck/example/stress.py ADDED
@@ -0,0 +1,64 @@
1
+ from ck.pgm import PGM
2
+
3
+
4
+ class Stress(PGM):
5
+ """
6
+ This PGM is the 'Stress' factor graph.
7
+
8
+ The Stress factor graph uses many of the different PGM software
9
+ components. This can be used to stress test the PGM implementation
10
+ and processing pipelines.
11
+ """
12
+
13
+ def __init__(self):
14
+ super().__init__(self.__class__.__name__)
15
+
16
+ no_name = self.new_rv('', 2)
17
+ x1 = self.new_rv('x1', 2)
18
+ x2 = self.new_rv('x2', ['one', 'two'])
19
+ x3 = self.new_rv('x3', ['one', 'two', 'non-ascii 😊'])
20
+ x4 = self.new_rv('x4', [1.1, 1.23, None, 'string'])
21
+ x5 = self.new_rv('x5', 3)
22
+ A = self.new_rv('A', 2)
23
+ B = self.new_rv('B', 2)
24
+ C = self.new_rv('C', 2)
25
+ D = self.new_rv('D', 2)
26
+ E = self.new_rv('E', [True, False])
27
+
28
+ self.new_factor(no_name).set_zero()
29
+
30
+ self.new_factor(x1).set_dense().set_flat(1, 2)
31
+
32
+ self.new_factor(x1, x2).set_clause(1, 0)
33
+
34
+ self.new_factor(x2, x3).set_cpt().set_all(
35
+ (0.1, 0.9),
36
+ (0.2, 0.8),
37
+ (0.3, 0.7),
38
+ )
39
+
40
+ f_3_4 = self.new_factor(x3, x4).set_sparse()
41
+ f_3_4[0, 0] = 11
42
+ f_3_4[1, 1] = 11
43
+ f_3_4[2, 2] = 22
44
+
45
+ f_4_5 = self.new_factor(x4, x5).set_compact()
46
+ f_4_5[0, 0] = 11
47
+ f_4_5[1, 1] = 11
48
+ f_4_5[2, 2] = 22
49
+
50
+ f_ab = self.new_factor(A, B)
51
+ f_ab.set_dense().set_flat(2, 3, 5, 7)
52
+
53
+ self.new_factor(A, C).set_sparse()[0, 1] = 11
54
+ self.new_factor(A, D).set_sparse()[1, 0] = 13
55
+ self.new_factor(B, C).set_clause(0, 1)
56
+ self.new_factor(B, D).set_cpt().set_all([0.2, 0.8], [0.3, 0.7])
57
+
58
+ self.new_factor(A, E) # leave with no potential function
59
+
60
+ self.new_factor(B, E).function = f_ab.function # shared function
61
+
62
+ # Two factors on the same random variables
63
+ self.new_factor(C, E).set_sparse()
64
+ self.new_factor(C, E).set_sparse()
ck/example/student.py ADDED
@@ -0,0 +1,43 @@
1
+ from ck.pgm import PGM
2
+
3
+
4
+ class Student(PGM):
5
+ """
6
+ This PGM is the well known, pedagogical 'Student' Bayesian network.
7
+
8
+ Reference:
9
+ Probabilistic Graphical Models: Principles and Techniques
10
+ Daphne Koller & Nir Friedman. MIT Press, 2009.
11
+ Figure 3.1, page 48.
12
+ """
13
+
14
+ def __init__(self):
15
+ super().__init__(self.__class__.__name__)
16
+
17
+ difficult = self.new_rv('difficult', ('Yes', 'No'))
18
+ intelligent = self.new_rv('intelligent', ('Yes', 'No'))
19
+ grade = self.new_rv('grade', ('1', '2', '3'))
20
+ sat = self.new_rv('sat', ('High', 'Low'))
21
+ letter = self.new_rv('letter', ('Yes', 'No'))
22
+
23
+ self.new_factor(difficult).set_cpt().set_all(
24
+ (0.6, 0.4),
25
+ )
26
+ self.new_factor(intelligent).set_cpt().set_all(
27
+ (0.7, 0.3),
28
+ )
29
+ self.new_factor(grade, difficult, intelligent).set_cpt().set_all(
30
+ (0.3, 0.4, 0.3),
31
+ (0.05, 0.25, 0.7),
32
+ (0.9, 0.08, 0.02),
33
+ (0.5, 0.3, 0.2)
34
+ )
35
+ self.new_factor(sat, intelligent).set_cpt().set_all(
36
+ (0.95, 0.05),
37
+ (0.2, 0.8),
38
+ )
39
+ self.new_factor(letter, grade).set_cpt().set_all(
40
+ (0.1, 0.9),
41
+ (0.4, 0.6),
42
+ (0.99, 0.01),
43
+ )
ck/example/survey.py ADDED
@@ -0,0 +1,46 @@
1
+ from ck.pgm import PGM
2
+
3
+
4
+ class Survey(PGM):
5
+
6
+ def __init__(self):
7
+ super().__init__(self.__class__.__name__)
8
+
9
+ pgm_rv0 = self.new_rv('A', ('young', 'adult', 'old'))
10
+ pgm_rv1 = self.new_rv('S', ('M', 'F'))
11
+ pgm_rv2 = self.new_rv('E', ('high', 'uni'))
12
+ pgm_rv3 = self.new_rv('O', ('emp', 'self'))
13
+ pgm_rv4 = self.new_rv('R', ('small', 'big'))
14
+ pgm_rv5 = self.new_rv('T', ('car', 'train', 'other'))
15
+ pgm_factor0 = self.new_factor(pgm_rv0)
16
+ pgm_factor1 = self.new_factor(pgm_rv1)
17
+ pgm_factor2 = self.new_factor(pgm_rv2, pgm_rv0, pgm_rv1)
18
+ pgm_factor3 = self.new_factor(pgm_rv3, pgm_rv2)
19
+ pgm_factor4 = self.new_factor(pgm_rv4, pgm_rv2)
20
+ pgm_factor5 = self.new_factor(pgm_rv5, pgm_rv3, pgm_rv4)
21
+
22
+ pgm_function0 = pgm_factor0.set_dense()
23
+ pgm_function0.set_flat(0.3, 0.5, 0.2)
24
+
25
+ pgm_function1 = pgm_factor1.set_dense()
26
+ pgm_function1.set_flat(0.6, 0.4)
27
+
28
+ pgm_function2 = pgm_factor2.set_dense()
29
+ pgm_function2.set_flat(
30
+ 0.75, 0.64, 0.72, 0.7, 0.88,
31
+ 0.9, 0.25, 0.36, 0.28, 0.3,
32
+ 0.12, 0.1
33
+ )
34
+
35
+ pgm_function3 = pgm_factor3.set_dense()
36
+ pgm_function3.set_flat(0.96, 0.92, 0.04, 0.08)
37
+
38
+ pgm_function4 = pgm_factor4.set_dense()
39
+ pgm_function4.set_flat(0.25, 0.2, 0.75, 0.8)
40
+
41
+ pgm_function5 = pgm_factor5.set_dense()
42
+ pgm_function5.set_flat(
43
+ 0.48, 0.58, 0.56, 0.7, 0.42,
44
+ 0.24, 0.36, 0.21, 0.1, 0.18,
45
+ 0.08, 0.09
46
+ )
@@ -0,0 +1,54 @@
1
+ import random as _random
2
+
3
+ from ck.pgm import PGM
4
+
5
+
6
+ class TriangleSquare(PGM):
7
+ r"""
8
+ This PGM is the 'TriangleSquare' factor graph.
9
+
10
+ The TriangleSquare is a factor graph with six random variables (a, b, c, ..., f).
11
+ Binary factors are between pairs of random variables crating the pattern:
12
+ b -- d
13
+ / | | \
14
+ a | | f
15
+ \ | | /
16
+ c -- e
17
+ If include_unaries then, also includes one unary factor per random variable.
18
+ """
19
+
20
+ def __init__(
21
+ self,
22
+ states_per_var=2,
23
+ include_unaries=True,
24
+ random_seed=123456
25
+ ):
26
+ params = (states_per_var, include_unaries)
27
+ super().__init__(f'{self.__class__.__name__}({",".join(str(param) for param in params)})')
28
+
29
+ random_stream = _random.Random(random_seed).random
30
+ rand_iter = iter(random_stream, None)
31
+
32
+ a = self.new_rv('a', states_per_var)
33
+ b = self.new_rv('b', states_per_var)
34
+ c = self.new_rv('c', states_per_var)
35
+ d = self.new_rv('d', states_per_var)
36
+ e = self.new_rv('e', states_per_var)
37
+ f = self.new_rv('f', states_per_var)
38
+
39
+ self.new_factor(a, b).set_dense().set_iter(rand_iter)
40
+ self.new_factor(a, c).set_dense().set_iter(rand_iter)
41
+ self.new_factor(b, c).set_dense().set_iter(rand_iter)
42
+ self.new_factor(b, d).set_dense().set_iter(rand_iter)
43
+ self.new_factor(c, e).set_dense().set_iter(rand_iter)
44
+ self.new_factor(d, e).set_dense().set_iter(rand_iter)
45
+ self.new_factor(d, f).set_dense().set_iter(rand_iter)
46
+ self.new_factor(e, f).set_dense().set_iter(rand_iter)
47
+
48
+ if include_unaries:
49
+ self.new_factor(a).set_dense().set_iter(rand_iter)
50
+ self.new_factor(b).set_dense().set_iter(rand_iter)
51
+ self.new_factor(c).set_dense().set_iter(rand_iter)
52
+ self.new_factor(d).set_dense().set_iter(rand_iter)
53
+ self.new_factor(e).set_dense().set_iter(rand_iter)
54
+ self.new_factor(f).set_dense().set_iter(rand_iter)