Re: eval date-problem solved

Posted by JURAJ HUSKA on
URL: http://quantlib.414.s1.nabble.com/eval-date-problem-solved-tp1121p1125.html

Hi,

thank you for you insights, I myself am aware of the weirdness of the proposed "solution", actually it took me almost a day before I tried that obviously very illogical "method" (I guess there are other more logical ways of going about this).

All I needed was to generate discount factor curves from swap rates using historical swap rates.
I myself would suspect the generated curves are right, however, I computed the same curves using a commercial software package and the results (discount factors) coincided to 5 decimal places.

Actually, I did run the code under  a debugger and it got hung up each time the eval date was reset, in fact, the time to complete the call to the swig generated dll

"[DllImport("NQuantLibc", EntryPoint="CSharp_Settings_setEvaluationDate")]
  public static extern void Settings_setEvaluationDate(HandleRef jarg1, HandleRef jarg2);"

took each time about twice as long as the previous time(!), which made the whole computation useless after a few iterations...

Interestingly, the same calculation, with swap rates replaced by bond data (using BondRateHelper), went through smoothly (I  didn't have to do any "dispose" gymnastics at all).

I would like to understand what exatly is going on but I am afraid I don't have time to do that right now.


best,

Juraj


On 7/9/07, Toyin Akin <[hidden email]> wrote:

Hi,

The C# solution to the problem posed before looks really wierd...

If you look at the following snippet...

########################################################
for(int i=0;i<30;i++)
                {
                    Quote quote = new SimpleQuote(
Convert.ToDouble(dfData[i]/100) );
                    QuoteHandle quoteHandle = new QuoteHandle( quote );
                    NQuantLib.Period period = new Period(i+1,
NQuantLib.TimeUnit.Years );


                    RateHelper swapRateHelper = new SwapRateHelper(
                        Convert.ToDouble(dfData[i]/100),
                        period,
                        0,
                        calendar,
                        fixedLegFrequency,
                        fixedLegConvention,
                        fixedLegDayCounter,
                        index
                        );

                    instruments.Add(swapRateHelper);

                    //testing only - works!!!
                    swapRateHelper.Dispose();
                    quoteHandle.Dispose ();
                    quote.Dispose();
                    period.Dispose();

                }
                index.Dispose();
########################################################

A swapRateHelper object is added into the  instruments array, then on the
next line, it is disposed of. Well the disposed swapRateHelper object still
points to the reference stored within the array and in effect, you are also
disposing the object within the array (the swapRateHelper object has not
been assigned to another object yet).

This is true for the quote object as well.

Then this instrument array is passed to the yieldcurve and the curve then
works!!

I would run this under a debugger mate...

The only thing I can think of, is that a deep copy is being performed. If
so, the .NET version of any QuantLib code would be really slow because of
all the deep copying being performed all over the place. I cannot believe
that this is the case.

I agree that .NET garbage collection may not be that efficient within
scenarios where you are creating/deleting large amounts of objects in a very
short periofd of time.

You could, at the end of the loop, force .NET to a garbage collect. The
thing is though, if you look at the code snippet above, every object created
within the loop should still be present in memory because they are all
required for the construction of the yieldcurve latter on.

It's only when the yieldcurve is not needed that everything should be
disposed of.

The solution posed above, I would not trust at all because this kind of
logic will get you in trouble when you start mixing swig generated C#
classes and native .NET classes. You have to know when to call Dispose() and
if delivering a solution/Library to another team, they would have to have a
very detailed, inner workings, of the delivered library.

As to why the said code is acting strangly without the Dispose() call is
strange indeed.

I would suggest running the code under a debugger, tracing from the C# code
to the C++ layer.

Maybe my analysis is flawed, but if the logic above resembled a C++ snippet
where dispose are calls to delete, you will see the problem.

As a side note, calling Dispose really just calls code implemented within
the Dispose() function implemeted within the generated C# class. The
implementation of this code is generated by swig, thus you may need to look
at it...

Best Regards,
Toyin Akin
CapeTools QuantTools.
www.QuantTools.com

>From: "JURAJ HUSKA" < [hidden email]>
>To: "Luigi Ballabio" <[hidden email]>
>CC: [hidden email]
>Subject: Re: [Quantlib-users] eval date-problem solved
>Date: Mon, 9 Jul 2007 08:51:14 -0500
>
> http://www.csharphelp.com/archives2/archive297.html
>
>Unfortunately, it isn't as deterministic as in C++, which could have been
>part of the problem, the other one is me not being (ever remotely close to)
>an expert in either of the languages...
>
>On 7/9/07, Luigi Ballabio <[hidden email]> wrote:
>>
>>On Fri, 2007-07-06 at 16:34 -0500, JURAJ HUSKA wrote:
>> > I finally solved the problem I had with evaluation date slowing down
>> > calculations, although don't quite have an explanation why my solution
>> > works.
>> >
>> > Here is the solution in words: I had to dispose of SwapRateHelper

>> > object immediately after I added it to the instruments
>> > RateHelperVector and also had to dispose of the constructed
>> > termstructure and instruments vector. See the code below.
>> > Interestingly, just disposing of the instruments vector (that is, not
>> > disposing separately of SwapRateHelpers) didn't do the job. Anyone a
>> > take on this?
>>
>>I guess the objects remained alive even though they had gone out of
>>scope. How does garbage collection in C# work?
>>
>>Luigi
>>
>>
>>--
>>
>>Discontent is the first necessity of progress.
>>-- Thomas A. Edison
>>
>>
>>


>-------------------------------------------------------------------------
>This SF.net email is sponsored by DB2 Express
>Download DB2 Express C - the FREE version of DB2 express and take
>control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/


>_______________________________________________
>QuantLib-users mailing list
>[hidden email]
>https://lists.sourceforge.net/lists/listinfo/quantlib-users

_________________________________________________________________
http://imagine-windowslive.com/hotmail/?locale=en-us&ocid=TXT_TAGHM_migration_HM_mini_pcmag_0507



-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users