Skip to content

Instantly share code, notes, and snippets.

@pims
Created June 16, 2010 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pims/440061 to your computer and use it in GitHub Desktop.
Save pims/440061 to your computer and use it in GitHub Desktop.
math:{
add: (a,b)-> a+b
mean: (data)->
data.reduce(math.add) / data.length
square: (x)-> x*x
sqrt: Math.sqrt
sum: (data)-> data.reduce(math.add)
stddev: (data)->
_mean: math.mean(data)
_dev_from_mean: (i-_mean) for i in data
_squared_dev_from_mean: math.square(i) for i in _dev_from_mean
_sum_squared_dev_from_mean: math.sum(_squared_dev_from_mean)
_variance: _sum_squared_dev_from_mean / (data.length - 1)
_stddev: math.sqrt(_variance)
}
euclidean: (v1,v2)->
_n: v1.length
sum_of_squares: math.sum(math.square(v1[i]-v2[i]) for i in [0..._n])
1/(1+sum_of_squares)
pearson: (v1,v2)->
_n: v1.length
_sum1: math.sum(v1)
_sum2: math.sum(v2)
_sum1_sq: math.sum(math.square(i) for i in v1)
_sum2_sq: math.sum(math.square(i) for i in v2)
_p_sum: math.sum(v1[i]*v2[i] for i in [0..._n])
_num: _p_sum - (_sum1*_sum2/_n)
_f1: _sum1_sq - (math.square(_sum1)/_n)
_f2: _sum2_sq - (math.square(_sum2)/_n)
_den = math.sqrt(_f1*_f2)
0 if _den is 0
r: _num/_den
stocks:{
aapl: [259.69,253.51,255.96,256.88,242.32,253.82,235.86,261.09,270.83,247.40,241.79,235.97,230.90,222.25,226.60,218.95,204.62,201.67,200.38,195.46,192.06,197.75,205.93,211.98,210.73,209.04,195.43,194.67,193.32,200.59,199.92,204.45,194.34,188.50,203.94,188.05,190.47,184.90,182.37,185.02,172.16,170.31,170.05,169.22,166.78,165.51,163.39,159.99,151.75,138.52,140.02,142.44,139.48,136.97,144.67,135.81,122.50,122.42,129.19,127.24,123.90,123.42,119.57,115.99,106.85,101.59,95.93,85.30,89.31,91.20,99.16,99.72,90.13,88.36,82.33,90.58,90.75,85.81,90.00,98.27,94.00,92.67,82.58,90.24,98.24,107.59,96.38,97.40,96.80,97.07,128.24,140.91,148.94,160.18,169.53,176.79,175.74,169.55,156.66,162.12,165.15,172.58,170.12,170.09,175.27,172.37,185.64,188.75,181.17,187.62,183.45,180.94,169.73,161.04,147.14,153.08,143.01,133.27,126.61,122.25,125.02,119.46,124.63,125.48,133.75,130.01,161.36,172.69,180.05,199.83,193.91,190.39,194.30,182.22,171.54,166.39,165.37,187.87,184.70,170.42,167.25,161.45,153.47,144.15,138.81,131.77,138.48,135.30,122.06,125.00,131.85,143.85,143.75,137.73,132.30,122.04,123.00,120.50,124.49,118.40,113.62,110.02,108.74,100.81,99.92,90.97,90.24,94.68,92.91,93.52,89.59,87.97,85.41,89.07,84.83,83.27,84.75,85.38,88.50,94.62,85.05,84.84,82.20,87.72,88.26,91.32,91.63,85.85,83.12,78.29,80.41,79.95,75.02,74.22,76.98,73.00,74.10,72.52,68.38,68.75,67.91,63.65,68.30,65.59,60.72,50.67,55.40,57.27,58.83,57.56,59.24,61.66,63.55,64.51,67.70,71.89,70.39,67.04,66.47,69.79,62.72,59.96,64.66,63.19,67.72,71.46,70.29,67.31,71.85,72.03,76.09,85.59,76.30,71.89,73.35,71.11,74.33,72.63,69.34,64.56,61.54,61.15,54.47,55.66,54.00,51.30,53.61,53.20,51.21,51.31,46.22,45.74,45.83,46.10,42.99,42.65,44.00,41.55,38.25,36.50,37.76,38.31]
goog: [497.99,488.50,498.72,485.63,472.05,507.53,493.14,525.70,544.99,550.15,566.22,568.80,562.69,560.00,579.54,564.21,526.80,540.76,533.12,531.29,529.94,550.01,580.00,602.02,619.98,618.48,596.42,590.51,585.01,579.76,569.96,572.05,551.10,536.12,553.69,549.85,516.25,484.58,492.48,491.46,472.14,461.30,464.75,465.24,460.00,457.10,443.05,446.72,430.25,414.40,408.49,425.32,420.09,424.84,444.32,417.23,393.50,390.00,407.33,393.69,389.49,392.24,372.50,369.78,347.70,330.16,324.42,308.57,337.99,346.45,357.68,371.28,338.53,324.70,299.67,315.07,321.32,300.36,310.17,315.76,283.99,292.96,262.43,310.02,331.14,359.36,339.29,372.54,332.00,386.91,431.04,449.15,437.66,444.25,463.29,490.59,510.15,495.01,467.86,491.98,481.32,533.80,537.00,528.07,546.43,571.51,567.00,585.80,544.62,580.07,573.20,581.29,544.06,539.41,457.45,471.09,438.08,433.55,437.92,433.35,471.18,507.80,529.64,516.69,515.90,566.40,600.25,638.25,657.00,702.53,696.69,689.96,714.87,693.00,676.70,633.63,663.97,711.25,674.60,644.71,637.39,594.05,567.27,560.10,528.75,519.35,515.25,515.00,500.04,515.75,503.00,511.89,520.12,552.16,539.40,522.70,524.98,505.89,515.49,500.40,483.52,470.32,466.74,471.12,479.01,482.48,466.29,471.51,458.16,461.83,440.85,452.96,438.68,470.62,469.94,461.89,481.50,495.84,489.75,505.00,487.19,460.48,455.58,480.30,484.11,480.80,505.00,498.79,473.55,471.80,475.20,459.67,427.30,420.50,401.90,403.78,409.88,377.85,378.60,373.26,383.36,368.50,373.85,388.12,390.11,403.50,420.45,419.33,404.86,390.70,386.57,379.44,381.35,370.02,374.13,394.30,417.94,437.10,402.16,406.16,390.00,365.80,339.79,337.50,378.18,377.40,368.75,362.61,381.55,433.49,399.46,466.25,465.66,414.86,430.93,430.15,409.20,417.70,428.62,400.21,390.40,390.43,358.17,339.90,296.14,312.99,316.46,315.36,300.20,299.09,288.45,283.58,280.00,289.72,292.35,287.76,302.40,301.19,296.23,291.25,297.25,280.30]
amzn: [126.84,123.03,122.77,125.46,122.72,128.53,124.98,137.10,143.63,142.17,140.06,131.81,135.06,130.35,131.82,128.91,118.40,117.52,119.66,117.39,125.41,121.43,127.14,133.52,134.52,138.47,128.48,134.15,137.58,131.74,129.66,132.97,126.20,118.81,118.49,95.32,95.71,89.85,90.52,90.28,84.54,78.87,82.76,85.00,83.58,85.32,85.76,86.49,85.85,77.63,79.32,83.88,82.96,84.08,87.56,77.99,75.64,73.60,77.95,78.96,84.46,78.05,79.77,78.17,70.52,69.96,68.63,61.69,64.79,63.86,63.26,66.55,58.82,50.63,51.59,55.51,54.36,51.78,51.56,51.25,48.26,42.70,37.87,41.75,49.21,57.24,48.96,50.65,56.25,67.00,70.70,81.00,78.30,79.19,80.81,85.26,86.40,80.51,75.75,78.31,69.12,68.54,72.00,74.66,81.10,79.17,80.63,81.62,78.35,76.46,72.41,77.31,80.86,80.10,71.99,76.87,69.76,73.19,68.22,64.09,64.47,72.08,72.96,73.50,74.63,77.60,79.76,81.08,88.79,94.45,91.26,89.08,94.31,90.56,81.43,78.60,78.89,85.98,90.00,89.76,92.37,93.43,93.15,91.30,87.77,84.52,79.91,79.25,75.02,74.78,76.80,84.04,71.63,75.10,68.97,68.41,68.86,72.40,73.24,68.58,68.55,63.30,61.56,63.23,62.60,44.95,42.41,41.68,39.79,38.98,37.85,38.84,37.69,40.78,40.33,38.72,37.39,36.85,37.02,38.20,38.37,39.46,40.24,40.01,38.46,39.41,42.41,42.55,39.26,37.46,38.24,32.57,33.32,32.59,32.12,30.84,32.52,30.51,31.76,28.03,29.12,26.07,27.29,27.17,33.19,32.92,36.11,38.68,36.55,35.09,33.78,34.76,36.07,33.94,32.73,35.04,35.21,36.03,36.62,37.09,36.53,35.47,35.99,36.22,37.21,38.35,39.19,38.52,38.33,45.22,43.92,44.40,47.87,47.15,49.22,49.21,48.74,49.06,48.06,47.98,42.68,41.09,38.95,45.95,43.79,43.72,45.30,42.19,42.80,42.95,41.51,42.37,43.72,44.20,45.27,45.15,37.95,37.15,34.74,32.91,35.02,35.31]
mfst: [26.58,25.66,25.79,25.80,26.84,28.80,28.08,30.40,30.82,30.53,30.20,29.03,29.53,29.46,29.14,28.46,28.54,28.64,27.68,27.76,27.92,28.70,30.58,30.38,30.20,30.72,30.08,29.58,29.71,28.95,29.35,29.23,28.14,27.36,27.64,26.14,25.21,24.62,25.21,24.92,24.52,24.29,24.35,24.08,23.24,23.11,23.07,23.00,23.83,21.96,22.93,22.91,23.61,22.89,21.72,20.49,19.37,19.71,18.93,19.73,20.38,18.72,19.17,18.28,17.67,16.63,16.23,14.90,15.74,17.55,18.48,19.03,16.56,16.65,19.08,18.90,19.68,18.52,18.51,18.74,19.24,19.58,19.05,19.29,20.68,21.47,21.12,23.01,20.68,25.31,26.35,24.20,26.56,24.67,26.24,26.77,26.64,26.94,24.37,25.06,24.77,24.19,24.88,26.46,27.04,27.84,26.33,27.13,26.87,28.73,28.05,27.90,28.47,28.63,26.99,27.83,26.64,27.85,26.68,26.60,25.96,26.42,27.02,27.15,28.95,31.31,31.38,32.24,32.68,34.34,34.28,33.57,32.83,31.94,32.43,32.41,31.96,35.11,33.19,28.59,28.59,28.27,27.91,27.15,27.52,26.95,27.22,27.30,26.77,27.11,27.34,27.75,29.42,28.16,28.30,27.83,27.84,28.79,28.37,28.88,28.78,29.11,29.07,28.76,28.35,27.31,26.93,26.87,26.23,26.37,25.72,25.68,26.13,27.20,27.05,27.18,28.31,28.70,29.18,29.27,27.80,28.01,27.80,28.31,27.57,27.31,27.91,27.57,27.33,26.85,26.49,26.57,26.52,26.05,25.56,24.92,25.10,23.93,24.15,24.16,24.11,22.75,22.62,22.58,22.23,20.76,21.70,21.70,20.95,20.58,20.41,21.20,22.09,21.01,21.49,22.08,22.40,25.19,25.11,25.28,25.24,25.06,25.51,25.20,24.98,24.70,24.77,24.68,25.46,25.69,24.42,25.14,24.88,24.18,24.63,24.87,25.62,25.90,25.66,25.95,25.15,24.58,23.53,22.84,22.74,22.67,23.72,23.29,24.03,24.50,24.91,24.86,24.63,24.86,25.51,23.54,23.60,23.70,23.06,22.71,23.01,23.01]
nasdaq: [2305.88,2243.60,2219.17,2257.04,2229.04,2346.85,2265.64,2461.19,2530.15,2481.26,2454.05,2402.58,2395.13,2374.41,2367.66,2326.35,2238.26,2243.87,2183.53,2141.12,2147.35,2205.29,2287.99,2317.17,2269.15,2285.69,2211.69,2190.31,2194.35,2138.44,2146.04,2167.88,2112.44,2045.11,2154.47,2156.80,2139.28,2048.11,2090.92,2132.86,2080.90,2018.78,2028.77,2020.90,1985.52,2000.25,1978.50,1965.96,1886.61,1756.03,1796.52,1838.22,1827.47,1858.80,1849.42,1774.33,1692.01,1680.14,1739.00,1719.20,1694.29,1673.07,1652.54,1621.87,1545.20,1457.27,1431.50,1293.85,1377.84,1441.23,1534.36,1591.71,1476.42,1477.29,1529.33,1571.59,1632.21,1530.24,1564.32,1540.72,1509.31,1535.57,1384.35,1516.85,1647.40,1720.95,1552.03,1711.29,1649.51,1947.39,2183.34,2273.90,2261.27,2255.88,2367.52,2414.71,2452.52,2414.10,2310.96,2310.53,2282.78,2239.08,2245.38,2315.63,2406.09,2454.50,2474.56,2522.66,2444.67,2528.85,2445.52,2476.99,2422.93,2402.97,2290.24,2370.98,2261.18,2258.11,2212.49,2212.49,2271.48,2303.35,2321.80,2304.85,2413.36,2326.20,2340.02,2439.94,2504.65,2674.46,2691.99,2635.74,2706.16,2660.96,2596.60,2637.24,2627.94,2810.38,2804.19,2725.16,2805.68,2780.32,2701.50,2671.22,2602.18,2565.70,2596.36,2576.69,2505.03,2544.89,2511.25,2562.24,2687.60,2707.00,2666.51,2603.23,2588.96,2626.71,2573.54,2613.92,2557.19,2558.45,2562.22,2572.15,2557.21,2526.39,2491.94,2471.34,2421.64,2448.93,2372.66,2387.55,2368.00,2515.10,2496.31,2459.82,2475.88,2435.49,2451.31,2502.82,2434.25,2415.29,2401.18,2457.20,2437.36,2413.21,2460.26,2445.86,2389.72,2330.79,2350.62,2342.30,2357.29,2299.99,2258.43,2218.93,2235.59,2165.79,2193.16,2140.29,2163.95,2057.71,2085.05,2094.14,2020.39,2037.35,2130.06,2172.09,2121.47,2129.95,2135.06,2219.41,2210.37,2193.88,2243.78,2342.57,2322.57,2342.86,2326.11,2339.02,2339.79,2312.82,2306.48,2262.04,2302.60,2287.04,2282.36,2261.88,2262.58,2304.23,2247.70,2317.04,2305.62,2205.32,2249.42,2252.48,2256.73,2273.37,2263.01,2227.07,2202.47,2169.43,2089.88,2082.21,2064.83,2090.35,2151.69,2116.84,2160.35,2175.51,2141.07,2120.77,2135.56,2156.90,2177.91,2184.83,2179.74,2156.78,2112.88,2057.37,2053.27,2090.11]
}
combinations:[
['aapl','goog']
['aapl','amzn']
['aapl','mfst']
['aapl','nasdaq']
['goog','amzn']
['goog','mfst']
['goog','nasdaq']
['amzn','mfst']
['amzn','nasdaq']
['mfst','nasdaq']
]
puts 'Computing Pearson coefficients for famous stocks'
for c in combinations
puts c[0] + " vs " + c[1] + ' = ' + pearson(stocks[c[0]],stocks[c[1]])
@pims
Copy link
Author

pims commented Jun 16, 2010

Cleaner than the php script I hacked 6 years ago for that finance paper ^^
AMZN and AAPL are best friends

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment