R Aufgabe zum Time Value of Money!

Allgemeine Statistik mit R, die Test-Methode ist noch nicht bekannt, ich habe noch keinen Plan!

Moderatoren: EDi, jogo

Antworten
Bjoernbb31

R Aufgabe zum Time Value of Money!

Beitrag von Bjoernbb31 »

HAllo Zusammen,

im Rahmen eines Studienprojekts haben wir folgende AUfgabe (im Anhang) in R zu lösen.

Hier der bisher generierte Code:
CBIA_Projekt_R.pdf
komplette Aufgabenstellung
(50.96 KiB) 90-mal heruntergeladen

Code: Alles auswählen

rm(list=ls())
#Topic 6 - Time value of money, ivnestment project valuation (DCF, NPv, IRR)
# Aufgabe a)

# initial invest is 100, 5 years, semiannually 6%
initialAmount <- 100
FVsemi <- 0

z <- 0.03
t <- 10

for (i in 1:t) {
  FVsemi[i] <- initialAmount*((1+z)^i)
  
}
FVsemi[t]
#-----------------------------------------
# initial invest is 100, 5 years, quarterly 6%
initialAmount <- 100
FVquarterly <- 0
z <- 0.015
t <- 20

for (i in 1:t) {
  FVquarterly[i] <- initialAmount*((1+z)^i)
}
FVquarterly[t]

#-----------------------------------------
# initial invest is 100, 5 years, monthly 6%
initialAmount <- 100
FVmthly <- 0
z <- 0.005
t <- 60

for (i in 1:t) {
  FVmthly[i] <- initialAmount*((1+z)^i)
}
FVmthly[t]

#------------------------------------------
# initial invest is 100, 5 years, continuously 6%
initialAmount <- 100
FVcont <- 0
z <- 0.06
t <- 5

for (i in 1:t) {
  FVcont[i] <- initialAmount*(exp(z*i))
}
FVcont[t]


#---------------------------------------------------------------------------------------------------

#b)
#future value discrete payments
II <- 100                  #Initial investment
t <- 1                     # time horizon
rf <- 0.03                 #risk free rate  
pm <- 5                    # periodic payment

FV30disc <-0               # future value
CFdisc <- 0                # periodic cashflow

for (i in 29:t){
  
  CFdisc[i+1] <- pm*(1+0.03)^i
  
}

CFdisc[1]<-5

FV30disc <- sum(CFdisc[1:30])

#NPV when discount rate = 0.03
NPVdisc <-  (FV30disc / ((1+rf)^30)) - II
NPVdisc

#zweite (einfachere) Variante:
FV30disc2 <- function(x){ 
  5+(5*(1+x)*((((1+x)^29)-1)/x)) #geometrische Reihe für 29 Perioden, plus Schluss-CF in Höhe von 5 
  
}

FV30disc2(0.03)

(FV30disc2(0.03)/(1+rf)^30)-II


#future value continuously compounded
II <- 100
t <- 1
rf <- 0.03
pm <- 5

FVcont <-0
CFcont <- 0

for (i in t:29){
  
  CFcont[i+1] <- pm*(exp(rf*i))
  
}
CFcont
CFcont[1] <- 5
FVcont <- sum(CFcont[1:30]) #Future Value for continuously compounded

#calculating NPV
NPVcont <- FVcont/exp(0.03*30)-II
NPVcont


CFcontMatrix <- matrix(CFcont, nrow = 30, ncol = 1)
colnames(CFcontMatrix) <- c("Cash Flow")



#---------------------------------------------------------------------------------------------------
library(FinancialMath)
# c)
#Zerobond berechnung
rm(list=ls())

IRR <- 0

zerobond <- function(x, y, z){
  IRR <- (log(x/y))/z
  print(IRR)
}

Zerobond.IRR <- zerobond(100,80,10)

#second option via uniroot function
IRRuniroot <- function (x){
  (80*(1+x)^10)-100
}

uniroot(IRRuniroot, lower = 0.02, upper = 0.03)

#third option via IRR-function from package "FinancialMath")
IRR(-80, 100, 10)

#-------------------------------------
#IRRs for b)
# there are none because the NPVs are both negative!

#---------------------------------------------------------------------------------------------------

#d)

library(FinCal)

#NPV Project 1 & Project 2
NPV.Project1 <- NPV(-1000, c(1250, 10, 10, 20, 20), c(1:5), 0.06 ) 
NPV.Project2 <- NPV(-1000, c(-10, 0, 10, 20, 2000), c(1:5), 0.06 )
#Solution: NPV2 with 509 > NPV1 with 227 -> Pick Project 2


#IRR Project 1 & Project 2
IRR.Project1 <- IRR(-1000, c(1250, 10, 10, 20, 20), c(1:5))
IRR.Project2 <- IRR(-1000, c(-10, 0, 10, 20, 2000), c(1:5))
#Solution: IRR1 with 28% > IRR2 with 15% -> Pick Project 1

#PB Project 1 & Project 2
#PB Project 1
cf0 <- -1000
cf <- c(1250, 10, 10, 20, 20)
t <- 5

temp <- 0

#Calculating the Payback period
for (i in 1:5){
  temp[i]=sum(cf[1:i])
  }
for (i in 1:5){
  if ((temp[i]+cf0) > 0){
    payback.Project1 <- i
    break
  } 
  }

#PB Project 2
cf0 <- -1000
cf <- c(-10, 0, 10, 20, 2000)
t <- 5

temp <- 0

#Calculating the Payback period
for (i in 1:5){
  temp[i]=sum(cf[1:i])
}
for (i in 1:5){
  if ((temp[i]+cf0) > 0){
    payback.Project2 <- i
    break
  } 
}

#Solution: Period for PB with Project 1 is way smaller (1 year) than with Project 2 (5 years)
Wir sind für weitere Hilfestelungen oder Kritik dankbar;)

VG
Zuletzt geändert von Bjoernbb31 am Di Dez 19, 2017 4:47 pm, insgesamt 2-mal geändert.
jogo
Beiträge: 2085
Registriert: Fr Okt 07, 2016 8:25 am

Re: R Aufgabe zum Time Value of Money!

Beitrag von jogo »

Hallo Bjoern,

willkommen im Forum!
Das ist ja ein ganz schöner Packen, den Du uns hier vor die Füße knallst. Was sollen wir denn jetzt damit machen?
Doch sicherlich nicht, dass wir kontrollieren, ob Du Deine Aufgabe vollständig und korrekt gelöst hast, oder?

Deshalb habe ich mir nur ein kleines Codestück rausgenommen, um es zu kommentieren:

Code: Alles auswählen

# initial invest is 100, 5 years, monthly 6%
initialAmount <- 100
FVmthly <- 0
z <- 0.005
t <- 60

for (i in 1:t) {
  FVmthly[i] <- initialAmount*((1+z)^i)
}
FVmthly[t]
Dies geht auch anders (Nutze die Vektorisierung!):

Code: Alles auswählen

FVmthly <- initialAmount * ((1+z)^(1:t))
oder

Code: Alles auswählen

FVmthly <- initialAmount * cumprod(rep(1+z, t))
oder

Code: Alles auswählen

FVmthly <- rep(1+z, t)
FVmthly[1] <- FVmthly[1] * initialAmount
FVmthly <- cumprod(FVmthly)
Diese letzte Variante kommt mit möglichst wenig Multiplikationen aus (falls es auf Geschwindigkeit ankommt).

Nachtrag:
Meine Vermutung über die Geschwindigkeit der dritten Variante hat sich nicht bestätigt. Hier der/die/das Benchmark:

Code: Alles auswählen

microbenchmark(unit = "relative",
  Bjoern=
    for (i in 1:t) {
      FVmthly[i] <- initialAmount*((1+z)^i)
    }, 
  powVektor= FVmthly <- initialAmount * ((1+z)^(1:t)),
  cumProd= FVmthly <- initialAmount * cumprod(rep(1+z, t)),
  cumProd2= { FVmthly <- rep(1+z, t); FVmthly[1] <- FVmthly[1] * initialAmount; FVmthly <- cumprod(FVmthly) },
  cumprod3= {
    FVmthly <- numeric(t)
    FVmthly[1] <- initialAmount * (1+z)
    for (i in 2:t) FVmthly[i] <- FVmthly[i-1] * (1+z)
  },
  cumprod4= {
    FVmthly <- numeric(t)
    z1 <- 1+z
    FVmthly[1] <- initialAmount * z1
    for (i in 2:t) FVmthly[i] <- FVmthly[i-1] * z1
  }
)
mit dem Ergebnis:

Code: Alles auswählen

Unit: relative
      expr         min          lq        mean     median         uq         max neval  cld
    Bjoern 2507.490047 1386.565195 111.9723505 805.774709 654.919347 1.441120154   100    d
 powVektor    4.783886    3.259231   0.3179261   2.246342   2.094215 0.006356585   100 a   
   cumProd    1.000000    1.000000   1.0000000   1.000000   1.000000 1.000000000   100 a   
  cumProd2    1.713744    1.993029   0.2075977   1.585249   1.448152 0.007697196   100 a   
  cumprod3 2169.131754 1196.271366  98.0200287 696.166468 565.158545 2.295665360   100   c 
  cumprod4 1722.149763  949.896979  79.6782292 553.783070 448.473956 2.033065086   100  b  
Gruß, Jörg

p.s.:
Auch wäre es höflich von Dir gewesen, wenn Du erwähnt hättest, dass Du die gleiche Frage auch in einem anderen Forum gestellt hast:
http://www.r-forum.de/projektborse-f10/ ... -t994.html
consuli
Beiträge: 479
Registriert: Mo Okt 10, 2016 8:18 pm

Re: R Aufgabe zum Time Value of Money!

Beitrag von consuli »

Ich hätte ein paar Anregungen, falls erwünscht.
Irmgard.
Bjoernbb31

Re: R Aufgabe zum Time Value of Money!

Beitrag von Bjoernbb31 »

jogo hat geschrieben: Mi Dez 13, 2017 8:38 am Hallo Bjoern,

willkommen im Forum!
Das ist ja ein ganz schöner Packen, den Du uns hier vor die Füße knallst. Was sollen wir denn jetzt damit machen?
Doch sicherlich nicht, dass wir kontrollieren, ob Du Deine Aufgabe vollständig und korrekt gelöst hast, oder?

p.s.:
Auch wäre es höflich von Dir gewesen, wenn Du erwähnt hättest, dass Du die gleiche Frage auch in einem anderen Forum gestellt hast:
http://www.r-forum.de/projektborse-f10/ ... -t994.html
Hallo jogo,

erstmal vielen Dank für deine Hilfe ! Ich bin auch noch leicht überfordert mit der eigentlichen Aufgabe. Vielleicht hätte ich erst zu einem späteren Zeitpunkt in das Forum posten sollen...
consuli hat geschrieben: Mi Dez 13, 2017 9:05 pm Ich hätte ein paar Anregungen, falls erwünscht.
@consuli Ich bin über jegliche Anregungen dankbar !
Antworten