NIPY logo

Site Navigation

NIPY Community

Github repo

Table Of Contents

This Page

algorithms.statistics.formula.formulae

Module: algorithms.statistics.formula.formulae

Inheritance diagram for nipy.algorithms.statistics.formula.formulae:

Formula objects

A formula is basically a sympy expression for the mean of something of the form:

mean = sum([Beta(e)*e for e in expr])

Or, a linear combination of sympy expressions, with each one multiplied by its own “Beta”. The elements of expr can be instances of Term (for a linear regression formula, they would all be instances of Term). But, in general, there might be some other parameters (i.e. sympy.Symbol instances) that are not Terms.

The design matrix is made up of columns that are the derivatives of mean with respect to everything that is not a Term, evaluted at a recarray that has field names given by [str(t) for t in self.terms].

For those familiar with R’s formula syntax, if we wanted a design matrix like the following:

> s.table = read.table("http://www-stat.stanford.edu/~jtaylo/courses/stats191/data/supervisor.table", header=T)
> d = model.matrix(lm(Y ~ X1*X3, s.table)
)
> d
   (Intercept) X1 X3 X1:X3
1            1 51 39  1989
2            1 64 54  3456
3            1 70 69  4830
4            1 63 47  2961
5            1 78 66  5148
6            1 55 44  2420
7            1 67 56  3752
8            1 75 55  4125
9            1 82 67  5494
10           1 61 47  2867
11           1 53 58  3074
12           1 60 39  2340
13           1 62 42  2604
14           1 83 45  3735
15           1 77 72  5544
16           1 90 72  6480
17           1 85 69  5865
18           1 60 75  4500
19           1 70 57  3990
20           1 58 54  3132
21           1 40 34  1360
22           1 61 62  3782
23           1 66 50  3300
24           1 37 58  2146
25           1 54 48  2592
26           1 77 63  4851
27           1 75 74  5550
28           1 57 45  2565
29           1 85 71  6035
30           1 82 59  4838
attr(,"assign")
[1] 0 1 2 3
>

With the Formula, it looks like this:

>>> r = np.rec.array([
...     (43, 51, 30, 39, 61, 92, 45), (63, 64, 51, 54, 63, 73, 47), 
...     (71, 70, 68, 69, 76, 86, 48), (61, 63, 45, 47, 54, 84, 35),
...     (81, 78, 56, 66, 71, 83, 47), (43, 55, 49, 44, 54, 49, 34),
...     (58, 67, 42, 56, 66, 68, 35), (71, 75, 50, 55, 70, 66, 41),
...     (72, 82, 72, 67, 71, 83, 31), (67, 61, 45, 47, 62, 80, 41),
...     (64, 53, 53, 58, 58, 67, 34), (67, 60, 47, 39, 59, 74, 41),
...     (69, 62, 57, 42, 55, 63, 25), (68, 83, 83, 45, 59, 77, 35),
...     (77, 77, 54, 72, 79, 77, 46), (81, 90, 50, 72, 60, 54, 36),
...     (74, 85, 64, 69, 79, 79, 63), (65, 60, 65, 75, 55, 80, 60),
...     (65, 70, 46, 57, 75, 85, 46), (50, 58, 68, 54, 64, 78, 52),
...     (50, 40, 33, 34, 43, 64, 33), (64, 61, 52, 62, 66, 80, 41),
...     (53, 66, 52, 50, 63, 80, 37), (40, 37, 42, 58, 50, 57, 49),
...     (63, 54, 42, 48, 66, 75, 33), (66, 77, 66, 63, 88, 76, 72),
...     (78, 75, 58, 74, 80, 78, 49), (48, 57, 44, 45, 51, 83, 38),
...     (85, 85, 71, 71, 77, 74, 55), (82, 82, 39, 59, 64, 78, 39)],
...              dtype=[('y', '<i8'), ('x1', '<i8'), ('x2', '<i8'),
...                     ('x3', '<i8'), ('x4', '<i8'), ('x5', '<i8'),
...                     ('x6', '<i8')])
>>> x1 = Term('x1'); x3 = Term('x3')
>>> f = Formula([x1, x3, x1*x3]) + I
>>> f.mean
_b0*x1 + _b1*x3 + _b2*x1*x3 + _b3

The I is the “intercept” term, I have explicity not used R’s default of adding it to everything.

>>> f.design(r)
array([(51.0, 39.0, 1989.0, 1.0), (64.0, 54.0, 3456.0, 1.0),
       (70.0, 69.0, 4830.0, 1.0), (63.0, 47.0, 2961.0, 1.0),
       (78.0, 66.0, 5148.0, 1.0), (55.0, 44.0, 2420.0, 1.0),
       (67.0, 56.0, 3752.0, 1.0), (75.0, 55.0, 4125.0, 1.0),
       (82.0, 67.0, 5494.0, 1.0), (61.0, 47.0, 2867.0, 1.0),
       (53.0, 58.0, 3074.0, 1.0), (60.0, 39.0, 2340.0, 1.0),
       (62.0, 42.0, 2604.0, 1.0), (83.0, 45.0, 3735.0, 1.0),
       (77.0, 72.0, 5544.0, 1.0), (90.0, 72.0, 6480.0, 1.0),
       (85.0, 69.0, 5865.0, 1.0), (60.0, 75.0, 4500.0, 1.0),
       (70.0, 57.0, 3990.0, 1.0), (58.0, 54.0, 3132.0, 1.0),
       (40.0, 34.0, 1360.0, 1.0), (61.0, 62.0, 3782.0, 1.0),
       (66.0, 50.0, 3300.0, 1.0), (37.0, 58.0, 2146.0, 1.0),
       (54.0, 48.0, 2592.0, 1.0), (77.0, 63.0, 4851.0, 1.0),
       (75.0, 74.0, 5550.0, 1.0), (57.0, 45.0, 2565.0, 1.0),
       (85.0, 71.0, 6035.0, 1.0), (82.0, 59.0, 4838.0, 1.0)], 
      dtype=[('x1', '<f8'), ('x3', '<f8'), ('x1*x3', '<f8'), ('1', '<f8')])

Classes

Beta

class nipy.algorithms.statistics.formula.formulae.Beta

Bases: sympy.core.symbol.Dummy

A symbol tied to a Term term

Methods

as_base_exp
as_basic
as_coeff_exponent
as_coeff_factors
as_coeff_terms
as_coefficient
as_dummy
as_independent
as_leading_term
as_numer_denom
as_poly
as_powers_dict
as_real_imag
atoms
coeff
compare
compare_pretty
conjugate
could_extract_minus_sign
count_ops
diff
doit
evalf
expand
extract_additively
extract_multiplicatively
fdiff
getO
has
has_all_symbols
has_any_symbols
integrate
is_hypergeometric
is_polynomial
is_rational_function
iter_basic_args
leadterm
limit
lseries
match
matches
n
new Create new objects of various types.
normal
nseries
removeO
rewrite
series
solve4linearsymbol
subs
__init__()

x.__init__(...) initializes x; see help(type(x)) for signature

args

Returns a tuple of arguments of ‘self’.

Example:

>>> from sympy import symbols, cot
>>> from sympy.abc import x, y
>>> cot(x).args
(x,)
>>> cot(x).args[0]
x
>>> (x*y).args
(x, y)
>>> (x*y).args[1]
y

Note for developers: Never use self._args, always use self.args. Only when you are creating your own new function, use _args in the __new__. Don’t override .args() from Basic (so that it’s easy to change the interface in the future if needed).

as_base_exp()
as_basic()

Converts polynomial to a valid sympy expression.

>>> from sympy import sin
>>> from sympy.abc import x, y
>>> p = (x**2 + x*y).as_poly(x, y)
>>> p.as_basic()
x*y + x**2
>>> f = sin(x)
>>> f.as_basic()
sin(x)
as_coeff_exponent(x)

c*x**e -> c,e where x can be any symbolic expression.

as_coeff_factors(x=None)
as_coeff_terms(x=None)
as_coefficient(expr)

Extracts symbolic coefficient at the given expression. In other words, this functions separates ‘self’ into product of ‘expr’ and ‘expr’-free coefficient. If such separation is not possible it will return None.

>>> from sympy import E, pi, sin, I
>>> from sympy.abc import x, y
>>> E.as_coefficient(E)
1
>>> (2*E).as_coefficient(E)
2
>>> (2*E + x).as_coefficient(E)
>>> (2*sin(E)*E).as_coefficient(E)
>>> (2*pi*I).as_coefficient(pi*I)
2
>>> (2*I).as_coefficient(pi*I)
as_dummy()
as_independent(*deps)

Returns a pair with separated parts of a given expression independent of specified symbols in the first place and dependent on them in the other. Both parts are valid SymPy expressions.

>>> from sympy import sin, cos
>>> from sympy.abc import x, y
>>> (2*x*sin(x)+y+x).as_independent(x)
(y, x + 2*x*sin(x))
>>> (x*sin(x)*cos(y)).as_independent(x)
(cos(y), x*sin(x))

All other expressions are multiplicative:

>>> (sin(x)).as_independent(x)
(1, sin(x))
>>> (sin(x)).as_independent(y)
(sin(x), 1)
as_leading_term(*args, **kw_args)

Returns the leading term.

Example:

>>> from sympy.abc import x
>>> (1+x+x**2).as_leading_term(x)
1
>>> (1/x**2+x+x**2).as_leading_term(x)
x**(-2)

Note:

self is assumed to be the result returned by Basic.series().

as_numer_denom()
as_poly(*symbols, **flags)

Converts ‘self’ to a polynomial or returns None.

When constructing a polynomial an exception will be raised in case the input expression is not convertible to a polynomial. There are situations when it is easier (simpler or prettier) to receive None on failure.

If no symbols were given and ‘self’ isn’t already a polynomial then all available symbols will be collected and used to form a new polynomial.

>>> from sympy import Poly, sin
>>> from sympy.abc import x, y
>>> print (x**2 + x*y).as_poly()
Poly(x**2 + x*y, x, y)
>>> print (x**2 + x*y).as_poly(x, y)
Poly(x**2 + x*y, x, y)
>>> print (x**2 + sin(y)).as_poly(x, y)
None
as_powers_dict()
as_real_imag()

Performs complex expansion on ‘self’ and returns a tuple containing collected both real and imaginary parts. This method can’t be confused with re() and im() functions, which does not perform complex expansion at evaluation.

However it is possible to expand both re() and im() functions and get exactly the same results as with a single call to this function.

>>> from sympy import symbols, I
>>> x, y = symbols('xy', real=True)
>>> (x + y*I).as_real_imag()
(x, y)
>>> from sympy.abc import z, w
>>> (z + w*I).as_real_imag()
(-im(w) + re(z), im(z) + re(w))
assumptions0

Return object type assumptions.

For example:

Symbol(‘x’, real=True) Symbol(‘x’, integer=True)

are different objects. In other words, besides Python type (Symbol in this case), the initial assumptions are also forming their typeinfo.

Example:

>>> from sympy import Symbol
>>> from sympy.abc import x
>>> x.assumptions0
{}
>>> x = Symbol("x", positive=True)
>>> x.assumptions0
{'commutative': True, 'complex': True, 'imaginary': False,
'negative': False, 'nonnegative': True, 'nonpositive': False,
'nonzero': True, 'positive': True, 'real': True, 'zero': False}
atoms(*types)

Returns the atoms that form the current object.

By default, only objects that are truly atomic and can’t be divided into smaller pieces are returned: symbols, numbers, and number symbols like I and pi. It is possible to request atoms of any type, however, as demonstrated below.

Examples:

>>> from sympy import I, pi, sin
>>> from sympy.abc import x, y
>>> list((1+x+2*sin(y+I*pi)).atoms())

[y, I, 2, x, 1, pi]

If one or more types are given, the results will contain only those types of atoms:

Examples:

>>> from sympy import Number, NumberSymbol, Symbol
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Symbol))

[x, y]

>>> sorted((1+x+2*sin(y+I*pi)).atoms(Number))
[1, 2]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Number, NumberSymbol))
[1, 2, pi]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Number, NumberSymbol, I))
[1, 2, pi, I]

Note that I (imaginary unit) and zoo (complex infinity) are special types of number symbols and are not part of the NumberSymbol class.

The type can be given implicitly, too:

>>> sorted((1+x+2*sin(y+I*pi)).atoms(x)) # x is a Symbol

[x, y]

Be careful to check your assumptions when using the implicit option since S(1).is_Integer = True but type(S(1)) is One, a special type of sympy atom, while type(S(2)) is type Integer and will find all integers in an expression:

>>> from sympy import S
>>> sorted((1+x+2*sin(y+I*pi)).atoms(S(1)))

[1]

>>> sorted((1+x+2*sin(y+I*pi)).atoms(S(2)))
[1, 2]

Finally, arguments to atoms() can select more than atomic atoms: any sympy type (loaded in core/__init__.py) can be listed as an argument and those types of “atoms” as found in scanning the arguments of the expression nonrecursively:

>>> from sympy import Function, Mul
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Function))

[sin(y + pi*I)]

>>> sorted((1+x+2*sin(y+I*pi)).atoms(Mul))
[2*sin(y + pi*I)]
coeff(x, expand=True)

Returns the coefficient of the term “x” or None if there is no “x”.

Optional expand keyword argument allows one to control whether the expression is expanded before terms are collected, which can be useful if the term “x” isn’t nested inside of terms and you don’t want the returned coefficient to be expanded.

Example:

>>> from sympy import symbols
>>> from sympy.abc import x, y, z
>>> (3+2*x+4*x**2).coeff(1)
>>> (3+2*x+4*x**2).coeff(x)
2
>>> (3+2*x+4*x**2).coeff(x**2)
4
>>> (3+2*x+4*x**2).coeff(x**3)
>>> (z*(x+y)**2).coeff(z)
2*x*y + x**2 + y**2
>>> (z*(x+y)**2).coeff(z, expand=False)
(x + y)**2
>>>
compare(other)

Return -1,0,1 if the object is smaller, equal, or greater than other.

Not in the mathematical sense. If the object is of a different type from the “other” then their classes are ordered according to the sorted_classes list.

Example:

>>> from sympy.abc import x, y
>>> x.compare(y)
-1
>>> x.compare(x)
0
>>> y.compare(x)
1
static compare_pretty(a, b)

Is a > b in the sense of ordering in printing?

yes ..... return 1 no ...... return -1 equal ... return 0

Strategy:

It uses Basic.compare as a fallback, but improves it in many cases, like x**3, x**4, O(x**3) etc. In those simple cases, it just parses the expression and returns the “sane” ordering such as:

1 < x < x**2 < x**3 < O(x**4) etc.

Example:

>>> from sympy.abc import x
>>> from sympy import Basic
>>> Basic._compare_pretty(x, x**2)
-1
>>> Basic._compare_pretty(x**2, x**2)
0
>>> Basic._compare_pretty(x**3, x**2)
1
conjugate()
could_extract_minus_sign()

Canonical way to choose an element in the set {e, -e} where e is any expression. If the canonical element is e, we have e.could_extract_minus_sign() == True, else e.could_extract_minus_sign() == False.

For any expression, the set {e.could_extract_minus_sign(), (-e).could_extract_minus_sign()} must be {True, False}.

>>> from sympy.abc import x, y
>>> (x-y).could_extract_minus_sign() != (y-x).could_extract_minus_sign()
True
count_ops(symbolic=True)
diff(*symbols, **assumptions)
doit(**hints)
dummy_index
evalf(x, n=15, **options)

Evaluate the given formula to an accuracy of n digits. Optional keyword arguments:

subs=<dict>
Substitute numerical values for symbols, e.g. subs={x:3, y:1+pi}.
maxprec=N
Allow a maximum temporary working precision of N digits (default=100)
chop=<bool>
Replace tiny real or imaginary parts in subresults by exact zeros (default=False)
strict=<bool>
Raise PrecisionExhausted if any subresult fails to evaluate to full accuracy, given the available maxprec (default=False)
quad=<str>
Choose algorithm for numerical quadrature. By default, tanh-sinh quadrature is used. For oscillatory integrals on an infinite interval, try quad=’osc’.
verbose=<bool>
Print debug information (default=False)
expand(deep=True, power_base=True, power_exp=True, mul=True, log=True, multinomial=True, basic=True, **hints)

Expand an expression using hints.

See the docstring in function.expand for more information.

extract_additively(c)

Return None if it’s not possible to make self in the form something + c in a nice way, i.e. preserving the properties of arguments of self.

>>> from sympy import symbols
>>> x, y = symbols('xy', real=True)
>>> ((x*y)**3).extract_additively(1)
>>> (x+1).extract_additively(x)
1
>>> (x+1).extract_additively(2*x)
>>> (x+1).extract_additively(-x)
1 + 2*x
>>> (-x+1).extract_additively(2*x)
1 - 3*x
extract_multiplicatively(c)

Return None if it’s not possible to make self in the form c * something in a nice way, i.e. preserving the properties of arguments of self.

>>> from sympy import symbols, Rational
>>> x, y = symbols('xy', real=True)
>>> ((x*y)**3).extract_multiplicatively(x**2 * y)
x*y**2
>>> ((x*y)**3).extract_multiplicatively(x**4 * y)
>>> (2*x).extract_multiplicatively(2)
x
>>> (2*x).extract_multiplicatively(3)
>>> (Rational(1,2)*x).extract_multiplicatively(3)
x/6
fdiff(*indices)
func

The top-level function in an expression.

The following should hold for all objects:

>> x == x.func(*x.args)

Example:

>>> from sympy.abc import x
>>> a = 2*x
>>> a.func
<class 'sympy.core.mul.Mul'>
>>> a.args
(2, x)
>>> a.func(*a.args)
2*x
>>> a == a.func(*a.args)
True
getO(e)

Returns the O(..) symbol, or None if there is none.

has(*patterns)

Return True if self has any of the patterns.

Example: >>> from sympy.abc import x >>> (2*x).has(x) True >>> (2*x/x).has(x) False

has_all_symbols(*args, **kw_args)

Return True if ‘self’ has all of the symbols.

>>> from sympy import sin
>>> from sympy.abc import x, y, z
>>> (x**2 + sin(x*y)).has_all_symbols(x, y)
True
>>> (x**2 + sin(x*y)).has_all_symbols(x, y, z)
False
has_any_symbols(*args, **kw_args)

Return True if ‘self’ has any of the symbols.

>>> from sympy import sin
>>> from sympy.abc import x, y, z
>>> (x**2 + sin(x*y)).has_any_symbols(z)
False
>>> (x**2 + sin(x*y)).has_any_symbols(x, y)
True
>>> (x**2 + sin(x*y)).has_any_symbols(x, y, z)
True
has_piecewise

Returns True if any args are Piecewise or has_piecewise

integrate(*args, **kwargs)
is_bounded
is_commutative
is_complex
is_composite
is_even
is_finite
is_hypergeometric(k)
is_imaginary
is_infinitesimal
is_integer
is_irrational
is_negative
is_noninteger
is_nonnegative
is_nonpositive
is_nonzero
is_number
is_odd
is_polynomial(*syms)
is_positive
is_prime
is_rational
is_rational_function(*syms)

Test whether function is rational function - ratio of two polynomials. When no arguments are present, Basic.atoms(Symbol) is used instead.

Example:

>>> from sympy import symbols, sin
>>> from sympy.abc import x, y
>>> (x/y).is_rational_function()
True
>>> (x**2).is_rational_function()
True
>>> (x/sin(y)).is_rational_function(y)
False
is_real
is_unbounded
is_zero
iter_basic_args()

Iterates arguments of ‘self’.

Example:

>>> from sympy.abc import x
>>> a = 2*x
>>> a.iter_basic_args()
<tupleiterator object at 0x...>
>>> list(a.iter_basic_args())
[2, x]
leadterm(x)

Returns the leading term a*x**b as a tuple (a, b).

Example:

>>> from sympy.abc import x
>>> (1+x+x**2).leadterm(x)
(1, 0)
>>> (1/x**2+x+x**2).leadterm(x)
(1, -2)

Note:

self is assumed to be the result returned by Basic.series().

limit(x, xlim, direction='+')

Compute limit x->xlim.

lseries(x, x0)

lseries is a generator yielding terms in the series.

Example: if you do:

for term in sin(x).lseries(x, 0):
print term

It will print all terms of the sin(x) series (i.e. it never terminates).

The advantage of lseries() over nseries() is that many times you are just interested in the next term in the series (i.e. the first term for example), but you don’t know how many you should ask for in nseries() using the “n” parameter.

See also nseries().

match(pattern)

Pattern matching.

Wild symbols match all.

Return None when expression (self) does not match with pattern. Otherwise return a dictionary such that

pattern.subs(self.match(pattern)) == self

Example:

>>> from sympy import symbols, Wild
>>> from sympy.abc import x, y
>>> p = Wild("p")
>>> q = Wild("q")
>>> r = Wild("r")
>>> e = (x+y)**(x+y)
>>> e.match(p**p)
{p_: x + y}
>>> e.match(p**q)
{p_: x + y, q_: x + y}
>>> e = (2*x)**2
>>> e.match(p*q**r)
{p_: 4, q_: x, r_: 2}
>>> (p*q**r).subs(e.match(p*q**r))
4*x**2
matches(expr, repl_dict, evaluate=False)
n(x, n=15, **options)

Evaluate the given formula to an accuracy of n digits. Optional keyword arguments:

subs=<dict>
Substitute numerical values for symbols, e.g. subs={x:3, y:1+pi}.
maxprec=N
Allow a maximum temporary working precision of N digits (default=100)
chop=<bool>
Replace tiny real or imaginary parts in subresults by exact zeros (default=False)
strict=<bool>
Raise PrecisionExhausted if any subresult fails to evaluate to full accuracy, given the available maxprec (default=False)
quad=<str>
Choose algorithm for numerical quadrature. By default, tanh-sinh quadrature is used. For oscillatory integrals on an infinite interval, try quad=’osc’.
verbose=<bool>
Print debug information (default=False)
name
new(*args)

Create new ‘similar’ object.

this is conceptually equivalent to:

type(self) (*args)

but takes type assumptions into account. See also: assumptions0

Example:

>>> from sympy.abc import x
>>> x.new("x")
x
normal()
nseries(x, x0, n)

Calculates a generalized series expansion.

nseries calculates “n” terms in the innermost expressions and then builds up the final series just by “cross-multiplying” everything out.

Advantage – it’s fast, because we don’t have to determine how many terms we need to calculate in advance.

Disadvantage – you may end up with less terms than you may have expected, but the O(x**n) term appended will always be correct and so the result, though perhaps shorter, will also be correct.

See also lseries().

removeO()

Removes the O(..) symbol if there is one

rewrite(*args, **hints)

Rewrites expression containing applications of functions of one kind in terms of functions of different kind. For example you can rewrite trigonometric functions as complex exponentials or combinatorial functions as gamma function.

As a pattern this function accepts a list of functions to to rewrite (instances of DefinedFunction class). As rule you can use string or a destination function instance (in this case rewrite() will use the str() function).

There is also possibility to pass hints on how to rewrite the given expressions. For now there is only one such hint defined called ‘deep’. When ‘deep’ is set to False it will forbid functions to rewrite their contents.

>>> from sympy import sin, exp, I
>>> from sympy.abc import x, y
>>> sin(x).rewrite(sin, exp)
-I*(exp(I*x) - exp(-I*x))/2
series(x, point=0, n=6, dir='+')

Series expansion of “self” around “point”.

Usage:

Returns the Taylor (Laurent or generalized) series of “self” around the point “point” (default 0) with respect to “x” until the n-th term (default n is 6).

For dir=”+” (default) it calculates the series from the right and for dir=”-” the series from the left. For smooth functions this argument doesn’t matter.

Notes:

This method is the most high level method and it returns the series including the O(x**n) term.

Internally, it executes a method nseries(), see nseries() docstring for more information.

solve4linearsymbol(eqn, rhs, symbols=None)

Solve equation “eqn == rhs” with respect to some linear symbol in eqn.

Returns (symbol, solution). If eqn is nonlinear with respect to all symbols, then return trivial solution (eqn, rhs).

subs(*args)

Substitutes an expression.

Calls either _subs_old_new, _subs_dict or _subs_list depending if you give it two arguments (old, new), a dictionary or a list.

Examples:

>>> from sympy import pi
>>> from sympy.abc import x, y
>>> (1+x*y).subs(x, pi)
1 + pi*y
>>> (1+x*y).subs({x:pi, y:2})
1 + 2*pi
>>> (1+x*y).subs([(x,pi), (y,2)])
1 + 2*pi

Factor

class nipy.algorithms.statistics.formula.formulae.Factor(name, levels, char='b')

Bases: nipy.algorithms.statistics.formula.formulae.Formula

A qualitative variable in a regression model

A Factor is similar to R’s factor. The levels of the Factor can be either strings or ints.

Methods

design
fromcol
fromrec
get_term
stratify
subs
__init__(name, levels, char='b')
Parameters :

name : str

levels : [str or int]

A sequence of strings or ints.

char : str

coefs

Coefficients in the linear regression formula.

design(input, param=None, return_float=False, contrasts=None)

Construct the design matrix, and optional contrast matrices.

Parameters :

input : np.recarray

Recarray including fields needed to compute the Terms in getparams(self.design_expr).

param : None or np.recarray

Recarray including fields that are not Terms in getparams(self.design_expr)

return_float : bool, optional

If True, return a np.float array rather than a np.recarray

contrasts : None or dict, optional

Contrasts. The items in this dictionary should be (str, Formula) pairs where a contrast matrix is constructed for each Formula by evaluating its design at the same parameters as self.design. If not None, then the return_float is set to True.

Returns :

des : 2D array

design matrix

cmatrices : dict, optional

Dictionary with keys from contrasts input, and contrast matrices corresponding to des design matrix. Returned only if contrasts input is not None

design_expr
dtype

The dtype of the design matrix of the Formula.

static fromcol(col, name)

Create a Factor from a column array.

Parameters :

col : ndarray

an array with ndim==1

name : str

name of the Factor

Returns :

factor : Factor

Examples

>>> data = np.array([(3,'a'),(4,'a'),(5,'b'),(3,'b')], np.dtype([('x', np.float), ('y', 'S1')]))
>>> f1 = Factor.fromcol(data['y'], 'y')
>>> f2 = Factor.fromcol(data['x'], 'x')
>>> d = f1.design(data)
>>> print d.dtype.descr
[('y_a', '<f8'), ('y_b', '<f8')]
>>> d = f2.design(data)
>>> print d.dtype.descr
[('x_3', '<f8'), ('x_4', '<f8'), ('x_5', '<f8')]
static fromrec(rec, keep=[], drop=[])

Construct Formula from recarray

For fields with a string-dtype, it is assumed that these are qualtiatitve regressors, i.e. Factors.

Parameters :

rec: recarray :

Recarray whose field names will be used to create a formula.

keep: [] :

Field names to explicitly keep, dropping all others.

drop: [] :

Field names to drop.

get_term(level)

Retrieve a term of the Factor...

main_effect
mean

Expression for the mean, expressed as a linear combination of terms, each with dummy variables in front.

params

The parameters in the Formula.

stratify(variable)

Create a new variable, stratified by the levels of a Factor.

Parameters :

variable : str or a simple sympy expression whose string representation

are all lower or upper case letters, i.e. it can be interpreted as a name

Returns :

formula : Formula

Formula whose mean has one parameter named variable%d, for each level in self.levels

Examples

>>> f = Factor('a', ['x','y'])
>>> sf = f.stratify('theta')
>>> sf.mean
_theta0*a_x + _theta1*a_y
subs(old, new)

Perform a sympy substitution on all terms in the Formula

Returns a new instance of the same class

Parameters :

old : sympy.Basic

The expression to be changed

new : sympy.Basic

The value to change it to.

Returns :

newf : Formula

Examples

>>> s, t = [Term(l) for l in 'st']
>>> f, g = [sympy.Function(l) for l in 'fg']
>>> form = Formula([f(t),g(s)])
>>> newform = form.subs(g, sympy.Function('h'))
>>> newform.terms
array([f(t), h(s)], dtype=object)
>>> form.terms
array([f(t), g(s)], dtype=object)
terms

Terms in the linear regression formula.

FactorTerm

class nipy.algorithms.statistics.formula.formulae.FactorTerm

Bases: nipy.algorithms.statistics.formula.formulae.Term

Boolean Term derived from a Factor.

Its properties are the same as a Term except that its product with itself is itself.

Methods

as_base_exp
as_basic
as_coeff_exponent
as_coeff_factors
as_coeff_terms
as_coefficient
as_dummy
as_independent
as_leading_term
as_numer_denom
as_poly
as_powers_dict
as_real_imag
atoms
coeff
compare
compare_pretty
conjugate
could_extract_minus_sign
count_ops
diff
doit
evalf
expand
extract_additively
extract_multiplicatively
fdiff
getO
has
has_all_symbols
has_any_symbols
integrate
is_hypergeometric
is_polynomial
is_rational_function
iter_basic_args
leadterm
limit
lseries
match
matches
n
new Create new objects of various types.
normal
nseries
removeO
rewrite
series
solve4linearsymbol
subs
__init__()

x.__init__(...) initializes x; see help(type(x)) for signature

args

Returns a tuple of arguments of ‘self’.

Example:

>>> from sympy import symbols, cot
>>> from sympy.abc import x, y
>>> cot(x).args
(x,)
>>> cot(x).args[0]
x
>>> (x*y).args
(x, y)
>>> (x*y).args[1]
y

Note for developers: Never use self._args, always use self.args. Only when you are creating your own new function, use _args in the __new__. Don’t override .args() from Basic (so that it’s easy to change the interface in the future if needed).

as_base_exp()
as_basic()

Converts polynomial to a valid sympy expression.

>>> from sympy import sin
>>> from sympy.abc import x, y
>>> p = (x**2 + x*y).as_poly(x, y)
>>> p.as_basic()
x*y + x**2
>>> f = sin(x)
>>> f.as_basic()
sin(x)
as_coeff_exponent(x)

c*x**e -> c,e where x can be any symbolic expression.

as_coeff_factors(x=None)
as_coeff_terms(x=None)
as_coefficient(expr)

Extracts symbolic coefficient at the given expression. In other words, this functions separates ‘self’ into product of ‘expr’ and ‘expr’-free coefficient. If such separation is not possible it will return None.

>>> from sympy import E, pi, sin, I
>>> from sympy.abc import x, y
>>> E.as_coefficient(E)
1
>>> (2*E).as_coefficient(E)
2
>>> (2*E + x).as_coefficient(E)
>>> (2*sin(E)*E).as_coefficient(E)
>>> (2*pi*I).as_coefficient(pi*I)
2
>>> (2*I).as_coefficient(pi*I)
as_dummy()
as_independent(*deps)

Returns a pair with separated parts of a given expression independent of specified symbols in the first place and dependent on them in the other. Both parts are valid SymPy expressions.

>>> from sympy import sin, cos
>>> from sympy.abc import x, y
>>> (2*x*sin(x)+y+x).as_independent(x)
(y, x + 2*x*sin(x))
>>> (x*sin(x)*cos(y)).as_independent(x)
(cos(y), x*sin(x))

All other expressions are multiplicative:

>>> (sin(x)).as_independent(x)
(1, sin(x))
>>> (sin(x)).as_independent(y)
(sin(x), 1)
as_leading_term(*args, **kw_args)

Returns the leading term.

Example:

>>> from sympy.abc import x
>>> (1+x+x**2).as_leading_term(x)
1
>>> (1/x**2+x+x**2).as_leading_term(x)
x**(-2)

Note:

self is assumed to be the result returned by Basic.series().

as_numer_denom()
as_poly(*symbols, **flags)

Converts ‘self’ to a polynomial or returns None.

When constructing a polynomial an exception will be raised in case the input expression is not convertible to a polynomial. There are situations when it is easier (simpler or prettier) to receive None on failure.

If no symbols were given and ‘self’ isn’t already a polynomial then all available symbols will be collected and used to form a new polynomial.

>>> from sympy import Poly, sin
>>> from sympy.abc import x, y
>>> print (x**2 + x*y).as_poly()
Poly(x**2 + x*y, x, y)
>>> print (x**2 + x*y).as_poly(x, y)
Poly(x**2 + x*y, x, y)
>>> print (x**2 + sin(y)).as_poly(x, y)
None
as_powers_dict()
as_real_imag()

Performs complex expansion on ‘self’ and returns a tuple containing collected both real and imaginary parts. This method can’t be confused with re() and im() functions, which does not perform complex expansion at evaluation.

However it is possible to expand both re() and im() functions and get exactly the same results as with a single call to this function.

>>> from sympy import symbols, I
>>> x, y = symbols('xy', real=True)
>>> (x + y*I).as_real_imag()
(x, y)
>>> from sympy.abc import z, w
>>> (z + w*I).as_real_imag()
(-im(w) + re(z), im(z) + re(w))
assumptions0

Return object type assumptions.

For example:

Symbol(‘x’, real=True) Symbol(‘x’, integer=True)

are different objects. In other words, besides Python type (Symbol in this case), the initial assumptions are also forming their typeinfo.

Example:

>>> from sympy import Symbol
>>> from sympy.abc import x
>>> x.assumptions0
{}
>>> x = Symbol("x", positive=True)
>>> x.assumptions0
{'commutative': True, 'complex': True, 'imaginary': False,
'negative': False, 'nonnegative': True, 'nonpositive': False,
'nonzero': True, 'positive': True, 'real': True, 'zero': False}
atoms(*types)

Returns the atoms that form the current object.

By default, only objects that are truly atomic and can’t be divided into smaller pieces are returned: symbols, numbers, and number symbols like I and pi. It is possible to request atoms of any type, however, as demonstrated below.

Examples:

>>> from sympy import I, pi, sin
>>> from sympy.abc import x, y
>>> list((1+x+2*sin(y+I*pi)).atoms())

[y, I, 2, x, 1, pi]

If one or more types are given, the results will contain only those types of atoms:

Examples:

>>> from sympy import Number, NumberSymbol, Symbol
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Symbol))

[x, y]

>>> sorted((1+x+2*sin(y+I*pi)).atoms(Number))
[1, 2]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Number, NumberSymbol))
[1, 2, pi]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Number, NumberSymbol, I))
[1, 2, pi, I]

Note that I (imaginary unit) and zoo (complex infinity) are special types of number symbols and are not part of the NumberSymbol class.

The type can be given implicitly, too:

>>> sorted((1+x+2*sin(y+I*pi)).atoms(x)) # x is a Symbol

[x, y]

Be careful to check your assumptions when using the implicit option since S(1).is_Integer = True but type(S(1)) is One, a special type of sympy atom, while type(S(2)) is type Integer and will find all integers in an expression:

>>> from sympy import S
>>> sorted((1+x+2*sin(y+I*pi)).atoms(S(1)))

[1]

>>> sorted((1+x+2*sin(y+I*pi)).atoms(S(2)))
[1, 2]

Finally, arguments to atoms() can select more than atomic atoms: any sympy type (loaded in core/__init__.py) can be listed as an argument and those types of “atoms” as found in scanning the arguments of the expression nonrecursively:

>>> from sympy import Function, Mul
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Function))

[sin(y + pi*I)]

>>> sorted((1+x+2*sin(y+I*pi)).atoms(Mul))
[2*sin(y + pi*I)]
coeff(x, expand=True)

Returns the coefficient of the term “x” or None if there is no “x”.

Optional expand keyword argument allows one to control whether the expression is expanded before terms are collected, which can be useful if the term “x” isn’t nested inside of terms and you don’t want the returned coefficient to be expanded.

Example:

>>> from sympy import symbols
>>> from sympy.abc import x, y, z
>>> (3+2*x+4*x**2).coeff(1)
>>> (3+2*x+4*x**2).coeff(x)
2
>>> (3+2*x+4*x**2).coeff(x**2)
4
>>> (3+2*x+4*x**2).coeff(x**3)
>>> (z*(x+y)**2).coeff(z)
2*x*y + x**2 + y**2
>>> (z*(x+y)**2).coeff(z, expand=False)
(x + y)**2
>>>
compare(other)

Return -1,0,1 if the object is smaller, equal, or greater than other.

Not in the mathematical sense. If the object is of a different type from the “other” then their classes are ordered according to the sorted_classes list.

Example:

>>> from sympy.abc import x, y
>>> x.compare(y)
-1
>>> x.compare(x)
0
>>> y.compare(x)
1
static compare_pretty(a, b)

Is a > b in the sense of ordering in printing?

yes ..... return 1 no ...... return -1 equal ... return 0

Strategy:

It uses Basic.compare as a fallback, but improves it in many cases, like x**3, x**4, O(x**3) etc. In those simple cases, it just parses the expression and returns the “sane” ordering such as:

1 < x < x**2 < x**3 < O(x**4) etc.

Example:

>>> from sympy.abc import x
>>> from sympy import Basic
>>> Basic._compare_pretty(x, x**2)
-1
>>> Basic._compare_pretty(x**2, x**2)
0
>>> Basic._compare_pretty(x**3, x**2)
1
conjugate()
could_extract_minus_sign()

Canonical way to choose an element in the set {e, -e} where e is any expression. If the canonical element is e, we have e.could_extract_minus_sign() == True, else e.could_extract_minus_sign() == False.

For any expression, the set {e.could_extract_minus_sign(), (-e).could_extract_minus_sign()} must be {True, False}.

>>> from sympy.abc import x, y
>>> (x-y).could_extract_minus_sign() != (y-x).could_extract_minus_sign()
True
count_ops(symbolic=True)
diff(*symbols, **assumptions)
doit(**hints)
evalf(x, n=15, **options)

Evaluate the given formula to an accuracy of n digits. Optional keyword arguments:

subs=<dict>
Substitute numerical values for symbols, e.g. subs={x:3, y:1+pi}.
maxprec=N
Allow a maximum temporary working precision of N digits (default=100)
chop=<bool>
Replace tiny real or imaginary parts in subresults by exact zeros (default=False)
strict=<bool>
Raise PrecisionExhausted if any subresult fails to evaluate to full accuracy, given the available maxprec (default=False)
quad=<str>
Choose algorithm for numerical quadrature. By default, tanh-sinh quadrature is used. For oscillatory integrals on an infinite interval, try quad=’osc’.
verbose=<bool>
Print debug information (default=False)
expand(deep=True, power_base=True, power_exp=True, mul=True, log=True, multinomial=True, basic=True, **hints)

Expand an expression using hints.

See the docstring in function.expand for more information.

extract_additively(c)

Return None if it’s not possible to make self in the form something + c in a nice way, i.e. preserving the properties of arguments of self.

>>> from sympy import symbols
>>> x, y = symbols('xy', real=True)
>>> ((x*y)**3).extract_additively(1)
>>> (x+1).extract_additively(x)
1
>>> (x+1).extract_additively(2*x)
>>> (x+1).extract_additively(-x)
1 + 2*x
>>> (-x+1).extract_additively(2*x)
1 - 3*x
extract_multiplicatively(c)

Return None if it’s not possible to make self in the form c * something in a nice way, i.e. preserving the properties of arguments of self.

>>> from sympy import symbols, Rational
>>> x, y = symbols('xy', real=True)
>>> ((x*y)**3).extract_multiplicatively(x**2 * y)
x*y**2
>>> ((x*y)**3).extract_multiplicatively(x**4 * y)
>>> (2*x).extract_multiplicatively(2)
x
>>> (2*x).extract_multiplicatively(3)
>>> (Rational(1,2)*x).extract_multiplicatively(3)
x/6
fdiff(*indices)
formula

Return a Formula with only terms=[self].

func

The top-level function in an expression.

The following should hold for all objects:

>> x == x.func(*x.args)

Example:

>>> from sympy.abc import x
>>> a = 2*x
>>> a.func
<class 'sympy.core.mul.Mul'>
>>> a.args
(2, x)
>>> a.func(*a.args)
2*x
>>> a == a.func(*a.args)
True
getO(e)

Returns the O(..) symbol, or None if there is none.

has(*patterns)

Return True if self has any of the patterns.

Example: >>> from sympy.abc import x >>> (2*x).has(x) True >>> (2*x/x).has(x) False

has_all_symbols(*args, **kw_args)

Return True if ‘self’ has all of the symbols.

>>> from sympy import sin
>>> from sympy.abc import x, y, z
>>> (x**2 + sin(x*y)).has_all_symbols(x, y)
True
>>> (x**2 + sin(x*y)).has_all_symbols(x, y, z)
False
has_any_symbols(*args, **kw_args)

Return True if ‘self’ has any of the symbols.

>>> from sympy import sin
>>> from sympy.abc import x, y, z
>>> (x**2 + sin(x*y)).has_any_symbols(z)
False
>>> (x**2 + sin(x*y)).has_any_symbols(x, y)
True
>>> (x**2 + sin(x*y)).has_any_symbols(x, y, z)
True
has_piecewise

Returns True if any args are Piecewise or has_piecewise

integrate(*args, **kwargs)
is_bounded
is_commutative
is_complex
is_composite
is_even
is_finite
is_hypergeometric(k)
is_imaginary
is_infinitesimal
is_integer
is_irrational
is_negative
is_noninteger
is_nonnegative
is_nonpositive
is_nonzero
is_number
is_odd
is_polynomial(*syms)
is_positive
is_prime
is_rational
is_rational_function(*syms)

Test whether function is rational function - ratio of two polynomials. When no arguments are present, Basic.atoms(Symbol) is used instead.

Example:

>>> from sympy import symbols, sin
>>> from sympy.abc import x, y
>>> (x/y).is_rational_function()
True
>>> (x**2).is_rational_function()
True
>>> (x/sin(y)).is_rational_function(y)
False
is_real
is_unbounded
is_zero
iter_basic_args()

Iterates arguments of ‘self’.

Example:

>>> from sympy.abc import x
>>> a = 2*x
>>> a.iter_basic_args()
<tupleiterator object at 0x...>
>>> list(a.iter_basic_args())
[2, x]
leadterm(x)

Returns the leading term a*x**b as a tuple (a, b).

Example:

>>> from sympy.abc import x
>>> (1+x+x**2).leadterm(x)
(1, 0)
>>> (1/x**2+x+x**2).leadterm(x)
(1, -2)

Note:

self is assumed to be the result returned by Basic.series().

limit(x, xlim, direction='+')

Compute limit x->xlim.

lseries(x, x0)

lseries is a generator yielding terms in the series.

Example: if you do:

for term in sin(x).lseries(x, 0):
print term

It will print all terms of the sin(x) series (i.e. it never terminates).

The advantage of lseries() over nseries() is that many times you are just interested in the next term in the series (i.e. the first term for example), but you don’t know how many you should ask for in nseries() using the “n” parameter.

See also nseries().

match(pattern)

Pattern matching.

Wild symbols match all.

Return None when expression (self) does not match with pattern. Otherwise return a dictionary such that

pattern.subs(self.match(pattern)) == self

Example:

>>> from sympy import symbols, Wild
>>> from sympy.abc import x, y
>>> p = Wild("p")
>>> q = Wild("q")
>>> r = Wild("r")
>>> e = (x+y)**(x+y)
>>> e.match(p**p)
{p_: x + y}
>>> e.match(p**q)
{p_: x + y, q_: x + y}
>>> e = (2*x)**2
>>> e.match(p*q**r)
{p_: 4, q_: x, r_: 2}
>>> (p*q**r).subs(e.match(p*q**r))
4*x**2
matches(expr, repl_dict, evaluate=False)
n(x, n=15, **options)

Evaluate the given formula to an accuracy of n digits. Optional keyword arguments:

subs=<dict>
Substitute numerical values for symbols, e.g. subs={x:3, y:1+pi}.
maxprec=N
Allow a maximum temporary working precision of N digits (default=100)
chop=<bool>
Replace tiny real or imaginary parts in subresults by exact zeros (default=False)
strict=<bool>
Raise PrecisionExhausted if any subresult fails to evaluate to full accuracy, given the available maxprec (default=False)
quad=<str>
Choose algorithm for numerical quadrature. By default, tanh-sinh quadrature is used. For oscillatory integrals on an infinite interval, try quad=’osc’.
verbose=<bool>
Print debug information (default=False)
name
new(*args)

Create new ‘similar’ object.

this is conceptually equivalent to:

type(self) (*args)

but takes type assumptions into account. See also: assumptions0

Example:

>>> from sympy.abc import x
>>> x.new("x")
x
normal()
nseries(x, x0, n)

Calculates a generalized series expansion.

nseries calculates “n” terms in the innermost expressions and then builds up the final series just by “cross-multiplying” everything out.

Advantage – it’s fast, because we don’t have to determine how many terms we need to calculate in advance.

Disadvantage – you may end up with less terms than you may have expected, but the O(x**n) term appended will always be correct and so the result, though perhaps shorter, will also be correct.

See also lseries().

removeO()

Removes the O(..) symbol if there is one

rewrite(*args, **hints)

Rewrites expression containing applications of functions of one kind in terms of functions of different kind. For example you can rewrite trigonometric functions as complex exponentials or combinatorial functions as gamma function.

As a pattern this function accepts a list of functions to to rewrite (instances of DefinedFunction class). As rule you can use string or a destination function instance (in this case rewrite() will use the str() function).

There is also possibility to pass hints on how to rewrite the given expressions. For now there is only one such hint defined called ‘deep’. When ‘deep’ is set to False it will forbid functions to rewrite their contents.

>>> from sympy import sin, exp, I
>>> from sympy.abc import x, y
>>> sin(x).rewrite(sin, exp)
-I*(exp(I*x) - exp(-I*x))/2
series(x, point=0, n=6, dir='+')

Series expansion of “self” around “point”.

Usage:

Returns the Taylor (Laurent or generalized) series of “self” around the point “point” (default 0) with respect to “x” until the n-th term (default n is 6).

For dir=”+” (default) it calculates the series from the right and for dir=”-” the series from the left. For smooth functions this argument doesn’t matter.

Notes:

This method is the most high level method and it returns the series including the O(x**n) term.

Internally, it executes a method nseries(), see nseries() docstring for more information.

solve4linearsymbol(eqn, rhs, symbols=None)

Solve equation “eqn == rhs” with respect to some linear symbol in eqn.

Returns (symbol, solution). If eqn is nonlinear with respect to all symbols, then return trivial solution (eqn, rhs).

subs(*args)

Substitutes an expression.

Calls either _subs_old_new, _subs_dict or _subs_list depending if you give it two arguments (old, new), a dictionary or a list.

Examples:

>>> from sympy import pi
>>> from sympy.abc import x, y
>>> (1+x*y).subs(x, pi)
1 + pi*y
>>> (1+x*y).subs({x:pi, y:2})
1 + 2*pi
>>> (1+x*y).subs([(x,pi), (y,2)])
1 + 2*pi

Formula

class nipy.algorithms.statistics.formula.formulae.Formula(seq, char='b')

Bases: object

A Formula is a model for a mean in a regression model.

It is often given by a sequence of sympy expressions, with the mean model being the sum of each term multiplied by a linear regression coefficient.

The expressions may depend on additional Symbol instances, giving a non-linear regression model.

Methods

design
fromrec
subs
__init__(seq, char='b')
Parameters :

seq : sequence of sympy.Basic

char : str, optional

character for regression coefficient

coefs

Coefficients in the linear regression formula.

design(input, param=None, return_float=False, contrasts=None)

Construct the design matrix, and optional contrast matrices.

Parameters :

input : np.recarray

Recarray including fields needed to compute the Terms in getparams(self.design_expr).

param : None or np.recarray

Recarray including fields that are not Terms in getparams(self.design_expr)

return_float : bool, optional

If True, return a np.float array rather than a np.recarray

contrasts : None or dict, optional

Contrasts. The items in this dictionary should be (str, Formula) pairs where a contrast matrix is constructed for each Formula by evaluating its design at the same parameters as self.design. If not None, then the return_float is set to True.

Returns :

des : 2D array

design matrix

cmatrices : dict, optional

Dictionary with keys from contrasts input, and contrast matrices corresponding to des design matrix. Returned only if contrasts input is not None

design_expr
dtype

The dtype of the design matrix of the Formula.

static fromrec(rec, keep=[], drop=[])

Construct Formula from recarray

For fields with a string-dtype, it is assumed that these are qualtiatitve regressors, i.e. Factors.

Parameters :

rec: recarray :

Recarray whose field names will be used to create a formula.

keep: [] :

Field names to explicitly keep, dropping all others.

drop: [] :

Field names to drop.

mean

Expression for the mean, expressed as a linear combination of terms, each with dummy variables in front.

params

The parameters in the Formula.

subs(old, new)

Perform a sympy substitution on all terms in the Formula

Returns a new instance of the same class

Parameters :

old : sympy.Basic

The expression to be changed

new : sympy.Basic

The value to change it to.

Returns :

newf : Formula

Examples

>>> s, t = [Term(l) for l in 'st']
>>> f, g = [sympy.Function(l) for l in 'fg']
>>> form = Formula([f(t),g(s)])
>>> newform = form.subs(g, sympy.Function('h'))
>>> newform.terms
array([f(t), h(s)], dtype=object)
>>> form.terms
array([f(t), g(s)], dtype=object)
terms

Terms in the linear regression formula.

RandomEffects

class nipy.algorithms.statistics.formula.formulae.RandomEffects(seq, sigma=None, char='e')

Bases: nipy.algorithms.statistics.formula.formulae.Formula

Covariance matrices for common random effects analyses.

Examples

Two subjects (here named 2 and 3):

>>> subj = make_recarray([2,2,2,3,3], 's')
>>> subj_factor = Factor('s', [2,3])

By default the covariance matrix is symbolic. The display differs a little between sympy versions (hence we don’t check it in the doctests):

>>> c = RandomEffects(subj_factor.terms)
>>> c.cov(subj) 
array([[_s2_0, _s2_0, _s2_0, 0, 0],
       [_s2_0, _s2_0, _s2_0, 0, 0],
       [_s2_0, _s2_0, _s2_0, 0, 0],
       [0, 0, 0, _s2_1, _s2_1],
       [0, 0, 0, _s2_1, _s2_1]], dtype=object)

With a numeric sigma, you get a numeric array:

>>> c = RandomEffects(subj_factor.terms, sigma=np.array([[4,1],[1,6]]))
>>> c.cov(subj)
array([[ 4.,  4.,  4.,  1.,  1.],
       [ 4.,  4.,  4.,  1.,  1.],
       [ 4.,  4.,  4.,  1.,  1.],
       [ 1.,  1.,  1.,  6.,  6.],
       [ 1.,  1.,  1.,  6.,  6.]])

Methods

cov
design
fromrec
subs
__init__(seq, sigma=None, char='e')

Initialize random effects instance

Parameters :

seq : [sympy.Basic]

sigma : ndarray

Covariance of the random effects. Defaults to a diagonal with entries for each random effect.

char : character for regression coefficient

coefs

Coefficients in the linear regression formula.

cov(term, param=None)

Compute the covariance matrix for some given data.

Parameters :

term : np.recarray

Recarray including fields corresponding to the Terms in getparams(self.design_expr).

param : np.recarray

Recarray including fields that are not Terms in getparams(self.design_expr)

Returns :

C : ndarray

Covariance matrix implied by design and self.sigma.

design(input, param=None, return_float=False, contrasts=None)

Construct the design matrix, and optional contrast matrices.

Parameters :

input : np.recarray

Recarray including fields needed to compute the Terms in getparams(self.design_expr).

param : None or np.recarray

Recarray including fields that are not Terms in getparams(self.design_expr)

return_float : bool, optional

If True, return a np.float array rather than a np.recarray

contrasts : None or dict, optional

Contrasts. The items in this dictionary should be (str, Formula) pairs where a contrast matrix is constructed for each Formula by evaluating its design at the same parameters as self.design. If not None, then the return_float is set to True.

Returns :

des : 2D array

design matrix

cmatrices : dict, optional

Dictionary with keys from contrasts input, and contrast matrices corresponding to des design matrix. Returned only if contrasts input is not None

design_expr
dtype

The dtype of the design matrix of the Formula.

static fromrec(rec, keep=[], drop=[])

Construct Formula from recarray

For fields with a string-dtype, it is assumed that these are qualtiatitve regressors, i.e. Factors.

Parameters :

rec: recarray :

Recarray whose field names will be used to create a formula.

keep: [] :

Field names to explicitly keep, dropping all others.

drop: [] :

Field names to drop.

mean

Expression for the mean, expressed as a linear combination of terms, each with dummy variables in front.

params

The parameters in the Formula.

subs(old, new)

Perform a sympy substitution on all terms in the Formula

Returns a new instance of the same class

Parameters :

old : sympy.Basic

The expression to be changed

new : sympy.Basic

The value to change it to.

Returns :

newf : Formula

Examples

>>> s, t = [Term(l) for l in 'st']
>>> f, g = [sympy.Function(l) for l in 'fg']
>>> form = Formula([f(t),g(s)])
>>> newform = form.subs(g, sympy.Function('h'))
>>> newform.terms
array([f(t), h(s)], dtype=object)
>>> form.terms
array([f(t), g(s)], dtype=object)
terms

Terms in the linear regression formula.

Term

class nipy.algorithms.statistics.formula.formulae.Term

Bases: sympy.core.symbol.Symbol

A sympy.Symbol type to represent a term an a regression model

Terms can be added to other sympy expressions with the single convention that a term plus itself returns itself.

It is meant to emulate something on the right hand side of a formula in R. In particular, its name can be the name of a field in a recarray used to create a design matrix.

>>> t = Term('x')
>>> xval = np.array([(3,),(4,),(5,)], np.dtype([('x', np.float)]))
>>> f = t.formula
>>> d = f.design(xval)
>>> print d.dtype.descr
[('x', '<f8')]
>>> f.design(xval, return_float=True)
array([ 3.,  4.,  5.])

Methods

as_base_exp
as_basic
as_coeff_exponent
as_coeff_factors
as_coeff_terms
as_coefficient
as_dummy
as_independent
as_leading_term
as_numer_denom
as_poly
as_powers_dict
as_real_imag
atoms
coeff
compare
compare_pretty
conjugate
could_extract_minus_sign
count_ops
diff
doit
evalf
expand
extract_additively
extract_multiplicatively
fdiff
getO
has
has_all_symbols
has_any_symbols
integrate
is_hypergeometric
is_polynomial
is_rational_function
iter_basic_args
leadterm
limit
lseries
match
matches
n
new Create new objects of various types.
normal
nseries
removeO
rewrite
series
solve4linearsymbol
subs
__init__()

x.__init__(...) initializes x; see help(type(x)) for signature

args

Returns a tuple of arguments of ‘self’.

Example:

>>> from sympy import symbols, cot
>>> from sympy.abc import x, y
>>> cot(x).args
(x,)
>>> cot(x).args[0]
x
>>> (x*y).args
(x, y)
>>> (x*y).args[1]
y

Note for developers: Never use self._args, always use self.args. Only when you are creating your own new function, use _args in the __new__. Don’t override .args() from Basic (so that it’s easy to change the interface in the future if needed).

as_base_exp()
as_basic()

Converts polynomial to a valid sympy expression.

>>> from sympy import sin
>>> from sympy.abc import x, y
>>> p = (x**2 + x*y).as_poly(x, y)
>>> p.as_basic()
x*y + x**2
>>> f = sin(x)
>>> f.as_basic()
sin(x)
as_coeff_exponent(x)

c*x**e -> c,e where x can be any symbolic expression.

as_coeff_factors(x=None)
as_coeff_terms(x=None)
as_coefficient(expr)

Extracts symbolic coefficient at the given expression. In other words, this functions separates ‘self’ into product of ‘expr’ and ‘expr’-free coefficient. If such separation is not possible it will return None.

>>> from sympy import E, pi, sin, I
>>> from sympy.abc import x, y
>>> E.as_coefficient(E)
1
>>> (2*E).as_coefficient(E)
2
>>> (2*E + x).as_coefficient(E)
>>> (2*sin(E)*E).as_coefficient(E)
>>> (2*pi*I).as_coefficient(pi*I)
2
>>> (2*I).as_coefficient(pi*I)
as_dummy()
as_independent(*deps)

Returns a pair with separated parts of a given expression independent of specified symbols in the first place and dependent on them in the other. Both parts are valid SymPy expressions.

>>> from sympy import sin, cos
>>> from sympy.abc import x, y
>>> (2*x*sin(x)+y+x).as_independent(x)
(y, x + 2*x*sin(x))
>>> (x*sin(x)*cos(y)).as_independent(x)
(cos(y), x*sin(x))

All other expressions are multiplicative:

>>> (sin(x)).as_independent(x)
(1, sin(x))
>>> (sin(x)).as_independent(y)
(sin(x), 1)
as_leading_term(*args, **kw_args)

Returns the leading term.

Example:

>>> from sympy.abc import x
>>> (1+x+x**2).as_leading_term(x)
1
>>> (1/x**2+x+x**2).as_leading_term(x)
x**(-2)

Note:

self is assumed to be the result returned by Basic.series().

as_numer_denom()
as_poly(*symbols, **flags)

Converts ‘self’ to a polynomial or returns None.

When constructing a polynomial an exception will be raised in case the input expression is not convertible to a polynomial. There are situations when it is easier (simpler or prettier) to receive None on failure.

If no symbols were given and ‘self’ isn’t already a polynomial then all available symbols will be collected and used to form a new polynomial.

>>> from sympy import Poly, sin
>>> from sympy.abc import x, y
>>> print (x**2 + x*y).as_poly()
Poly(x**2 + x*y, x, y)
>>> print (x**2 + x*y).as_poly(x, y)
Poly(x**2 + x*y, x, y)
>>> print (x**2 + sin(y)).as_poly(x, y)
None
as_powers_dict()
as_real_imag()

Performs complex expansion on ‘self’ and returns a tuple containing collected both real and imaginary parts. This method can’t be confused with re() and im() functions, which does not perform complex expansion at evaluation.

However it is possible to expand both re() and im() functions and get exactly the same results as with a single call to this function.

>>> from sympy import symbols, I
>>> x, y = symbols('xy', real=True)
>>> (x + y*I).as_real_imag()
(x, y)
>>> from sympy.abc import z, w
>>> (z + w*I).as_real_imag()
(-im(w) + re(z), im(z) + re(w))
assumptions0

Return object type assumptions.

For example:

Symbol(‘x’, real=True) Symbol(‘x’, integer=True)

are different objects. In other words, besides Python type (Symbol in this case), the initial assumptions are also forming their typeinfo.

Example:

>>> from sympy import Symbol
>>> from sympy.abc import x
>>> x.assumptions0
{}
>>> x = Symbol("x", positive=True)
>>> x.assumptions0
{'commutative': True, 'complex': True, 'imaginary': False,
'negative': False, 'nonnegative': True, 'nonpositive': False,
'nonzero': True, 'positive': True, 'real': True, 'zero': False}
atoms(*types)

Returns the atoms that form the current object.

By default, only objects that are truly atomic and can’t be divided into smaller pieces are returned: symbols, numbers, and number symbols like I and pi. It is possible to request atoms of any type, however, as demonstrated below.

Examples:

>>> from sympy import I, pi, sin
>>> from sympy.abc import x, y
>>> list((1+x+2*sin(y+I*pi)).atoms())

[y, I, 2, x, 1, pi]

If one or more types are given, the results will contain only those types of atoms:

Examples:

>>> from sympy import Number, NumberSymbol, Symbol
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Symbol))

[x, y]

>>> sorted((1+x+2*sin(y+I*pi)).atoms(Number))
[1, 2]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Number, NumberSymbol))
[1, 2, pi]
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Number, NumberSymbol, I))
[1, 2, pi, I]

Note that I (imaginary unit) and zoo (complex infinity) are special types of number symbols and are not part of the NumberSymbol class.

The type can be given implicitly, too:

>>> sorted((1+x+2*sin(y+I*pi)).atoms(x)) # x is a Symbol

[x, y]

Be careful to check your assumptions when using the implicit option since S(1).is_Integer = True but type(S(1)) is One, a special type of sympy atom, while type(S(2)) is type Integer and will find all integers in an expression:

>>> from sympy import S
>>> sorted((1+x+2*sin(y+I*pi)).atoms(S(1)))

[1]

>>> sorted((1+x+2*sin(y+I*pi)).atoms(S(2)))
[1, 2]

Finally, arguments to atoms() can select more than atomic atoms: any sympy type (loaded in core/__init__.py) can be listed as an argument and those types of “atoms” as found in scanning the arguments of the expression nonrecursively:

>>> from sympy import Function, Mul
>>> sorted((1+x+2*sin(y+I*pi)).atoms(Function))

[sin(y + pi*I)]

>>> sorted((1+x+2*sin(y+I*pi)).atoms(Mul))
[2*sin(y + pi*I)]
coeff(x, expand=True)

Returns the coefficient of the term “x” or None if there is no “x”.

Optional expand keyword argument allows one to control whether the expression is expanded before terms are collected, which can be useful if the term “x” isn’t nested inside of terms and you don’t want the returned coefficient to be expanded.

Example:

>>> from sympy import symbols
>>> from sympy.abc import x, y, z
>>> (3+2*x+4*x**2).coeff(1)
>>> (3+2*x+4*x**2).coeff(x)
2
>>> (3+2*x+4*x**2).coeff(x**2)
4
>>> (3+2*x+4*x**2).coeff(x**3)
>>> (z*(x+y)**2).coeff(z)
2*x*y + x**2 + y**2
>>> (z*(x+y)**2).coeff(z, expand=False)
(x + y)**2
>>>
compare(other)

Return -1,0,1 if the object is smaller, equal, or greater than other.

Not in the mathematical sense. If the object is of a different type from the “other” then their classes are ordered according to the sorted_classes list.

Example:

>>> from sympy.abc import x, y
>>> x.compare(y)
-1
>>> x.compare(x)
0
>>> y.compare(x)
1
static compare_pretty(a, b)

Is a > b in the sense of ordering in printing?

yes ..... return 1 no ...... return -1 equal ... return 0

Strategy:

It uses Basic.compare as a fallback, but improves it in many cases, like x**3, x**4, O(x**3) etc. In those simple cases, it just parses the expression and returns the “sane” ordering such as:

1 < x < x**2 < x**3 < O(x**4) etc.

Example:

>>> from sympy.abc import x
>>> from sympy import Basic
>>> Basic._compare_pretty(x, x**2)
-1
>>> Basic._compare_pretty(x**2, x**2)
0
>>> Basic._compare_pretty(x**3, x**2)
1
conjugate()
could_extract_minus_sign()

Canonical way to choose an element in the set {e, -e} where e is any expression. If the canonical element is e, we have e.could_extract_minus_sign() == True, else e.could_extract_minus_sign() == False.

For any expression, the set {e.could_extract_minus_sign(), (-e).could_extract_minus_sign()} must be {True, False}.

>>> from sympy.abc import x, y
>>> (x-y).could_extract_minus_sign() != (y-x).could_extract_minus_sign()
True
count_ops(symbolic=True)
diff(*symbols, **assumptions)
doit(**hints)
evalf(x, n=15, **options)

Evaluate the given formula to an accuracy of n digits. Optional keyword arguments:

subs=<dict>
Substitute numerical values for symbols, e.g. subs={x:3, y:1+pi}.
maxprec=N
Allow a maximum temporary working precision of N digits (default=100)
chop=<bool>
Replace tiny real or imaginary parts in subresults by exact zeros (default=False)
strict=<bool>
Raise PrecisionExhausted if any subresult fails to evaluate to full accuracy, given the available maxprec (default=False)
quad=<str>
Choose algorithm for numerical quadrature. By default, tanh-sinh quadrature is used. For oscillatory integrals on an infinite interval, try quad=’osc’.
verbose=<bool>
Print debug information (default=False)
expand(deep=True, power_base=True, power_exp=True, mul=True, log=True, multinomial=True, basic=True, **hints)

Expand an expression using hints.

See the docstring in function.expand for more information.

extract_additively(c)

Return None if it’s not possible to make self in the form something + c in a nice way, i.e. preserving the properties of arguments of self.

>>> from sympy import symbols
>>> x, y = symbols('xy', real=True)
>>> ((x*y)**3).extract_additively(1)
>>> (x+1).extract_additively(x)
1
>>> (x+1).extract_additively(2*x)
>>> (x+1).extract_additively(-x)
1 + 2*x
>>> (-x+1).extract_additively(2*x)
1 - 3*x
extract_multiplicatively(c)

Return None if it’s not possible to make self in the form c * something in a nice way, i.e. preserving the properties of arguments of self.

>>> from sympy import symbols, Rational
>>> x, y = symbols('xy', real=True)
>>> ((x*y)**3).extract_multiplicatively(x**2 * y)
x*y**2
>>> ((x*y)**3).extract_multiplicatively(x**4 * y)
>>> (2*x).extract_multiplicatively(2)
x
>>> (2*x).extract_multiplicatively(3)
>>> (Rational(1,2)*x).extract_multiplicatively(3)
x/6
fdiff(*indices)
formula

Return a Formula with only terms=[self].

func

The top-level function in an expression.

The following should hold for all objects:

>> x == x.func(*x.args)

Example:

>>> from sympy.abc import x
>>> a = 2*x
>>> a.func
<class 'sympy.core.mul.Mul'>
>>> a.args
(2, x)
>>> a.func(*a.args)
2*x
>>> a == a.func(*a.args)
True
getO(e)

Returns the O(..) symbol, or None if there is none.

has(*patterns)

Return True if self has any of the patterns.

Example: >>> from sympy.abc import x >>> (2*x).has(x) True >>> (2*x/x).has(x) False

has_all_symbols(*args, **kw_args)

Return True if ‘self’ has all of the symbols.

>>> from sympy import sin
>>> from sympy.abc import x, y, z
>>> (x**2 + sin(x*y)).has_all_symbols(x, y)
True
>>> (x**2 + sin(x*y)).has_all_symbols(x, y, z)
False
has_any_symbols(*args, **kw_args)

Return True if ‘self’ has any of the symbols.

>>> from sympy import sin
>>> from sympy.abc import x, y, z
>>> (x**2 + sin(x*y)).has_any_symbols(z)
False
>>> (x**2 + sin(x*y)).has_any_symbols(x, y)
True
>>> (x**2 + sin(x*y)).has_any_symbols(x, y, z)
True
has_piecewise

Returns True if any args are Piecewise or has_piecewise

integrate(*args, **kwargs)
is_bounded
is_commutative
is_complex
is_composite
is_even
is_finite
is_hypergeometric(k)
is_imaginary
is_infinitesimal
is_integer
is_irrational
is_negative
is_noninteger
is_nonnegative
is_nonpositive
is_nonzero
is_number
is_odd
is_polynomial(*syms)
is_positive
is_prime
is_rational
is_rational_function(*syms)

Test whether function is rational function - ratio of two polynomials. When no arguments are present, Basic.atoms(Symbol) is used instead.

Example:

>>> from sympy import symbols, sin
>>> from sympy.abc import x, y
>>> (x/y).is_rational_function()
True
>>> (x**2).is_rational_function()
True
>>> (x/sin(y)).is_rational_function(y)
False
is_real
is_unbounded
is_zero
iter_basic_args()

Iterates arguments of ‘self’.

Example:

>>> from sympy.abc import x
>>> a = 2*x
>>> a.iter_basic_args()
<tupleiterator object at 0x...>
>>> list(a.iter_basic_args())
[2, x]
leadterm(x)

Returns the leading term a*x**b as a tuple (a, b).

Example:

>>> from sympy.abc import x
>>> (1+x+x**2).leadterm(x)
(1, 0)
>>> (1/x**2+x+x**2).leadterm(x)
(1, -2)

Note:

self is assumed to be the result returned by Basic.series().

limit(x, xlim, direction='+')

Compute limit x->xlim.

lseries(x, x0)

lseries is a generator yielding terms in the series.

Example: if you do:

for term in sin(x).lseries(x, 0):
print term

It will print all terms of the sin(x) series (i.e. it never terminates).

The advantage of lseries() over nseries() is that many times you are just interested in the next term in the series (i.e. the first term for example), but you don’t know how many you should ask for in nseries() using the “n” parameter.

See also nseries().

match(pattern)

Pattern matching.

Wild symbols match all.

Return None when expression (self) does not match with pattern. Otherwise return a dictionary such that

pattern.subs(self.match(pattern)) == self

Example:

>>> from sympy import symbols, Wild
>>> from sympy.abc import x, y
>>> p = Wild("p")
>>> q = Wild("q")
>>> r = Wild("r")
>>> e = (x+y)**(x+y)
>>> e.match(p**p)
{p_: x + y}
>>> e.match(p**q)
{p_: x + y, q_: x + y}
>>> e = (2*x)**2
>>> e.match(p*q**r)
{p_: 4, q_: x, r_: 2}
>>> (p*q**r).subs(e.match(p*q**r))
4*x**2
matches(expr, repl_dict, evaluate=False)
n(x, n=15, **options)

Evaluate the given formula to an accuracy of n digits. Optional keyword arguments:

subs=<dict>
Substitute numerical values for symbols, e.g. subs={x:3, y:1+pi}.
maxprec=N
Allow a maximum temporary working precision of N digits (default=100)
chop=<bool>
Replace tiny real or imaginary parts in subresults by exact zeros (default=False)
strict=<bool>
Raise PrecisionExhausted if any subresult fails to evaluate to full accuracy, given the available maxprec (default=False)
quad=<str>
Choose algorithm for numerical quadrature. By default, tanh-sinh quadrature is used. For oscillatory integrals on an infinite interval, try quad=’osc’.
verbose=<bool>
Print debug information (default=False)
name
new(*args)

Create new ‘similar’ object.

this is conceptually equivalent to:

type(self) (*args)

but takes type assumptions into account. See also: assumptions0

Example:

>>> from sympy.abc import x
>>> x.new("x")
x
normal()
nseries(x, x0, n)

Calculates a generalized series expansion.

nseries calculates “n” terms in the innermost expressions and then builds up the final series just by “cross-multiplying” everything out.

Advantage – it’s fast, because we don’t have to determine how many terms we need to calculate in advance.

Disadvantage – you may end up with less terms than you may have expected, but the O(x**n) term appended will always be correct and so the result, though perhaps shorter, will also be correct.

See also lseries().

removeO()

Removes the O(..) symbol if there is one

rewrite(*args, **hints)

Rewrites expression containing applications of functions of one kind in terms of functions of different kind. For example you can rewrite trigonometric functions as complex exponentials or combinatorial functions as gamma function.

As a pattern this function accepts a list of functions to to rewrite (instances of DefinedFunction class). As rule you can use string or a destination function instance (in this case rewrite() will use the str() function).

There is also possibility to pass hints on how to rewrite the given expressions. For now there is only one such hint defined called ‘deep’. When ‘deep’ is set to False it will forbid functions to rewrite their contents.

>>> from sympy import sin, exp, I
>>> from sympy.abc import x, y
>>> sin(x).rewrite(sin, exp)
-I*(exp(I*x) - exp(-I*x))/2
series(x, point=0, n=6, dir='+')

Series expansion of “self” around “point”.

Usage:

Returns the Taylor (Laurent or generalized) series of “self” around the point “point” (default 0) with respect to “x” until the n-th term (default n is 6).

For dir=”+” (default) it calculates the series from the right and for dir=”-” the series from the left. For smooth functions this argument doesn’t matter.

Notes:

This method is the most high level method and it returns the series including the O(x**n) term.

Internally, it executes a method nseries(), see nseries() docstring for more information.

solve4linearsymbol(eqn, rhs, symbols=None)

Solve equation “eqn == rhs” with respect to some linear symbol in eqn.

Returns (symbol, solution). If eqn is nonlinear with respect to all symbols, then return trivial solution (eqn, rhs).

subs(*args)

Substitutes an expression.

Calls either _subs_old_new, _subs_dict or _subs_list depending if you give it two arguments (old, new), a dictionary or a list.

Examples:

>>> from sympy import pi
>>> from sympy.abc import x, y
>>> (1+x*y).subs(x, pi)
1 + pi*y
>>> (1+x*y).subs({x:pi, y:2})
1 + 2*pi
>>> (1+x*y).subs([(x,pi), (y,2)])
1 + 2*pi

Functions

nipy.algorithms.statistics.formula.formulae.contrast_from_cols_or_rows(L, D, pseudo=None)

Construct a contrast matrix from a design matrix D

(possibly with its pseudo inverse already computed) and a matrix L that either specifies something in the column space of D or the row space of D.

Parameters :

L : ndarray

Matrix used to try and construct a contrast.

D : ndarray

Design matrix used to create the contrast.

pseudo : None or array-like, optional

If not None, gives pseudo-inverse of D. Allows you to pass this if it is already calculated.

Returns :

C : ndarray

Matrix with C.shape[1] == D.shape[1] representing an estimable contrast.

Notes

From an n x p design matrix D and a matrix L, tries to determine a p x q contrast matrix C which determines a contrast of full rank, i.e. the n x q matrix

dot(transpose(C), pinv(D))

is full rank.

L must satisfy either L.shape[0] == n or L.shape[1] == p.

If L.shape[0] == n, then L is thought of as representing columns in the column space of D.

If L.shape[1] == p, then L is thought of as what is known as a contrast matrix. In this case, this function returns an estimable contrast corresponding to the dot(D, L.T)

This always produces a meaningful contrast, not always with the intended properties because q is always non-zero unless L is identically 0. That is, it produces a contrast that spans the column space of L (after projection onto the column space of D).

nipy.algorithms.statistics.formula.formulae.define(*args, **kwargs)
nipy.algorithms.statistics.formula.formulae.getparams(expression)

Return the parameters of an expression that are not Term instances but are instances of sympy.Symbol.

Examples

>>> x, y, z = [Term(l) for l in 'xyz']
>>> f = Formula([x,y,z])
>>> getparams(f)
[]
>>> f.mean
_b0*x + _b1*y + _b2*z
>>> getparams(f.mean)
[_b0, _b1, _b2]
>>> th = sympy.Symbol('theta')
>>> f.mean*sympy.exp(th)
(_b0*x + _b1*y + _b2*z)*exp(theta)
>>> getparams(f.mean*sympy.exp(th))
[theta, _b0, _b1, _b2]
nipy.algorithms.statistics.formula.formulae.getterms(expression)

Return the all instances of Term in an expression.

Examples

>>> x, y, z = [Term(l) for l in 'xyz']
>>> f = Formula([x,y,z])
>>> getterms(f)
[x, y, z]
>>> getterms(f.mean)
[x, y, z]
nipy.algorithms.statistics.formula.formulae.is_factor(obj)

Is obj a Factor?

nipy.algorithms.statistics.formula.formulae.is_factor_term(obj)

Is obj a FactorTerm?

nipy.algorithms.statistics.formula.formulae.is_formula(obj)

Is obj a Formula?

nipy.algorithms.statistics.formula.formulae.is_term(obj)

Is obj a Term?

nipy.algorithms.statistics.formula.formulae.make_dummy(name)

Make dummy variable of given name

Parameters :

name : str

name of dummy variable

Returns :

dum : Dummy instance

Notes

The interface to Dummy changed between 0.6.7 and 0.7.0

nipy.algorithms.statistics.formula.formulae.make_recarray(rows, names, dtypes=None)

Create recarray from rows with field names names

Create a recarray with named columns from a list of rows and names for the columns. If dtype is None, the dtype is based on rows if it is an np.ndarray, else the data is cast as np.float. If dtypes are supplied, it uses the dtypes to create a np.dtype unless rows is an np.ndarray, in which case dtypes are ignored

Parameters :

rows: array-like :

Rows that will be turned into an recarray.

names: sequence :

Sequence of strings - names for the columns.

dtypes: [str or np.dtype] :

Used to create a np.dtype, can be np.dtypes or string.

Returns :

v : np.ndarray

Examples

The following tests depend on machine byte order to pass

>>> arr = np.array([[3,4 ], [4, 6], [6, 8]])
>>> make_recarray(arr, ['x', 'y']) 
array([[(3, 4)],
       [(4, 6)],
       [(6, 8)]],
      dtype=[('x', '...'), ('y', '...')])
>>> r = make_recarray(arr, ['w', 'u'])
>>> make_recarray(r, ['x', 'y']) 
array([[(3, 4)],
       [(4, 6)],
       [(6, 8)]],
      dtype=[('x', '...'), ('y', '...')])
>>> make_recarray([[3, 4], [4, 6], [7, 9]], 'wv', [np.float, np.int]) 
array([(3.0, 4), (4.0, 6), (7.0, 9)],
      dtype=[('w', '...'), ('v', '...')])
nipy.algorithms.statistics.formula.formulae.natural_spline(t, knots=None, order=3, intercept=False)

Return a Formula containing a natural spline

Spline for a Term with specified knots and order.

Parameters :

t : Term

knots : None or sequence, optional

Sequence of float. Default None (same as empty list)

order : int, optional

Order of the spline. Defaults to a cubic (==3)

intercept : bool, optional

If True, include a constant function in the natural spline. Default is False

Returns :

formula : Formula

A Formula with (len(knots) + order) Terms (if intercept=False, otherwise includes one more Term), made up of the natural spline functions.

Examples

>>> x = Term('x')
>>> n = natural_spline(x, knots=[1,3,4], order=3)
>>> xval = np.array([3,5,7.]).view(np.dtype([('x', np.float)]))
>>> n.design(xval, return_float=True)
array([[   3.,    9.,   27.,    8.,    0.,   -0.],
       [   5.,   25.,  125.,   64.,    8.,    1.],
       [   7.,   49.,  343.,  216.,   64.,   27.]])
>>> d = n.design(xval)
>>> print d.dtype.descr
[('ns_1(x)', '<f8'), ('ns_2(x)', '<f8'), ('ns_3(x)', '<f8'), ('ns_4(x)', '<f8'), ('ns_5(x)', '<f8'), ('ns_6(x)', '<f8')]
nipy.algorithms.statistics.formula.formulae.terms(names, **kwargs)

Return list of terms with names given by names

This is just a convenience in defining a set of terms, and is the equivalent of sympy.symbols for defining symbols in sympy.

We enforce the sympy 0.7.0 behavior of returning symbol “abc” from input “abc”, rthan than 3 symbols “a”, “b”, “c”.

Parameters :

names : str or sequence of str

If a single str, can specify multiple ``Term``s with string containing space or ‘,’ as separator.

**kwargs : keyword arguments

keyword arguments as for sympy.symbols

Returns :

ts : Term or tuple

Term instance or list of Term instance objects named from names

Examples

>>> terms(('a', 'b', 'c'))
(a, b, c)
>>> terms('a, b, c')
(a, b, c)
>>> terms('abc')
abc