compiled-knowledge 4.0.0a20__cp312-cp312-macosx_10_13_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 +37525 -0
  4. ck/circuit/_circuit_cy.cpython-312-darwin.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 +19826 -0
  13. ck/circuit_compiler/cython_vm_compiler/_compiler.cpython-312-darwin.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 +10620 -0
  23. ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_cy.cpython-312-darwin.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 +16398 -0
  90. ck/pgm_compiler/support/circuit_table/_circuit_table_cy.cpython-312-darwin.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 +6 -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
@@ -0,0 +1,154 @@
1
+ """
2
+ This is a simple demo creating then using a PGM model.
3
+ """
4
+
5
+ from ck.pgm import PGM
6
+ from ck.pgm_circuit import PGMCircuit
7
+ from ck.pgm_circuit.mpe_program import MPEProgram
8
+ from ck.pgm_circuit.wmc_program import WMCProgram
9
+ from ck.pgm_compiler import DEFAULT_PGM_COMPILER
10
+ from ck_demos.utils.stop_watch import timer
11
+
12
+ pgm = PGM()
13
+
14
+ rain = pgm.new_rv('rain', ['no', 'yes'])
15
+ sprinkler = pgm.new_rv('sprinkler', ['off', 'on'])
16
+ grass = pgm.new_rv('grass', ['dry', 'damp', 'wet'])
17
+
18
+ f_g = pgm.new_factor(grass, rain, sprinkler) # same as a Conditional Probability Table (CPT)
19
+ f_r = pgm.new_factor(rain)
20
+ f_s = pgm.new_factor(sprinkler)
21
+
22
+ # Instead of learning the parameter values from data, in this simple
23
+ # demo we will hard code the parameter values.
24
+ f_r.set_dense().set_flat(0.8, 0.2)
25
+ f_s.set_dense().set_flat(0.9, 0.1)
26
+ f_g.set_dense().set_flat(
27
+ # not raining raining # rain
28
+ # off on off on # sprinkler
29
+ 0.90, 0.01, 0.02, 0.01, # grass dry
30
+ 0.09, 0.01, 0.08, 0.04, # grass damp
31
+ 0.01, 0.98, 0.90, 0.95, # grass wet
32
+ )
33
+
34
+ # ---------------------------------------
35
+ # Directly use the model
36
+ # ---------------------------------------
37
+
38
+ print('is_structure_bayesian', pgm.is_structure_bayesian)
39
+ print('factors_are_cpts', pgm.factors_are_cpts())
40
+
41
+ for inst in pgm.instances():
42
+ inst_str = ','.join(f'{rv}={state}' for rv, state in zip(pgm.rvs, inst))
43
+ inst_w = pgm.value_product(inst)
44
+ print(f'weight({inst_str}) = {inst_w}')
45
+
46
+ # ---------------------------------------
47
+ # Compile the PGM for complex queries
48
+ # ---------------------------------------
49
+
50
+ with timer('compiling PGM'):
51
+ pgm_cct: PGMCircuit = DEFAULT_PGM_COMPILER(pgm)
52
+ wmc = WMCProgram(pgm_cct)
53
+ circuit = pgm_cct.circuit_top.circuit
54
+ print(f'number of ops: {circuit.number_of_op_nodes}')
55
+ print(f'number of arcs: {circuit.number_of_arcs}')
56
+
57
+ # ---------------------------------------
58
+ # Here are some example uses of a model
59
+ # ---------------------------------------
60
+
61
+ print()
62
+ print('--------')
63
+ print('Show selected probabilities')
64
+
65
+ # What is the probability of 'not raining' and 'sprinkler off' and 'grass damp'?
66
+ pr001 = wmc.probability(rain[0], sprinkler[0], grass[1])
67
+ print(pgm.indicator_str(rain[0], sprinkler[0], grass[1]), 'Pr =', pr001)
68
+
69
+ # What is the probability of 'not raining' and 'sprinkler on' and 'grass damp'?
70
+ pr011 = wmc.probability(rain[0], sprinkler[1], grass[1])
71
+ print(pgm.indicator_str(rain[0], sprinkler[1], grass[1]), 'Pr =', pr011)
72
+
73
+ # What is the probability of 'not raining' and 'grass damp'?
74
+ pr0_1 = wmc.probability(rain[0], grass[1])
75
+ print(pgm.indicator_str(rain[0], grass[1]), 'Pr =', pr0_1)
76
+
77
+ # What is the probability of 'grass damp'?
78
+ pr__1 = wmc.probability(grass[1])
79
+ print(pgm.indicator_str(grass[1]), 'Pr =', pr__1)
80
+
81
+ # What is the probability of 'raining' given 'grass wet'?
82
+ pr_val = wmc.probability(rain[1], condition=grass[2])
83
+ pr_str = pgm.indicator_str(rain[1])
84
+ pr_cond = pgm.indicator_str(grass[2])
85
+ print(f'Pr[{pr_str} | {pr_cond}] = {pr_val}')
86
+
87
+
88
+ # Show the probability of each possible world
89
+ print('--------')
90
+ print('Show every possible world and its probability')
91
+ for inst in pgm.instances_as_indicators():
92
+ pr_str = pgm.indicator_str(*inst)
93
+ pr_val = wmc.probability(*inst)
94
+ print(f'Pr[{pr_str}] = {pr_val}')
95
+
96
+ print('--------')
97
+ print('Show selected marginal distributions')
98
+
99
+ # What is the marginal probability distribution over sprinkler?
100
+ pr_sprinkler = wmc.marginal_distribution(sprinkler)
101
+ print(f'Pr[{sprinkler}] = {pr_sprinkler}')
102
+
103
+ # What is the marginal probability distribution over sprinkler,
104
+ # given 'not raining' and 'grass wet'?
105
+ condition = (rain[0], grass[2])
106
+ print('Pr[{} | {}] = {}'.format(
107
+ sprinkler, pgm.indicator_str(*condition),
108
+ wmc.marginal_distribution(sprinkler, condition=condition)
109
+ ))
110
+
111
+ # What is the marginal probability distribution over sprinkler,
112
+ # given 'raining' and 'grass wet'?
113
+ condition = (rain[1], grass[2])
114
+ print('Pr[{} | {}] = {}'.format(
115
+ sprinkler,
116
+ pgm.indicator_str(*condition),
117
+ wmc.marginal_distribution(sprinkler, condition=condition)
118
+ ))
119
+
120
+
121
+ print('--------')
122
+ print('Show selected MAP results')
123
+
124
+ pr, states = wmc.map(sprinkler, rain)
125
+ print(f'MAP[{sprinkler}, {rain}] = {states} with probability = {pr}')
126
+
127
+ pr, states = wmc.map(sprinkler, grass, condition=[grass[1], rain[0]])
128
+ cond_str = pgm.indicator_str(grass[1], rain[0])
129
+ print(f'MAP[{sprinkler}, {grass} | {cond_str}] = {states} with probability = {pr}')
130
+
131
+
132
+ print('--------')
133
+ print('Show selected MPE results')
134
+
135
+ z = wmc.z
136
+ mpe = MPEProgram(pgm_cct)
137
+
138
+ mpe_result = mpe.mpe()
139
+ print('MPE = {} with probability = {}'.format(mpe_result.mpe, mpe_result.wmc/z))
140
+
141
+ mpe_result = mpe.mpe(grass[2])
142
+ print('given wet grass: MPE = {} with probability = {}'.format(mpe_result.mpe, mpe_result.wmc/z))
143
+
144
+ print('--------')
145
+ print('Draw 20 independent samples from the probability distribution')
146
+ num_samples = 20
147
+ sampler = wmc.sample_direct()
148
+ for i, sample in zip(range(1, 1 + num_samples), sampler):
149
+ sample_indicators = pgm.state_idxs_to_indicators(sample)
150
+ ind_str = pgm.indicator_str(*sample_indicators)
151
+ print(f'{i:2} {sample} {ind_str}')
152
+
153
+ print('--------')
154
+ print('Done.')
@@ -0,0 +1,110 @@
1
+ """
2
+ This is a simple demo creating then using a PGM model.
3
+ """
4
+
5
+ from ck.pgm import PGM, rv_instances_as_indicators, Indicator, RandomVariable
6
+ from ck.pgm_circuit.wmc_program import WMCProgram
7
+ from ck.pgm_compiler import DEFAULT_PGM_COMPILER
8
+ from ck.probability.probability_space import Condition, check_condition
9
+
10
+ # -------------------------------------------------------------------------
11
+ # Construct the 'student' network from
12
+ # Koller & Friedman, Probabilistic Graphical Models, 2009, Figure 3.4, p53.
13
+ # -------------------------------------------------------------------------
14
+
15
+ pgm = PGM()
16
+
17
+ difficult = pgm.new_rv('difficult', ('Yes', 'No'))
18
+ intelligent = pgm.new_rv('intelligent', ('Yes', 'No'))
19
+ grade = pgm.new_rv('grade', ('1', '2', '3'))
20
+ sat = pgm.new_rv('sat', ('High', 'Low'))
21
+ letter = pgm.new_rv('letter', ('Yes', 'No'))
22
+
23
+ pgm.new_factor(difficult).set_cpt().set_all(
24
+ (0.6, 0.4),
25
+ )
26
+ pgm.new_factor(intelligent).set_cpt().set_all(
27
+ (0.7, 0.3),
28
+ )
29
+ pgm.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
+ pgm.new_factor(sat, intelligent).set_cpt().set_all(
36
+ (0.95, 0.05),
37
+ (0.2, 0.8),
38
+ )
39
+ pgm.new_factor(letter, grade).set_cpt().set_all(
40
+ (0.1, 0.9),
41
+ (0.4, 0.6),
42
+ (0.99, 0.01),
43
+ )
44
+
45
+ # ---------------------------------------
46
+ # Compile the PGM
47
+ # ---------------------------------------
48
+
49
+ wmc = WMCProgram(DEFAULT_PGM_COMPILER(pgm))
50
+
51
+
52
+ # ---------------------------------------
53
+ # Here are some example uses of a model
54
+ # ---------------------------------------
55
+
56
+ def show_probability(*indicators: Indicator, condition: Condition = ()):
57
+ """
58
+ Print the probability of the given indicators,
59
+ with an optional set of condition indicators.
60
+ """
61
+ condition = check_condition(condition)
62
+ indicator_str = pgm.indicator_str(*indicators)
63
+ probability = wmc.probability(*indicators, condition=condition)
64
+ if len(condition) == 0:
65
+ print(f'P({indicator_str}) = {probability:.4g}')
66
+ else:
67
+ condition_str = pgm.indicator_str(*condition)
68
+ print(f'P({indicator_str} | {condition_str}) = {probability:.4g}')
69
+
70
+
71
+ def show_marginal_distribution(rv: RandomVariable, condition: Condition = ()):
72
+ """
73
+ Print the marginal probability distribution of the random variable,
74
+ with an optional set of condition indicators.
75
+ """
76
+ condition = check_condition(condition)
77
+ probabilities = wmc.marginal_distribution(rv, condition=condition)
78
+ if len(condition) == 0:
79
+ print(f'P({rv}) = {probabilities}')
80
+ else:
81
+ condition_str = pgm.indicator_str(*condition)
82
+ print(f'P({rv} | {condition_str}) = {probabilities}')
83
+
84
+
85
+ print()
86
+ show_probability(difficult('Yes'))
87
+ show_probability(difficult('No'))
88
+
89
+ print()
90
+ show_probability(intelligent('Yes'))
91
+ show_probability(intelligent('No'))
92
+
93
+ print()
94
+ show_probability(difficult('Yes'), intelligent('No'))
95
+ show_probability(grade('3'), condition=intelligent('Yes'))
96
+ show_probability(grade('3'), condition=(intelligent('Yes'), letter('Yes')))
97
+ show_probability(intelligent('No'), condition=letter('Yes'))
98
+ show_probability(intelligent('Yes'), condition=letter('Yes'))
99
+
100
+ print()
101
+ show_marginal_distribution(difficult)
102
+ show_marginal_distribution(intelligent)
103
+ show_marginal_distribution(grade)
104
+
105
+ print()
106
+ for example_condition in rv_instances_as_indicators(difficult, intelligent):
107
+ show_marginal_distribution(grade, condition=example_condition)
108
+
109
+ print()
110
+ print('Done.')
File without changes
@@ -0,0 +1,24 @@
1
+ from ck.circuit_compiler.interpret_compiler import compile_circuit
2
+ from ck.circuit import Circuit
3
+ from ck.program.program_buffer import ProgramBuffer
4
+ from ck.program.raw_program import RawProgram
5
+
6
+
7
+ def main() -> None:
8
+ cct = Circuit()
9
+ x = cct.new_vars(4)
10
+ y = cct.add(x[0], x[1])
11
+ z = cct.add(x[2], x[3])
12
+ top = cct.add(cct.mul(y, z, 12), 10)
13
+
14
+ raw_program: RawProgram = compile_circuit(top)
15
+ prog = ProgramBuffer(raw_program)
16
+
17
+ prog[:] = (4, 5, 6, 7)
18
+ result = prog.compute()
19
+ print('expect =', (4 + 5) * (6 + 7) * 12 + 10)
20
+ print('result =', result)
21
+
22
+
23
+ if __name__ == '__main__':
24
+ main()
@@ -0,0 +1,24 @@
1
+ from ck.circuit_compiler.interpret_compiler import compile_circuit
2
+ from ck.circuit import Circuit
3
+ from ck.program import Program
4
+ from ck.program.raw_program import RawProgram
5
+
6
+
7
+ def main() -> None:
8
+ cct = Circuit()
9
+ x = cct.new_vars(4)
10
+ y = cct.add(x[0], x[1])
11
+ z = cct.add(x[2], x[3])
12
+ top1 = cct.add(1, cct.mul(y, -12))
13
+ top2 = cct.add(1, cct.mul(z, 12))
14
+
15
+ raw_program: RawProgram = compile_circuit(top1, top2)
16
+ prog = Program(raw_program)
17
+
18
+ result = prog(4, 5, 6, 7)
19
+ print('expect = [-107, 157]')
20
+ print('result =', result)
21
+
22
+
23
+ if __name__ == '__main__':
24
+ main()
@@ -0,0 +1,19 @@
1
+ from ck.circuit_compiler.interpret_compiler import compile_circuit
2
+ from ck.circuit import Circuit
3
+ from ck.program import Program
4
+ from ck.program.raw_program import RawProgram
5
+
6
+
7
+ def main() -> None:
8
+ circuit = Circuit()
9
+ input_vars = circuit.new_vars(4)
10
+
11
+ raw_program: RawProgram = compile_circuit(input_vars=input_vars)
12
+ prog = Program(raw_program)
13
+
14
+ result = prog(4, 5, 6, 7)
15
+ print('result =', result)
16
+
17
+
18
+ if __name__ == '__main__':
19
+ main()
@@ -0,0 +1,23 @@
1
+ from ck.circuit_compiler.interpret_compiler import compile_circuit
2
+ from ck.circuit import Circuit
3
+ from ck.program import Program
4
+ from ck.program.raw_program import RawProgram
5
+
6
+
7
+ def main() -> None:
8
+ cct = Circuit()
9
+ x = cct.new_vars(4)
10
+ y = cct.add(x[0], x[1])
11
+ z = cct.add(x[2], x[3])
12
+ top = cct.add(cct.mul(y, z, 12), -10)
13
+
14
+ raw_program: RawProgram = compile_circuit(top)
15
+ prog = Program(raw_program)
16
+
17
+ result = prog(4, 5, 6, 7)
18
+ print('expect =', (4 + 5) * (6 + 7) * 12 - 10)
19
+ print('result =', result)
20
+
21
+
22
+ if __name__ == '__main__':
23
+ main()
@@ -0,0 +1,21 @@
1
+ from ck.circuit_compiler.interpret_compiler import compile_circuit
2
+ from ck.circuit import Circuit
3
+ from ck.program.raw_program import RawProgram
4
+
5
+
6
+ def main() -> None:
7
+ cct = Circuit()
8
+ x = cct.new_vars(4)
9
+ y = cct.add(x[0], x[1])
10
+ z = cct.add(x[2], x[3])
11
+ top = cct.add(cct.mul(y, z, 12), -10)
12
+
13
+ prog: RawProgram = compile_circuit(top)
14
+
15
+ result = prog([4, 5, 6, 7])
16
+ print('expect =', (4 + 5) * (6 + 7) * 12 - 10)
17
+ print('result =', result)
18
+
19
+
20
+ if __name__ == '__main__':
21
+ main()
@@ -0,0 +1,21 @@
1
+ from ck.circuit_compiler.llvm_compiler import compile_circuit
2
+ from ck.circuit import Circuit
3
+ from ck.program.raw_program import RawProgram
4
+
5
+
6
+ def main() -> None:
7
+ cct = Circuit()
8
+ x = cct.new_vars(4)
9
+ y = cct.add(x[0], x[1])
10
+ z = cct.add(x[2], x[3])
11
+ top = cct.add(cct.mul(y, z, 12), -10)
12
+
13
+ prog: RawProgram = compile_circuit(top)
14
+
15
+ result = prog([4, 5, 6, 7])
16
+ print('expect =', (4 + 5) * (6 + 7) * 12 - 10)
17
+ print('result =', result)
18
+
19
+
20
+ if __name__ == '__main__':
21
+ main()
File without changes
@@ -0,0 +1,71 @@
1
+ """
2
+ This demo script can be used to see how well samplers perform.
3
+ """
4
+ import random
5
+
6
+ from ck.pgm import PGM
7
+ from ck.utils.random_extras import random_permutation
8
+ from ck_demos.utils.sample_model import sample_model, STANDARD_SAMPLERS, BURN_IN
9
+
10
+ # Parameters for random PGM
11
+ num_of_rvs = 5
12
+ states_per_rv = 3
13
+ proportion_zeros = 0.1
14
+
15
+ # Experiment parameters
16
+ num_of_trials = 3
17
+ limit_conditioning = None
18
+ num_of_samples = 100 * (states_per_rv ** num_of_rvs)
19
+ seed = None
20
+
21
+ # Formatting options
22
+ show_each_analysis = False
23
+
24
+
25
+ def main():
26
+ # Manage randomness, if requested
27
+ if seed is not None:
28
+ random.seed(seed)
29
+
30
+ # Create a random PGM
31
+ pgm = PGM()
32
+ rvs = [pgm.new_rv(f'x_{i}', states_per_rv) for i in range(num_of_rvs)]
33
+
34
+ # Add unary factors to all but the first rv (uniform prior).
35
+ for rv in rvs[1:]:
36
+ pgm.new_factor(rv).set_dense().set_uniform()
37
+ # Make the first rv the child of all others (random probabilities).
38
+ f = pgm.new_factor(*rvs).set_dense().set_stream(random.random)
39
+
40
+ # Force some zero probabilities.
41
+ num_to_zero = int(f.number_of_parameters * proportion_zeros + 0.5)
42
+ if num_to_zero > 0:
43
+ to_zero = random_permutation(f.number_of_parameters)[:num_to_zero]
44
+ for param_idx in to_zero:
45
+ f.set_param_value(param_idx, 0)
46
+
47
+ # Make the PGM a Bayesian network.
48
+ f.normalise_cpt()
49
+
50
+ samplers_str = ', '.join([name for name in STANDARD_SAMPLERS.keys()])
51
+ print(f'Samplers: {samplers_str}')
52
+ print(f'Number of trials: {num_of_trials}')
53
+ print(f'Number of samples: {num_of_samples}')
54
+ print(f'States per RV: {states_per_rv}')
55
+ print(f'Burn in (where used): {BURN_IN}')
56
+ print(f'Random seed: {seed}')
57
+
58
+ sample_model(
59
+ pgm,
60
+ STANDARD_SAMPLERS,
61
+ num_of_trials,
62
+ num_of_samples,
63
+ limit_conditioning=limit_conditioning,
64
+ show_each_analysis=show_each_analysis
65
+ )
66
+
67
+ print('Done.')
68
+
69
+
70
+ if __name__ == '__main__':
71
+ main()
@@ -0,0 +1,40 @@
1
+ import random
2
+
3
+ from ck import example
4
+ from ck.pgm import PGM
5
+ from ck.pgm_compiler import factor_elimination
6
+ from ck.pgm_circuit.marginals_program import MarginalsProgram
7
+ from ck.pgm_circuit import PGMCircuit
8
+ from ck.probability.empirical_probability_space import EmpiricalProbabilitySpace
9
+
10
+ num_of_samples_to_show = 100
11
+ num_of_samples_to_calculate = 10_000
12
+ rand_seed = None
13
+
14
+
15
+ def main():
16
+ if rand_seed is not None:
17
+ random.seed(rand_seed)
18
+
19
+ pgm: PGM = example.Rain()
20
+
21
+ pgm_cct: PGMCircuit = factor_elimination.compile_pgm(pgm)
22
+ marginals = MarginalsProgram(pgm_cct)
23
+ sampler = marginals.sample_direct()
24
+
25
+ # Show some samples
26
+ for sample in sampler.take(num_of_samples_to_show):
27
+ print(sample)
28
+ print()
29
+
30
+ # Show empirical and theoretical marginal distribution for each random variable
31
+ sample_pr = EmpiricalProbabilitySpace(sampler.rvs, sampler.take(num_of_samples_to_calculate))
32
+ for rv in pgm.rvs:
33
+ print(rv, sample_pr.marginal_distribution(rv), marginals.marginal_distribution(rv))
34
+ print()
35
+
36
+ print('Done.')
37
+
38
+
39
+ if __name__ == '__main__':
40
+ main()
@@ -0,0 +1,38 @@
1
+ import random
2
+
3
+ from ck import example
4
+ from ck.pgm import PGM
5
+ from ck.pgm_compiler import factor_elimination
6
+ from ck.pgm_circuit import PGMCircuit
7
+ from ck.pgm_circuit.wmc_program import WMCProgram
8
+ from ck.probability.empirical_probability_space import EmpiricalProbabilitySpace
9
+
10
+ num_of_samples = 100
11
+ seed = None
12
+
13
+
14
+ def main():
15
+ if seed is not None:
16
+ random.seed(seed)
17
+
18
+ pgm: PGM = example.Rain()
19
+ pgm_cct: PGMCircuit = factor_elimination.compile_pgm(pgm)
20
+ wmc = WMCProgram(pgm_cct)
21
+ sampler = wmc.sample_uniform()
22
+
23
+ # Show some samples
24
+ for sample in sampler.take(num_of_samples):
25
+ print(sample)
26
+ print()
27
+
28
+ # Show empirical marginal distribution for each random variable
29
+ pr = EmpiricalProbabilitySpace(sampler.rvs, sampler.take(100_000))
30
+ for rv in pgm.rvs:
31
+ print(rv, pr.marginal_distribution(rv))
32
+ print()
33
+
34
+ print('Done.')
35
+
36
+
37
+ if __name__ == '__main__':
38
+ main()
@@ -0,0 +1,40 @@
1
+ import random
2
+
3
+ from ck import example
4
+ from ck.pgm import PGM
5
+ from ck.pgm_compiler import factor_elimination
6
+ from ck.pgm_circuit import PGMCircuit
7
+ from ck.pgm_circuit.wmc_program import WMCProgram
8
+ from ck.probability.empirical_probability_space import EmpiricalProbabilitySpace
9
+
10
+ num_of_samples_to_show = 100
11
+ num_of_samples_to_calculate = 10_000
12
+ rand_seed = None
13
+
14
+
15
+ def main():
16
+ if rand_seed is not None:
17
+ random.seed(rand_seed)
18
+
19
+ pgm: PGM = example.Rain()
20
+
21
+ pgm_cct: PGMCircuit = factor_elimination.compile_pgm(pgm)
22
+ wmc = WMCProgram(pgm_cct)
23
+ sampler = wmc.sample_direct()
24
+
25
+ # Show some samples
26
+ for sample in sampler.take(num_of_samples_to_show):
27
+ print(sample)
28
+ print()
29
+
30
+ # Show empirical and theoretical marginal distribution for each random variable
31
+ sample_pr = EmpiricalProbabilitySpace(sampler.rvs, sampler.take(num_of_samples_to_calculate))
32
+ for rv in pgm.rvs:
33
+ print(rv, sample_pr.marginal_distribution(rv), wmc.marginal_distribution(rv))
34
+ print()
35
+
36
+ print('Done.')
37
+
38
+
39
+ if __name__ == '__main__':
40
+ main()
File without changes