-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathscriptSF.R
69 lines (53 loc) · 1.43 KB
/
scriptSF.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
library(PortfolioAnalytics)
# Use edhec data
data(edhec)
R <- edhec[,1:10]
# Dimensions of data
m <- nrow(R)
N <- ncol(R)
# Number of factors to use
k <- 1
##### Step 1 #####
fit <- statistical.factor.model(R, k)
names(fit)
beta <- fit$factor_loadings
f <- fit$factor_realizations
res <- fit$residuals
##### Step 2 #####
# Compute the moments of the factors and idiosyncratic risk factors
# Note: The idiosyncratic factors are the residuals in the model (i.e. the
# unexplained asset return variation)
# Check for equality with functions from Kris Boudt and functions I have
# included in the package
# residual moments
denom <- m - k - 1
stockM2 <- colSums(res^2) / denom
stockM3 <- colSums(res^3) / denom
stockM4 <- colSums(res^4) / denom
# Compute the centered factors
# f.centered <- center(f)
# factor moments
# scalar
factorM2 <- cov(f)
# scalar
factorM3 <- PerformanceAnalytics:::M3.MM(f)
# scalar
factorM4 <- PerformanceAnalytics:::M4.MM(f)
##### Step 3 #####
# Compute the covariance, coskewness, and cokurtosis estimates from the statistical
# factor model.
# covariance matrix
all.equal(
PortfolioAnalytics:::covarianceSF(beta, stockM2, factorM2),
extractCovariance(fit)
)
# coskewness matrix
all.equal(
PortfolioAnalytics:::coskewnessSF(beta, stockM3, factorM3),
extractCoskewness(fit)
)
# # cokurtosis matrix
all.equal(
PortfolioAnalytics:::cokurtosisSF(beta, stockM2, stockM4, factorM2, factorM4),
extractCokurtosis(fit)
)