[R] Odp: Currency return calculations

Petr PIKAL
2011-01-07T12:59:06Z

Hi

What is wrong with my suggestion then.

> rate1
      USD    GBP  EURO   CHF    AUD
1  112.05 171.52 42.71 41.50 109.55
2  112.90 168.27 42.68 41.47 102.52
3  110.85 169.03 41.86 42.84 114.91
4  109.63 169.64 44.71 43.44 122.48
5  108.08 169.29 44.14 43.69 122.12
6  111.23 169.47 44.58 42.30 123.96
7  112.49 170.90 41.07 42.05 100.36
8  108.87 168.69 42.23 41.23 110.19
9  109.33 170.90 44.55 42.76 121.58
10 111.88 169.96 41.12 43.79 103.46
> portfolio<-c("USD", "USD", "CHF",
"AUD", "USD")
> log(rate1[-1,portfolio]/rate1[-nrow(rate1),portfolio])
            USD        USD.1          CHF          AUD        USD.2
2   0.007557271  0.007557271 -0.000723153 -0.066323165  0.007557271
3  -0.018324535 -0.018324535  0.032501971  0.114091312 -0.018324535
4  -0.011066876 -0.011066876  0.013908430  0.063798538 -0.011066876
5  -0.014239366 -0.014239366  0.005738567 -0.002943583 -0.014239366
6   0.028728436  0.028728436 -0.032332157  0.014954765  0.028728436
7   0.011264199  0.011264199 -0.005927700 -0.211195211  0.011264199
8  -0.032709819 -0.032709819 -0.019693240  0.093442427 -0.032709819
9   0.004216322  0.004216322  0.036436939  0.098366334  0.004216322
10  0.023056037  0.023056037  0.023802395 -0.161387418
0.023056037
>

As I said instead fiddling with several loop/if/function/variables attempt
it seems to me better to use powerful R indexing and "whole object"
approach where it is possible.

Regards
Petr



Amelia Vettori <amelia_vettori at yahoo.co.nz> napsal dne 07.01.2011
13:46:53:

> Dear sir, I am extremely sorry for messing up the logic asking for help
w.r.t.
> my earlier mails
>
> I have tried to explain below what I am looking for.
>
>
> I have a database (say, currency_rates) storing datewise currency
exchange
> rates with some base currency XYZ.
>
> currency_rates <- data.frame(date = c("12/31/2010",
"12/30/2010",
"12/29/
> 2010", "12/28/2010",
"12/27/2010","12/24/2010", "12/23/2010",
"12/22/2010",
> "12/21/2010", "12/20/2010"),
> USD  = c(112.05, 112.9, 110.85, 109.63, 108.08, 111.23, 112.49, 108.87,
109.33, 111.88),
> GBP  = c(171.52, 168.27,169.03, 169.64, 169.29, 169.47, 170.9, 168.69,
170.9, 169.96),
> EURO = c(42.71, 42.68, 41.86, 44.71, 44.14, 44.58, 41.07, 42.23, 44.55,
41.12),
> CHF  = c(41.5, 41.47, 42.84, 43.44, 43.69, 42.3, 42.05, 41.23, 42.76,
43.79),
> AUD  = c(109.55, 102.52, 114.91, 122.48, 122.12, 123.96, 100.36, 110.19,
121.
> 58, 103.46))
>
> I have a portfolio consisting of some of these currencies.
>
> At this moment, suppose my portfolio has following currency
transactions. i.e
> following is my current portfolio and
> has 2 USD transactions, 2 EURO transactions and a CHF transactions.
>
> portfolio_currency_names = c("USD", "USD",
"EURO", "CHF", "EURO", "USD")
>
>
> # ____________________________________
>
> My objective is AS PER THE PORTFOLIO, I need to generate a data.frame
giving
> respective currency returns.
>
> Thus, I need to have an output like
>
>  USD                USD                 EURO              CHF
> EURO                USD
> -0.0076            -0.0076              0.0007           0.0007   0.
> 0007            -0.0076
>  0.0183            0.0183              0.0194           -0.0325   0.
> 0194               0.0183
>  0.0111            0.0111            -0.0659           -0.0139  -0.
> 0659              0.0111
>  0.0142            0.0142              0.0128           -0.0057   0.
> 0128              0.0142
> -0.0287            -0.0287            -0.0099           0.0323  -0.
> 0099            -0.0287
> -0.0113            -0.0113              0.0820           0.0059   0.
> 0820            -0.0113
>  0.0327            0.0327            -0.0279           0.0197 -0.
> 0279              0.0327
> -0.0042            -0.0042            -0.0535           -0.0364   -0.
> 0535            -0.0042
> -0.0231            -0.0231              0.0801           -0.0238    0.
> 0801            -0.0231
>
> Thus, my requirement is to have the dataframe as per the composition of
my
> portfolio. Thus, if there are only 2 transactions i.e. if my portfolio
> contains say only CHF and AUD, I need the return calculations done only
forCHF and AUD.
>
>
> CHF                    AUD
> 0.0007               0.0663
> -0.0325            -0.1141
> -0.0139            -0.0638
> -0.0057             0.0029
> 0.0323             -0.0150
> 0.0059              0.2112
> 0.0197             -0.0934
> -0.0364            -0.0984
> -0.0238             0.1614
>
> I once again apologize for not asking my requirement properly thereby
causing
> not only inconvenience to all of you, but also wasting your valuable
time. Its
> not that I wasn't careful while asking for guidance for my requirement,
I
> wasn't clear about it. I am sorry for the same once again.
>
> I request you to please help me.
>
> Amelia Vettori
>
>
>