Ticket #6058: trac_6058_basic_stats1.patch

File trac_6058_basic_stats1.patch, 1.6 KB (added by mhampton, 12 years ago)

A baby step towards top-level statistics

  • sage/stats/all.py

    # HG changeset patch
    # User Marshall Hampton <hamptonio@gmail.com>
    # Date 1242596893 18000
    # Node ID 433e36b523625c380addbfc3ad5ab19c7471e841
    # Parent  7f9164053b2a6e9302193d8a3c710b826c6041cf
    Baby step towards top-level statistics functionality.
    
    diff -r 7f9164053b2a -r 433e36b52362 sage/stats/all.py
    a b  
    11from r import (ttest)
    22
    33import hmm.all as hmm
     4from basic_stats import mean, variance, std
     5
  • new file sage/stats/basic_stats.py

    diff -r 7f9164053b2a -r 433e36b52362 sage/stats/basic_stats.py
    - +  
     1"""
     2Basic statistics.
     3
     4This module aims to provide some of the most basic and commonly-needed
     5statistics functionality at the top import level.
     6
     7AUTHOR:
     8    -- Marshall Hampton, first version, 2009
     9"""
     10
     11from sage.calculus.calculus import sqrt
     12
     13def mean(a_list):
     14    """
     15    Returns the mean of a list of objects if possible.
     16
     17    EXAMPLES:
     18        sage: mean([1,2,3])
     19        2
     20    """
     21    sum = 0
     22    for q in a_list:
     23        sum = sum + q
     24    return sum/len(a_list)
     25
     26def variance(a_list):
     27    """
     28    Returns the population variance of a list.
     29
     30    EXAMPLES:
     31        sage: variance([0,2,2,4])
     32        2
     33    """
     34    m = mean(a_list)
     35    out = 0
     36    for q in a_list:
     37        out = out + (q-m)**2
     38    return out/len(a_list)
     39
     40def std(a_list):
     41    """
     42    Returns the population standard deviation of a list.
     43
     44    EXAMPLES:
     45        sage: std([1,2,3])
     46        sqrt(2)/sqrt(3)
     47    """
     48    return sqrt(variance(a_list))