mattak's blog

人生を1ミリ進める

haskellで利回りを計算してあそぶ

ちょっとめんどい計算をhaskellで数行すらっとかけるマンになりたい

利回り計算が面倒だったのでhaskellで書いてみた

n年でx1円がx2円になった。 年利(rate)はいくら?

計算

x1 * rate ^ n = x2
rate ^ n = x2 / x1
n * log rate = log (x2/x1)
log rate = 1/n * log(x2/x1)
log rate = log (x2/x1)^(1/n)
rate = (x2/x1)^(1/n)
module Interest where

-- interest calculation by step
-- e.g. let year = 10, start = 100, end = 200, what is interest by year?
reverse_interest :: Float -> Float -> Float -> Float
reverse_interest start end year = (end / start) ** (1 / year)

試しに計算 72の法則. 年利6%で12年で2倍くらいになる

1.06 ^ 12 = 2.012196471835552 =~ 2.0
2 ^ (1/12) = 1.0594630943592953 =~ 1.06

あっていそう。

theoとwealthnaviの利回りを計算してみる.

  • theo: 10年で410 -> 571
  • wealthnavi: 30年で1180 -> 2443
$ ghci

*Interest> reverse_interest 410 571 10
1.0336779
*Interest> reverse_interest 1180 2443 30
1.0245537

theoが3.4% / wealthnaviが2.5% となる.