def sample(self, index={}):
        """ in order to sample from this distributions, all parents must be known """
#		 mean = self.mean.copy()
#		 sigma = self.sigma.copy()
##		  if index:
##			  # discrete parents
##			  for v,i in enumerate(reversed(self.discrete_parents)):
##			  # reverse: avoid missing axes when taking in random
##			  # we start from the end, that way all other dimensions keep the same index
##				  if index.has_key(v.name): 
##					  # take the corresponding mean; +1 because first axis is the mean
##					  mean = na.take(mean, index[v], axis=(i+1) )
##					  # take the corresponding covariance; +2 because first 2 axes are the cov
##					  sigma = na.take(sigma, index[v], axis=(i+2) )
##			  
##			  # continuous parents
##			  for v in reversed(self.continuous_parents):
##				  if index.has_key(v):

        d_index, c_index = self._numIndexFromDict(index)
        mean  = na.array(self.mean[tuple([slice(None, None, None)] + d_index)])
        sigma = self.sigma[tuple([slice(None, None, None)] * 2 +d_index)]
        wi = na.sum(self.weights * na.array(c_index)[na.NewAxis,...], axis=1)

#		 if self.continuous_parents:
#			 wi = na.array(self.weights[tuple([slice(None,None,None)]+c_index)])
#		 else: wi = 0.0

        # return a random number from a normal multivariate distribution
        return float(ra.multivariate_normal(mean + wi, sigma))
 # than zero given the forecasts.
 if _use_numarray:
     from numarray.random_array import multivariate_normal
     import numarray.mlab as mlab
 else:
     from numpy.oldnumeric.random_array import multivariate_normal
     import numpy.oldnumeric.mlab as mlab
 # number of realizations.
 nsamps = 100000
 # correlations
 r12 = 0.5  # average correlation between the first predictor and the obs.
 r13 = 0.25  # avg correlation between the second predictor and the obs.
 r23 = 0.125  # avg correlation between predictors.
 # random draws from trivariate normal distribution
 x = multivariate_normal(
     NA.array([0, 0, 0]),
     NA.array([[1, r12, r13], [r12, 1, r23], [r13, r23, 1]]), nsamps)
 x2 = multivariate_normal(
     NA.array([0, 0, 0]),
     NA.array([[1, r12, r13], [r12, 1, r23], [r13, r23, 1]]), nsamps)
 print 'correlations (r12,r13,r23) = ', r12, r13, r23
 print 'number of realizations = ', nsamps
 # training data.
 obs = x[:, 0]
 climprob = NA.sum((obs > 0).astype('f')) / nsamps
 fcst = NA.transpose(x[:, 1:])  # 2 predictors.
 obs_binary = obs > 0.
 # independent data for verification.
 obs2 = x2[:, 0]
 fcst2 = NA.transpose(x2[:, 1:])
 # compute logistic regression.
 # the conditional probability that the observation will be greater
 # than zero given the forecasts.
 if _use_numarray:
     from numarray.random_array import multivariate_normal
     import numarray.mlab as mlab
 else:
     from numpy.oldnumeric.random_array import multivariate_normal
     import numpy.oldnumeric.mlab as mlab
 # number of realizations.
 nsamps = 100000
 # correlations
 r12 = 0.5 # average correlation between the first predictor and the obs.
 r13 = 0.25 # avg correlation between the second predictor and the obs.
 r23 = 0.125 # avg correlation between predictors.
 # random draws from trivariate normal distribution
 x = multivariate_normal(NA.array([0,0,0]),NA.array([[1,r12,r13],[r12,1,r23],[r13,r23,1]]), nsamps)
 x2 = multivariate_normal(NA.array([0,0,0]),NA.array([[1,r12,r13],[r12,1,r23],[r13,r23,1]]), nsamps)
 print 'correlations (r12,r13,r23) = ',r12,r13,r23
 print 'number of realizations = ',nsamps
 # training data.
 obs = x[:,0]
 climprob = NA.sum((obs > 0).astype('f'))/nsamps
 fcst = NA.transpose(x[:,1:]) # 2 predictors.
 obs_binary = obs > 0.
 # independent data for verification.
 obs2 = x2[:,0]
 fcst2 = NA.transpose(x2[:,1:])
 # compute logistic regression.
 beta,Jbar,llik = logistic_regression(fcst,obs_binary,verbose=True)
 covmat = LA.inverse(Jbar)
 stderr = NA.sqrt(mlab.diag(covmat))
Esempio n. 4
0
 def observe_pos(self):
     """
     """
     self.pos = random_array.multivariate_normal(self.initial_pos,self.true_err)
Esempio n. 5
0
 def observe_pos(self):
     """
     """
     self.pos = random_array.multivariate_normal(self.initial_pos,
                                                 self.true_err)