;---------------------------------------------------------------------------- ; \$Id: complexity_1.pro,v 1.6 2002/01/30 22:35:28 johnny Exp \$ ;+ ; NAME: ; COMPLEXITY_1 ; ; PURPOSE: ; Calculate the first order complexity, as defined by Elsner and Tsonis ; (1993), for finite word length length_n. Full reference is below in ; the "Notes" section. ; ; CATEGORY: ; Statistics. ; ; CALLING SEQUENCE: ; Result = COMPLEXITY_1(in_length_n, in_sequence) ; ; INPUTS: ; in_length_n: Word length for which to calculate the first-order ; complexity for finite word length (scalar long). ; ; in_sequence: Input sequence vector. Each element is of value '0' ; or '1' (string vector). ; ; KEYWORD PARAMETERS: ; LOGBASE: The default base of the logarithmic operator that is ; used in the equation to calculate first-order complexity ; is e. To choose a different base, set the Logbase key- ; word to that new base (any integer or floating type). ; ; OUTPUTS: ; Result: Calculated first-order complexity (scalar float). ; ; FILE DEVICE I/O: ; None. ; ; COMMON BLOCKS: ; None. ; ; EXAMPLE: ; This is an example almost identical to the periodic test comparison ; case shown in Elsner and Tsonis's (1993) Fig. 2. ; Create periodic data: ; data = REPLICATE('0',14) ; data = [data,'1'] ; for i=0,14 do data = [data,data] ; data = data[0L:344786L] ; coeff = COMPLEXITY_1(5L, data, Logbase=2.) ; PRINT, coeff ; IDL prints: ; 0.516993 ; ; MODIFICATION HISTORY: ; - 25 Jan 2002: Orig. ver. Johnny Lin, CIRES/University of Colorado. ; Email: air_jlin@yahoo.com. Passed reasonably adequate tests. ; ; NOTES: ; - Written for IDL 5.5. May work with versions 5.2.1-5.4 (haven't ; tested yet). ; - Reference: Elsner, J. B. and A. A. Tsonis (1993), "Complexity and ; predictability of hourly precipitation," J. Atmos. Sci., Vol. 50, ; No. 3, pp. 400-405. ; - All keyword parameters are optional unless otherwise stated. ; - No procedures called with _Extra keyword invoked. ; - User-written procedures called: FIND_WORDS ;- ; Copyright (c) 2002 Johnny Lin. For licensing, distribution conditions, ; and contact information, see http://www.johnny-lin.com/lib.html. ;---------------------------------------------------------------------------- FUNCTION COMPLEXITY_1, in_length_n, in_sequence \$ , LOGBASE = in_logbase \$ , _EXTRA = extra ; -------------------- Error Check and Parameter Setting -------------------- ON_ERROR, 0 FORWARD_FUNCTION FIND_WORDS length_n = LONG(in_length_n) ;- protect small input param.(s) if (N_ELEMENTS(in_logbase) ne 0) then begin logbase = in_logbase if (N_ELEMENTS(logbase) ne 1) then \$ MESSAGE, 'error--bad logbase' if (logbase lt 1.0) then \$ MESSAGE, 'error--logbase lt 1' endif NS = N_ELEMENTS(in_sequence) ;- no. of elem. in in_sequence if (SIZE(in_sequence, /Type) ne 7) then \$ ;- error check other inputs MESSAGE, 'error--bad input sequence type' if (SIZE(in_sequence, /N_Dimensions) ne 1) then \$ MESSAGE, 'error--input sequence not a vector' if ((length_n le 0) or (length_n gt NS)) then \$ MESSAGE, 'error--length_n wrong' tmp = WHERE((in_sequence ne '0') and (in_sequence ne '1'), count) if (count ne 0) then \$ MESSAGE, 'error--bad values in input sequence' ; ------------------------------- Word Count -------------------------------- ; ; Notes: Count unique words, and put them in array words_found. words_found = FIND_WORDS(length_n, in_sequence) ; -------------------------- Calculate Complexity --------------------------- ; ; Notes: complexity_value is the eqn. for first-order complexity, for ; length_n -> infinity. The default (i.e. logbase undefined) uses base e. ; If logbase is defined, complexity_value is calculated using that as the ; logabase as the base. NWF = (SIZE(words_found, /Dimensions))[1] ;- num. of words found (i.e. ; admissible words) if (N_ELEMENTS(logbase) eq 0) then \$ ;- first-order complexity, if complexity_value = ALOG(NWF) / length_n \$ ; length_n -> infinity else \$ complexity_value = ALOG(NWF) / (ALOG(logbase)*length_n) ; ----------------------------- Prepare Output ------------------------------ Result = TEMPORARY(complexity_value) RETURN, Result END ;=== end of function === ; ========== end file ==========