samman - 10 months ago 24

Python Question

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.

Answer

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