| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

RSouls24November2006

Page history last edited by PBworks 17 years, 2 months ago

This is driving me loopy!

 

Background

 

Whilst using loops in R is possible it is generally not recommended. This is because R is an interpreted language and therefore loops can be quite slow.

 

In general, functions such as apply(), tapply(), sapply(), etc. are prefered. Initially, this may be a little tricky, but generally many problems may be solved using these functions.

 

Unfortunately, I came a little unstuck when I looked at this example, and it seemed that loops were the only way to solve it (this was a a couple of years ago - I'll have a think before next meeting to see if experience has helped, perhaps you can have a look too).

 

Example

 

The example comes from A Simulation Approach in Excess Reinsurance Pricing by Dmitry Papush.

 

I have copied his approach to price the first contract, without resorting to loops.

 

Unfortunately, I'm having trouble pricing the second contract (or layer 2), as it depends on layer 1. I'll expand later, but here's my code so far.

 

Nigel


Markus

 

Unfortunately, I couldn't figure out a quick way to perform this simulation. Here's what I have so far. Perhaps we'll do better at the meeting?

 


 

Nigel,

 

I translated your for-loop into a function in C, see riCalcs.c.

You than need a C-compiler, like gcc, to compile it into a DLL (Windows), or a so-library (UNIX, Mac OS X).

We can than embed this compiled C-code into your R-code. I modified your R-code slightly, see ripricing-2.R.

Unfortunately it does not seem to improve your code tremendously, well not on my three year old iBook.

However, I am looking forward to discussing this with you.

 

Markus


Nigel,

 

 

I created a little package of the example files above, you will find all the files in the

archive RandC.zip.

 

Using R CMD build RandC we can create a source package:

RandC_0.1-1.tar.gz

while with R CMD build --binary RandC we create a binary package:

RandC_0.1-1_R_powerpc-apple-darwin8.6.0.tar.gz

Though this binary package will only work on a Power PC Mac running Mac OS X.

 

For more information on building packages see http://cran.r-project.org/doc/manuals/R-exts.html.

 

Markus


Markus

 

Unfortunately, I can't download .zip or .exe files at work, so can't check out your package yet. With nearly no knowledge of C, I've attempted to adjust your C function to perform the whole calculation rather than using a by() function in R. My code is here: riCalcs2.c and ripricing-3.R

 

Unfortunately, I can't see if it works, because I don't have a compiler and wouldn't know where to stick it! I look forward to your demo.

 

Nigel


 

Markus

 

Well it was the by() function. I've adjusted your package a little. Here's the source code for the following windows package. Use it with the following R code.

 

On my laptop 5,000 simulations, using the by() function, takes 9.3 seconds. By putting the entire calculation in C, 50,000 simulations takes 0.04 seconds!


C to R interfacing Primer

Comments (1)

Anonymous said

at 10:17 pm on Nov 26, 2006

Thanks Markus. We could do the entire RI calc in C. Am going to have to figure this stuff out and have a go shortly. Looking forward to discussing it.

You don't have permission to comment on this page.