Skip to content

Instantly share code, notes, and snippets.

@derwiki
Last active May 31, 2018 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save derwiki/5603db8ffaa4490935c6224e02cf5e3d to your computer and use it in GitHub Desktop.
Save derwiki/5603db8ffaa4490935c6224e02cf5e3d to your computer and use it in GitHub Desktop.
Using historical housing price data for an area, determine the minimum appreciation for every year interval
prices = [
247901,
313230,
362438,
361772,
345520,
338346,
329087,
328406,
342284,
365954,
393909,
479031,
560485,
728901,
751361,
751273,
810006,
947624,
1079112,
1103005,
1204167,
1116976,
961689,
996505,
948797,
1103974,
1291523,
1444416,
1631313,
1709147
]
def median(ary)
mid = ary.length / 2
sorted = ary.sort
ary.length.odd? ? sorted[mid] : 0.5 * (sorted[mid] + sorted[mid - 1])
end
c = prices.count
puts "prices: #{c}"
nets = {}
c.times do |i|
c.times do |years|
next if years == 0
next if i+years > c
slice = prices[i.. i+years]
# puts slice.join(',')
net = slice.last - slice.first
nets[years] ||= []
nets[years] << net
end
end
puts %w(years avg median min max count years).join("\t")
nets.each do |years, arr|
sum = arr.inject{ |acc, el| acc + el }.to_i
avg = sum / arr.count
puts [years, avg, median(arr).to_i, arr.min, arr.max, arr.count, years].join(?\t)
end
# prices: 30
# years avg median min max count years
# 1 48708 31385 -155287 187549 30 1
# 2 98522 77834 -242478 342726 29 2
# 3 147358 139516 -207662 527339 28 3
# 4 192914 218201 -255370 682516 27 4
# 5 236718 269524 -154208 760350 26 5
# 6 276812 327440 -130315 760350 25 6
# 7 314167 302932 1173 747458 24 7
# 8 356874 388075 31471 747458 23 8
# 9 401251 398198 80679 725136 22 9
# 10 456121 480919 146008 810258 21 10
# 11 519440 511454 219896 838213 20 11
# 12 579620 567780 312584 861883 19 12
# 13 640633 602350 388835 899141 18 13
# 14 691184 715515 438043 957874 17 14
# 15 738048 737412 496776 957874 16 15
# 16 787251 741233 562105 1070828 15 16
# 17 839295 768669 620391 1152282 14 17
# 18 887031 831211 616169 1237404 13 18
# 19 917731 873020 599917 1315238 12 19
# 20 944437 956266 599251 1343193 11 20
# 21 957854 911126 587025 1366863 10 21
# 22 980836 946003 586359 1380741 9 22
# 23 1026015 1014323 635567 1380741 8 23
# 24 1077146 1082644 700896 1380060 7 24
# 25 1153385 1175759 856073 1370801 6 25
# 26 1230937 1268875 1043622 1363627 5 26
# 27 1302170 1332396 1196515 1347375 4 27
# 28 1375346 1383412 1346709 1395917 3 28
# 29 1428581 1428581 1395917 1461246 2 29
# with additional 18 year forecast
# years avg median min max count years
# 1 51550 53970 -302479 224059 48 1
# 2 103904 113077 -302479 431520 47 2
# 3 164040 132894 -287386 623614 46 3
# 4 221884 218723 -255370 801479 45 4
# 5 278012 314052 -154208 966169 44 5
# 6 332450 327440 -130315 966169 43 6
# 7 385466 394833 1173 913384 42 7
# 8 437069 461968 31471 857859 41 8
# 9 490904 534612 80679 852562 40 9
# 10 548246 552201 146008 1013467 39 10
# 11 614045 624286 219896 1162454 38 11
# 12 676403 693143 312584 1300405 37 12
# 13 735493 653630 388835 1428137 36 13
# 14 788891 733592 438043 1428137 35 14
# 15 840712 800190 496776 1387198 34 15
# 16 891806 897614 562105 1384319 33 16
# 17 944257 916944 620391 1315644 32 17
# 18 996974 949239 616169 1393478 31 18
# 19 1054888 1058015 599917 1580375 30 19
# 20 1112165 1111064 599251 1733268 29 20
# 21 1167140 1116476 587025 1920817 28 21
# 22 1222495 1202745 586359 2075994 27 22
# 23 1282200 1287695 635567 2028286 26 23
# 24 1337787 1330477 700896 2063102 25 24
# 25 1393528 1359239 856073 1907815 24 25
# 26 1449444 1413306 1043622 1820624 23 26
# 27 1500335 1495092 1196515 1921786 22 27
# 28 1556187 1518145 1251134 1952099 21 28
# 29 1612639 1599449 1234882 2077167 20 29
# 30 1665613 1664713 1234216 2214785 19 30
# 31 1717011 1756702 1234216 2273518 18 31
# 32 1777426 1829403 1283424 2273430 17 32
# 33 1839349 1842033 1348753 2295890 16 33
# 34 1905614 1881028 1476485 2464306 15 34
# 35 1972228 1939571 1614436 2545760 14 35
# 36 2036750 1983570 1713102 2630882 13 36
# 37 2085576 2052434 1696850 2658837 12 37
# 38 2121247 2098107 1696184 2682507 11 38
# 39 2138162 2155291 1696184 2696385 10 39
# 40 2181796 2247751 1745392 2695704 9 40
# 41 2239978 2312362 1810721 2686445 8 41
# 42 2312801 2383966 1975411 2679271 7 42
# 43 2382564 2380379 2153276 2663019 6 43
# 44 2446511 2376792 2345370 2662353 5 44
# 45 2496201 2456685 2359874 2711561 4 45
# 46 2515282 2409082 2359874 2776890 3 46
# 47 2441746 2441746 2409082 2474411 2 47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment