I have this code snippet I am trying to understand that is in python. I don't understand how scalars operate on arrays in all cases. In most code I read it makes sense that operations work on each value of an array.
sig_sq_samples = beta*invgamma.rvs(alpha,size=n_samples)
var_norm = sqrt(sig_sq_samples/kN)
mu_samples = norm.rvs(mean_norm,scale=var_norm,size=n_samples)
I want to know how each line is functioning. The reason being is that I don't have a linux machine setup with the library and thought someone may be able to help me understand this python code I have found in an article. I can not setup the environment in a reasonable amount of time.
invgamma.rvs() - returns an array of numeric values
beta - is a scalar value
sig_sq_samples (I'm assuming)- is an array of beta * each array value of
what invgamma.rvs() function returns.
var_norm - I have no idea what this value is supposed to be because
the norm.rvs function underneath takes a scalar (scale=var_norm).
In short how is sqrt(siq_sq_samples/kn) with kN also a scalar returning back a scalar? What is happening here? This one line is what is getting me. Like I said earlier sig_sq_samples is an array. I hope I'm not wrong about that line that is producing sig_sq_samples. At one point or another the values being worked on are scalars. I am from c# where hard types are used and I have worked with scripting languages such as PERL where I had a lot of experience with what "shortcut" operations do. Ex. C# does not allow you to multiply a scalar to an array. I tried to look up how scalars work with arrays but it doesn't clarify this code to me. Anyone answering is more than welcome to look up the functions above in case I am wrong about anything. I put a lot of effort and I have many years of development experience. Either this code snippet is wrong or I'm just not seeing something real obvious.
In the line
mu_samples = norm.rvs(mean_norm,scale=var_norm,size=n_samples)
n_samples
has the same size as var_norm
, so what is happening is that for the ith sample of n_samples
, it generates it using the ith scale parameter of var_norm
, var_norm[i]
Internal to the code is vals = vals * scale + loc
, when scale is an array it uses broadcasting which is a common feature of numpy. norm.rvs
already generated an array of n_samples
random values. When multiplied by scale
, it does an element-wise multiplication between each array. The result is that the left hand side will also be an array value. For more information see here
sig_sq_samples = beta*invgamma.rvs(alpha,size=n_samples)
if invgamma.rvs() - returns an array of numeric values beta - is a scalar value
then
sig_sq_samples = beta*invgamma.rvs(alpha,size=n_samples)
produces another array of the same size. Scalar beta
just multiplies each element.
In
var_norm = sqrt(sig_sq_samples/kN)
kN
is scalar doing the same thing - dividing each element. I assume sqrt
is numpy.sqrt
, which takes the sqrt of each element. So var_norm
is again an array of the original size (of invgammas.rvs()
).
mu_samples = norm.rvs(mean_norm,scale=var_norm,size=n_samples)
I don't know what norm.rvs
does, or where it is from. It's not numpy
, but could be a package in scipy
. I'd have to google it. It takes one postional argument, here mean_norm
, and two (at least) keyword values. n_samples
is probably a number, eg. 100. But scale
could certainly take an array, such as var_norm
.
======================
appears to be the documentation for the rvs
method ( norm
is a subclass of rv_continuous
).
Arguments are:
arg1, arg2, arg3,... : array_like
The shape parameter(s) for the distribution (see docstring of the instance object for more information).
scale : array_like, optional
Scale parameter (default=1).
size : int or tuple of ints, optional
Defining number of random variates (default is 1).
and the result is
rvs : ndarray or scalar
Random variates of given size.
I'm guessing invgamma.rvs
is the similar method for a different subclass. alpha
must be the shape
argument for the first, and norm_mean
the shape for the 2nd.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.