The basic problem is that you build up the stack by starting with y and
decrementing it until
you hit zero, then doing your work by going back up the deep stack you've
built.

If you turn the loop around to start with zero and the initial value "initb"
below:
NB. Since +/S>k is constant:
kl=: +/S<:k
kh=: 16,~NAS-kl
initb=: (kl$b);kh$b

you should be able to make the loop explicit and avoid stack problems.

On 9/11/07, Devon McCormick <[EMAIL PROTECTED]> wrote:
>
> It looks as though Mr. Boss is looking in the same direction as I am -
> yeah, we're
> looking at you, recursion - and how to unroll it into an explicit loop.
>  Similarly to
> what he wrote, I came up with:
>
> Vrec2=: (>@:{.(V_ltk;V_gtk)>@:{:)@:V2@:<:
> V2=: 3 : 0
>    if. 0=y do. ((b$~+/S<:k);(b$~16,~+/S>k)"0) y
>    else. Vrec2 y end.
> )
>    (V-:V2) 2000
> 1
> NB. But this only buys a little stack space:
>    6!:2 'V2 2853'
> 0.53711614
>
>    6!:2 'V2 2854'
> |stack error: dV_dL
> | Vrec2 y
>
> I'm afraid the solution will be to further unroll your elegant recursion
> into a loop.
>
> On 9/11/07, R.E. Boss <[EMAIL PROTECTED]> wrote:
> >
> > IMO, your V is defined by:
> >
> > V=: 3 : 0
> > if.y=0 do. ((b$~+/S<:k);(b$~16,~+/S>k)"0) 0
> > else. (>@:{.(V_ltk;V_gtk)>@:{:)@:V@:<:
> > end.
> > )
> >
> > But this is equivalent to
> >
> > V1=: 3 : 0
> > (>@:{.(V_ltk;V_gtk)>@:{:)^:y ((b$~+/S<:k);(b$~16,~+/S>k)"0)0
> > )
> >
> > as is shown by:
> >    (V 1000)-:(>@:{.(V_ltk;V_gtk)>@:{:)^:1000
> > ((b$~+/S<:k);(b$~16,~+/S>k)"0)0
> > 1
> >    (V 2000)-:(>@:{.(V_ltk;V_gtk)>@:{:)^:2000
> > ((b$~+/S<:k);(b$~16,~+/S>k)"0)0
> > 1
> >
> >    ts'V 2000'
> > 0.28364355 587648
> >    ts'(>@:{.(V_ltk;V_gtk)>@:{:)^:2000 ((b$~+/S<:k);(b$~16,~+/S>k)"0)0'
> > 0.2691088 57280
> >
> >    a little faster, but look at higher values of y:
> >
> >    ts'(>@:{.(V_ltk;V_gtk)>@:{:)^:3000 ((b$~+/S<:k);(b$~16,~+/S>k)"0)0'
> > 0.40596979 57280
> >
> >    ts'V 3000'
> > |stack error: V
> > |       V 3000
> >
> >
> > Hope this helps.
> >
> >
> > R.E. Boss
> >
> >
> >
> > > -----Oorspronkelijk bericht-----
> > > Van: [EMAIL PROTECTED] [mailto:programming-
> > > [EMAIL PROTECTED] Namens Alistair Tucker
> > > Verzonden: dinsdag 11 september 2007 14:19
> > > Aan: Programming forum
> > > Onderwerp: Re: [Jprogramming] stack overflow
> > >
> > > Yes that's right, the argument for V is the iteration count.
> > > So V 2496 translates to about 1.4 years.
> > > The step size dt *is* very small to satisfy convergence criteria
> > (actually
> > > it maybe should be even smaller).
> > > I could have a larger dt if I were to implement the finite difference
> > > scheme implicitly rather than explicitly.
> > > But that's quite a bit more work which I'd like to avoid if possible!
> > > So if there's some way to get around this limitation in J, I'd be very
> >
> > > pleased to hear it...
> > > Alistair
> > >
> > >
> > > ----- Original Message ----
> > > From: Devon McCormick <[EMAIL PROTECTED]>
> > > To: Programming forum < [email protected]>
> > > Sent: Tuesday, 11 September, 2007 12:36:49 PM
> > > Subject: Re: [Jprogramming] stack overflow
> > >
> > > I see that it looks like an iteration count.  It fails for me thusly:
> > >
> > >    6!:2 'rr=. V 2496'
> > > 0.50126902
> > >    6!:2 'rr=. V 2497'
> > > |stack error: Gamma
> > > | rr=. V 2497
> > >
> > >
> > > On 9/11/07, Devon McCormick < [EMAIL PROTECTED]> wrote:
> > > >
> > > > What does an argument to "V" look like?
> > > >
> > > > On 9/11/07, Alistair Tucker <[EMAIL PROTECTED] > wrote:
> > > > >
> > > > > Hi
> > > > > I'm a little disconcerted to find that I'm getting stack overflows
> > for
> > > > > more than two thousand iterations of my recursive function.
> > > > > This is a problem.  As it stands, my program (below) is unable to
> > > price
> > > > > a convertible bond with more than about a year to maturity.
> > > > > Is there some way round this?  Perhaps I should be implementing
> > the
> > > > > iteration in some other way?
> > > > > Alistair
> > > > >
> > > > > NAS=: 100
> > > > > dS=: 2
> > > > > S=: dS*i.NAS
> > > > > k=: 135
> > > > > r=: 0.05
> > > > > b=: 113.22
> > > > > dL=: %365
> > > > > sigma=: 0.4
> > > > > dt=: 0.9%*:sigma*NAS
> > > > >
> > > > > Sgtk=: S#~S>k
> > > > > Sltk=: S#~S<:k
> > > > >
> > > > > Delta=: (2*dS)%~2&}.-_2&}.
> > > > > Gamma=: (*:dS)%~2&}.+_2&}.-2*}.@:}:
> > > > > dV_dL=: dL%~}."_1-}:"_1
> > > > >
> > > > > negTheta_ltk=: ([:-r*{.@:[),(((r*}.Sltk)*Delta)+( 0.5**:sigma*
> > > > > }.Sltk)*Gamma)@:([,{.@:{.@:])-r*}.@:[
> > > > >
> > > > > step_gtk=: ((}:@:]+dt*(((r*}:Sgtk)*Delta)+(0.5**:sigma*
> > > > > }:Sgtk)*Gamma)@:({:@:[,])-r*}:@:])(}:"_1@:]))+(dt*dV_dL@:}:@:])
> > > > >
> > > > > V_ltk=: Sltk>.[+dt*negTheta_ltk
> > > > > V_gtk=: (Sgtk>.b,.~],({.-~2*{:)@:(_2&{.))@:step_gtk
> > > > >
> > > > > Vrec=: (>@:{.(V_ltk;V_gtk)>@:{:)@:V@:<:
> > > > > V=: Vrec`((b$~+/S<:k);(b$~16,~+/S>k)"0)@.(0=])
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >       ___________________________________________________________
> > > > > Want ideas for reducing your carbon footprint? Visit Yahoo! For
> > Good
> > > > > http://uk.promotions.yahoo.com/forgood/environment.html
> > > > >
> > ----------------------------------------------------------------------
> > > > > For information about J forums see http://www.jsoftware.com/forums.htm
> >
> > > > >
> > > >
> > > >
> > > >
> > > > --
> > > > Devon McCormick, CFA
> > > > ^me^ at acm.
> > > > org is my
> > > > preferred e-mail
> > >
> > >
> > >
> > >
> > > --
> > > Devon McCormick, CFA
> > > ^me^ at acm.
> > > org is my
> > > preferred e-mail
> > > ----------------------------------------------------------------------
> > > For information about J forums see http://www.jsoftware.com/forums.htm
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >       ___________________________________________________________
> > > Yahoo! Answers - Got a question? Someone out there knows the answer.
> > Try
> > > it
> > > now.
> > > http://uk.answers.yahoo.com/
> > > ----------------------------------------------------------------------
> > > For information about J forums see http://www.jsoftware.com/forums.htm
> >
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> >
>
>
>
> --
> Devon McCormick, CFA
> ^me^ at acm.
> org is my
> preferred e-mail
>



-- 
Devon McCormick, CFA
^me^ at acm.
org is my
preferred e-mail
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to