Suppose the coefficient
of the high order term (a) is always 1
in a setting we call "normalized".
Then we can define
normquad: quad[1]
quad[1;4;2] = normquad[4;2]
Lambda Expressions
Executing Strings
Exercises
-
Try to delete all blanks from a string.
-
Try to delete the initial blanks.
-
Write a string edit (sed) function that replaces
all instances of string old by string new in a line.
-
Try to delete the trailing blanks.
Delete All Blanks
deleteallblanks:{[s]
x: s = " " / gives binary 1 or zero saying whether s is blank or not.
i: & ~ x / gives all indices that are not blank
s[i]}
Delete Leading Blanks
deleteleadingblanks:{[s]
x: s = " " / 1s whenever blank, 0s elsewhere
(x ? 0) _ s} / drop up to first non-1.
Delete Trailing Blanks
Find Most Consecutive Ones
The Road to Ruin
-
You are an insurance company.
You have a revenue stream which you can predict
and m possible payment streams whose values depend on
the likelihoods
of accidents and nature.
Here are your initial conditions:
/ probability of ruin.
/ Given capital, a flow of revenue
/ and m possible payment scenarios that are equi-probable
/ what is the probability of ruin?
n: 200 / time periods
m: 100 / payment scenarios
capital: 1000
revenue: n _draw 2000
payments:(m;n) # ((m*n) _draw 2000)
-
Your job is to determine whether you will ever have
a net negative capital, i.e. the sum of your capital
plus your accumulated revenue is less than the sum
of your payments.
The solution follows.
Don't look until you think about it.
/ the ruin function takes two flows (in and out)
/ and determines if the cumulative sum for out
/ is at any point greater than the cumulative sum for in
/ plus the capital.
/ If so, ruin returns 1, else 0.
ruin: {[in; out]
in[0]+: capital / add in the capital as the initial value
incum: +\ in
outcum: +\ out
|/outcum > incum}
numruins: +/ruin[revenue]'payments
("probability of ruin is"; numruins % m)