Skip to content

Commit 4d4e6cd

Browse files
committed
added binary operators and any()/all() and added log1p and expm1
1 parent 01c28bd commit 4d4e6cd

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

pymc/CommonDeterministics.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -676,9 +676,9 @@ def op_to_jacobians(op, module):
676676
for op in ['div', 'truediv', 'floordiv', 'mod', 'divmod', 'pow', 'lshift', 'rshift', 'and', 'xor', 'or']:
677677
create_rl_bin_method(op, Variable, jacobians = op_to_jacobians(op, locals()))
678678

679-
# # Binary operators
680-
# for op in ['lt', 'le', 'eq', 'ne', 'gt', 'ge']:
681-
# create_bin_method(op ,Variable)
679+
# Binary operators
680+
for op in ['lt', 'le', 'eq', 'ne', 'gt', 'ge']:
681+
create_bin_method(op ,Variable)
682682

683683
# Unary operators
684684
neg_jacobians = {'self' : lambda self: -ones(shape(self))}

pymc/Node.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def logp_gradient_of_set(variable_set, calculation_set = None):
3535
3636
Returns a dictionary of the gradients.
3737
"""
38+
calculation_set = set(calculation_set) # this is important because using the 'in' operator on lists will use the __eq__method which will create a new deterministic
39+
3840
logp_gradients = {}
3941
for variable in variable_set:
4042
logp_gradients[variable] = logp_gradient(variable, calculation_set)

pymc/NumpyDeterministics.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@
1010
import inspect
1111

1212
#accumulations
13+
_boolean_accumulation_deterministics = ['any' , 'all']
1314
_accumulation_deterministics = ['sum']#['sum', 'prod']
1415

1516

1617
#transformations (broadcasted)
17-
_generic = ['abs', 'exp', 'log', 'sqrt']
18+
_generic = ['abs', 'exp', 'log', 'sqrt','expm1', 'log1p]
1819
_trig = ['sin', 'cos', 'tan', 'arcsin', 'arccos', 'arctan']
1920
_hyp_trig = ['sinh', 'cosh', 'tanh', 'arcsinh', 'arccosh', 'arctanh']
2021
_transformation_deterministics = _generic + _trig + _hyp_trig
2122
_misc_funcs1 = ['arctan2', 'hypot']
2223

23-
__all__ = _accumulation_deterministics + _transformation_deterministics + _misc_funcs1
24+
__all__ = _accumulation_deterministics + _boolean_accumulation_deterministics+ _transformation_deterministics + _misc_funcs1
2425

2526
def deterministic_from_funcs(name, eval, jacobians=None, jacobian_formats=None, dtype=np.float, mv=False):
2627
"""
@@ -160,6 +161,8 @@ def sum_jacobian_a (a, axis):
160161
sqrt_jacobians = {'x': lambda x : .5 * x **-.5}
161162
hypot_jacobians = {'x1' : lambda x1, x2 : (x1**2 + x2**2)**-.5 * x1,
162163
'x2' : lambda x1, x2 : (x1**2 + x2**2)**-.5 * x2}
164+
expm1_jacobians = exp_jacobians
165+
log1p_jacobians = {'x' : lambda x : 1.0/(1.0 + x)}
163166

164167
sin_jacobians = {'x' : lambda x : np.cos(x) }
165168
cos_jacobians = {'x' : lambda x : -np.sin(x) }
@@ -196,6 +199,13 @@ def wrapped_function(a, axis = None):
196199
locals()[function_name] = deterministic_from_funcs(function_name, wrapped_function, jacobians, jacobian_formats = {'a' : 'accumulation_operation'})
197200

198201

202+
for function_name in _boolean_accumulation_deterministics:
203+
wrapped_function = wrap_function_accum(find_element(function_name, np, error_on_fail = True))
204+
205+
locals()[function_name] = deterministic_from_funcs(function_name, wrapped_function)
206+
207+
208+
199209
def wrapped_function_trans(function):
200210
def wrapped_function(x):
201211
return function(x)

pymc/tests/test_gradients.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ def test_gradients(self):
311311

312312
def check_model_gradients(self, model):
313313

314+
model = set(model)
314315
# find the markov blanket
315316
children = set([])
316317
for s in model:

0 commit comments

Comments
 (0)