Time for action – handling NaNs with the nanmean(), nanvar(), and nanstd() functions
We will apply jackknife resampling to the stock data. Each value will be omitted by setting it to Not a Number (NaN). The nanmean()
, nanvar()
, and nanstd()
can then be used to compute the arithmetic mean, variance, and standard deviation.
First, initialize a 30-by-3 array for the estimates as follows:
estimates = np.zeros((len(c), 3))
Loop through the values and generate a new dataset by setting one value to NaN at each iteration of the loop. For each new set of values, compute the estimates:
for i in xrange(len(c)): a = c.copy() a[i] = np.nan estimates[i,] = [np.nanmean(a), np.nanvar(a), np.nanstd(a)]
Print the variance for each estimate (you can also print the mean or standard deviation if you prefer):
print("Estimates variance", estimates.var(axis=0))
The following is printed on the screen:
Estimates variance [ 0.05960347 3.63062943 0.01868965]
What just happened?
We estimated the variances of the...