### Present Remotely

Send the link below via email or IM

CopyPresent to your audience

Start remote presentation- Invited audience members
**will follow you**as you navigate and present - People invited to a presentation
**do not need a Prezi account** - This link expires
**10 minutes**after you close the presentation - A maximum of
**30 users**can follow your presentation - Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

### Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.

You can change this under Settings & Account at any time.

# Folding Financial Data with Scala

One of the cornerstones of functional programming involves folding lists of data. Let's dig in a crack some finance data

by

Tweet## Greg Turnquist

on 13 November 2012#### Transcript of Folding Financial Data with Scala

by Greg Turnquist (@gregturn) Folding Financial Data with Scala What rate of return can I expect from the S&P 500 over the long run? We can use folding.

Folding is a way of iterating over a set of data and gathering the results. We need a function to calculate an average

def aMean(data: Seq[Double]): Double = {

val sum = data.foldLeft(0.0)(

(subtotal, relChange) => subtotal + relChange

)

sum / data.size

} I need to invest for my retirement and have heard of index funds. Is there a way to evaluate their performance? aMean(List(1.0, 2.0, 3.0)) =

List(1.0, 2.0, 3.0).foldLeft(0.0) {

(subtotal, val) => subtotal+val

} / 3 Use foldLeft to find the total:

foldLeft(0.0)(1.0, 2.0, 3.0) ->

0.0 + 1.0 -> foldLeft(1.0)(2.0, 3.0) ->

1.0 + 2.0-> foldLeft(3.0)(3.0) ->

3.0 + 3.0 -> foldLeft(6.0)() ->

6.0 6.0 / 3 -> 2.0 Take the total, and divide by the number of items to get the arithmetic mean. What would the aMean be if the S&P 500 had -50%, 20%, 20%, and 20% returns over 4 years? aMean(List(-50.0, 20.0, 20.0, 20.0)) ->

(-50.0 + 20.0 + 20.0 + 20.0) / 4 ->

10.0 / 4 ->

2.5 That's a +2.5% average annual growth! But, what if you had invested $1000. How much would it be worth? $1000 -50% -> $500

$500 + 20% -> $600

$600 + 20% -> $720

$720 + 20% -> $864 That's a $136 loss! We need a different sort of average that multiplies instead of adds.

def gMean(data: Seq[Double]): Double = {

val totalGrowth = data.foldLeft(1.0)(

(subtotal, relChange) => subtotal * (1+relChange/100.0)

)

val annualizedGrowth = math.pow(totalGrowth, 1.0/data.size)

(annualizedGrowth - 1.0) * 100.0)

} Let's plug those figures into our geometric mean function!

gMean(List(-50.0, 20.0, 20.0, 20.0)) ->

val totalGrowth = List(-50.0, 20.0, 20.0, 20.0).foldLeft(1.0) {

(subtotal, relChange) -> subtotal * (1+relChange/100.0)

}

(math.pow(totalGrowth, 1.0/4) - 1.0) * 100.0 foldLeft(1.0)(-50.0, 20.0, 20.0, 20.0) ->

1.0 * 0.5 -> foldLeft(0.5)(20.0, 20.0, 20.0) ->

0.5 * 1.2 -> foldLeft(0.6)(20.0, 20.0) ->

0.6 * 1.2 -> foldLeft(0.72)(20.0) ->

0.72 * 1.2 -> foldLeft(0.864)() ->

0.864

Converted to percents, that is a -13.6% total loss.

If we had invested $1000, we would have lost $136. When you look at the S&P 500 from 1951-2010,

aMean = 8.23%

gMean = 6.87% If you average every 25-year window, the average gMean is 7.15%. Checkout:

github.com/gregturn/finance

learn how to calculate sliding windows

find out what happens when you avoid losses

GregLTurnquist.com/search/label/scala

Read "Scala makes it easy" for a walk-thru of the code

forecast-chart.com/historical-sp-500.html

S&P 500 historical data By the way,

I am not a licensed financial adviser nor an insurance agent, and cannot give out financial advice. This is deranged wealth building opinion written by someone who enjoys math. This would mean you are barely keeping up with inflation or pulling slightly ahead.

You need double digit growth to actually build a decent retirement. Given that mutual funds have historically underperformed the market over long term, betting your retirement on the S&P 500 is a real gamble. Some people say we should expect 12% growth. Others say that is too optimistic, and that we should instead expect closer to 7%. So who's right? Let's "fold" the historic performance of the S&P 500 and figure out it's performance, so we can decide for ourselves! The 4th root of 0.864, converted to percents -> -3.6%

That's a -3.6% annualized loss! It spanned from 3.94% to 13.05% There is a 2/3 chance of getting between 4.77% and 9.53% Just kidding! Folding Financial Data with Scala

by Greg Turnquist Things we'll be covering:

Endofunctors

Asymmetric lenses

Monads, monoids, oh my!

Applicatives

And..........

Full transcriptFolding is a way of iterating over a set of data and gathering the results. We need a function to calculate an average

def aMean(data: Seq[Double]): Double = {

val sum = data.foldLeft(0.0)(

(subtotal, relChange) => subtotal + relChange

)

sum / data.size

} I need to invest for my retirement and have heard of index funds. Is there a way to evaluate their performance? aMean(List(1.0, 2.0, 3.0)) =

List(1.0, 2.0, 3.0).foldLeft(0.0) {

(subtotal, val) => subtotal+val

} / 3 Use foldLeft to find the total:

foldLeft(0.0)(1.0, 2.0, 3.0) ->

0.0 + 1.0 -> foldLeft(1.0)(2.0, 3.0) ->

1.0 + 2.0-> foldLeft(3.0)(3.0) ->

3.0 + 3.0 -> foldLeft(6.0)() ->

6.0 6.0 / 3 -> 2.0 Take the total, and divide by the number of items to get the arithmetic mean. What would the aMean be if the S&P 500 had -50%, 20%, 20%, and 20% returns over 4 years? aMean(List(-50.0, 20.0, 20.0, 20.0)) ->

(-50.0 + 20.0 + 20.0 + 20.0) / 4 ->

10.0 / 4 ->

2.5 That's a +2.5% average annual growth! But, what if you had invested $1000. How much would it be worth? $1000 -50% -> $500

$500 + 20% -> $600

$600 + 20% -> $720

$720 + 20% -> $864 That's a $136 loss! We need a different sort of average that multiplies instead of adds.

def gMean(data: Seq[Double]): Double = {

val totalGrowth = data.foldLeft(1.0)(

(subtotal, relChange) => subtotal * (1+relChange/100.0)

)

val annualizedGrowth = math.pow(totalGrowth, 1.0/data.size)

(annualizedGrowth - 1.0) * 100.0)

} Let's plug those figures into our geometric mean function!

gMean(List(-50.0, 20.0, 20.0, 20.0)) ->

val totalGrowth = List(-50.0, 20.0, 20.0, 20.0).foldLeft(1.0) {

(subtotal, relChange) -> subtotal * (1+relChange/100.0)

}

(math.pow(totalGrowth, 1.0/4) - 1.0) * 100.0 foldLeft(1.0)(-50.0, 20.0, 20.0, 20.0) ->

1.0 * 0.5 -> foldLeft(0.5)(20.0, 20.0, 20.0) ->

0.5 * 1.2 -> foldLeft(0.6)(20.0, 20.0) ->

0.6 * 1.2 -> foldLeft(0.72)(20.0) ->

0.72 * 1.2 -> foldLeft(0.864)() ->

0.864

Converted to percents, that is a -13.6% total loss.

If we had invested $1000, we would have lost $136. When you look at the S&P 500 from 1951-2010,

aMean = 8.23%

gMean = 6.87% If you average every 25-year window, the average gMean is 7.15%. Checkout:

github.com/gregturn/finance

learn how to calculate sliding windows

find out what happens when you avoid losses

GregLTurnquist.com/search/label/scala

Read "Scala makes it easy" for a walk-thru of the code

forecast-chart.com/historical-sp-500.html

S&P 500 historical data By the way,

I am not a licensed financial adviser nor an insurance agent, and cannot give out financial advice. This is deranged wealth building opinion written by someone who enjoys math. This would mean you are barely keeping up with inflation or pulling slightly ahead.

You need double digit growth to actually build a decent retirement. Given that mutual funds have historically underperformed the market over long term, betting your retirement on the S&P 500 is a real gamble. Some people say we should expect 12% growth. Others say that is too optimistic, and that we should instead expect closer to 7%. So who's right? Let's "fold" the historic performance of the S&P 500 and figure out it's performance, so we can decide for ourselves! The 4th root of 0.864, converted to percents -> -3.6%

That's a -3.6% annualized loss! It spanned from 3.94% to 13.05% There is a 2/3 chance of getting between 4.77% and 9.53% Just kidding! Folding Financial Data with Scala

by Greg Turnquist Things we'll be covering:

Endofunctors

Asymmetric lenses

Monads, monoids, oh my!

Applicatives

And..........