# 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: |
1675 | 1675 | right -- right bound on random distribution |
1676 | 1676 | |
1677 | 1677 | 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 |
1678 | 1690 | """ |
1679 | 1691 | if left >= right: |
1680 | 1692 | raise ValueError, "left must be less than right" |
… |
… |
cdef class TimeSeries: |
1688 | 1700 | def _randomize_normal(self, double m, double s): |
1689 | 1701 | """ |
1690 | 1702 | 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. |
1692 | 1705 | |
1693 | 1706 | INPUT: |
1694 | 1707 | m -- mean |
1695 | 1708 | s -- standard deviation |
1696 | 1709 | |
1697 | 1710 | 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 |
1698 | 1730 | """ |
1699 | 1731 | # Ported from http://users.tkk.fi/~nbeijar/soft/terrain/source_o2/boxmuller.c |
1700 | 1732 | # This the box muller algorithm. |
… |
… |
cdef class TimeSeries: |
1725 | 1757 | center -- the center of the semicircle distribution |
1726 | 1758 | |
1727 | 1759 | 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 |
1728 | 1778 | """ |
1729 | 1779 | cdef Py_ssize_t k |
1730 | 1780 | cdef double x, y, s, d = 2, left = center - 1, z |
… |
… |
cdef class TimeSeries: |
1751 | 1801 | s -- standard deviation |
1752 | 1802 | |
1753 | 1803 | 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] |
1754 | 1829 | """ |
1755 | 1830 | # Ported from http://users.tkk.fi/~nbeijar/soft/terrain/source_o2/boxmuller.c |
1756 | 1831 | # This the box muller algorithm. |