causing 2.4.4__tar.gz → 2.4.5__tar.gz
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.
- {causing-2.4.4 → causing-2.4.5}/PKG-INFO +1 -1
- {causing-2.4.4 → causing-2.4.5}/causing/model.py +42 -36
- {causing-2.4.4 → causing-2.4.5}/causing.egg-info/PKG-INFO +1 -1
- {causing-2.4.4 → causing-2.4.5}/setup.py +1 -1
- {causing-2.4.4 → causing-2.4.5}/LICENSE.md +0 -0
- {causing-2.4.4 → causing-2.4.5}/README.md +0 -0
- {causing-2.4.4 → causing-2.4.5}/causing/__init__.py +0 -0
- {causing-2.4.4 → causing-2.4.5}/causing/graph.py +0 -0
- {causing-2.4.4 → causing-2.4.5}/causing/utils.py +0 -0
- {causing-2.4.4 → causing-2.4.5}/causing.egg-info/SOURCES.txt +0 -0
- {causing-2.4.4 → causing-2.4.5}/causing.egg-info/dependency_links.txt +0 -0
- {causing-2.4.4 → causing-2.4.5}/causing.egg-info/requires.txt +0 -0
- {causing-2.4.4 → causing-2.4.5}/causing.egg-info/top_level.txt +0 -0
- {causing-2.4.4 → causing-2.4.5}/setup.cfg +0 -0
- {causing-2.4.4 → causing-2.4.5}/tests/__init__.py +0 -0
- {causing-2.4.4 → causing-2.4.5}/tests/examples/__init__.py +0 -0
- {causing-2.4.4 → causing-2.4.5}/tests/examples/models.py +0 -0
- {causing-2.4.4 → causing-2.4.5}/tests/test_estimate.py +0 -0
- {causing-2.4.4 → causing-2.4.5}/tests/utils.py +0 -0
|
@@ -51,65 +51,56 @@ class Model:
|
|
|
51
51
|
def compute(
|
|
52
52
|
self,
|
|
53
53
|
xdat: np.array,
|
|
54
|
-
# fix a yval
|
|
55
54
|
fixed_yval: np.array = None,
|
|
56
55
|
fixed_yind: int = None,
|
|
57
|
-
# fix an arbitrary node going into a yval
|
|
58
56
|
fixed_from_ind: int = None,
|
|
59
57
|
fixed_to_yind: int = None,
|
|
60
58
|
fixed_vals: list = None,
|
|
61
|
-
# override default parameter values
|
|
62
59
|
parameters: dict[str, float] = {},
|
|
63
60
|
) -> np.array:
|
|
64
|
-
"""Compute y values for given x values
|
|
65
|
-
|
|
61
|
+
"""Compute y values for given x values (Optimized Version)
|
|
66
62
|
xdat: m rows, tau columns
|
|
67
63
|
returns: n rows, tau columns
|
|
68
64
|
"""
|
|
65
|
+
|
|
69
66
|
assert xdat.ndim == 2, f"xdat must be m*tau (is {xdat.ndim}-dimensional)"
|
|
70
67
|
assert xdat.shape[0] == self.mdim, f"xdat must be m*tau (is {xdat.shape})"
|
|
71
68
|
tau = xdat.shape[1]
|
|
72
69
|
parameters = self.parameters | parameters
|
|
73
70
|
|
|
74
|
-
|
|
71
|
+
# Use np.full for clarity and potential small performance gain
|
|
72
|
+
yhat = np.full((self.ndim, tau), np.nan)
|
|
73
|
+
|
|
75
74
|
for i, eq in enumerate(self._model_lam):
|
|
75
|
+
|
|
76
76
|
if fixed_yind == i:
|
|
77
77
|
yhat[i, :] = fixed_yval
|
|
78
78
|
else:
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
79
|
+
# 1. Build inputs (Vectorized)
|
|
80
|
+
# Use np.vstack for efficient vertical stacking.
|
|
81
|
+
# `yhat` will have NaNs for unsolved variables, which is correct.
|
|
82
|
+
eq_inputs = np.vstack([xdat, yhat])
|
|
83
|
+
|
|
84
|
+
# 2. Apply fixed values if needed (Vectorized)
|
|
82
85
|
if fixed_to_yind == i:
|
|
83
|
-
|
|
86
|
+
# Directly modify the correct "row" in the input matrix.
|
|
87
|
+
# This is much cleaner and faster.
|
|
88
|
+
eq_inputs[fixed_from_ind, :] = fixed_vals
|
|
84
89
|
|
|
90
|
+
# 3. Evaluate equation (Vectorized)
|
|
85
91
|
try:
|
|
86
|
-
# print(f"Comuting variable: {self.yvars[i]}")
|
|
87
|
-
# yhat[i] = np.array(
|
|
88
|
-
# [eq(*eq_in, *parameters.values()) for eq_in in eq_inputs],
|
|
89
|
-
# dtype=np.float64,
|
|
90
|
-
# )
|
|
91
92
|
np.seterr(under="ignore")
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
yhat[i] =
|
|
97
|
-
|
|
98
|
-
dtype=np.float64,
|
|
99
|
-
)
|
|
93
|
+
|
|
94
|
+
# This is the core optimization. We unpack the rows of `eq_inputs`
|
|
95
|
+
# as separate arguments into the lambdified function. NumPy will
|
|
96
|
+
# then compute the results for all `tau` columns at once.
|
|
97
|
+
yhat[i, :] = eq(*eq_inputs, *parameters.values())
|
|
98
|
+
|
|
100
99
|
except Exception as e:
|
|
101
|
-
# for eq_in in eq_inputs:
|
|
102
|
-
# print("--", self.yvars[i])
|
|
103
|
-
# for var, val in zip(
|
|
104
|
-
# self.vars + list(parameters.keys()),
|
|
105
|
-
# list(eq_in) + list(parameters.values()),
|
|
106
|
-
# ):
|
|
107
|
-
# print(var, "=", val)
|
|
108
|
-
# eq(*eq_in, *parameters.values())
|
|
109
100
|
raise NumericModelError(
|
|
110
101
|
f"Failed to compute model value for yvar {self.yvars[i]}: {e}"
|
|
111
102
|
) from e
|
|
112
|
-
|
|
103
|
+
|
|
113
104
|
return yhat
|
|
114
105
|
|
|
115
106
|
def calc_effects(self, xdat: np.array, xdat_mean=None, yhat_mean=None):
|
|
@@ -198,10 +189,25 @@ class Model:
|
|
|
198
189
|
|
|
199
190
|
@cached_property
|
|
200
191
|
def _model_lam(self) -> Iterable[Callable]:
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
192
|
+
"""Create lambdified equations with NumPy-compatible functions."""
|
|
193
|
+
lambdas = []
|
|
194
|
+
ordered_vars = self.vars + list(self.parameters.keys())
|
|
195
|
+
|
|
196
|
+
# Define placeholder for vectorized max function
|
|
197
|
+
vectorized_max = sympy.Function("vectorized_max")
|
|
198
|
+
|
|
199
|
+
# Define custom translation mapping
|
|
200
|
+
custom_modules = [{"vectorized_max": np.maximum}, "numpy"]
|
|
201
|
+
|
|
202
|
+
for i, eq in enumerate(self.equations):
|
|
203
|
+
# Replace sympy.Max with our placeholder
|
|
204
|
+
fixed_eq = eq.subs(sympy.Max, vectorized_max)
|
|
205
|
+
|
|
206
|
+
# Lambdify with custom NumPy mapping
|
|
207
|
+
lam = sympy.lambdify(ordered_vars, fixed_eq, modules=custom_modules)
|
|
208
|
+
lambdas.append(lam)
|
|
209
|
+
|
|
210
|
+
return lambdas
|
|
205
211
|
|
|
206
212
|
@cached_property
|
|
207
213
|
def final_ind(self):
|
|
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
|
|
|
5
5
|
|
|
6
6
|
setuptools.setup(
|
|
7
7
|
name="causing",
|
|
8
|
-
version="2.4.
|
|
8
|
+
version="2.4.5",
|
|
9
9
|
author="Dr. Holger Bartel",
|
|
10
10
|
author_email="holger.bartel@realrate.ai",
|
|
11
11
|
description="Causing: CAUSal INterpretation using Graphs",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|