Package parsimony :: Package datasets :: Package simulate :: Module beta
[hide private]
[frames] | no frames]

Source Code for Module parsimony.datasets.simulate.beta

 1  # -*- coding: utf-8 -*- 
 2  """ 
 3  Created on Thu Sep 26 10:12:56 2013 
 4   
 5  Copyright (c) 2013-2014, CEA/DSV/I2BM/Neurospin. All rights reserved. 
 6   
 7  @author:  Tommy Löfstedt 
 8  @email:   tommy.loefstedt@cea.fr 
 9  @license: BSD 3-clause. 
10  """ 
11  import numpy as np 
12  import utils 
13   
14  __all__ = ['random'] 
15   
16   
17 -def random(shape, density=1.0, rng=utils.RandomUniform(0, 1).rand, 18 sort=False, normalise=False):
19 """Generates a random p-by-1 vector. 20 21 shape : A tuple. The shape of the underlying data. E.g., beta may represent 22 an underlying 2-by-3-by-4 image, and will in that case be 24-by-1. 23 24 density : A scalar in (0, 1]. The density of the returned regression vector 25 (fraction of non-zero elements). Zero-elements will be randomly 26 distributed in the vector. Default is 1.0. 27 28 rng : The random number generator. Must be a function that takes *shape as 29 input. Default is utils.RandomUniform in the interval [0, 1). 30 31 sort : A boolean. Whether or not to sort the vector. The vector is sorted 32 along the dimensions in order from the first. Default is False. 33 34 normalise : A boolean. Whether or not to normalise the vector. Default is 35 False. 36 """ 37 if not isinstance(shape, (list, tuple)): 38 shape = (shape,) 39 40 density = max(0.0, min(density, 1.0)) 41 42 p = np.prod(shape) 43 ps = int(density * p + 0.5) 44 45 beta = rng(p) 46 beta[ps:] = 0.0 47 beta = np.random.permutation(beta) 48 49 if sort: 50 beta = np.reshape(beta, shape) 51 for i in xrange(len(shape)): 52 beta = np.sort(beta, axis=i) 53 54 beta = np.reshape(beta, (p, 1)) 55 56 if normalise: 57 beta *= 1.0 / utils.norm2(beta) 58 59 return beta
60 61 if __name__ == "__main__": 62 import doctest 63 doctest.testmod() 64