Ticket #3356: sage-3356-part7.patch

File sage-3356-part7.patch, 4.8 KB (added by cswiercz, 14 years ago)

Added doctests for TimeSeries?.randomize

  • sage/finance/time_series.pyx

    # HG changeset patch
    # User Chris Swierczewski <cswiercz@gmail.com>
    # Date 1213985203 25200
    # Node ID a12c0994d77cac3a88639c81e7b777ed34d4e8c0
    # Parent  d823dad321bb50a6b23b9a9f717725b244d065aa
    Added TimeSeries.randomize doctests.
    
    diff -r d823dad321bb -r a12c0994d77c sage/finance/time_series.pyx
    a b cdef class TimeSeries: 
    16751675            right -- right bound on random distribution
    16761676
    16771677        EXAMPLES:
     1678        We generate 5 values distributed with respect to the uniform
     1679        distribution over the interval [0,1].
     1680            sage: v = finance.TimeSeries(5)
     1681            sage: set_random_seed(0)
     1682            sage: v.randomize('uniform')
     1683            [0.8685, 0.2816, 0.0229, 0.1456, 0.7314]
     1684
     1685        We now test that the mean is indeed 0.5.
     1686            sage: v = finance.TimeSeries(10^6)
     1687            sage: set_random_seed(0)
     1688            sage: v.randomize('uniform').mean()
     1689            0.50069085504319877
    16781690        """
    16791691        if left >= right:
    16801692            raise ValueError, "left must be less than right"
    cdef class TimeSeries: 
    16881700    def _randomize_normal(self, double m, double s):
    16891701        """
    16901702        Generates a normal random distribution of doubles with mean m and
    1691         standard deviation s and stores values in place.
     1703        standard deviation s and stores values in place. Uses the
     1704        Box-Muller algorithm.
    16921705       
    16931706        INPUT:
    16941707            m -- mean
    16951708            s -- standard deviation
    16961709
    16971710        EXAMPLES:
     1711        We generate 5 values distributed with respect to the normal
     1712        distribution with mean 0 and standard deviation 1.
     1713            sage: set_random_seed(0)
     1714            sage: v = finance.TimeSeries(5)
     1715            sage: v.randomize('normal')
     1716            [0.6767, -0.4011, 0.3576, -0.5864, -0.9365]
     1717
     1718        We now test that the mean is indeed 0.
     1719            sage: set_random_seed(0)
     1720            sage: v = finance.TimeSeries(10^6)
     1721            sage: v.randomize('normal').mean()
     1722            6.2705472723385207e-05
     1723
     1724        The same test with mean equal to 2 and standard deviation equal
     1725        to 5.
     1726            sage: set_random_seed(0)
     1727            sage: v = finance.TimeSeries(10^6)
     1728            sage: v.randomize('normal', 2, 5).mean()
     1729            2.0003135273636117
    16981730        """
    16991731        # Ported from http://users.tkk.fi/~nbeijar/soft/terrain/source_o2/boxmuller.c
    17001732        # This the box muller algorithm.
    cdef class TimeSeries: 
    17251757            center -- the center of the semicircle distribution
    17261758
    17271759        EXAMPLES:
     1760        We generate 5 values distributed with respect to the semicircle
     1761        distribution located at center.
     1762            sage: v = finance.TimeSeries(5)
     1763            sage: set_random_seed(0)
     1764            sage: v.randomize('semicircle')
     1765            [0.7369, -0.9541, 0.4628, -0.7990, -0.4187]
     1766
     1767        We now test that the mean is indeed the center.
     1768            sage: v = finance.TimeSeries(10^6)
     1769            sage: set_random_seed(0)
     1770            sage: v.randomize('semicircle').mean()
     1771            0.00072074971804614557
     1772
     1773        The same test with center equal to 2.
     1774            sage: v = finance.TimeSeries(10^6)
     1775            sage: set_random_seed(0)
     1776            sage: v.randomize('semicircle', 2).mean()
     1777            2.0007207497179227
    17281778        """
    17291779        cdef Py_ssize_t k
    17301780        cdef double x, y, s, d = 2, left = center - 1, z
    cdef class TimeSeries: 
    17511801            s -- standard deviation
    17521802
    17531803        EXAMPLES:
     1804        We generate 5 values distributed with respect to the lognormal
     1805        distribution with mean 0 and standard deviation 1.
     1806            sage: set_random_seed(0)
     1807            sage: v = finance.TimeSeries(5)
     1808            sage: v.randomize('lognormal')
     1809            [1.9674, 0.6696, 1.4299, 0.5563, 0.3920]
     1810
     1811        We now test that the mean is indeed sqrt(e).
     1812            sage: set_random_seed(0)
     1813            sage: v = finance.TimeSeries(10^6)
     1814            sage: v.randomize('lognormal').mean()
     1815            1.6473519736548801
     1816            sage: e^0.5
     1817            1.648721270700128
     1818           
     1819        A log-normal distribution can be simply thought of as the logarithm
     1820        of a normally distributed dataset. We test that here by generating
     1821        5 values distributed with respect to the normal distribution with mean
     1822        0 and standard deviation 1.
     1823            sage: set_random_seed(0)
     1824            sage: w = finance.TimeSeries(5)
     1825            sage: w.randomize('normal')
     1826            [0.6767, -0.4011, 0.3576, -0.5864, -0.9365]
     1827            sage: exp(w)
     1828            [1.9674, 0.6696, 1.4299, 0.5563, 0.3920]
    17541829        """
    17551830        # Ported from http://users.tkk.fi/~nbeijar/soft/terrain/source_o2/boxmuller.c
    17561831        # This the box muller algorithm.