scipy stats.cosine() in Python
The SciPy library comes with many variables to facilitate users to explore the world of statistics. One such variable is the scipy stats.cosine() variable in Python which we are going to discuss today. It is a continuous random variable with a standard format. Also, its definition needs some shape parameters.
So first, let us see what are the parameters of this variable. These are x, q, loc, scale, size, and moments:
- x (array) – number of quantiles.
- q (array) – lower or upper-end probability.
- loc (array) – location parameter. The default is 0.
- scale (array) – scale parameter. The default is 1.
- size (int/tuple of ints) – the shape of random variables.
- moments (string) – composed of letters [‘mvsk’] specifying the moments to compute. Here, ‘m’, ‘v’, ‘s’, and ‘k’ are ‘mean’, ‘variance’, ‘(Fisher’s) skew’, and ‘(Fisher’s) kurtosis’. The default is ‘mv’.
Before we see the usage of this function, let us find out what it stands for. So, the cosine distribution approximates the normal distribution. Its probability density function is given by:
where
Random Variates
Let us look at the random variates so that we can understand the basic functions.
from scipy.stats import cosine print(cosine.rvs(scale = 2, size = 5))
Output:
[ 2.63039544 -1.80921027 -1.34999499 -4.21529217 1.33511733]
Probability Distribution
Let us have a look at how to generate a cosine distribution mentioning quantile, location, and scale.
import numpy as np from scipy.stats import cosine quantile = np.arange (0.05, 1, 0.2) print(cosine.pdf(quantile, loc = 0, scale = 1))
Output:
[0.31811098 0.31336214 0.30246555 0.28585561 0.26419452]
Plotting the Distribution
Let us plot a cosine distribution.
import numpy as np import matplotlib.pyplot as plt from scipy.stats import cosine dist = np.linspace(-1, np.minimum(cosine().dist.b, 4)) print(dist) plot = plt.plot(dist, cosine().pdf(dist))
Output:
Hence. the diagram above plots the distribution printed above. As you can notice, the values range from -1 to ‘pi’.
Plotting Cosine and Stepfilled Distributions together
from scipy.stats import cosine import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) mean, var, skew, kurt = cosine.stats(moments='mvsk') l = np.linspace(cosine.ppf(0.02), cosine.ppf(0.9), 100)) ax.plot(l, cosine.pdf(l), 'r-', lw=10, alpha=0.6, label='cosine pdf') ax.plot(l, cosine().pdf(l), lw=2, label='frozen pdf') nums = cosine.ppf([0.001, 0.5, 0.9]) np.allclose([0.001, 0.5, 0.9], cosine.cdf(nums)) d = cosine.rvs(size=100) ax.hist(d, density=True, histtype='stepfilled', alpha=0.5) ax.legend(loc='best', frameon=False) plt.show()
Output:
As discussed earlier, the cosine distribution is actually an approximation of the normal distribution. Hence the illustration above legitimizes the claim. If you want to bring changes in the manner of the visualization, you can easily do that. Though this is optional.
So this concludes a simplified tutorial on the scipy stats.cosine() variable in python. But a lot of work is still on you as you have to decide what parameters to use as per the problem statement.
Leave a Reply