[ncl-talk] Function for frequency distribution

Gabriel Medeiros gabriel.abrahao at ufv.br
Wed Sep 2 18:23:31 MDT 2015


Hi,

I've stumbled upon the problem of getting the frequency distribution
of data and separating quantiles quite a few times. As i didn't see
any function doing this specifically, i'd like to share mine with you.

It's very simple and not as elaborated as the ones used in the
histogram plotting procedures, but work fine for generating CDF's and
getting quantiles. If there's a built in one for this that i couldn't
find, i apologize and please let me know.

Gabriel Abrahao
Grupo de Pesquisa em Interação Atmosfera-Biosfera
Universidade Federal de Viçosa

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;        Function: freq_wgt
;
;        Gabriel Abrahao: gabriel.abrahao at ufv.br
;
;        Get the weighted frequency distribution of the n-length data
vector, returning a 2 by n matrix containing both
;        the sorted data and the fraction of the total weight below each value
;
;        Removes missing values for the output, and relies on the fact
that default missing values are very large
;        (positive or negative) for handling them
;
;        function freq_wgt(
;                    indata [*]
;                    inwegt [*]    )
;
;        Return value: A matrix of two rows, the first with the sorted
values and the second with the fraction
;        of total nonmissing weights corresponding to them.
;

undef("freq_wgt")
function freq_wgt(indata[*],inwegt[*])
local indata,inwegt,nmiss,permv,tdata,twegt,cumwegt,out
begin

;Make copies of the input variables in order no to mess with them
data = indata
wegt = inwegt

;Makes sure that missing values are large negative numbers
data at _FillValue = -1*abs(default_fillvalue(typeof(data)))
wegt at _FillValue = -1*abs(default_fillvalue(typeof(wegt)))

;Synchronize missing values between data and weights
data = where(ismissing(wegt),data at _FillValue,data)
wegt = where(ismissing(data),wegt at _FillValue,wegt)

;Get number of missing values
nmiss = num(ismissing(data))

;Sort in respect to data
permv = dim_pqsort(data,2)
wegt = wegt(permv)

;Trim to non-missing values only
tdata = data(nmiss:dimsizes(data)-1)
twegt = wegt(nmiss:dimsizes(wegt)-1)

;Delete the original copied data to save memory
delete(data)
delete(wegt)

;Calculate cumulative weights and generate fraction vector
cumwegt = cumsum(twegt,2)
cumwegt = cumwegt/cumwegt(dimsizes(cumwegt)-1)

out = (/tdata,cumwegt/)
out at column0 = "Values"
out at column1 = "Fraction below value"

return(out)
end
-------------- next part --------------
A non-text attachment was scrubbed...
Name: freq_wgt.ncl
Type: text/x-ncl
Size: 1886 bytes
Desc: not available
Url : http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20150902/fa0d7ead/attachment.bin 


More information about the ncl-talk mailing list