Friday, February 21, 2020

R uses data science

R uses
It uses the symbols for addition, subtraction, multiplication, division, and exponents nonsense percentage can be used to specify the order of operation.

R  use data science
1. Arithmetic
>pi
3.141.......
2. Variables
It can be letters numbers and dot,-.
>x<- 200
>x
[1] 100
good programming practice is to use informative name for your variables to improve readability.
3. Functions
Function text 1 and more inputs and produces one and more outputs.
>seq(from=1, to= 8,by=2)
[1] 1 3 5 7 9
4.
Vectors
> (x <-s (1,15, by =3)
[1] 1 3 5 7 9 11 13
5. Matrices
It is created from a vector using the function matrix

[1] "AAPL" "CSCO"
>
> aapl = as.matrix (AAPL[ , 6 ] )
>
> csco = as.matrix(CSCO[,6])
> stkdata = cbind ( aapl , csco )
>
> head(stkdata)
           AAPL.Adjusted CSCO.Adjusted
2007-01-03      10.39169      21.46619
2007-01-04      10.62234      22.03129
2007-01-05      10.54669      22.03904
2007-01-08      10.59878      22.16290
2007-01-09      11.47922      22.03904
2007-01-10      12.02857      22.20160
> tail(stkdata)
           AAPL.Adjusted CSCO.Adjusted
2020-02-11        319.61         49.13
2020-02-12        327.20         49.93
2020-02-13        324.87         47.32
2020-02-14        324.95         46.97
2020-02-18        319.00         46.59
2020-02-19        323.62         46.29
> dim( stkdata )
[1] 3305    2
> Now, compute daily returns. This time, we do log returns in continuoustime. The mean returns are:
Error: unexpected ',' in "Now,"
> n = length ( stkdata [ , 1 ] )
> n
[1] 3305
> rets = log ( stkdata [ 2:n , ] / stkdata[ 1 :( n−1 ),] )
> colMeans ( rets )
AAPL.Adjusted CSCO.Adjusted
 0.0010407275  0.0002325807
> we  can find covariance matrix and correlation matrix:
Error: unexpected symbol in "we  can"
>   
> cv = cov ( rets )
> cv
              AAPL.Adjusted CSCO.Adjusted
AAPL.Adjusted  0.0003870351  0.0001754235
CSCO.Adjusted  0.0001754235  0.0003271735
> print ( cv , 2 )
              AAPL.Adjusted CSCO.Adjusted
AAPL.Adjusted       0.00039       0.00018
CSCO.Adjusted       0.00018       0.00033
>
> cr = cor ( rets )
> cr
              AAPL.Adjusted CSCO.Adjusted
AAPL.Adjusted     1.0000000     0.4929735
CSCO.Adjusted     0.4929735     1.0000000

No comments:

Post a Comment

Black-Scholes formula-R

 Black-Scholes formula-R > BlackScholes <- function(TypeFlag = c("c", "p"), S, X, Time, r, b, sigma) { TypeFla...