Ticket #7197: trac_7197_part9.patch

File trac_7197_part9.patch, 3.2 KB (added by amhou, 12 years ago)
  • sage/stats/basic_stats.py

    # HG changeset patch
    # User Andrew Hou <hou.andrew@gmail.com>
    # Date 1258757263 28800
    # Node ID f62aabcf6f73320aa14b926278afdc3b2fcff920
    # Parent  b3ad60106bf5fa4cb2e98caee947b23ecc0db4d8
    Mode function now returns lowest value if values occur equally. However, if all values only occur once, the mode is still null.
    
    diff -r b3ad60106bf5 -r f62aabcf6f73 sage/stats/basic_stats.py
    a b  
    1111the geometric and harmonic mean. The ``median`` function returns the number
    1212separating the higher half of a sample from the lower half. The ``mode``
    1313returns the most common occuring member of a sample, plus the number of times
    14 it occurs. If entries occur equally common, a list of the most common  entries
    15 are returned. The ``moving_average`` is a finite impulse response filter,
    16 creating a series of averages using a user-defined number of subsets of the
    17 full data set. The ``standard deviation`` and the ``variance`` return a
     14it occurs. If entries occur equally common, the smallest of a list of the most
     15common  entries is returned. The ``moving_average`` is a finite impulse
     16response filter, creating a series of averages using a user-defined number of
     17subsets of the full data set. The ``std`` and the ``variance`` return a
    1818measurement of how far data points tend to be from the arithmetic mean.
    1919
    2020Functions are available in the namespace ``stats``, i.e. you can use them by
     
    8888   
    8989    If `v` is empty, we define the mode to be null.
    9090    If all elements occur only once, we define the mode to be null.
    91     If multiple elements occur at the same frequency, all will be
    92     displayed.
     91    If multiple elements occur at the same frequency, the lowest element
     92    will be displayed.
    9393
    9494   
    9595    INPUT:
     
    108108        []
    109109        sage: mode([1,2,3,4,5])
    110110        []
     111        sage: mode([1,2,1,2,3,3])
     112        [(1, 2)]
    111113        sage: mode(['sage', 4, I, 3/5, 'sage', pi])
    112114        [('sage', 2)]
    113115        sage: class MyClass:
     
    130132    if not s or s[0][1]==1:
    131133        # no mode if all occur equally often
    132134        return []
    133     else:
    134         return [i for i in s if i[1]==s[0][1]]
     135    else:
     136        # returns lowest value of equally common values
     137        return [min([i for i in s if i[1]==s[0][1]])]
    135138
    136139
    137140
     
    179182
    180183
    181184    """
     185    if hasattr(v, 'standard_deviation'): return v.standard_deviation(bias=bias)
     186
    182187    import numpy
    183188
    184189    x = 0
     
    188193            return v.std()
    189194        elif bias == False:
    190195            return v.std(ddof=1)
    191     if hasattr(v, 'std'): return v.std(bias=bias)
    192     if hasattr(v, 'standard_deviation'): return v.standard_deviation(bias=bias)
     196
    193197    if len(v) == 0:
    194198        # standard deviation of empty set defined as NaN
    195199        return NaN
     
    316320        'python'
    317321        sage: median([])
    318322        []
     323        sage: class MyClass:
     324        ...      def median(self):
     325        ...         return 1
     326        sage: stats.median(MyClass())
     327        1
    319328    """
    320    
     329    if hasattr(v, 'median'): return v.median()
     330
    321331    if len(v) == 0:
    322332        #median of empty set defined as null
    323333        return []