def dirichlet_logpdf_vec(x, alpha):
 shape = x.shape
 if (len(shape) == 1):
  try:
   return stats.dirichlet.logpdf(x, alpha)
  except:
   x[(-1)] = (1.0 - np.sum(x[:(-1)]))
   return stats.dirichlet.logpdf(x, alpha)
 elif (len(shape) == 2):
  size = shape[0]
  if (len(alpha.shape) == 1):
   return np.array([dirichlet_logpdf_vec(x[i, :], alpha) for i in range(size)])
  else:
   return np.array([dirichlet_logpdf_vec(x[i, :], alpha[i, :]) for i in range(size)])
 elif (len(shape) == 3):
  size = shape[0]
  return np.array([dirichlet_logpdf_vec(x[i, :, :], alpha) for i in range(size)])
 else:
  raise NotImplementedError()

def dirichlet_logpdf_vec ( x , alpha ) : shape = x . shape if ( len ( shape ) == 1 ) : try : return stats . dirichlet . logpdf ( x , alpha ) except : x [ ( - 1 ) ] = ( 1.0 - np . sum ( x [ : ( - 1 ) ] ) ) return stats . dirichlet . logpdf ( x , alpha ) elif ( len ( shape ) == 2 ) : size = shape [ 0 ] if ( len ( alpha . shape ) == 1 ) : return np . array ( [ dirichlet_logpdf_vec ( x [ i , : ] , alpha ) for i in range ( size ) ] ) else : return np . array ( [ dirichlet_logpdf_vec ( x [ i , : ] , alpha [ i , : ] ) for i in range ( size ) ] ) elif ( len ( shape ) == 3 ) : size = shape [ 0 ] return np . array ( [ dirichlet_logpdf_vec ( x [ i , : , : ] , alpha ) for i in range ( size ) ] ) else : raise NotImplementedError ( )
vectorized version of stats .