NCL Home > Documentation > Functions > General applied math, Statistics

dim_variance

Computes the unbiased estimates of the variance of a variable's rightmost dimension.

Prototype

	function dim_variance (
		x  : numeric   
	)

	return_val  :  float or double

Arguments

x

A variable of numeric type and any dimensionality.

Return value

The output will be double if x is double, and float otherwise.

The output dimensionality is the same as the first n-2 dimensions of the input variable. That is, the dimension rank of the input variable will be reduced by one.

Description

The dim_variance function computes the unbiased estimate of the variance of all elements of the n-1 dimension for each index of the dimensions 0...n-2. Missing values are ignored.

Technically, this function calculates an estimate of the sample variance. This means that it divides by [1/(N-1)] where N is the total number of non-missing values.

Use dim_variance_n if you want to specify which dimension(s) to calculate the variance.

Use dim_variance_Wrap if retention of metadata is desired.

See Also

dim_variance_Wrap , dim_variance_n_Wrap , variance, dim_avg, dim_median, dim_num, dim_product, dim_rmsd, dim_rmvmean, dim_rmvmed, dim_standardize, dim_stat4, dim_sum, dim_variance_n

Examples

Example 1

The following calculates the population and sample variance of 5 values. For illustration, a step-by-step calculation of the variance is done. The variances returned by NCL's three built-in variance functions are included. All return the sample variance.

  f      = (/ 7, 9, -2, -8, 2/)

  favg   = avg(f)             ; average
  fdev   = f-favg             ; deviations
  fdev2  = fdev^2             ; deviations squared
  nf     = dimsizes(f)        ; N

  pvar   = sum(fdev2)/nf      ; population variance 

  svar   = sum(fdev2)/(nf-1)  ; sample variance 
  var1   = variance(f)
  var2   = dim_variance(f)
  var3   = dim_variance_n(f,0)

  print("pvar="+pvar+"  svar="+svar+"  var1="+var1+"  var2="+var2+"  var3="+var3)

===
output:   pvar=37.84  svar=47.3  var1=47.3  var2=47.3  var3=47.3

Example 2

Create a variable q of size (3,5,10) array. Then calculate the sample variance of the rightmost dimension.

    q   = random_uniform(-20,100,(/3,5,10/))
    qVar= dim_variance(q)   ;==>  qVar(3,5)
Example 3

Let x be of size (ntim,nlat,mlon) and with named dimensions "time", "lat" and "lon", respectively. Then, for each time and latitude, the the variance is:

    xVarLon= dim_variance( x )    ; ==> xVarLon(ntim,nlat)
Generally, users prefer that the returned variable have metadata associated with it. This can be accomplished via the dim_variance_Wrap function:

    xVarLon = dim_variance_Wrap( x )    ; ==> xVarLon(time,lat)
Example 4

Let x be defined as in Example 3: x(time,lat,lon). Compute the temporal variance at each latitude/longitude grid point. Use NCL's Named Subscripting to reorder the input array such that "time" is the rightmost dimension.

Note: in V5.1.1, you will be able to use dim_variance_n to avoid having to reorder your data.

    xVarTime = dim_variance( x(lat|:, lon|:, time|:) )    ; ==> xVarTime(nlat,nlon)
    xVarTime = dim_variance_n(x, 0)                       ; no reordering needed
If metadata is desired use:

    xVarTime = dim_variance_Wrap( x(lat|:, lon|:, time|:) )    ; ==> xVarTime(lat,lon)
    xVarTime = dim_variance_n_Wrap(x,0)                        ; no reordering needed