This function is useful for debugging purposes, especially if you’re dealing with
custom asts not generated by python itself.
It could be that the source code is evaluable when the AST itself is not
compilable / evaluable. The reason for this is that the AST contains some
more data than regular sourcecode does, which is dropped during
conversion.
Each level of indentation is replaced with indent_with. Per default this
parameter is equal to four spaces as suggested by PEP 8, but it might be
adjusted to match the application’s styleguide.
If add_line_information is set to True comments for the line numbers
of the nodes are added to the output. This can be used to spot wrong line
number information of statement nodes.
f is a guvectorized function: f(x1, x2, ,xn, y1,..yp)
args is a list of vectors [x1,…,xn]
add_args is a list of vectors [y1,…,yn]
the function returns a list [r, dx1, …, dxn] where:
r is the vector value value of f at (x1, xn, y1, yp)
dxi is jacobian w.r.t. xi
TODO: generalize when x1, …, xn have non-core dimensions
snowdrop.src.preprocessor.function_compiler.get_deps(incidence, var, visited=None)[source]¶
If True use AST tree to parse equations, stringify variables and convert to sumpy expressions.
Otherwise parse equations with the aid of regular expressions of ReGex module.
If True use AST tree to parse equations, stringify variables and convert to sumpy expressions.
Otherwise parse equations with the aid of regular expressions of ReGex module.
The Beta distribution is a special case of the Dirichlet distribution,
and is related to the Gamma distribution. It has the probability
distribution function
Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. If size is None (default),
a single value is returned if a and b are both scalars.
Otherwise, np.broadcast(a,b).size samples are drawn.
Samples are drawn from a binomial distribution with specified
parameters, n trials and p probability of success where
n an integer >= 0 and p is in the interval [0,1]. (n may be
input as a float, but it is truncated to an integer in use)
Note
New code should use the ~numpy.random.Generator.binomial
method of a ~numpy.random.Generator instance instead;
please see the random-quick-start.
Parameter of the distribution, >= 0. Floats are also accepted,
but they will be truncated to integers.
pfloat or array_like of floats
Parameter of the distribution, >= 0 and <=1.
sizeint or tuple of ints, optional
Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. If size is None (default),
a single value is returned if n and p are both scalars.
Otherwise, np.broadcast(n,p).size samples are drawn.
The probability density for the binomial distribution is
\[P(N) = \binom{n}{N}p^N(1-p)^{n-N},\]
where \(n\) is the number of trials, \(p\) is the probability
of success, and \(N\) is the number of successes.
When estimating the standard error of a proportion in a population by
using a random sample, the normal distribution works well unless the
product p*n <=5, where p = population proportion estimate, and n =
number of samples, in which case the binomial distribution is used
instead. For example, a sample of 15 people shows 4 who are left
handed, and 11 who are right handed. Then p = 4/15 = 27%. 0.27*15 = 4,
so the binomial distribution should be used in this case.
>>> n,p=10,.5# number of trials, probability of each trial>>> s=np.random.binomial(n,p,1000)# result of flipping a coin 10 times, tested 1000 times.
A real world example. A company drills 9 wild-cat oil exploration
wells, each with an estimated probability of success of 0.1. All nine
wells fail. What is the probability of that happening?
Let’s do 20,000 trials of the model, and count the number that
generate zero positive results.
>>> sum(np.random.binomial(9,0.1,20000)==0)/20000.# answer = 0.38885, or 38%.
Samples are drawn from a Gamma distribution with specified parameters,
shape (sometimes designated “k”) and scale (sometimes designated
“theta”), where both parameters are > 0.
Note
New code should use the ~numpy.random.Generator.gamma
method of a ~numpy.random.Generator instance instead;
please see the random-quick-start.
The shape of the gamma distribution. Must be non-negative.
scalefloat or array_like of floats, optional
The scale of the gamma distribution. Must be non-negative.
Default is equal to 1.
sizeint or tuple of ints, optional
Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. If size is None (default),
a single value is returned if shape and scale are both scalars.
Otherwise, np.broadcast(shape,scale).size samples are drawn.
where \(k\) is the shape and \(\theta\) the scale,
and \(\Gamma\) is the Gamma function.
The Gamma distribution is often used to model the times to failure of
electronic components, and arises naturally in processes for which the
waiting times between Poisson distributed events are relevant.
Parameter of the distribution. Must be non-negative.
Default is 1.
sizeint or tuple of ints, optional
Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. If size is None (default),
a single value is returned if loc and scale are both scalars.
Otherwise, np.broadcast(loc,scale).size samples are drawn.
The Logistic distribution is used in Extreme Value problems where it
can act as a mixture of Gumbel distributions, in Epidemiology, and by
the World Chess Federation (FIDE) where it is used in the Elo ranking
system, assuming the performance of each player is a logistically
distributed random variable.
Draw samples from a log-normal distribution with specified mean,
standard deviation, and array shape. Note that the mean and standard
deviation are not the values for the distribution itself, but of the
underlying normal distribution it is derived from.
Note
New code should use the ~numpy.random.Generator.lognormal
method of a ~numpy.random.Generator instance instead;
please see the random-quick-start.
Mean value of the underlying normal distribution. Default is 0.
sigmafloat or array_like of floats, optional
Standard deviation of the underlying normal distribution. Must be
non-negative. Default is 1.
sizeint or tuple of ints, optional
Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. If size is None (default),
a single value is returned if mean and sigma are both scalars.
Otherwise, np.broadcast(mean,sigma).size samples are drawn.
A variable x has a log-normal distribution if log(x) is normally
distributed. The probability density function for the log-normal
distribution is:
\[p(x) = \frac{1}{\sigma x \sqrt{2\pi}}
e^{(-\frac{(ln(x)-\mu)^2}{2\sigma^2})}\]
where \(\mu\) is the mean and \(\sigma\) is the standard
deviation of the normally distributed logarithm of the variable.
A log-normal distribution results if a random variable is the product
of a large number of independent, identically-distributed variables in
the same way that a normal distribution results if the variable is the
sum of a large number of independent, identically-distributed
variables.
Demonstrate that taking the products of random samples from a uniform
distribution can be fit well by a log-normal probability density
function.
>>> # Generate a thousand samples: each is the product of 100 random>>> # values, drawn from a normal distribution.>>> b=[]>>> foriinrange(1000):... a=10.+np.random.standard_normal(100)... b.append(np.prod(a))
>>> b=np.array(b)/np.min(b)# scale values to be positive>>> count,bins,ignored=plt.hist(b,100,density=True,align='mid')>>> sigma=np.std(np.log(b))>>> mu=np.mean(np.log(b))
Draw random samples from a multivariate normal distribution.
The multivariate normal, multinormal or Gaussian distribution is a
generalization of the one-dimensional normal distribution to higher
dimensions. Such a distribution is specified by its mean and
covariance matrix. These parameters are analogous to the mean
(average or “center”) and variance (standard deviation, or “width,”
squared) of the one-dimensional normal distribution.
Note
New code should use the
~numpy.random.Generator.multivariate_normal
method of a ~numpy.random.Generator instance instead;
please see the random-quick-start.
Covariance matrix of the distribution. It must be symmetric and
positive-semidefinite for proper sampling.
sizeint or tuple of ints, optional
Given a shape of, for example, (m,n,k), m*n*k samples are
generated, and packed in an m-by-n-by-k arrangement. Because
each sample is N-dimensional, the output shape is (m,n,k,N).
If no shape is specified, a single (N-D) sample is returned.
The mean is a coordinate in N-dimensional space, which represents the
location where samples are most likely to be generated. This is
analogous to the peak of the bell curve for the one-dimensional or
univariate normal distribution.
Covariance indicates the level to which two variables vary together.
From the multivariate normal distribution, we draw N-dimensional
samples, \(X = [x_1, x_2, ... x_N]\). The covariance matrix
element \(C_{ij}\) is the covariance of \(x_i\) and \(x_j\).
The element \(C_{ii}\) is the variance of \(x_i\) (i.e. its
“spread”).
Instead of specifying the full covariance matrix, popular
approximations include:
Spherical covariance (cov is a multiple of the identity matrix)
Diagonal covariance (cov has non-negative elements, and only on
the diagonal)
This geometrical property can be seen in two dimensions by plotting
generated data-points:
Note that the covariance matrix must be positive semidefinite (a.k.a.
nonnegative-definite). Otherwise, the behavior of this method is
undefined and backwards compatibility is not guaranteed.
Here we generate 800 samples from the bivariate normal distribution
with mean [0, 0] and covariance matrix [[6, -3], [-3, 3.5]]. The
expected variances of the first and second components of the sample
are 6 and 3.5, respectively, and the expected correlation
coefficient is -3/sqrt(6*3.5) ≈ -0.65465.
Check that the mean, covariance, and correlation coefficient of the
sample are close to the expected values:
>>> pts.mean(axis=0)array([ 0.0326911 , -0.01280782]) # may vary>>> np.cov(pts.T)array([[ 5.96202397, -2.85602287], [-2.85602287, 3.47613949]]) # may vary>>> np.corrcoef(pts.T)[0,1]-0.6273591314603949 # may vary
We can visualize this data with a scatter plot. The orientation
of the point cloud illustrates the negative correlation of the
components of this sample.
Draw random samples from a normal (Gaussian) distribution.
The probability density function of the normal distribution, first
derived by De Moivre and 200 years later by both Gauss and Laplace
independently [2]_, is often called the bell curve because of
its characteristic shape (see the example below).
The normal distributions occurs often in nature. For example, it
describes the commonly occurring distribution of samples influenced
by a large number of tiny, random disturbances, each with its own
unique distribution [2]_.
Note
New code should use the ~numpy.random.Generator.normal
method of a ~numpy.random.Generator instance instead;
please see the random-quick-start.
Standard deviation (spread or “width”) of the distribution. Must be
non-negative.
sizeint or tuple of ints, optional
Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. If size is None (default),
a single value is returned if loc and scale are both scalars.
Otherwise, np.broadcast(loc,scale).size samples are drawn.
where \(\mu\) is the mean and \(\sigma\) the standard
deviation. The square of the standard deviation, \(\sigma^2\),
is called the variance.
The function has its peak at the mean, and its “spread” increases with
the standard deviation (the function reaches 0.607 times its maximum at
\(x + \sigma\) and \(x - \sigma\)[2]_). This implies that
normal is more likely to return samples lying close to the mean, rather
than those far away.
Samples are uniformly distributed over the half-open interval
[low,high) (includes low, but excludes high). In other words,
any value within the given interval is equally likely to be drawn
by uniform.
Note
New code should use the ~numpy.random.Generator.uniform
method of a ~numpy.random.Generator instance instead;
please see the random-quick-start.
Lower boundary of the output interval. All values generated will be
greater than or equal to low. The default value is 0.
highfloat or array_like of floats
Upper boundary of the output interval. All values generated will be
less than or equal to high. The high limit may be included in the
returned array of floats due to floating-point rounding in the
equation low+(high-low)*random_sample(). The default value
is 1.0.
sizeint or tuple of ints, optional
Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. If size is None (default),
a single value is returned if low and high are both scalars.
Otherwise, np.broadcast(low,high).size samples are drawn.
randint : Discrete uniform distribution, yielding integers.
random_integers : Discrete uniform distribution over the closed
interval [low,high].
random_sample : Floats uniformly distributed over [0,1).
random : Alias for random_sample.
rand : Convenience function that accepts dimensions as input, e.g.,
rand(2,2) would generate a 2-by-2 array of floats,
uniformly distributed over [0,1).
random.Generator.uniform: which should be used for new code.
The probability density function of the uniform distribution is
\[p(x) = \frac{1}{b - a}\]
anywhere within the interval [a,b), and zero elsewhere.
When high == low, values of low will be returned.
If high < low, the results are officially undefined
and may eventually raise an error, i.e. do not rely on this
function to behave when passed arguments satisfying that
inequality condition. The high limit may be included in the
returned array of floats due to floating-point rounding in the
equation low+(high-low)*random_sample(). For example:
The Beta distribution is a special case of the Dirichlet distribution,
and is related to the Gamma distribution. It has the probability
distribution function
Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. If size is None (default),
a single value is returned if a and b are both scalars.
Otherwise, np.broadcast(a,b).size samples are drawn.
Samples are drawn from a binomial distribution with specified
parameters, n trials and p probability of success where
n an integer >= 0 and p is in the interval [0,1]. (n may be
input as a float, but it is truncated to an integer in use)
Note
New code should use the ~numpy.random.Generator.binomial
method of a ~numpy.random.Generator instance instead;
please see the random-quick-start.
Parameter of the distribution, >= 0. Floats are also accepted,
but they will be truncated to integers.
pfloat or array_like of floats
Parameter of the distribution, >= 0 and <=1.
sizeint or tuple of ints, optional
Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. If size is None (default),
a single value is returned if n and p are both scalars.
Otherwise, np.broadcast(n,p).size samples are drawn.
The probability density for the binomial distribution is
\[P(N) = \binom{n}{N}p^N(1-p)^{n-N},\]
where \(n\) is the number of trials, \(p\) is the probability
of success, and \(N\) is the number of successes.
When estimating the standard error of a proportion in a population by
using a random sample, the normal distribution works well unless the
product p*n <=5, where p = population proportion estimate, and n =
number of samples, in which case the binomial distribution is used
instead. For example, a sample of 15 people shows 4 who are left
handed, and 11 who are right handed. Then p = 4/15 = 27%. 0.27*15 = 4,
so the binomial distribution should be used in this case.
>>> n,p=10,.5# number of trials, probability of each trial>>> s=np.random.binomial(n,p,1000)# result of flipping a coin 10 times, tested 1000 times.
A real world example. A company drills 9 wild-cat oil exploration
wells, each with an estimated probability of success of 0.1. All nine
wells fail. What is the probability of that happening?
Let’s do 20,000 trials of the model, and count the number that
generate zero positive results.
>>> sum(np.random.binomial(9,0.1,20000)==0)/20000.# answer = 0.38885, or 38%.
Samples are drawn from a Gamma distribution with specified parameters,
shape (sometimes designated “k”) and scale (sometimes designated
“theta”), where both parameters are > 0.
Note
New code should use the ~numpy.random.Generator.gamma
method of a ~numpy.random.Generator instance instead;
please see the random-quick-start.
The shape of the gamma distribution. Must be non-negative.
scalefloat or array_like of floats, optional
The scale of the gamma distribution. Must be non-negative.
Default is equal to 1.
sizeint or tuple of ints, optional
Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. If size is None (default),
a single value is returned if shape and scale are both scalars.
Otherwise, np.broadcast(shape,scale).size samples are drawn.
where \(k\) is the shape and \(\theta\) the scale,
and \(\Gamma\) is the Gamma function.
The Gamma distribution is often used to model the times to failure of
electronic components, and arises naturally in processes for which the
waiting times between Poisson distributed events are relevant.
Parameter of the distribution. Must be non-negative.
Default is 1.
sizeint or tuple of ints, optional
Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. If size is None (default),
a single value is returned if loc and scale are both scalars.
Otherwise, np.broadcast(loc,scale).size samples are drawn.
The Logistic distribution is used in Extreme Value problems where it
can act as a mixture of Gumbel distributions, in Epidemiology, and by
the World Chess Federation (FIDE) where it is used in the Elo ranking
system, assuming the performance of each player is a logistically
distributed random variable.
Draw samples from a log-normal distribution with specified mean,
standard deviation, and array shape. Note that the mean and standard
deviation are not the values for the distribution itself, but of the
underlying normal distribution it is derived from.
Note
New code should use the ~numpy.random.Generator.lognormal
method of a ~numpy.random.Generator instance instead;
please see the random-quick-start.
Mean value of the underlying normal distribution. Default is 0.
sigmafloat or array_like of floats, optional
Standard deviation of the underlying normal distribution. Must be
non-negative. Default is 1.
sizeint or tuple of ints, optional
Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. If size is None (default),
a single value is returned if mean and sigma are both scalars.
Otherwise, np.broadcast(mean,sigma).size samples are drawn.
A variable x has a log-normal distribution if log(x) is normally
distributed. The probability density function for the log-normal
distribution is:
\[p(x) = \frac{1}{\sigma x \sqrt{2\pi}}
e^{(-\frac{(ln(x)-\mu)^2}{2\sigma^2})}\]
where \(\mu\) is the mean and \(\sigma\) is the standard
deviation of the normally distributed logarithm of the variable.
A log-normal distribution results if a random variable is the product
of a large number of independent, identically-distributed variables in
the same way that a normal distribution results if the variable is the
sum of a large number of independent, identically-distributed
variables.
Demonstrate that taking the products of random samples from a uniform
distribution can be fit well by a log-normal probability density
function.
>>> # Generate a thousand samples: each is the product of 100 random>>> # values, drawn from a normal distribution.>>> b=[]>>> foriinrange(1000):... a=10.+np.random.standard_normal(100)... b.append(np.prod(a))
>>> b=np.array(b)/np.min(b)# scale values to be positive>>> count,bins,ignored=plt.hist(b,100,density=True,align='mid')>>> sigma=np.std(np.log(b))>>> mu=np.mean(np.log(b))
Draw random samples from a multivariate normal distribution.
The multivariate normal, multinormal or Gaussian distribution is a
generalization of the one-dimensional normal distribution to higher
dimensions. Such a distribution is specified by its mean and
covariance matrix. These parameters are analogous to the mean
(average or “center”) and variance (standard deviation, or “width,”
squared) of the one-dimensional normal distribution.
Note
New code should use the
~numpy.random.Generator.multivariate_normal
method of a ~numpy.random.Generator instance instead;
please see the random-quick-start.
Covariance matrix of the distribution. It must be symmetric and
positive-semidefinite for proper sampling.
sizeint or tuple of ints, optional
Given a shape of, for example, (m,n,k), m*n*k samples are
generated, and packed in an m-by-n-by-k arrangement. Because
each sample is N-dimensional, the output shape is (m,n,k,N).
If no shape is specified, a single (N-D) sample is returned.
The mean is a coordinate in N-dimensional space, which represents the
location where samples are most likely to be generated. This is
analogous to the peak of the bell curve for the one-dimensional or
univariate normal distribution.
Covariance indicates the level to which two variables vary together.
From the multivariate normal distribution, we draw N-dimensional
samples, \(X = [x_1, x_2, ... x_N]\). The covariance matrix
element \(C_{ij}\) is the covariance of \(x_i\) and \(x_j\).
The element \(C_{ii}\) is the variance of \(x_i\) (i.e. its
“spread”).
Instead of specifying the full covariance matrix, popular
approximations include:
Spherical covariance (cov is a multiple of the identity matrix)
Diagonal covariance (cov has non-negative elements, and only on
the diagonal)
This geometrical property can be seen in two dimensions by plotting
generated data-points:
Note that the covariance matrix must be positive semidefinite (a.k.a.
nonnegative-definite). Otherwise, the behavior of this method is
undefined and backwards compatibility is not guaranteed.
Here we generate 800 samples from the bivariate normal distribution
with mean [0, 0] and covariance matrix [[6, -3], [-3, 3.5]]. The
expected variances of the first and second components of the sample
are 6 and 3.5, respectively, and the expected correlation
coefficient is -3/sqrt(6*3.5) ≈ -0.65465.
Check that the mean, covariance, and correlation coefficient of the
sample are close to the expected values:
>>> pts.mean(axis=0)array([ 0.0326911 , -0.01280782]) # may vary>>> np.cov(pts.T)array([[ 5.96202397, -2.85602287], [-2.85602287, 3.47613949]]) # may vary>>> np.corrcoef(pts.T)[0,1]-0.6273591314603949 # may vary
We can visualize this data with a scatter plot. The orientation
of the point cloud illustrates the negative correlation of the
components of this sample.
Draw random samples from a normal (Gaussian) distribution.
The probability density function of the normal distribution, first
derived by De Moivre and 200 years later by both Gauss and Laplace
independently [2]_, is often called the bell curve because of
its characteristic shape (see the example below).
The normal distributions occurs often in nature. For example, it
describes the commonly occurring distribution of samples influenced
by a large number of tiny, random disturbances, each with its own
unique distribution [2]_.
Note
New code should use the ~numpy.random.Generator.normal
method of a ~numpy.random.Generator instance instead;
please see the random-quick-start.
Standard deviation (spread or “width”) of the distribution. Must be
non-negative.
sizeint or tuple of ints, optional
Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. If size is None (default),
a single value is returned if loc and scale are both scalars.
Otherwise, np.broadcast(loc,scale).size samples are drawn.
where \(\mu\) is the mean and \(\sigma\) the standard
deviation. The square of the standard deviation, \(\sigma^2\),
is called the variance.
The function has its peak at the mean, and its “spread” increases with
the standard deviation (the function reaches 0.607 times its maximum at
\(x + \sigma\) and \(x - \sigma\)[2]_). This implies that
normal is more likely to return samples lying close to the mean, rather
than those far away.
Samples are uniformly distributed over the half-open interval
[low,high) (includes low, but excludes high). In other words,
any value within the given interval is equally likely to be drawn
by uniform.
Note
New code should use the ~numpy.random.Generator.uniform
method of a ~numpy.random.Generator instance instead;
please see the random-quick-start.
Lower boundary of the output interval. All values generated will be
greater than or equal to low. The default value is 0.
highfloat or array_like of floats
Upper boundary of the output interval. All values generated will be
less than or equal to high. The high limit may be included in the
returned array of floats due to floating-point rounding in the
equation low+(high-low)*random_sample(). The default value
is 1.0.
sizeint or tuple of ints, optional
Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. If size is None (default),
a single value is returned if low and high are both scalars.
Otherwise, np.broadcast(low,high).size samples are drawn.
randint : Discrete uniform distribution, yielding integers.
random_integers : Discrete uniform distribution over the closed
interval [low,high].
random_sample : Floats uniformly distributed over [0,1).
random : Alias for random_sample.
rand : Convenience function that accepts dimensions as input, e.g.,
rand(2,2) would generate a 2-by-2 array of floats,
uniformly distributed over [0,1).
random.Generator.uniform: which should be used for new code.
The probability density function of the uniform distribution is
\[p(x) = \frac{1}{b - a}\]
anywhere within the interval [a,b), and zero elsewhere.
When high == low, values of low will be returned.
If high < low, the results are officially undefined
and may eventually raise an error, i.e. do not rely on this
function to behave when passed arguments satisfying that
inequality condition. The high limit may be included in the
returned array of floats due to floating-point rounding in the
equation low+(high-low)*random_sample(). For example:
Ideas :
* recursive blocks [by default]
* (order left hand side ?) [by default]
* dependency across blocks
* dummy blocks that are basically substituted everywhere else