just got a trial version of k 2.8 (2000-10-10) from
http://www.cs.nyu.edu/shasha/papers/cousins.html.
In its readme file(http://kx.com/a/k/readme.txt) there is a loop free
example of BDT interest model as follows:
bdt:{[p;v] bn:{.5*+':0,x,0} / binomial step function
l:(-1+%%':p)*_exp v*(2*!:'x+1)-x:1_!#p / generate approximate lattice
l:{x:bn x;x%1+y*(+/x%1+y*)?z}\[,*p;l;1_ p] / fit wtd-discount exactly
(*l){x%bn y}':l} / wtd-disc to forward
Define a test func as test:{-1+%bdt[*\x#%(1+0.05%x);(.2%_sqrt
x)][x-1;0]},then run with \t,
I got the following result on my linux machine(P4 2.8G/512M):
\t test 100
4
\t test 1000
376
\t test 2000
1644
test 100
6.774911e-05
test 1000
8.846258e-08
test 2000
0n
I write a simple J verb using the brent method(from math package) to find
function root as
BDT=:4 :0
bn=.2%~2+/\0,0,~[
d=.{.y
l=._1+%d
for_v. x do.
d=.bn d
p=.^ 2*v* i.#d
f=:(d&%)@:(1&+)@:(p&*)
r=.(+/@:f) brent 0,1,1e_10,(>:v_index){y
d=.f r
l=.l,r
end.
l
)
and a test verb as
test=.3 :'{: ((<:y)#0.2%%:y) BDT (*/\y#%>:0.05%y)'
then got following result on the same box:
* 6!:2 'test 100'
0.0865
6!:2 'test 1000'
1.007692
6!:2 'test 2000'
2.527017
test 100
6.774911e_5
test 1000
8.846258e_8
test 2000
3.215086e_9
*It seems like k has fewer internal precision,but my question is:
a)can we write a loop free algorithm in J?
b)can we write a fast algorithm to keep up with Arthur's example?
Thanks.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm