Search code examples
pythonstatisticsquantile

Create this simple array


I have two one-dimensional arrays u and q as follows:

import numpy as np
n = 300
u1 = np.random.uniform(0.5, 4.5, size=n)
u2 = 2 / u1 + np.random.normal(scale=0.09, size=n)
u = np.array([u1,u2])

and

d=np.random.uniform(0.0, np.pi, size=100)
    q = np.array([np.cos(d), np.sin(d)])

I have the following

np.max(np.quantile(np.dot(q[:, 0],u[:,]), 0.01))-np.max(np.quantile(np.dot(q[:, 0],u[:,]), 0.99))

for q[:, 0], but I want to have it for every q[:,m] from m=1 to 100 (I mean for all columns of q) and save them all in a new array.

How can I do that?


Solution

  • I'm not sure what you're trying to do and what result you expect. I'm going slow with my explanation so that if I got your idea wrong, you can modify my code to suit your need.


    Replace np.dot(q[:, 0],u[:,] with:

    np.dot(q.transpose(), u)
    

    The shape of np.dot(q[:, 0],u[:,] is (300), and that of np.dot(q.transpose(), u) is (100, 300). This basically dot-product each of 100 columns of q with u.

    Replace your quantile operation with:

    np.quantile(np.dot(q.transpose(), u), q=0.01, axis=-1) 
    # or q=0.99 for the other one
    

    By specifying an axis, you restrict quantile to only operate on that axis, rather than on the entire array. The result has shape (100).

    Before, your quantile operation returns a scalar, so wrapping it in np.max does nothing. Now it returns the max of a 100-size array. The final line should be:

    np.max(np.quantile(np.dot(q.transpose(), u), q=0.01, axis=-1)) 
    - np.max(np.quantile(np.dot(q.transpose(), u), q=0.99, axis=-1))
    

    The result is a scalar.