Friday 18 August 2017

How to calculate historical VAR using R Programming?

stockdata<-read.csv(choose.file(),header=TRUE)close<-stockdata$Close #assigning closing pricereturns<-(close[1:(length(close)-1)]-close[2:length(close)])/close[2:length(close)]#calculating returns
library(PerformanceAnalytics)
Var99<-(VaR(returns,p=0.99,method="historical")*100)#Value at Risk at 99% Confidence
Var95<-(VaR(returns,p=0.95, method = "historical")*100) #Value at risk at 95% Confidence
var90<-(VaR(returns,p=0.90,method = "historical")*100) #value at risk at 90% confidence
varmatrix<-cbind(var90,Var95,Var99)
colnames(varmatrix)<-c("var90","Var95","Var99")#creating head
varmatrix
write.csv(varmatrix,file = "VAR of stock") #exporting
barplot(varmatrix,ylab = "Risk in %",ylim = c(-1:-2)) #plotting VAR

How to Calculate Beta of a Stock using R Programming?

R is a powerful tool. Apart from using it for data science, it can be used an excel spreadsheet. The last couple of years I showed you how to calculate beta of a stock on excel, now I take an opportunity to calculate beta of a stock. Click Here to know how to calculate beta of a stock on excel.

To get beta you do not need any fancy library to load. Your base package will work fine.

The process is simple and as follows:

  1. Import data to R
  2. calculate returns (arithmetic, logarithmic)
  3.  apply linear regression
  4. select slop as beta.
The codes are as follows:

Importing Data

We will import the data in the name of nifty and ril. which are in the forms of OHLC data collected from nseindia.com. Where I used nifty and Reliance Ltd data.

nifty<-read.csv(choose.files(),header = TRUE)
ril<-read.csv(choose.files(),header = TRUE)

Renaming the values

This is not necessary as specifing the complete data is hectic. I like to cut it short.

NC<-nifty$Close
RC<-ril$Close.Price

Plotting the Closing Price

plot(NC,type = "l",main="Nifty",col="Red")
plot(RC,type="l",main="RIL",col="green")

Calculating the closing price

I tried to calcualte the returns using Arthamatic menthod and even log menthod can be better and most desireable than this.

indexR<-(NC[1:(length(NC)-1)]-NC[2:length(NC)])/NC[2:length(NC)]
stockR<-(RC[1:(length(RC)-1)]-RC[2:length(RC)])/RC[2:length(RC)]

Finding the regression of x and y

result<-lm(stockR~indexR)
beta<-result$coefficients[2,1]
print(beta)

Thursday 17 August 2017

Text Analysis: Easy way to Web Scrapping (Part 1)

In this article, we will do the basic 1st step of Data Scientist is to import the data from SQL, Excel, Apache Spark etc. But when you have a data in a website you tend to do something called web scraping/web crawler/spider etc..

What is web scrapping?

Web scraping is a technique of extracting desired information from the website to the desired database.

In this article, we will try to scrap an article from our own blog to analyse and see the output using R Programming.

Requirements for web scrapping

To do web scraping we require

  1. rvest package
  2.  HTML XPath/ CSS Selector. 

If you don't have rvest package in R. No need to worry install the package using the code below.

 install.packages("rvest")

If you don't know how to use HTML XPath..CSS can be a great help.
To know more about the installation process Visit Here

Process of Web Scrapping 


article<- html("desired URL")

article<-article %>%
html_node("Xpath/CSS") %>% html_text()

We are creating a vector called "article" which consists an HTML URL. Later, you are specifying using %>% (pipe) that the article consists an HTML node and it is in the text format.

Example:
article<- html("https://experimentswithdatascience.blogspot.in/")

article<-article %>%
html_node(".column-center-inner") %>% html_text()


Output

> article
[1] "\n\n\n\n          \n        \nMonday, 14 August 2017\n\n          \n        \n\n\n\n\nAbout the writter\n\n\n\n\n\n\nAbout Sangamesh K S\n\n\nSangamesh is an MBA Finance with a non-technical background. He specializes in Technical Analysis, Statistics, Machine Learning and Data Science .\n\n\n\nPreviously, Sangamesh had applied his skills on Financial Markets to forecast price of Indian stocks and commodities. He is also a Technical Analysis Blogger who write articles for Experiments With Stocks.  \n\n\nSangamesh use R for Data Analysis. R is as potential tool as its competitor Python . As I know both language, R is more elegant and do contain specific libraries and more inclined to statistics. Python in another hand is easy and flexible.\nHere in this blog you will learn everything about data science form intermediate to advance level.\n\n\n\n\n\n\n\n\nPosted by\n\n\nSangamesh KS\n\n\n\n\nat\n04:25\n\n\n\n\n\nNo comments:\n    \n\n\n\n\n\n\n\n\n\n\nEmail ThisBlogThis!Share to Twi... <truncated>

Note: The data is not clear. so we require data mining to sanitize the article from unwanted words.

Monday 14 August 2017

About the writter



Sangamesh is an MBA Finance with a non-technical background. He specializes in Technical Analysis, Statistics, Machine Learning and Data Science .

Previously, Sangamesh had applied his skills on Financial Markets to forecast price of Indian stocks and commodities. He is also a Technical Analysis Blogger who write articles for Experiments With Stocks.  

Sangamesh use R for Data Analysis. R is as potential tool as its competitor Python . As I know both language, R is more elegant and do contain specific libraries and more inclined to statistics. Python in another hand is easy and flexible.

Here in this blog you will learn everything about data science form intermediate to advance level.