compiled-knowledge 4.1.0a2__cp313-cp313-macosx_11_0_arm64.whl → 4.2.0a1__cp313-cp313-macosx_11_0_arm64.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 (36) hide show
  1. ck/circuit/_circuit_cy.c +1 -1
  2. ck/circuit/_circuit_cy.cpython-313-darwin.so +0 -0
  3. ck/circuit_compiler/cython_vm_compiler/_compiler.c +152 -152
  4. ck/circuit_compiler/cython_vm_compiler/_compiler.cpython-313-darwin.so +0 -0
  5. ck/circuit_compiler/llvm_compiler.py +4 -4
  6. ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_cy.c +1 -1
  7. ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_cy.cpython-313-darwin.so +0 -0
  8. ck/circuit_compiler/support/input_vars.py +4 -4
  9. ck/dataset/cross_table.py +143 -79
  10. ck/dataset/dataset.py +95 -7
  11. ck/dataset/dataset_builder.py +11 -4
  12. ck/dataset/dataset_from_crosstable.py +21 -2
  13. ck/learning/coalesce_cross_tables.py +403 -0
  14. ck/learning/model_from_cross_tables.py +296 -0
  15. ck/learning/parameters.py +117 -0
  16. ck/learning/train_generative_bn.py +198 -0
  17. ck/pgm.py +10 -8
  18. ck/pgm_circuit/marginals_program.py +5 -0
  19. ck/pgm_circuit/wmc_program.py +5 -0
  20. ck/pgm_compiler/support/circuit_table/_circuit_table_cy.c +1 -1
  21. ck/pgm_compiler/support/circuit_table/_circuit_table_cy.cpython-313-darwin.so +0 -0
  22. ck/probability/divergence.py +226 -0
  23. ck/probability/probability_space.py +43 -19
  24. ck/utils/map_dict.py +89 -0
  25. ck_demos/dataset/demo_dataset_from_sampler.py +18 -0
  26. ck_demos/learning/__init__.py +0 -0
  27. ck_demos/learning/demo_bayesian_network_from_cross_tables.py +70 -0
  28. ck_demos/learning/demo_simple_learning.py +55 -0
  29. ck_demos/sampling/demo_wmc_direct_sampler.py +2 -2
  30. {compiled_knowledge-4.1.0a2.dist-info → compiled_knowledge-4.2.0a1.dist-info}/METADATA +2 -1
  31. {compiled_knowledge-4.1.0a2.dist-info → compiled_knowledge-4.2.0a1.dist-info}/RECORD +35 -26
  32. ck/learning/train_generative.py +0 -149
  33. /ck/{dataset/cross_table_probabilities.py → probability/cross_table_probability_space.py} +0 -0
  34. {compiled_knowledge-4.1.0a2.dist-info → compiled_knowledge-4.2.0a1.dist-info}/WHEEL +0 -0
  35. {compiled_knowledge-4.1.0a2.dist-info → compiled_knowledge-4.2.0a1.dist-info}/licenses/LICENSE.txt +0 -0
  36. {compiled_knowledge-4.1.0a2.dist-info → compiled_knowledge-4.2.0a1.dist-info}/top_level.txt +0 -0
@@ -1,149 +0,0 @@
1
- from dataclasses import dataclass
2
- from typing import Dict, Tuple, List
3
-
4
- import numpy as np
5
-
6
- from ck.dataset import SoftDataset, HardDataset
7
- from ck.dataset.cross_table import CrossTable, cross_table_from_dataset
8
- from ck.pgm import PGM, Instance, DensePotentialFunction, Shape, natural_key_idx, SparsePotentialFunction
9
- from ck.utils.iter_extras import multiply
10
- from ck.utils.np_extras import NDArrayFloat64
11
-
12
-
13
- @dataclass
14
- class ParameterValues:
15
- """
16
- A ParameterValues object represents learned parameter values of a PGM.
17
- """
18
- pgm: PGM
19
- """
20
- The PGM that the parameter values pertains to.
21
- """
22
-
23
- cpts: List[Dict[Instance, NDArrayFloat64]]
24
- """
25
- A list of CPTs co-indexed with `pgm.factors`. Each CPT is a dict
26
- mapping from instances of the parent random variables (of the factors)
27
- to the child conditional probability distribution (CPD).
28
- """
29
-
30
- def set_zero(self) -> None:
31
- """
32
- Set the potential function of each PGM factor to zero.
33
- """
34
- for factor in self.pgm.factors:
35
- factor.set_zero()
36
-
37
- def set_cpt(self) -> None:
38
- """
39
- Set the potential function of each PGM factor to a CPTPotentialFunction,
40
- using our parameter values.
41
- """
42
- for factor, cpt in zip(self.pgm.factors, self.cpts):
43
- factor.set_cpt().set(*cpt.items())
44
-
45
- def set_dense(self) -> None:
46
- """
47
- Set the potential function of each PGM factor to a DensePotentialFunction,
48
- using our parameter values.
49
- """
50
- for factor, cpt in zip(self.pgm.factors, self.cpts):
51
- pot_function: DensePotentialFunction = factor.set_dense()
52
- parent_shape: Shape = factor.shape[1:]
53
- child_state: int
54
- value: float
55
- if len(parent_shape) == 0:
56
- cpd: NDArrayFloat64 = cpt[()]
57
- for child_state, value in enumerate(cpd):
58
- pot_function[child_state] = value
59
- else:
60
- parent_space: int = multiply(parent_shape)
61
- parent_states: Instance
62
- cpd: NDArrayFloat64
63
- for parent_states, cpd in cpt.items():
64
- idx: int = natural_key_idx(parent_shape, parent_states)
65
- for value in cpd:
66
- pot_function[idx] = value
67
- idx += parent_space
68
-
69
- def set_sparse(self) -> None:
70
- """
71
- Set the potential function of each PGM factor to a SparsePotentialFunction,
72
- using our parameter values.
73
- """
74
- for factor, cpt in zip(self.pgm.factors, self.cpts):
75
- pot_function: SparsePotentialFunction = factor.set_sparse()
76
- parent_states: Instance
77
- child_state: int
78
- cpd: NDArrayFloat64
79
- value: float
80
- for parent_states, cpd in cpt.items():
81
- for child_state, value in enumerate(cpd):
82
- key = (child_state,) + parent_states
83
- pot_function[key] = value
84
-
85
-
86
- def train_generative_bn(
87
- pgm: PGM,
88
- dataset: HardDataset | SoftDataset,
89
- *,
90
- dirichlet_prior: float = 0,
91
- check_bayesian_network: bool = True,
92
- ) -> ParameterValues:
93
- """
94
- Maximum-likelihood, generative training for a Bayesian network.
95
-
96
- Args:
97
- pgm: the probabilistic graphical model defining the model structure.
98
- Potential function values are ignored and need not be set.
99
- dataset: a dataset of random variable states.
100
- dirichlet_prior: a real number >= 0. See `CrossTable` for an explanation.
101
- check_bayesian_network: if true and not pgm.is_structure_bayesian an exception will be raised.
102
-
103
- Returns:
104
- a ParameterValues object that can be used to update the parameters of the given PGM.
105
-
106
- Raises:
107
- ValueError: if the given PGM does not have a Bayesian network structure, and check_bayesian_network is True.
108
- """
109
- if check_bayesian_network and not pgm.is_structure_bayesian:
110
- raise ValueError('the given PGM is not a Bayesian network')
111
- cpts: List[Dict[Instance, NDArrayFloat64]] = [
112
- cpt_from_crosstab(cross_table_from_dataset(dataset, factor.rvs, dirichlet_prior=dirichlet_prior))
113
- for factor in pgm.factors
114
- ]
115
- return ParameterValues(pgm, cpts)
116
-
117
-
118
- def cpt_from_crosstab(crosstab: CrossTable) -> Dict[Instance, NDArrayFloat64]:
119
- """
120
- Make a conditional probability table (CPT) from a cross-table.
121
-
122
- Args:
123
- crosstab: a CrossTable representing the weight of unique instances.
124
-
125
- Returns:
126
- a mapping from instances of the parent random variables to the child
127
- conditional probability distribution (CPD).
128
-
129
- Assumes:
130
- the first random variable in `crosstab.rvs` is the child random variable.
131
- """
132
- # Number of states for the child random variable.
133
- child_size: int = len(crosstab.rvs[0])
134
-
135
- # Get distribution over child states for seen parent states
136
- parents_weights: Dict[Instance, NDArrayFloat64] = {}
137
- for state, weight in crosstab.items():
138
- parent_state: Tuple[int, ...] = state[1:]
139
- child_state: int = state[0]
140
- parent_weights = parents_weights.get(parent_state)
141
- if parent_weights is None:
142
- parents_weights[parent_state] = parent_weights = np.zeros(child_size, dtype=np.float64)
143
- parent_weights[child_state] += weight
144
-
145
- # Normalise
146
- for parent_state, parent_weights in parents_weights.items():
147
- parent_weights /= parent_weights.sum()
148
-
149
- return parents_weights