Pages

Saturday, September 28, 2019

Indian Economy & Inflation - 2019


Continuing on my earlier article  http://quantfinanceindia.blogspot.com/2016/05/real-interest-rates-india-vs-brazil.html.

Inflation is a necessary evil which has been trending lower from last few years, thanks to the work done by RBI & surplus agri production. Lower inflation has given the space for MPC to cut rates further & provide necessary incentive (reduced cost of capital) for businesses to increase investment.

Reduced inflation, is that causing slowdown?

Taking a leaf from RaghuRam Rajan's book, In the high inflation times - real interest rates stays negative which makes it easy for corporate to post growth in their EPS because increase in prices (read inflation) will be more than the interest they pay.

This takes economy to exuberance which is not good because the purchasing power of households reduces everyday and RBI has no option but to intervene and bring it(inflation) back to level which does not adversely impact households and bring stability to other macro economic indicators like currency rate.

By way of RBI actions, Once inflation start coming down - businesses will not be able to increase (much) the price of their goods that they use to do earlier, hence they don't have the incentive to borrow more & produce more since they will not able to make delta (increased profit - borrowing cost) from increased prices.

This reduces the rate at which profit grows and gives a feeling that there is a slowdown however this is nothing but a temporary adjustment which is required for the betterment of all stakeholders!

Other factors??

Younger generation is not interested in buying fixed assets and are happy to live with less strings attached life which technology is helping them to achieve.

More and more people are getting concerned about Environment and very soon industries which impact environment will not be there anymore or will have lesser customers!


Sunday, July 14, 2019

To Plot Additional Parameters in Time Series Graph




In this Article, we will learn to plot additional parameters to the time-series graph to understand if additional variables has any impact on predicted (to be estimated) values.

We will take SBI closing price on daily basis and will analyze the impact of RBI Repo Rate changes on that. Below code reads the RBI Repo Rate table and rename 'Last.Update' column to Date


> Repo <- read.csv(file = "Repo-Rate.csv", header=TRUE,sep = ",")
> names(Repo)[names(Repo)== "Last.Update"] <- "Date"
> typeof(Repo$Date)
[1] "integer"

We have taken the closing NSE price from NSE website and retained only Date & Closing price information for our comparison.

> SBI <- read.csv(file = "03-06-2018-TO-31-05-2019SBINALLN.csv", header=TRUE,sep = ",")
> SBIEQ <- SBI[SBI$Series == 'EQ',]
> SBIEQ$Date <- as.Date(SBIEQ$Date,format = "%d-%b-%Y")
> SBIRepo <- data.frame(SBIEQ$Date,SBIEQ$Close.Price)
> names(SBIRepo)[names(SBIRepo) == "SBIEQ.Date"] <- "Date"

Changing the date format to text so we can combine easily using Merge function in R

> SBIRepo$Date <- as.character(SBIRepo$Date)
> Repo$Date <- as.character(Repo$Date)
> PlotG <- merge.data.frame(SBIRepo,Repo,by= 'Date',all.x=T)

Convert Date back to original format as ggplot() will not be able to plot time series graph on text data

> PlotG$Date <- as.Date(PlotG$Date,format="%Y-%m-%d")
> ggplot(PlotG,aes(x=Date, y= SBIEQ.Close.Price, color = Rate )) + geom_point() + scale_x_date()






Saturday, June 8, 2019

Data Visualisations in R


Graphs are one of the easiest way to do some quick analysis on the data and help us get the basic information like variance, spread etc..

In this article we will understand how to do data visualisation using R and will download  SBI one year data (from NSE website) to play with it -  You can do the same by using Quantmod library as well.

> library(ggplot2)

#! read sbi stock data from the downloaded file
> SBI <- read.csv(file = "03-06-2018-TO-31-05-2019SBINALLN.csv", header=TRUE,sep = ",")

#! take only EQ series from the data
> SBIEQ <- SBI[SBI$Series == 'EQ',]

#! checking the types of data in various columns
> sapply(SBIEQ, typeof)
                 Symbol                  Series                    Date              Prev.Close
              "integer"               "integer"               "integer"                "double"
             Open.Price              High.Price               Low.Price              Last.Price
               "double"                "double"                "double"                "double"
            Close.Price           Average.Price   Total.Traded.Quantity                Turnover
               "double"                "double"               "integer"                "double"
          No..of.Trades         Deliverable.Qty X..Dly.Qt.to.Traded.Qty
              "integer"               "integer"                "double"

#!  converting Date into correct format as it was not in date format
> SBIEQ$Date <- as.Date(SBIEQ$Date,format = "%d-%b-%Y")

#! Plotting the graph
#! first argument is the data frame, second argument defines the variables to be used while 3rd argument is to define the kind of graph
#! please refer ggplot document as there are multiple options of graphs are available
> ggplot(SBIEQ,aes(x=Date, y= Close.Price)) + geom_point() + scale_x_date()

#! Adding one additional argument to see how the delivered Qty is vis-a-vis price
> ggplot(SBIEQ,aes(x=Date, y= Close.Price, color = Deliverable.Qty )) + geom_point() + scale_x_date()



Sunday, March 10, 2019

Installing & Running Python Code in Ubuntu


To Run Python, you will need 2 software's

Python - to compile and run the code & Jupyter - to manage project and programming using GUI
Both of these are shipped together and Installing Anaconda will take care of both !!


Installing Python & Jupyter

  1. Download Python sh file from https://www.anaconda.com/distribution/#linux
  2. Run this command in the folder where Binary was saved in previous step 
  • bash Anaconda2-5.3.1-Linux-x86_64.sh 
Detailed instructions at Anaconda website - https://docs.anaconda.com/anaconda/install/linux/ 

Running 'Hello' code

Open Jupyter using command 

./anaconda2/bin/jupyter-notebook

Type below Python Code after selecting NEW & Select Run

print "Hello Python"

Regression in Python


Earlier we have discussed how to use to conduct Regression testing in R - Regression in R

In this article ,we will delve into some of the details to conduct the same using Python

Assumption -  Dependent and independent data sets have been stored in respective variables sbiR & niftyR

Code to Run Linear Regression

from sklearn.linear_model import LinearRegression
regression = LinearRegression(normalize=True)
regression.fit(niftyR,sbiR)

As you might notice, Sklearn is the library in Python which has linear module and Linear Regression is the function that we have imported to run Regression.

You can also print the r2 value by function regression.score()

Code to Run Logistic Regression

from sklearn.linear_model import LogisticRegression
logistic = LogisticRegression()

logistic.fit(niftyR,sbiR)

Everything else remain same however depending upon the dependent variable or output, we might need to use Logistic regression

Cross Validation & Train - Test Data sets

To test data sets, we first need to split the available data into 2 parts - train data & test data. Below library should be used to call split function

from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split( X, Y, test_size=0.33, random_state=42)

Array X contains Independent data while Y is the output, we will split whole data into 2 parts and will run regression on train data and check the results on test data to establish if model can be used for prediction

regression.fit(X_train,y_train)
print mean_squared_error(y_true=y_train, y_pred=regression.predict(X_train))
print mean_squared_error(y_true=y_test, y_pred=regression.predict(X_test))

This article has details on installing & Running Python Hello Code from Ubuntu Machine