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


Tuesday, December 25, 2018

Onion Farming Price vs Production


Onion is one of the main ingredient of Indian diet and is known to have changed Govts when the price goes up as it is consumed daily by majority households.Even after having such robust market, farmers still are not getting the right price for their produce and end up getting stuck in the period of high/low productions.

Below is the data which i have taken from Govt website to show how this pattern is leading to mismatch b/w Demand & Supply.

Notice the spike in prices of 2010 when the production was lesser (only by 10%), main reason was not enough remuneration of the produce in previous year 2009.

After price went up in 2010, there was an increase in area under production which further resulted in record production of 15,118 thousand tons and brought the prices down to 12.

Is contract farming is a better solution to plan supply against demand?
Or our(whole world) production levels have gone high and there isn't enough demand? given the fact that Indian Productivity levels are still much lesser than developed countries or China?


Production Data India


AR AREA(000’ ha) PRODUCTION (000’tons)
1996-97 404 4180
2000-01 450 4721
2006-07 768 10847
2007-08 821 13900
2008-09 834 13565
2009-10 756.2 12158.8
2010-11 1064 15118


Production Data Bhopal


District_Name Crop_Year Season Crop Area Production
BHOPAL 1999 Whole Year Onion 581 6850
BHOPAL 2000 Whole Year Onion 564 6677
BHOPAL 2001 Whole Year Onion 638 6407
BHOPAL 2002 Whole Year Onion 615 5932
BHOPAL 2003 Whole Year Onion 748 8103
BHOPAL 2004 Whole Year Onion 832 11195
BHOPAL 2005 Whole Year Onion 852 10042
BHOPAL 2006 Whole Year Onion 1036 13434
BHOPAL 2007 Whole Year Onion 941 9048
BHOPAL 2008 Whole Year Onion 829 8898
BHOPAL 2009 Whole Year Onion 805 9499
BHOPAL 2010 Whole Year Onion 644 3397
BHOPAL 2011 Whole Year Onion 691 15643
BHOPAL 2012 Whole Year Onion 733 22922
BHOPAL 2013 Whole Year Onion 696 10216


Price Data Bhopal


Date Centre_Name Commodity_Name Price
31-12-99 BHOPAL Onion 4
29-12-00 BHOPAL Onion 6
31-12-01 BHOPAL Onion 5
31-12-02 BHOPAL Onion 4
31-12-03 BHOPAL Onion 12
31-12-04 BHOPAL Onion 6
30-12-05 BHOPAL Onion 10
29-12-06 BHOPAL Onion 4
31-12-07 BHOPAL Onion 10
31-12-08 BHOPAL Onion 6
31-12-09 BHOPAL Onion 10
31-12-10 BHOPAL Onion 30
31-12-11 BHOPAL Onion 12
31-12-12 BHOPAL Onion 12
31-12-13 BHOPAL Onion 18






https://data.gov.in/catalog/district-wise-season-wise-crop-production-statistics

Sunday, December 16, 2018

Bull Call Spread strategy on Karnataka Bank


Bull call spread helps you take a directional call and it has lesser cost as compare to buying a naked call since you get some cash flow by selling a higher strike call.

In the below table, we are buying a 105 strike call for KTK Bank and selling 112.5 strike call. View we have taken is that stock price will go up which will increase the value of 105 while the increase in the value of 112.5 will be lower thus increasing our profit.

In the cost column, we can further analyze that the strategy is cheaper when the stock price is away from the strike price however we can't wait for that to happen and get into the order as soon as we have the confidence about the direction.



Symbol Date Expiry Option Strike Price Strike Price LTP – 105 LTP – 112.5 LTP – Stock Cost
KTKBANK 30-Nov-2018 27-Dec-2018 CE 105 112.5 3.9 1.55 103.55 2.35
KTKBANK 03-Dec-2018 27-Dec-2018 CE 105 112.5 4.15 1.75 104.85 2.4
KTKBANK 04-Dec-2018 27-Dec-2018 CE 105 112.5 3.55 1.25 103.7 2.3
KTKBANK 05-Dec-2018 27-Dec-2018 CE 105 112.5 2.6 0.9 102.15 1.7
KTKBANK 06-Dec-2018 27-Dec-2018 CE 105 112.5 2.7 0.95 103 1.75
KTKBANK 07-Dec-2018 27-Dec-2018 CE 105 112.5 3.2 0.85 103.1 2.35
KTKBANK 10-Dec-2018 27-Dec-2018 CE 105 112.5 2.2 0.5 101.2 1.7
KTKBANK 11-Dec-2018 27-Dec-2018 CE 105 112.5 2.55 0.5 103.35 2.05
KTKBANK 12-Dec-2018 27-Dec-2018 CE 105 112.5 4.85 1.35 107.2 3.5
KTKBANK 13-Dec-2018 27-Dec-2018 CE 105 112.5 5.1 1.4 108.05 3.7
KTKBANK 14-Dec-2018 27-Dec-2018 CE 105 112.5 5.2 1.3 108.55 3.9

Having a put call strategy would have yielded almost similar result however liquidity of the options is a concern for KTK Bank.


Symbol Date Expiry Option
type
Strike Price Strike Price LTP-105 LTP-112.5 Underlying Value Inflow
KTKBANK 30-Nov-2018 27-Dec-2018 PE 105 112.5 4.65 9.25 103.55 4.6
KTKBANK 03-Dec-2018 27-Dec-2018 PE 105 112.5 3.7 8.1 104.85 4.4
KTKBANK 04-Dec-2018 27-Dec-2018 PE 105 112.5 4.4 9 103.7 4.6
KTKBANK 05-Dec-2018 27-Dec-2018 PE 105 112.5 4.4 10.3 102.15 5.9
KTKBANK 06-Dec-2018 27-Dec-2018 PE 105 112.5 4.75 9.5 103 4.75
KTKBANK 07-Dec-2018 27-Dec-2018 PE 105 112.5 4.5 9.35 103.1 4.85
KTKBANK 10-Dec-2018 27-Dec-2018 PE 105 112.5 6.65 11.1 101.2 4.45
KTKBANK 11-Dec-2018 27-Dec-2018 PE 105 112.5 3.75 9.1 103.35 5.35
KTKBANK 12-Dec-2018 27-Dec-2018 PE 105 112.5 1.7 6.1 107.2 4.4
KTKBANK 13-Dec-2018 27-Dec-2018 PE 105 112.5 1.35 5.4 108.05 4.05
KTKBANK 14-Dec-2018 27-Dec-2018 PE 105 112.5 1.1 4.85 108.55 3.75