Showing posts with label R-MATRIX. Show all posts
Showing posts with label R-MATRIX. Show all posts

## Rmatrix

> x=(1:20)
> y=20:1
> z=c(1:20,19:1)
> tmp <-c(4,6,3)
> rep(tmp,10)
[1] 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6
[30] 3
> rep(tmp,l=31)
[1] 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6
[30] 3 4
> rep(tmp,l=34)
[1] 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6
[30] 3 4 6 3 4
> rep(tmp,times=c(5,20,30))
[1] 4 4 4 4 4 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 3 3 3 3
[30] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
> tmp <- seq(3,6,by=0.1)
> exp(tmp)*cos(tmp)
[1] -19.884531 -22.178753 -24.490697 -26.773182 -28.969238
[6] -31.011186 -32.819775 -34.303360 -35.357194 -35.862834
[11] -35.687732 -34.685042 -32.693695 -29.538816 -25.032529
[16] -18.975233 -11.157417  -1.362099  10.632038  25.046705
[21]  42.099201  61.996630  84.929067 111.061586 140.525075
[26] 173.405776 209.733494 249.468441 292.486707 338.564378
[31] 387.360340
>
> plot(tmp)
> plot(exp(tmp)*cos(tmp))
> plot(tmp,exp(tmp)*cos(tmp))
> plot(exp(tmp)*cos(tmp),tmp)
> plot(tmp/3,exp(tmp)*cos(tmp))
> plot(tmp*5,exp(tmp)*cos(tmp),colour=7)
y <- rnorm(100)
> hist(y, probability=TRUE)
> lines(density(y))
> lines(density(y),col=99)

## The Basics R language Statistics

Algebra R has all the functions of a normal calculator; here we‘ll quickly look at how to use these functions. Try entering the following:
> 1 + 1
The output should look like this: [1] 2 Don‘t worry about what the [1] in front of the answer means, for now, that will be covered later, in Section 4.3 —

### Matrices.

From now on the input and output will be put right after each other, like so:
> 1 - 1 [1] 0 The operators available are + (addition), - (subtraction), / (division), * (multiplication), and ^ (raise to a power). Most of the time, however, you will not want to just have the calculation appear in the console window, you will instead want to save the calculation as a ―variable‖.

### To save a calculation as a variable, simply think up a variable name, and then use an equal sign to assign the calculation‘s output to that variable. For example:

> variable1 = 1 + 1 Now, there will not be an output of ―2‖ after you hit enter because instead of outputting the result, R has saved the result to ―variable1‖. OK, now that we‘ve covered some background information and made sure everything is installed correctly, let‘s get down to the nitty-gritty of actual programming. This section will walk you through the nuts and bolts of using R. Remember to look back to Section 1.3 — 10 Now, try typing the following:
> variable1 [1] 2 You get the output that you saw before, as the calculation was the exact same, it was just saved to the variable. Note: Some other manuals you read may assign calculations to variables as follows:
> variable1 <- 1 + 1 The “<-” operator is, for our purposes, exactly the same as the “=“ operator in R except for one exception, which is covered in Section 9: Functions. I prefer to use the “=“ operator simply because it is more intuitive for me and is a keystroke less than “<-”. Ok, the last main point to cover about algebra in R is the order of operations. The order is exactly the same as the one you likely learned in high school; that is, multiplication and division are done first in order from left to right and then addition and subtraction are done, again from left to right.
For example:
> 1 + 2 / 3 - 2 * 6.5 [1] -11.33333 The order of operations can be controlled by using parenthesis. Operations are performed from the innermost parenthesis outwards, so in the following example:
> 1 * (2 / (1 + 1)) [1] 1 The calculation (1 + 1) is performed first. Then, (2 / 2) is calculated, followed by 1 * 1. Whereas if this operation were written without parenthesis the output would be as follows:
> 1 * 2 / 1 + 1 [1] 3 With 1 * 2 calculated initially, followed by 2 / 1 and finally 2 + 1. 11 4.2 — Vectors The next main task that you should get comfortable within R is creating and manipulating vectors (the word ―vector‖ is always used in the mathematical sense in this section, the data type vector will be covered later). A vector is a string of numbers; sometimes vectors are sequential numbers, other times vectors are random numbers — there is no restriction on the type or amount of numbers that a vector can contain. The simplest way to create a vector is the following command: > vector1 = 1:9
> vector1 [1] 1 2 3 4 5 6 7 8 9 The colon ―:‖ operator creates a sequence of numbers from the left hand value to the right hand value, i 12 [1] 1 2 3 4 5 6 7 8 9
> vector4 = 9:1
> vector4 [1] 9 8 7 6 5 4 3 2 1 > vector3 + vector4 [1] 10 10 10 10 10 10 10 10 10 The first element of this new vector was 9 + 1, the second 8 + 2, etc. Now, you may be wondering what happens if the vectors are not the same size, well, let‘s try:
> vector5 = 1:3
> vector5 [1] 1 2 3
> vector3 [1] 1 2 3 4 5 6 7 8 9
> vector3 + vector5 [1] 2 4 6 5 7 9 8 10 12
What happens, as you can see, is that R loops the smaller vector as many times as necessary to add something to each element of the larger vector. In this case the first element is 1 + 1, the second element is 2 + 2, the third element is 3 + 3, but then the smaller vector loops back to its the first element to add to the fourth element of the larger vector, giving the calculation 1 + 4 for the fourth element. When the larger vector is not a multiple of the smaller vector the process is the same, except R will give a warning message informing you that the larger vector is not an integer multiple, so not all elements of the smaller vector were added to the larger vector an equal number of times. Try this situation for yourself. Multiplying, dividing, exponentiating, and subtracting vectors works in the same, element-by-element fashion. 1

### Black-Scholes formula-R

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