Skip to content

Commit

Permalink
Streamlined ImputeMissing.
Browse files Browse the repository at this point in the history
git-svn-id: https://pymc.googlecode.com/svn/trunk@1155 15d7aa0b-6f1a-0410-991a-d59f85d14984
  • Loading branch information
fonnesbeck committed Jan 15, 2009
1 parent 63d03ad commit b125991
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions pymc/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,7 @@ def multinomial_like(x, n, p):

x = np.atleast_2d(x) #flib expects 2d arguments. Do we still want to support multiple p values along realizations ?
p = np.atleast_2d(p)

return flib.multinomial(x, n, p)

# Multivariate hypergeometric------------------------------
Expand Down Expand Up @@ -2676,7 +2677,7 @@ def __init__(self, name, n, p, trace=True, value=None, rseed=False, observed=Fal
parents={'n':n,'p':p}, random=rmultinomial, trace=trace, value=value, dtype=np.int, rseed=rseed,
observed=observed, cache_depth=cache_depth, plot=plot, verbose=verbose, **kwds)

def ImputeMissing(name, dist_class, masked_values, **parents):
def ImputeMissing(name, dist_class, values, missing=None, **parents):
"""
This function accomodates missing elements for the data of simple
Stochastic distribution subclasses. The masked_values argument is an
Expand All @@ -2690,11 +2691,21 @@ def ImputeMissing(name, dist_class, masked_values, **parents):
Name of the data stochastic
- dist_class : Stochastic
Stochastic subclass such as Poisson, Normal, etc.
- value : numpy.ma.core.MaskedArray
A masked array with missing elements. Where mask=True, value is assumed missing.
- values : numpy.ma.core.MaskedArray or iterable
A masked array with missing elements (where mask=True, value is assumed missing),
or an iterable that contains missing elements, identified by 'missing' argument
- missing (optional): obj
A placeholder value that indicates missing data values. Only required if 'values'
is not a masked array already.
- parents (optional): dict
Arbitrary keyword arguments.
"""
masked_values = values
if not type(masked_values) == np.ma.core.MaskedArray:
# Generate mask
mask = np.array(values) == missing
# Generate masked array
masked_values = np.ma.masked_array(values, mask)

# Initialise list
vars = []
Expand All @@ -2705,7 +2716,7 @@ def ImputeMissing(name, dist_class, masked_values, **parents):
these_parents = {}
# Parse parents
for key, parent in parents.iteritems():
if len(parent.value) > 1:
if np.size(parent.value) > 1:
these_parents[key] = Lambda(key + '[%i]'%i, lambda p=parent, i=i: p[i])
else:
these_parents[key] = parent
Expand All @@ -2716,6 +2727,8 @@ def ImputeMissing(name, dist_class, masked_values, **parents):
# Observed values
vars.append(dist_class(this_name, value=masked_values[i], observed=True, **these_parents))
return vars

Impute = ImputeMissing

if __name__ == "__main__":
import doctest
Expand Down

0 comments on commit b125991

Please sign in to comment.