Comment coder selon S
Help R :: Coder sur R :: Votre 1er forum
Page 1 sur 1
Comment coder selon S
##########################1
##1
sharperatio<-function(r,rf){
sr<-(mean(r)-mean(rf))/sd(r)
return(sr)
}
##2
covars<-function(x){
M<-t(matrix(colMeans(x),ncol(x), nrow(x)))
return(t(x-M)%*%(x-M)/(nrow(x)-1))
}
covars(X)
##3
portret<-function(w,x){t(w)%*%r}
portsig<-function(w,x){
sqrt(t(w)%*%covars(x)%*%w)
}
##4
install.packages("tseries")
library("tseries")
GetData<-function(x){
get.hist.quote("x",quote="Close", compression="m", start="2000-01-01")
}
##5
Code.R<-{
FC<-read.csv(file=file.choose(), header=TRUE)
MKT<-FC$MKT
SMB<-FC$SMB
ss<-sharperatio(MKT,SMB)
A<-paste("le ratio de sharpe est:",ss, sep="")
print(A)
}
##################2
##1
BMW<-GetData("BMW.DE")
TEL<-GetData("TEF.MC")
LOR<-GetData("OR.PA")
UNI<-GetData("UNA.AS")
UNICRED<-GetData("UCG.MI")
EUROSTOX<-GetData("^STOXX50E")
FCT<-read.csv(file=file.choose(),header=TRUE)
head(FCT)
##2
class(FCT)
FCT<-as.matrix(FCT)
RBMW<-diff(log(BMW))
RTEL<-diff(log(TEL))
RLOR<-diff(log(LOR))
RUNI<-diff(log(UNI))
RUNICRED<-diff(log(UNICRED))
REUSTOX<-diff(log(EUROSTOX))
stocks<-cbind(RBMW,RTEL,RLOR,RUNI,RUNICRED,REUSTOX)
head(stocks)
class(stocks)
stocks<-as.matrix(stocks)
install.packages('zoo')
library('zoo')
stocks<-na.locf(stocks)
##3
install.packages("Quandl")
library("Quandl")
#code quandl
RF<-RF[order(RF[,1]),]
RF[,2]<-RF[,2]/100
prime-actif<-stocks-RF[,2]
capm1<-lm(prime-actif[,2]~prime-actif[,6])
capm2<-lm(prime-actif[,3]~prime-actif[,6])
capm3<-lm(prime-actif[,4]~prime-actif[,6])
capm4<-lm(prime-actif[,5]~prime-actif[,6])
capm5<-lm(prime-actif[,1]~prime-actif[,6])
summary(capm1)
summary(capm1)$r.squared
##4
ff<-lm(prime-actif~FCT)
summary(ff)
FF <- get.hist.quote("FFIDX",quote="Close",start="2001-01-01")
plot(FF)
#Vérifier la classe et le type de données des variables {FF} et {Factors}.
class(stocks)
class(FCT)
#Estimer un modèle de Fama-French à 3 facteurs
FCT <- zoo(FCT,order.by=as.Date(rownames(FCT)))
plot(FCT,type='l')
FCT$MktRf <- FCT$Mkt-FCT$RF #A vérifier si c'est nécessaire!
DF <- FCT #Juste un nom plus court pour ce data frame
summary(reg <- lm(DF$FFund~DF$MktRf+DF$SMB+DF$HML))
##5
CAPM<-as.matric(summary(capm1)$coefficients,summary(capm1)$r.squared
FF<-cbind(summary(fama)$coefficients,summary(fama)$r.squared)
######################3
##1
install.packages("stats")
library("stats")
install.packages("urca")
library("urca")
CAC<-GetData("^FCHI")
DLCAC<-diff(log(CAC))
head(DLCAC)
##2
acf(CAC)
pacf(CAC)
acf(LCAC)
pacf(LCAC)
?ur.df
summary(ur.df(CAC, type ="trend", lags = 1, selectlags = c("Fixed", "AIC", "BIC")))
summary(ur.df(CAC, type ="drift", lags = 1, selectlags = c("Fixed", "AIC", "BIC")))
summary(ur.df(CAC, type ="none", lags = 1, selectlags = c("Fixed", "AIC", "BIC")))
summary(ur.df(CAC))
summary(ur.df(LCAC))
##3
AR<-arima(LCAC,c(1,0,0))
AR
MA<-arima(LCAC,c(0,0,1))
MA
ARMA<-arima(LCAC,c(1,0,1))
ARMA
ARMA22<-arima(LCAC,c(2,0,2))
ARMA12<-arima(LCAC,c(1,0,2))
ARMA21<-arima(LCAC,c(2,0,1))
####################4
##1
rstock1<-na.locf(rstock,na.rm =TRUE,fromLast=TRUE)
head(rstock1)
sup_na<-function(x){
for(j in 1:dim(x)[2]){
for(i in dim(x)[1]:1){
if (is.na(x[i,j])){x[i,j]<-x[i+1,j]}
}
}
return(x)
}
rstock2<-sup_na(rstock)
S<-cov(stocks2)
##2
install.packages("quadprog")
library("quadprog")
Dmat<-S #covariance
dvec<-as.matrix(colMeans(rstock2))##rdt
amat<-rbind(rep(1,5),diag(5))
Amat<-t(amat)##contrainte
bvec<-c(1,rep(0,5))
result<-solve.QP(Dmat,dvec,Amat,bvec,meq=1,factorized=FALSE)
results$solution
portret(results$solution,dvec)
ou
Amat <- rbind(rep(1,N),diag(N))
Amat
bvec <- matrix(c(1, rep(0,N)),4,1)
res <- solve.QP(SIGMA, RET, t(Amat), bvec, meq = 1)
##3
opt <- function(lambda){solve.QP(lambda*SIGMA, RET, t(Amat), bvec, meq = 1)$solution}
lambda <- seq(0.5,20,1)
lambda
L <- length(lambda)
Wopt <- t(sapply(lambda,opt))
Wopt
vol.port <- function(w,s){sqrt((t(w)%*%s)%*%w)}
ret.port <- function(w,r){t(w)%*%r}
RR <- array()
SS <- array()
for (l in seq(L)){
SS[l] <- vol.port(Wopt[l,],SIGMA)
RR[l] <- ret.port(Wopt[l,],RET)
}
RR
SS
plot(SS,RR,type="l")
##1
sharperatio<-function(r,rf){
sr<-(mean(r)-mean(rf))/sd(r)
return(sr)
}
##2
covars<-function(x){
M<-t(matrix(colMeans(x),ncol(x), nrow(x)))
return(t(x-M)%*%(x-M)/(nrow(x)-1))
}
covars(X)
##3
portret<-function(w,x){t(w)%*%r}
portsig<-function(w,x){
sqrt(t(w)%*%covars(x)%*%w)
}
##4
install.packages("tseries")
library("tseries")
GetData<-function(x){
get.hist.quote("x",quote="Close", compression="m", start="2000-01-01")
}
##5
Code.R<-{
FC<-read.csv(file=file.choose(), header=TRUE)
MKT<-FC$MKT
SMB<-FC$SMB
ss<-sharperatio(MKT,SMB)
A<-paste("le ratio de sharpe est:",ss, sep="")
print(A)
}
##################2
##1
BMW<-GetData("BMW.DE")
TEL<-GetData("TEF.MC")
LOR<-GetData("OR.PA")
UNI<-GetData("UNA.AS")
UNICRED<-GetData("UCG.MI")
EUROSTOX<-GetData("^STOXX50E")
FCT<-read.csv(file=file.choose(),header=TRUE)
head(FCT)
##2
class(FCT)
FCT<-as.matrix(FCT)
RBMW<-diff(log(BMW))
RTEL<-diff(log(TEL))
RLOR<-diff(log(LOR))
RUNI<-diff(log(UNI))
RUNICRED<-diff(log(UNICRED))
REUSTOX<-diff(log(EUROSTOX))
stocks<-cbind(RBMW,RTEL,RLOR,RUNI,RUNICRED,REUSTOX)
head(stocks)
class(stocks)
stocks<-as.matrix(stocks)
install.packages('zoo')
library('zoo')
stocks<-na.locf(stocks)
##3
install.packages("Quandl")
library("Quandl")
#code quandl
RF<-RF[order(RF[,1]),]
RF[,2]<-RF[,2]/100
prime-actif<-stocks-RF[,2]
capm1<-lm(prime-actif[,2]~prime-actif[,6])
capm2<-lm(prime-actif[,3]~prime-actif[,6])
capm3<-lm(prime-actif[,4]~prime-actif[,6])
capm4<-lm(prime-actif[,5]~prime-actif[,6])
capm5<-lm(prime-actif[,1]~prime-actif[,6])
summary(capm1)
summary(capm1)$r.squared
##4
ff<-lm(prime-actif~FCT)
summary(ff)
FF <- get.hist.quote("FFIDX",quote="Close",start="2001-01-01")
plot(FF)
#Vérifier la classe et le type de données des variables {FF} et {Factors}.
class(stocks)
class(FCT)
#Estimer un modèle de Fama-French à 3 facteurs
FCT <- zoo(FCT,order.by=as.Date(rownames(FCT)))
plot(FCT,type='l')
FCT$MktRf <- FCT$Mkt-FCT$RF #A vérifier si c'est nécessaire!
DF <- FCT #Juste un nom plus court pour ce data frame
summary(reg <- lm(DF$FFund~DF$MktRf+DF$SMB+DF$HML))
##5
CAPM<-as.matric(summary(capm1)$coefficients,summary(capm1)$r.squared
FF<-cbind(summary(fama)$coefficients,summary(fama)$r.squared)
######################3
##1
install.packages("stats")
library("stats")
install.packages("urca")
library("urca")
CAC<-GetData("^FCHI")
DLCAC<-diff(log(CAC))
head(DLCAC)
##2
acf(CAC)
pacf(CAC)
acf(LCAC)
pacf(LCAC)
?ur.df
summary(ur.df(CAC, type ="trend", lags = 1, selectlags = c("Fixed", "AIC", "BIC")))
summary(ur.df(CAC, type ="drift", lags = 1, selectlags = c("Fixed", "AIC", "BIC")))
summary(ur.df(CAC, type ="none", lags = 1, selectlags = c("Fixed", "AIC", "BIC")))
summary(ur.df(CAC))
summary(ur.df(LCAC))
##3
AR<-arima(LCAC,c(1,0,0))
AR
MA<-arima(LCAC,c(0,0,1))
MA
ARMA<-arima(LCAC,c(1,0,1))
ARMA
ARMA22<-arima(LCAC,c(2,0,2))
ARMA12<-arima(LCAC,c(1,0,2))
ARMA21<-arima(LCAC,c(2,0,1))
####################4
##1
rstock1<-na.locf(rstock,na.rm =TRUE,fromLast=TRUE)
head(rstock1)
sup_na<-function(x){
for(j in 1:dim(x)[2]){
for(i in dim(x)[1]:1){
if (is.na(x[i,j])){x[i,j]<-x[i+1,j]}
}
}
return(x)
}
rstock2<-sup_na(rstock)
S<-cov(stocks2)
##2
install.packages("quadprog")
library("quadprog")
Dmat<-S #covariance
dvec<-as.matrix(colMeans(rstock2))##rdt
amat<-rbind(rep(1,5),diag(5))
Amat<-t(amat)##contrainte
bvec<-c(1,rep(0,5))
result<-solve.QP(Dmat,dvec,Amat,bvec,meq=1,factorized=FALSE)
results$solution
portret(results$solution,dvec)
ou
Amat <- rbind(rep(1,N),diag(N))
Amat
bvec <- matrix(c(1, rep(0,N)),4,1)
res <- solve.QP(SIGMA, RET, t(Amat), bvec, meq = 1)
##3
opt <- function(lambda){solve.QP(lambda*SIGMA, RET, t(Amat), bvec, meq = 1)$solution}
lambda <- seq(0.5,20,1)
lambda
L <- length(lambda)
Wopt <- t(sapply(lambda,opt))
Wopt
vol.port <- function(w,s){sqrt((t(w)%*%s)%*%w)}
ret.port <- function(w,r){t(w)%*%r}
RR <- array()
SS <- array()
for (l in seq(L)){
SS[l] <- vol.port(Wopt[l,],SIGMA)
RR[l] <- ret.port(Wopt[l,],RET)
}
RR
SS
plot(SS,RR,type="l")
Sujets similaires
» Comment coder selon O
» Comment coder trois problèmes selon J
» Comment coder quatre exercice selon J
» Comment coder deux exercice selon J
» Comment coder plein d'exercice selon H
» Comment coder trois problèmes selon J
» Comment coder quatre exercice selon J
» Comment coder deux exercice selon J
» Comment coder plein d'exercice selon H
Help R :: Coder sur R :: Votre 1er forum
Page 1 sur 1
Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum
|
|