Skip to content

Prior

add(prior_a, prior_b)

import pprint a = {'a': {'a_1': 3, 'a_2': 2}, 'b': {'b_1': .1, 'b_2': .3}} b = {'a': {'a_1': 2, 'a_2': 4}, 'c': {'b_1': 3, 'b_2': 3}} pprint.pprint(add(a, b)) {'a': {'a_1': 5, 'a_2': 6}, 'b': {'b_1': 0.1, 'b_2': 0.3}, 'c': {'b_1': 3, 'b_2': 3}}

Source code in src/equation_tree/prior.py
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
def add(prior_a, prior_b):
    """
    >>> import pprint
    >>> a = {'a': {'a_1': 3, 'a_2': 2}, 'b': {'b_1': .1, 'b_2': .3}}
    >>> b = {'a': {'a_1': 2, 'a_2': 4}, 'c': {'b_1': 3, 'b_2': 3}}
    >>> pprint.pprint(add(a, b))
    {'a': {'a_1': 5, 'a_2': 6},
     'b': {'b_1': 0.1, 'b_2': 0.3},
     'c': {'b_1': 3, 'b_2': 3}}

    """
    tmp_a = copy.deepcopy(prior_a)
    tmp_b = copy.deepcopy(prior_b)

    def _rec_add(a, b):
        for k, val in a.items():
            if k in b.keys():
                if isinstance(val, dict) and isinstance(b[k], dict):
                    _rec_add(a[k], b[k])
                elif not isinstance(val, dict) and not isinstance(b[k], dict):
                    a[k] += b[k]

    _rec_add(tmp_a, tmp_b)
    return append(tmp_a, tmp_b)

filter_keys(dict_a, dict_b)

Examples:

>>> import pprint
>>> a = {'a': {'a_1': 3, 'a_2': 2}, 'b': {'b_1': .1, 'b_2': .3}, 'c': {}}
>>> b = {'a': {'a_1': 2, 'a_2': 4}, 'b': {'b_1': 3, 'c_1': 3}}
>>> pprint.pprint(filter_keys(a, b))
{'a': {'a_1': 3, 'a_2': 2}, 'b': {'b_1': 0.1}}
Source code in src/equation_tree/prior.py
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
def filter_keys(dict_a, dict_b):
    """
    Examples:
        >>> import pprint
        >>> a = {'a': {'a_1': 3, 'a_2': 2}, 'b': {'b_1': .1, 'b_2': .3}, 'c': {}}
        >>> b = {'a': {'a_1': 2, 'a_2': 4}, 'b': {'b_1': 3, 'c_1': 3}}
        >>> pprint.pprint(filter_keys(a, b))
        {'a': {'a_1': 3, 'a_2': 2}, 'b': {'b_1': 0.1}}
    """
    tmp_a = copy.deepcopy(dict_a)
    tmp_b = copy.deepcopy(dict_b)

    def _rec_f_key(a, b):
        for k in list(a.keys()):
            if k in b.keys():
                if isinstance(a[k], dict) and isinstance(b[k], dict):
                    _rec_f_key(a[k], b[k])
                else:
                    a[k] = a[k]
            else:
                del a[k]

    _rec_f_key(tmp_a, tmp_b)
    return tmp_a

get_defined_functions(prior)

Examples:

>>> test_prior = {
...     'functions': {'sin': 2},
...     'function_conditionals': {'sin': {'features': {'constants': 0, 'variables': 2},
...                                   'functions': {},
...                                       'operators': {}}},
...     'operator_conditionals': {'+': {'features': {'constants': 0, 'variables': 1},
...                                     'functions': {'cos': 2},
...                                     'operators': {'-': 1}},
...                               '-': {'features': {'constants': 0, 'variables': 0},
...                                     'functions': {'sin': 2, 'abs': 1},
...                                     'operators': {}}},
...     'operators': {'+': 1, '-': 1},
...     'structure': [0, 1, 2, 3, 2, 3, 1]}
>>> set(get_defined_functions(test_prior)) == set(['sin', 'abs', 'cos'])
True
Source code in src/equation_tree/prior.py
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
def get_defined_functions(prior):
    """
    Examples:
        >>> test_prior = {
        ...     'functions': {'sin': 2},
        ...     'function_conditionals': {'sin': {'features': {'constants': 0, 'variables': 2},
        ...                                   'functions': {},
        ...                                       'operators': {}}},
        ...     'operator_conditionals': {'+': {'features': {'constants': 0, 'variables': 1},
        ...                                     'functions': {'cos': 2},
        ...                                     'operators': {'-': 1}},
        ...                               '-': {'features': {'constants': 0, 'variables': 0},
        ...                                     'functions': {'sin': 2, 'abs': 1},
        ...                                     'operators': {}}},
        ...     'operators': {'+': 1, '-': 1},
        ...     'structure': [0, 1, 2, 3, 2, 3, 1]}
        >>> set(get_defined_functions(test_prior)) == set(['sin', 'abs', 'cos'])
        True
    """
    return _get_defined(prior, "functions", "function_conditionals")

get_defined_operators(prior)

Examples:

>>> test_prior = {
...     'function_conditionals': {'sin': {'features': {'constants': 0, 'variables': 2},
...                                   'functions': {},
...                                       'operators': {}}},
...     'functions': {'sin': 2},
...     'operator_conditionals': {'+': {'features': {'constants': 0, 'variables': 1},
...                                     'functions': {'cos': 2},
...                                     'operators': {'**': 1}},
...                               '/': {'features': {'constants': 0, 'variables': 0},
...                                     'functions': {'sin': 2},
...                                     'operators': {}}},
...     'operators': {'+': 1, '-': 1},
...     'structure': [0, 1, 2, 3, 2, 3, 1]}
>>> set(get_defined_operators(test_prior)) == set(['+', '/', '**', '-'])
True
Source code in src/equation_tree/prior.py
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
def get_defined_operators(prior):
    """
    Examples:
        >>> test_prior = {
        ...     'function_conditionals': {'sin': {'features': {'constants': 0, 'variables': 2},
        ...                                   'functions': {},
        ...                                       'operators': {}}},
        ...     'functions': {'sin': 2},
        ...     'operator_conditionals': {'+': {'features': {'constants': 0, 'variables': 1},
        ...                                     'functions': {'cos': 2},
        ...                                     'operators': {'**': 1}},
        ...                               '/': {'features': {'constants': 0, 'variables': 0},
        ...                                     'functions': {'sin': 2},
        ...                                     'operators': {}}},
        ...     'operators': {'+': 1, '-': 1},
        ...     'structure': [0, 1, 2, 3, 2, 3, 1]}
        >>> set(get_defined_operators(test_prior)) == set(['+', '/', '**', '-'])
        True
    """
    return _get_defined(prior, "operators", "operator_conditionals")

multiply(a, b)

Examples:

>>> import pprint
>>> prior_a = {'a':
...                 {'a_1':  .2, 'a_2': .8},
...             'b': {
...                 'b_1' : {'b_1_1': .25, 'b_1_2': .5, 'b_1_3': .25}
...             }
...         }
>>> prior_b = {'a':
...                 {'a_1':  4, 'a_2': 1},
...             'b': {
...                 'b_1' : {'b_1_1': 4 , 'b_1_2': 1, 'b_1_3': 4}
...             },
...             'c': {'c_1': .9, 'c_2': .1}
...         }
>>> product = multiply(prior_a, prior_b)
>>> pprint.pprint(product)
{'a': {'a_1': 0.8, 'a_2': 0.8},
 'b': {'b_1': {'b_1_1': 1.0, 'b_1_2': 0.5, 'b_1_3': 1.0}},
 'c': {'c_1': 0.9, 'c_2': 0.1}}
>>> normalize(product)
>>> pprint.pprint(product)
{'a': {'a_1': 0.5, 'a_2': 0.5},
 'b': {'b_1': {'b_1_1': 0.4, 'b_1_2': 0.2, 'b_1_3': 0.4}},
 'c': {'c_1': 0.9, 'c_2': 0.1}}
Source code in src/equation_tree/prior.py
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
def multiply(a, b):
    """
    Examples:
        >>> import pprint
        >>> prior_a = {'a':
        ...                 {'a_1':  .2, 'a_2': .8},
        ...             'b': {
        ...                 'b_1' : {'b_1_1': .25, 'b_1_2': .5, 'b_1_3': .25}
        ...             }
        ...         }
        >>> prior_b = {'a':
        ...                 {'a_1':  4, 'a_2': 1},
        ...             'b': {
        ...                 'b_1' : {'b_1_1': 4 , 'b_1_2': 1, 'b_1_3': 4}
        ...             },
        ...             'c': {'c_1': .9, 'c_2': .1}
        ...         }
        >>> product = multiply(prior_a, prior_b)
        >>> pprint.pprint(product)
        {'a': {'a_1': 0.8, 'a_2': 0.8},
         'b': {'b_1': {'b_1_1': 1.0, 'b_1_2': 0.5, 'b_1_3': 1.0}},
         'c': {'c_1': 0.9, 'c_2': 0.1}}
        >>> normalize(product)
        >>> pprint.pprint(product)
        {'a': {'a_1': 0.5, 'a_2': 0.5},
         'b': {'b_1': {'b_1_1': 0.4, 'b_1_2': 0.2, 'b_1_3': 0.4}},
         'c': {'c_1': 0.9, 'c_2': 0.1}}
    """
    tmp_a = copy.deepcopy(a)
    tmp_b = copy.deepcopy(b)

    def _multiply(_a, _b):
        for k, val in _a.items():
            if k in _b.keys():
                if isinstance(val, dict) and isinstance(_b[k], dict):
                    _multiply(val, _b[k])
                elif not isinstance(val, dict) and not isinstance(_b[k], dict):
                    res = val * _b[k]
                    _a[k] = res

    _multiply(tmp_a, tmp_b)
    tmp_a = append(tmp_a, tmp_b)

    return tmp_a

prior_from_space(space)

Uniform prior from a list of strings Examples: >>> import pprint >>> operator_space = ['+', '-', '', '/', '^'] >>> operator_prior = prior_from_space(operator_space) >>> pprint.pprint(operator_prior) {'': 0.2, '+': 0.2, '-': 0.2, '/': 0.2, '^': 0.2}

>>> function_space = ['sin', 'cos', 'tan', 'exp', 'log', 'sqrt', 'abs']
>>> function_prior = prior_from_space(function_space)
>>> pprint.pprint(function_prior)
{'abs': 0.14285714285714285,
 'cos': 0.14285714285714285,
 'exp': 0.14285714285714285,
 'log': 0.14285714285714285,
 'sin': 0.14285714285714285,
 'sqrt': 0.14285714285714285,
 'tan': 0.14285714285714285}
Source code in src/equation_tree/prior.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def prior_from_space(space):
    """
    Uniform prior from a list of strings
    Examples:
        >>> import pprint
        >>> operator_space = ['+', '-', '*', '/', '^']
        >>> operator_prior = prior_from_space(operator_space)
        >>> pprint.pprint(operator_prior)
        {'*': 0.2, '+': 0.2, '-': 0.2, '/': 0.2, '^': 0.2}

        >>> function_space = ['sin', 'cos', 'tan', 'exp', 'log', 'sqrt', 'abs']
        >>> function_prior = prior_from_space(function_space)
        >>> pprint.pprint(function_prior)
        {'abs': 0.14285714285714285,
         'cos': 0.14285714285714285,
         'exp': 0.14285714285714285,
         'log': 0.14285714285714285,
         'sin': 0.14285714285714285,
         'sqrt': 0.14285714285714285,
         'tan': 0.14285714285714285}
    """
    return priors_from_space(space)

re_normalize(target, origin)

Source code in src/equation_tree/prior.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
def re_normalize(target, origin):
    """ """
    tmp_t = copy.deepcopy(target)
    tmp_o = copy.deepcopy(origin)

    def _rec_re_normalize(t, p):
        for k, val in t.items():
            if isinstance(val, dict) and isinstance(p[k], dict):
                _rec_re_normalize(t[k], p[k])
            elif not isinstance(val, dict):
                if not isinstance(p[k], dict) and val <= 0 < p[k]:
                    t[k] = MIN
                elif val <= 0:
                    t[k] = 0

    _rec_re_normalize(tmp_t, tmp_o)
    normalize(tmp_t)
    return tmp_t

scalar_multiply(scalar, prior)

Examples:

>>> import pprint
>>> m = {'a': {'a_1': 3, 'a_2': 2}, 'b': {'b_1': .1, 'b_2': .3}}
>>> s = 2
>>> pprint.pprint(scalar_multiply(s, m))
{'a': {'a_1': 6, 'a_2': 4}, 'b': {'b_1': 0.2, 'b_2': 0.6}}
Source code in src/equation_tree/prior.py
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
def scalar_multiply(scalar, prior):
    """
    Examples:
        >>> import pprint
        >>> m = {'a': {'a_1': 3, 'a_2': 2}, 'b': {'b_1': .1, 'b_2': .3}}
        >>> s = 2
        >>> pprint.pprint(scalar_multiply(s, m))
        {'a': {'a_1': 6, 'a_2': 4}, 'b': {'b_1': 0.2, 'b_2': 0.6}}
    """
    tmp_p = copy.deepcopy(prior)

    def _rec_s_mult(s, p):
        for k, val in p.items():
            if isinstance(val, dict):
                _rec_s_mult(s, p[k])
            else:
                p[k] *= s

    _rec_s_mult(scalar, tmp_p)
    return tmp_p

structure_prior_from_depth(depth)

Uniform structure prior for a given depth Examples: >>> import pprint >>> pprint.pprint(structure_prior_from_depth(3)) {'[0, 1, 1]': 0.5, '[0, 1, 2]': 0.5} >>> pprint.pprint(structure_prior_from_depth(4)) {'[0, 1, 1, 2]': 0.25, '[0, 1, 2, 1]': 0.25, '[0, 1, 2, 2]': 0.25, '[0, 1, 2, 3]': 0.25}

Source code in src/equation_tree/prior.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def structure_prior_from_depth(depth):
    """
    Uniform structure prior for a given depth
    Examples:
        >>> import pprint
        >>> pprint.pprint(structure_prior_from_depth(3))
        {'[0, 1, 1]': 0.5, '[0, 1, 2]': 0.5}
        >>> pprint.pprint(structure_prior_from_depth(4))
        {'[0, 1, 1, 2]': 0.25,
         '[0, 1, 2, 1]': 0.25,
         '[0, 1, 2, 2]': 0.25,
         '[0, 1, 2, 3]': 0.25}

    """
    structures = _gen_tree_structures(depth)
    structures = [s for s in structures if len(s) == depth]
    return prior_from_space([str(s) for s in structures])

structure_prior_from_max_depth(max_depth)

Uniform structure prior up to a given depth (from min depth 3) Examples: >>> import pprint >>> pprint.pprint(structure_prior_from_max_depth(3)) {'[0, 1, 1]': 0.5, '[0, 1, 2]': 0.5} >>> pprint.pprint(structure_prior_from_max_depth(4)) {'[0, 1, 1, 2]': 0.16666666666666666, '[0, 1, 1]': 0.16666666666666666, '[0, 1, 2, 1]': 0.16666666666666666, '[0, 1, 2, 2]': 0.16666666666666666, '[0, 1, 2, 3]': 0.16666666666666666, '[0, 1, 2]': 0.16666666666666666} >>> pprint.pprint(structure_prior_from_max_depth(5)) {'[0, 1, 1, 2, 2]': 0.06666666666666667, '[0, 1, 1, 2, 3]': 0.06666666666666667, '[0, 1, 1, 2]': 0.06666666666666667, '[0, 1, 1]': 0.06666666666666667, '[0, 1, 2, 1, 2]': 0.06666666666666667, '[0, 1, 2, 1]': 0.06666666666666667, '[0, 1, 2, 2, 1]': 0.06666666666666667, '[0, 1, 2, 2, 3]': 0.06666666666666667, '[0, 1, 2, 2]': 0.06666666666666667, '[0, 1, 2, 3, 1]': 0.06666666666666667, '[0, 1, 2, 3, 2]': 0.06666666666666667, '[0, 1, 2, 3, 3]': 0.06666666666666667, '[0, 1, 2, 3, 4]': 0.06666666666666667, '[0, 1, 2, 3]': 0.06666666666666667, '[0, 1, 2]': 0.06666666666666667}

Source code in src/equation_tree/prior.py
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def structure_prior_from_max_depth(max_depth):
    """
    Uniform structure prior up to a given depth (from min depth 3)
    Examples:
        >>> import pprint
        >>> pprint.pprint(structure_prior_from_max_depth(3))
        {'[0, 1, 1]': 0.5, '[0, 1, 2]': 0.5}
        >>> pprint.pprint(structure_prior_from_max_depth(4))
        {'[0, 1, 1, 2]': 0.16666666666666666,
         '[0, 1, 1]': 0.16666666666666666,
         '[0, 1, 2, 1]': 0.16666666666666666,
         '[0, 1, 2, 2]': 0.16666666666666666,
         '[0, 1, 2, 3]': 0.16666666666666666,
         '[0, 1, 2]': 0.16666666666666666}
        >>> pprint.pprint(structure_prior_from_max_depth(5))
        {'[0, 1, 1, 2, 2]': 0.06666666666666667,
         '[0, 1, 1, 2, 3]': 0.06666666666666667,
         '[0, 1, 1, 2]': 0.06666666666666667,
         '[0, 1, 1]': 0.06666666666666667,
         '[0, 1, 2, 1, 2]': 0.06666666666666667,
         '[0, 1, 2, 1]': 0.06666666666666667,
         '[0, 1, 2, 2, 1]': 0.06666666666666667,
         '[0, 1, 2, 2, 3]': 0.06666666666666667,
         '[0, 1, 2, 2]': 0.06666666666666667,
         '[0, 1, 2, 3, 1]': 0.06666666666666667,
         '[0, 1, 2, 3, 2]': 0.06666666666666667,
         '[0, 1, 2, 3, 3]': 0.06666666666666667,
         '[0, 1, 2, 3, 4]': 0.06666666666666667,
         '[0, 1, 2, 3]': 0.06666666666666667,
         '[0, 1, 2]': 0.06666666666666667}
    """
    structures = _gen_tree_structures(max_depth)
    return prior_from_space([str(s) for s in structures])

subtract(minuend, subtrahend)

Minuend - Subtrahend Examples: >>> import pprint >>> m = {'a': {'a_1': 3, 'a_2': 2}, 'b': {'b_1': .1, 'b_2': .3}} >>> s = {'a': {'a_1': 2, 'a_2': 4}} >>> pprint.pprint(subtract(m, s)) {'a': {'a_1': 1, 'a_2': -2}, 'b': {'b_1': 0.0, 'b_2': 0.0}}

Source code in src/equation_tree/prior.py
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
def subtract(minuend, subtrahend):
    """
    Minuend - Subtrahend
    Examples:
        >>> import pprint
        >>> m = {'a': {'a_1': 3, 'a_2': 2}, 'b': {'b_1': .1, 'b_2': .3}}
        >>> s = {'a': {'a_1': 2, 'a_2': 4}}
        >>> pprint.pprint(subtract(m, s))
        {'a': {'a_1': 1, 'a_2': -2}, 'b': {'b_1': 0.0, 'b_2': 0.0}}

    """
    tmp_m = copy.deepcopy(minuend)
    tmp_s = copy.deepcopy(subtrahend)

    def _rec_sub(m, s):
        for k, val in m.items():
            if k in s.keys():
                if isinstance(val, dict) and isinstance(s[k], dict):
                    _rec_sub(val, s[k])
                elif (
                    not isinstance(val, dict)
                    and is_numeric(val)
                    and not isinstance(s[k], dict)
                    and is_numeric(s[k])
                ):
                    m[k] -= s[k]
                else:
                    if not isinstance(val, dict):
                        m[k] = 0.0
                    else:
                        _set_zero(m[k])
            else:
                if not isinstance(val, dict):
                    m[k] = 0.0
                else:
                    _set_zero(m[k])

    _rec_sub(tmp_m, tmp_s)
    return tmp_m