[R] Vectorization

ManojW manojsw at gmail.com
Thu Jun 16 02:45:48 CEST 2005


Greetings,
    Can anyone suggest me if we can vectorize the following problem
effectively?

    I have two datasets, one dataset is portfolio of stocks returns on a
historical basis and another dataset consist of a bunch of factors (again on
a historical basis). I intend to compute a rolling n-day sensitivitiesfor
each stock for each factor, so the output will be a data frame with
<ticker><dt><sensitivities>.

    How would you go onto vector this situation effectively?

    I end up with a psuedo code like this:

            # For each date
            For curr dt in all dates
                # Get Universe of stocks as of that date
                Get Universe for curr date
                # Calculate Sensitivity for each factor between n days back
dt to curr date
                sensitivity    =
sapply(univ{ticker},CalcSensitivity,n_days_back_dt,dt)
            Next date

    I would highly appreciate if the above logic could be improved (if at
all) by a more effective solution since I do get into such situations on a
regular basis.

    Thanks in advance

Cheers

Manoj




More information about the R-help mailing list