Category Archives: R
Project Euler Question 15
Posted by Shawn Zhang
on December 23, 2012
No comments
Question:
Starting in the top left corner of a 2*2 grid, there are 6 routes (without backtracking) to the bottom right corner.How many routes are there through a 20*20 grid?
Solution:
This is not a particular question for programming ,it seems like a more mathematics problem for me . for moving from top left to right down , only consist two direction "down/ right", all path is a combination of 20 down and 20 right moves, so ,solution is just number of combination 20 of 40 instruction on move .
For Read more [...]
Simple Debugging with R
Posted by Shawn Zhang
on July 15, 2012
No comments
Unlike other programming language such as Python and Java, R is less equipped with state of art level Integrated Development Environment. Thus, debugging program in R comes with a little trivial.
Perform "Assert"
in Python/C, Assert(test_expr) expression will throw out an exception if "test_expr" was false. In R , this feature can be implement with function "stopifnot()", let's demo this function via below example:
demo <- function (a){
stopifnot(a < 2000)
if (a < 2000){
Read more [...]
Construction Bond Portfolio Management in a Object-Oriented R Way
Posted by Shawn Zhang
on July 8, 2012
No comments
I've been read a lot of articles, blogs and other online resources about programming R, most of them discuss a lot about it's powerful statistic features. But there are less topics about it's Object-Oriented features, I'm going to navigate these features to construct a Portfolio Management. (PS: all following R code will be in a S4 Style)
Define a Bond Class
A portfolio means a collection of assets, asset could be different financial products ,like Bond, Future, Stock or other derivatives . We Read more [...]
Correlation Analysis of Shibor Rate in R
Posted by Shawn Zhang
on July 7, 2012
No comments
Correlation is define as degree of co-movement of two variables . I'll try to analysis the correlation of 7days shibor rate and 1 month shibor rate . Let's examine the relation in visual first,
> ts.plot(shibor$X1W, type="l",col="red")
> lines(shibor$X1M, type="l",col="blue")
As figure shows , for the most of time 1 Month Shibor rate will be greater than the 1 Week Shibor rate, that makes sense as it require higher return for taking premium Read more [...]
VaR Measurement with PerformanceAnalystics in R
Posted by Shawn Zhang
on July 5, 2012
No comments
VaR is most popular risk measurement of downside risk in recent years. It can aggregate risk across products departments ,and even across different risk type. I am going to dive into the VaR() function in the package "PerformanceAnalystics"
Here is full parameter of function VaR()
VaR(R = NULL, p = 0.95, method = c("modified", "gaussian","historical", "kernel")...)
R can be assign a data.frame, vector, matrix, or xts, timeSeries object etc . p is assigned Read more [...]
Implement Exponential Smooth in R
Posted by Shawn Zhang
on July 3, 2012
No comments
Introduction
Exponential Smooth is common technique in time series data analysis . There are 3 types of Exponential smoothing methods , which corresponding to different type of time series data.
Time series which don't demonstrate trend or seasonality , use single exponential smoothing
Time series which demonstrate trend but not seasonality , use double exponential smoothing
Time series which demonstrate both trend and seasonality , use triple exponential smoothing
Single Exponential Read more [...]
Time Series Analysis on Shibor Rate
Posted by Shawn Zhang
on July 2, 2012
No comments
R provide powerful functions in time series analysis, I'll try to explore a few functions regarding to Shibor(Shanghai Interbank Offered Rate) rate from 2006-10-08 to 2012-06-26 .
> str(shibor)
'data.frame': 1432 obs. of 9 variables:
$ Date: Date, format: "2006-10-08" "2006-10-09" ...
$ O.N : num 2.12 2.1 2.09 2.1 2.09 ...
$ X1W : num 2.29 2.3 2.3 2.29 2.29 ...
$ X2W : num 2.38 2.4 2.42 2.49 2.52 ...
$ X1M : num 2.53 2.55 2.57 2.59 2.59 ...
$ Read more [...]
A Peek in ggplot2 Part 1
Posted by Shawn Zhang
on June 27, 2012
No comments
Intro
Ggplot2 has gained a significant popularity in recent year due to its elegant design and compared with Lattice package , ggplot2 provide more generic graphic solution and more fancy graphic view . Recently I am reading a book <ggplot2 elegant graphic for Data Analysis> , try to grasp a skill on plotting on ggplot2 .
Start
Lets start with the qplot() function which provide similar function to plot(). "shibor " is a dataframe with daily interbank borrow rate in Shanghai from 2007 to Read more [...]
Secant Method in R
Posted by Shawn Zhang
on June 24, 2012
No comments
Intro
The main disadvantage of Newton-Raphson Algorithm is that it require the derivative of original function
. That create a little mess within R implementation . Scant Method excels in that it only requirement of the function is being continuous .
Secant Method
Essentially , Secant is much alike to Newton-Raphson Algorithm, difference is that will replace the derivative
with a analogous expression below:
First Step, we Read more [...]
. That create a little mess within R implementation . Scant Method excels in that it only requirement of the function is being continuous .
Secant Method
Essentially , Secant is much alike to Newton-Raphson Algorithm, difference is that will replace the derivative
with a analogous expression below:
First Step, we Read more [...] Generic Newton-Raphson Method in R
Posted by Shawn Zhang
on June 23, 2012
2 comments
As previous written blog regarding how to calculate Yield to Maturity in R I've mention Newton-Raphson method to find quick root solution of Yield which will equals the present value of future cash flow to current market price of bond .
Generally , we guess a initial value , and then comes into a iteration process in the form of blow:
Get Generic First Derivative of Function
In this specific Bond Yield to Maturity case , the first derivative of function can be get by beforehand. But in Read more [...]