how to compound the interest monthly in recurring deposit calculation? - math

how to calculate recurring deposit in monthly basis?
M = ( R * [(1+r)n - 1 ] ) / (1-(1+r)-1/3)
M is Maturity value
R is deposit amount
r is rate of interest
n is number of quarters
if i take 'n' as 4(no of Quarters) for 1 year its showing yearly Maturity value.can anyone tel me how to do monthly calculation.Thanks

I'm not sure what the 1/3 is doing in the denominator, could you explain that? As integer division it will likely evaluate to 0 anyway.
That said, the formula for payment at the end of each payment interval is indeed
M = R * ( (1+r/p)^n-1 )/( (1+r/p) -1) = R * p/r * ( (1+r/p)^n-1 )
resulting in M = 125365.3694 for the given data;
and for payments at the start of each payment interval (month, quarter, ...)
M = R * (1+r/p)*( (1+r/p)^n-1 )/( (1+r/p) -1) = R * (p/r+1) * ( (1+r/p)^n-1 )
resulting in M = 126357.8452 for the given data.
Here p is the number of parts of the year that is used, i.e., p=4 for quarterly and p=12 for monthly, n is the number of payments, i.e., the payment schedule lasts n/p years, and then r is the nominal annual interest rate, used in r/p to give the interest rate over each part of the year.
Note that the effective interest rate (1+r/p)^p-1 depends on p, for p=1 it is r, for very large p it approaches exp(r)-1.
A more realistic result is obtained by taking the number of days in each month into account
days:=[31,28,31,30,31,30,31,31,30,31,30,31];
for k in [1..12] do
sum:=0;
for j in [1..12] do
sum+:=1;
sum*:=1+days[(j+k-1) mod 12 + 1]*0.095/365;
end for;
k, sum*10000;
end for;
gives as result the maturity value if started in month[k], with k=1 corresponding to january
1 126402.9195
2 126324.3970
3 126343.1642
4 126329.4573
5 126348.2653
6 126334.5983
7 126353.4478
8 126372.4494
9 126358.9711
10 126378.0173
11 126364.5825
12 126383.6740

Related

In R, categorize time series data based on regex

My data is organised as follows: for each product, there is a tax rate for each year as well as a base year tax (baseyr).
product<-c("01","02","03","04")
baseyr <-c("10","8 GBP/tonne","8GBP/tonne + 8GBP/tonne","8")
yr1<-c("5","5 GBP/tonne","5GBP/tonne + 10GBP/tonne","5 + 5GBP/tonne")
yr2<-c("3","3GBP/tonne + 6GBP/tonne","3 GBP/tonne","3 + 5GBP/tonne")
yr3<-c("2","2","2GBP/tonne + 2GBP/tonne","excluded")
sched<-data.frame(product,baseyr,yr1,yr2,yr3)
For each year, I need to classify each product by tax type in a new column based on the following conditions:
#number -> only numbers in the tax
#nonnumber -> numbers and strings in the tax
#mixed -> either two strings or number and string; the two strings are specified by a plus sign
#baseyr -> if the tax is "excluded" from the list, the tax to be used should be the value in base year, and the classification based on this
So if there are 3 years I need to generate 3 tax type columns. However the number of years changes randomly per dataset so I need to code with this in mind. My code is currently something like this:
yearnum<-3 #set number of years; it is between 1 and around 10 but there is no limit
schedule<-c(paste0("yr",1:yearnum)
tax<-c(paste0(schedule,"_tax")
for(i in 1:nrow(sched)){
#for each new tax type
for(j in tax){
#columns 3 to five where the yearly tax rates are
for(yr in 3:5){
#if the tax is excluded from the list, the base year tax should be used to determine the tax nature
if(sched[i,yr] =="excluded"){sched[i,yr] <- sched[i,baseyr]}
#if there is a plus sign it is a mixed tax
if(grepl("\\+",sched[i,yr])){sched[i,j] <- "mixed"}
#if it is not mixed but contains strings it is a nonnumber tax
if(grepl("[:alpha:]",sched[i,yr])){sched[i,j] <- "nonnumber"}
#finally if it is neither of the above it must be a number tax
if(is.na(sched[i,j])){sched[i,j] <- "number"}
}}}
NOTE: I do not know at the start how many years there will be in total; this has to be generated in the code. Any advice much appreciated, especially to avoid these for loops that don't seem to work properly for me.
The final output should be:
#so the output should be:
yr1_tax<-c("number","nonnumber","mixed","mixed")
yr2_tax<-c("number","mixed","nonnumber","mixed")
yr3_tax<-c("number","number","mixed","number")
#and the final dataframe:
sched<-data.frame(product,baseyr,yr1,yr2,yr3,yr1_tax,yr2_tax,yr3_tax)
You could use if_else to change all the excluded into baseyr. Then use case when with regular expressions as shown below:
sched %>%
mutate(
across(starts_with('yr'), ~ifelse(.x == 'excluded', baseyr, .x),
.names = '{.col}_tax'),
across(ends_with('tax'),
~case_when(grepl("^\\d+$", .x) ~ 'number',
grepl('^[^+]$', .x)~'nonnumber',
grepl('[+]', .x)~'mixed')))
product baseyr yr1 yr2 yr3 yr1_tax yr2_tax yr3_tax
1 01 10 5 3 2 number number number
2 02 8 GBP/tonne 5 GBP/tonne 3GBP/tonne + 6GBP/tonne 2 nonnumber mixed number
3 03 8GBP/tonne + 8GBP/tonne 5GBP/tonne + 10GBP/tonne 3 GBP/tonne 2GBP/tonne + 2GBP/tonne mixed nonnumber mixed
4 04 8 5 + 5GBP/tonne 3 + 5GBP/tonne excluded mixed mixed number
The regex is simplified in that I am checking for digits only (number), If there is + then mixed, then if no + then nonnumber.

How to do a row-specific iterative calculation as many times as the value of a specific variable the row has? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a monthly financial data. I want to do some iterative calculation for each individual row. I have a variable REMCOUPS, and each row has an integer value for REMCOUPS.
Thus, for each row, I want to do the iteration for the number of times indicated by the value of REMCOUPS.
My codes run as follows:
for(i in 1:REMCOUPS){ Price <- 0
if(i!=REMCOUPS)Price <- Price + (Bonds_2016_2020B$COUPON/2)/(1+(as.numeric(Bonds_2016_2020B$YIELD1.y/2))^i)
else Price <- Price + (Bonds_2016_2020B$COUPON/2)/(1+(as.numeric(Bonds_2016_2020B$YIELD1.y/2))^i)
+ 100/(1+(as.numeric(Bonds_2016_2020B$YIELD1.y/2))^i)
}
Bonds_2016_2020B is my dataset name. For each row, I want to do an interactive calculation starting from Price=0 and adding (COUPON/2)/(1+YIELD)^1, (COUPON/2)/(1+YIELD)^2 ... (COUPON/2)/(1+YIELD)^(the value-1 of REMCOUP), and finally, when iteration reaches REMCOUP times, I add
(COUPON/2)/(1+YIELD)^(the value of REMCOUP) and 100/(1+YIELD)^(the value of REMCOUP).
As I ran my codes, my first error was:
Error in REMCOUPS : object 'REMCOUPS' not found
How can I achieve this?
Update
Here is further information.
REMCOUPS or REMCOUPS1 is the number of remaining coupons.
CUSIP is the bond identifier.
So, for each month, DATE, I am going to calculate the present value of all the remaining future cash flows. The number of future cash flows is REMCOUPS (or REMCOUPS1).
Thus, looking at the picture, let's suppose we are going to calculate the present value of a Bond(cusip id: 00130HBT1) at 2018-05-31, which has 10 remaining cash flows (REMCOUPS1). Then, the formula is:
The present Value of that bond at 2018-05-31 = (Cash flow)/(1+YIELD)^1 + (Cash flow)/(1+YIELD)^2 + ... + (Cash flow)/(1+YIELD)^REMCOUP1 + 100/(1+YIELD)^REMCOUP1
100/(1+YIELD)^REMCOUP1, this is the final payment of the bond's written price.
Since REMCOUPS1 is 10 in this case, I should calculate and add up 10 items in the above (11 items including the final bond's price).
The main problem for me is that since I am a beginner in R coming from SAS, I am not understanding the logic of R yet.
Here's a tidyverse approach. I'm understanding these to be bonds which pay out semiannually.
First, here's three made-up bonds -- a premium bond, a par bond, and a discount bond.
Bonds_2016_2020B <- data.frame(BOND = 1:3,
COUPON = c(0.04, 0.06, 0.08),
YIELD1 = c(0.03, 0.06, 0.10),
REMCOUPS = 5:7)
Now, I'll make a line for each coupon payment, calculate the discount rate at that time, and apply that discount to any coupon or principal payments due. The price is the sum of those discounted cash flows.
library(tidyverse)
Bonds_2016_2020B %>%
# uncount, from the tidyr package in tidyverse, will copy each row REMCOUPS times
uncount(REMCOUPS, .id = "COUPNUM", .remove = FALSE) %>%
# each payment of coupon or principal will be discounted based on the # of semiannual periods. I'm assuming the first payment is 6 months away.
mutate(discount = 1/ ((1+YIELD1/2)^(COUPNUM)),
COUP_disc = discount * COUPON/2,
PRINC_disc = discount * if_else(COUPNUM == REMCOUPS, 1, 0)) %>%
group_by(BOND) %>%
summarize(price = 100*sum(COUP_disc + PRINC_disc)) %>%
mutate(price_showing_more_digits = format(price, nsmall = 6))
Result
# A tibble: 3 x 3
BOND price price_showing_more_digits
* <int> <dbl> <chr>
1 1 102. "102.391322"
2 2 100. "100.000000"
3 3 94.2 " 94.213627"
BTW, this matches exactly my check in Excel:

Time series with multiple stores multiple products

I have a 300 stores and 20000 products in my data set and I want to predict next 3 months sales forecast for each outlet each product level. And my data frame looks like this (sample) like this data frame I m taking from SQL Server 2016
Date outlet produ price
2019-Jan A W 10
2019-Feb A R 20
2019-Feb A W 15
2019-Jan B W 30
2019-Jan B F 40
2019-Feb B W 40
what I tried is get the single product observation entire time series and set to model and get the output
##getthe data set like this
outlet <-c('A','A','B','B')
produ <-c('W','R','W','F')
price <-c(10,20,30,40)
df <- data.frame(outlet,produ,price)
##tried to get single product
dpSingle <- dplyr::filter(df,df$produ == 'W')
data.ts=ts(Quntity, start=c(year,month), frequency=12)
fit_arima <- auto.arima(data.ts,d=1,D=1,stepwise = FALSE
,approximation = FALSE, trace = TRUE)
fcast<-forecast(fit_arima,h=24)
autoplot(fcast) + ggtitle("Forecasted for next 24 months")+
ylab("quntity")+xlab("Time in days")
print((exp(fcast$mean )))
but what i want is loop through a data frame and identify outlet first and then product and get particulars observations with features and pass to my time series model and get predictions individually for each outlet each product.

K means cluster analysis result using R

I tried a k means cluster analysis on a data set. The data set for customers includes the order number (the number of time that a customer has placed an order with the company;can be any number) ,order day (the day of the week the most recent order was placed; 0 to 6) and order hour (the hour of the day the most recent order was placed; 0 to 23) for loyal customers. I scaled the values and used.
# K-Means Cluster Analysis
fit <- kmeans(mydata, 3) # 5 cluster solution
# get cluster means
aggregate(mydata,by=list(fit$cluster),FUN=mean)
However, I am getting a few negative values as well. On the internet they say that this means the differences within group are greater than with that for other groups. However, I cannot understand how to interpret the output.
Can you please give an example of how to interpret?
Group.1 order_number order_dow order_hour_of_day
1 1 -0.4434400796 0.80263819338 -0.04766613741
2 2 1.6759259419 0.09051366962 0.07815242904
3 3 -0.3936748015 -1.00553744774 0.01377787416

Select single column from array returned by GetQuote extension

Ted Schlossmacher's free GetQuote extension for OpenOffice.org Calc allows users to access quotes for several types of symbols tracked by Yahoo! Finance. Specifically, the GETHISTORY() function returns a range of past and present quotes.
After installing the extension, try highlighting a 5-column range and then typing =GETHISTORY("PETR4.SA",1,TODAY()-1) (you might need to use semicolons instead of commas) and then pressing Ctrl+Shift+Return. That should provide you with date, open, high, low and close quotes for PETR4, the preferred stock of Brazilian oil giant Petrobras S.A.
My question is: how can I, in one cell, insert a formula that would return me the value of the 5th column of the above array?
This can be done with the INDEX function. You don't need to use ctrl+shift+enter for it to work as it does't return an array.
=INDEX(GETHISTORY("PETR4.SA",1,TODAY()-1),1,5)
The 2 end parameters are row,column, and are a 1-based index into the array.
More information about INDEX can be found on any Excel website, or in the LibreOffice Calc help at https://help.libreoffice.org/Calc/Spreadsheet_Functions#INDEX
Yesterday's closing price can be retrieved using a second argument, for example:
=GETQUOTE("TD.TO",21)
From the manual:
GETQUOTE can fetch 31 types of quotes. The types are numbered from 0 to 30. The function accepts these numbers as the second argument.
0 = Last traded price
1 = Change in price for the day
2 = Opening price of the day
3 = High price of the day
4 = Low price of the day
5 = Volume
6 = Average Daily Volume
7 = Ask Price
8 = Bid Price
9 = Book Value
10 = Dividend/Share
11 = Earnings/Share
12 = Earnings/Share Estimate Current Year
13 = Earnings/Share Estimate Next Year
14 = Earnings/Share Estimate Next Quarter
15 = 52-week low
16 = Change from 52-week low
17 = 52-week high
18 = Change from 52-week high
19 = 50-day Moving Average
20 = 200-day Moving Average
21 = Previous Close
22 = Price/Earning Ratio
23 = Dividend Yield
24 = Price/Sales
25 = Price/Book
26 = PEG Ratio
27 = Price/EPS Estimate Current Year
28 = Price/EPS Estimate Next Year
29 = Short Ratio
30 = 1-year Target Price
If you need only the latest price (which is the fifth field) I believe you can simply use:
=GETQUOTE("PETR4.SA")
I'm not certain this works to return the current price when markets are open, but it does return the last trade price when markets are closed.

Resources