package com.imsl.test.example.stat; import com.imsl.stat.*; import com.imsl.math.PrintMatrix; import com.imsl.math.PrintMatrixFormat; /** *

* Selects a sample from a data set.

*

* Consider the Wolfer Sunspot Data (Anderson 1971, p. 660) consisting of the * number of sunspots observed each year from 1749 through 1924. In this * example, the getSamples() method is used to randomly select 5 * observations from the total 176 observations. The sampled indices are * returned via getIndices().

* * @see Code * @see Output */ public class RandomSamplesEx3 { public static void main(String[] args) { double[][] x = { {1749.0, 80.9}, {1750.0, 83.4}, {1751.0, 47.7}, {1752.0, 47.8}, {1753.0, 30.7}, {1754.0, 12.2}, {1755.0, 9.6}, {1756.0, 10.2}, {1757.0, 32.4}, {1758.0, 47.6}, {1759.0, 54.0}, {1760.0, 62.9}, {1761.0, 85.9}, {1762.0, 61.20}, {1763.0, 45.0}, {1764.0, 36.4}, {1765.0, 20.9}, {1766.0, 11.4}, {1767.0, 37.8}, {1768.0, 69.8}, {1769.0, 106.1}, {1770.0, 100.8}, {1771.0, 81.6}, {1772.0, 66.5}, {1773.0, 34.8}, {1774.0, 30.6}, {1775.0, 7.0}, {1776.0, 19.8}, {1777.0, 92.5}, {1778.0, 154.4}, {1779.0, 125.9}, {1780.0, 84.8}, {1781.0, 68.1}, {1782.0, 38.5}, {1783.0, 22.8}, {1784.0, 10.2}, {1785.0, 24.1}, {1786.0, 82.9}, {1787.0, 132.0}, {1788.0, 130.9}, {1789.0, 118.1}, {1790.0, 89.9}, {1791.0, 66.6}, {1792.0, 60.0}, {1793.0, 46.9}, {1794.0, 41.0}, {1795.0, 21.3}, {1796.0, 16.0}, {1797.0, 6.4}, {1798.0, 4.1}, {1799.0, 6.8}, {1800.0, 14.5}, {1801.0, 34.0}, {1802.0, 45.0}, {1803.0, 43.1}, {1804.0, 47.5}, {1805.0, 42.2}, {1806.0, 28.1}, {1807.0, 10.1}, {1808.0, 8.1}, {1809.0, 2.5}, {1810.0, 0.0}, {1811.0, 1.4}, {1812.0, 5.0}, {1813.0, 12.2}, {1814.0, 13.9}, {1815.0, 35.4}, {1816.0, 45.8}, {1817.0, 41.1}, {1818.0, 30.4}, {1819.0, 23.9}, {1820.0, 15.7}, {1821.0, 6.6}, {1822.0, 4.0}, {1823.0, 1.8}, {1824.0, 8.5}, {1825.0, 16.6}, {1826.0, 36.3}, {1827.0, 49.7}, {1828.0, 62.5}, {1829.0, 67.0}, {1830.0, 71.0}, {1831.0, 47.8}, {1832.0, 27.5}, {1833.0, 8.5}, {1834.0, 13.2}, {1835.0, 56.9}, {1836.0, 121.5}, {1837.0, 138.3}, {1838.0, 103.2}, {1839.0, 85.8}, {1840.0, 63.2}, {1841.0, 36.8}, {1842.0, 24.2}, {1843.0, 10.7}, {1844.0, 15.0}, {1845.0, 40.1}, {1846.0, 61.5}, {1847.0, 98.5}, {1848.0, 124.3}, {1849.0, 95.9}, {1850.0, 66.5}, {1851.0, 64.5}, {1852.0, 54.2}, {1853.0, 39.0}, {1854.0, 20.6}, {1855.0, 6.7}, {1856.0, 4.3}, {1857.0, 22.8}, {1858.0, 54.8}, {1859.0, 93.8}, {1860.0, 95.7}, {1861.0, 77.2}, {1862.0, 59.1}, {1863.0, 44.0}, {1864.0, 47.0}, {1865.0, 30.5}, {1866.0, 16.3}, {1867.0, 7.3}, {1868.0, 37.3}, {1869.0, 73.9}, {1870.0, 139.1}, {1871.0, 111.2}, {1872.0, 101.7}, {1873.0, 66.3}, {1874.0, 44.7}, {1875.0, 17.1}, {1876.0, 11.3}, {1877.0, 12.3}, {1878.0, 3.4}, {1879.0, 6.0}, {1880.0, 32.3}, {1881.0, 54.3}, {1882.0, 59.7}, {1883.0, 63.7}, {1884.0, 63.5}, {1885.0, 52.2}, {1886.0, 25.4}, {1887.0, 13.1}, {1888.0, 6.8}, {1889.0, 6.3}, {1890.0, 7.1}, {1891.0, 35.6}, {1892.0, 73.0}, {1893.0, 84.9}, {1894.0, 78.0}, {1895.0, 64.0}, {1896.0, 41.8}, {1897.0, 26.2}, {1898.0, 26.7}, {1899.0, 12.1}, {1900.0, 9.5}, {1901.0, 2.7}, {1902.0, 5.0}, {1903.0, 24.4}, {1904.0, 42.0}, {1905.0, 63.5}, {1906.0, 53.8}, {1907.0, 62.0}, {1908.0, 48.5}, {1909.0, 43.9}, {1910.0, 18.6}, {1911.0, 5.7}, {1912.0, 3.6}, {1913.0, 1.4}, {1914.0, 9.60}, {1915.0, 47.4}, {1916.0, 57.10}, {1917.0, 103.9}, {1918.0, 80.6}, {1919.0, 63.6}, {1920.0, 37.6}, {1921.0, 26.1}, {1922.0, 14.2}, {1923.0, 5.8}, {1924.0, 16.7} }; int nSamp = 5; RandomSamples rs = new RandomSamples(); Random r = new Random(123457L); r.setMultiplier(16807); rs.setRandomObject(r); double[][] samples = rs.getSamples(x, nSamp); int[] idx = rs.getIndices(); PrintMatrix pm = new PrintMatrix("Random Samples"); PrintMatrixFormat mf = new PrintMatrixFormat(); mf.setNumberFormat(new java.text.DecimalFormat("0")); mf.setNoColumnLabels(); pm.print(mf, samples); pm = new PrintMatrix("Sampled Indices"); pm.print(mf, idx); } }