Name: [SUN Zhe] Student ID: [1155225796]
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# 加载 ggplot2 包
library(ggplot2)
# 固定参数
epsilon <- 0.01
eta <- 0.01
# 计算最小的 n
n <- ((1+epsilon)/(epsilon*epsilon)*log(1/eta))+1/epsilon
# 定义 p 值
p_values <- seq(0.1, 0.9, by = 0.1)
# 计算满足条件的概率
probabilities <- sapply(p_values, function(p) {
# 计算满足 |m/n - p| < epsilon 的概率
# 使用正态近似计算
z <- qnorm(1 - eta) # 计算 z 值
prob <- pnorm(p + epsilon, mean = p, sd = sqrt(p * (1 - p) / n)) -
pnorm(p - epsilon, mean = p, sd = sqrt(p * (1 - p) / n))
return(prob)
})
# 创建数据框
data <- data.frame(p = p_values, Probability = probabilities)
# 绘制图形
ggplot(data, aes(x = p, y = Probability)) +
geom_line(color = "blue") +
geom_hline(yintercept = 1 - eta, linetype = "dashed", color = "red") +
labs(title = "Probability that |m/n - p| < epsilon",
x = "Probability of Success (p)",
y = "Probability") +
theme_minimal()
(a).
library(dplyr)
cpi_data <- read.csv("~/Desktop/assignment1 /cpi (3).csv")
month_mapping <- c('Jan' = 1, 'Feb' = 2, 'Mar' = 3, 'Apr' = 4, 'May' = 5, 'Jun' = 6,
'Jul' = 7, 'Aug' = 8, 'Sep' = 9, 'Oct' = 10, 'Nov' = 11, 'Dec' = 12)
cpi_data$Month <- month_mapping[cpi_data$Month]
cpi_data <- na.omit(cpi_data)
monthly_stats <- cpi_data %>%
group_by(Month) %>%
summarise(
mean_cpi = mean(CPI),
sd_cpi = sd(CPI)
)
print(monthly_stats)
## # A tibble: 12 × 3
## Month mean_cpi sd_cpi
## <dbl> <dbl> <dbl>
## 1 1 4.06 4.36
## 2 2 4.02 4.33
## 3 3 3.99 4.25
## 4 4 3.97 4.30
## 5 5 3.93 4.29
## 6 6 3.92 4.28
## 7 7 3.94 4.41
## 8 8 3.88 4.24
## 9 9 3.90 4.27
## 10 10 3.79 4.12
## 11 11 3.80 4.09
## 12 12 3.87 4.13
(b).
library(ggplot2)
library(dplyr)
library(zoo)
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
cpi_data <- read.csv("~/Desktop/assignment1 /cpi (3).csv")
month_mapping <- c('Jan' = 1, 'Feb' = 2, 'Mar' = 3, 'Apr' = 4, 'May' = 5, 'Jun' = 6,
'Jul' = 7, 'Aug' = 8, 'Sep' = 9, 'Oct' = 10, 'Nov' = 11, 'Dec' = 12)
cpi_data$Month <- month_mapping[cpi_data$Month]
cpi_data <- na.omit(cpi_data)
cpi_data$Date <- as.Date(paste(cpi_data$Year, cpi_data$Month, "01", sep = "-"))
cpi_data$H1 <- ave(cpi_data$CPI, cpi_data$Year, FUN = function(x) zoo::rollmean(x, 6, fill = NA, align = "right"))
cpi_data$H2 <- ave(cpi_data$CPI, cpi_data$Year, FUN = function(x) zoo::rollmean(x, 6, fill = NA, align = "right"))
cpi_data_clean <- na.omit(cpi_data[, c("Date", "H1", "H2")])
ggplot(cpi_data_clean, aes(x = Date)) +
geom_line(aes(y = H1, color = "H1 (May to Oct)", linetype = "solid")) + # H1 with solid line
geom_line(aes(y = H2, color = "H2 (Nov to Apr)", linetype = "dashed")) + # H2 with dashed line
labs(title = "Time Series of H1 and H2", x = "Year", y = "Average CPI") +
scale_color_manual(values = c("green", "red")) + # Assigning colors to the lines
scale_linetype_manual(values = c("solid", "dashed")) + # Assigning line types
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5)) + # Center the title
theme(legend.title = element_blank()) + # Remove legend title
theme(legend.position = "bottom") + # Place legend at the bottom
scale_x_date(labels = scales::date_format("%Y"), breaks = "2 years") # Format x-axis with year labels
(a).
initial_loan_hkd <- 1.5e6 # 初始借款(港元)
interest_payment_1 <- 1873.9 # 第一次利息支付(港元)
interest_payment_2 <- 39767 # 第二次利息支付(港元)
usd_deposit_amount <- 193518.2 # 定期存款到期金额(美元)
loan_settlement_hkd <- 14656686 # 贷款结清金额(港元)
exchange_rates <- seq(7.75, 7.85, by = 0.005) # 汇率从7.75到7.85
irr_values <- numeric(length(exchange_rates))
for (i in 1:length(exchange_rates)) {
exchange_rate <- exchange_rates[i]
cash_flows <- c(-initial_loan_hkd,
interest_payment_1,
interest_payment_2,
usd_deposit_amount * exchange_rate - loan_settlement_hkd)
npv <- function(rate) {
sum(cash_flows / (1 + rate)^(0:3))
}
rates <- seq(0, 1, by = 0.0001)
npv_values <- sapply(rates, npv)
irr_values[i] <- rates[which.min(abs(npv_values))] * 100
}
plot(exchange_rates, irr_values, type = "l", col = "blue", lwd = 2,
xlab = "USD/HKD Exchange Rate", ylab = "Annualized IRR (%)",
main = "IRR for Different USD/HKD Exchange Rates")
(b).
# 设置现金流和概率
C0 <- -100 # 初始投资
C1_1 <- 70 # C1节点现金流
C1_2 <- 50 # C1节点现金流
C2_1 <- 80 # C2节点现金流
C2_2 <- 50 # C2节点现金流
# 转换概率
p_C1_1 <- 0.4
p_C1_2 <- 0.6
p_C2_1 <- 0.2
p_C2_2 <- 0.8
# 折现率
discount_rate <- 0.06
# 计算C1的期望现金流
E_C1 <- p_C1_1 * C1_1 + p_C1_2 * C1_2
# 计算C2的期望现金流(对于C1的两种情况)
E_C2_1 <- p_C2_1 * C2_1 + p_C2_2 * C2_2
E_C2_2 <- p_C2_1 * C2_1 + p_C2_2 * C2_2
# 计算期望现金流的NPV
NPV_C0 <- C0 / (1 + discount_rate)^0 # C0折现
NPV_C1 <- E_C1 / (1 + discount_rate)^1 # C1折现
NPV_C2_1 <- E_C2_1 / (1 + discount_rate)^2 # C2折现(C1=70)
NPV_C2_2 <- E_C2_2 / (1 + discount_rate)^2 # C2折现(C1=50)
# 计算总NPV
NPV_total <- NPV_C0 + NPV_C1 + NPV_C2_1 + NPV_C2_2
# 输出决策结果
if (NPV_total > 0) {
print("接受项目,净现值大于0")
} else {
print("拒绝项目,净现值小于0")
}
## [1] "接受项目,净现值大于0"
E(C1)=0.4×70+0.6×50=28+30=58
E(C2∣C1=70)=0.2×80+0.8×50=16+40=56
E(C2∣C1=50)=50
E(C2)=0.4×56+0.6×50=22.4+30=52.4
EPV= −100+54.72+46.64=1.36
Since the expected present value (EPV) is positive(EPV=1.36), according to the Hazen (2009) mean decision rule, the project should be accepted.
# Load necessary libraries
library(dplyr)
# Load the dataset
data <- read.csv("~/Desktop/assignment1 /return (1).csv")
# Separate the data for Benchmark and CUHKFund portfolios
benchmark_data <- data[data$Portfolio == "Benchmark", ]
cuhkfund_data <- data[data$Portfolio == "CUHKFund", ]
# Merge the benchmark and CUHKFund data on the 'Sector' column
merged_data <- merge(cuhkfund_data, benchmark_data, by = "Sector", suffixes = c("_portfolio", "_benchmark"))
# Calculate the Allocation Effect and Selection Effect for each sector
merged_data$Allocation_Effect <- (merged_data$Weight_portfolio - merged_data$Weight_benchmark) * merged_data$Return_benchmark
merged_data$Selection_Effect <- merged_data$Weight_portfolio * (merged_data$Return_portfolio - merged_data$Return_benchmark)
# Calculate Total Return for Portfolio and Benchmark
total_return_portfolio <- sum(merged_data$Weight_portfolio * merged_data$Return_portfolio) / 100
total_return_benchmark <- sum(merged_data$Weight_benchmark * merged_data$Return_benchmark) / 100
# Calculate Total Allocation Effect and Total Selection Effect
total_allocation_effect <- sum(merged_data$Allocation_Effect)
total_selection_effect <- sum(merged_data$Selection_Effect)
# Output the results
result <- list(
Total_Return_Portfolio = total_return_portfolio,
Total_Return_Benchmark = total_return_benchmark,
Total_Allocation_Effect = total_allocation_effect,
Total_Selection_Effect = total_selection_effect,
Allocation_Effect_By_Sector = merged_data[, c("Sector", "Allocation_Effect")],
Selection_Effect_By_Sector = merged_data[, c("Sector", "Selection_Effect")]
)
# Print results
print(result)
## $Total_Return_Portfolio
## [1] 1.32
##
## $Total_Return_Benchmark
## [1] 1.095
##
## $Total_Allocation_Effect
## [1] 37.5
##
## $Total_Selection_Effect
## [1] -15
##
## $Allocation_Effect_By_Sector
## Sector Allocation_Effect
## 1 Commerce -6.0
## 2 Finance 34.5
## 3 Properties 4.0
## 4 Utilities 5.0
##
## $Selection_Effect_By_Sector
## Sector Selection_Effect
## 1 Commerce -14
## 2 Finance -10
## 3 Properties 5
## 4 Utilities 4
Total Return Portfolio: 132.0%
Total Return Benchmark: 109.5%
Total Allocation Effect: 37.5%
Total Selection Effect: -15.0%
This means that the portfolio outperformed the benchmark due to the allocation decisions (37.5%), but the poor selection within sectors contributed negatively (-15.0%).
library(dplyr)
data <- read.csv("~/Desktop/assignment1 /data1 (1).csv")
head(data)
## Stock.Return Strategy.A Strategy.B Strategy.C
## 1 -0.39 0 0 1
## 2 2.25 0 0 1
## 3 1.66 0 0 1
## 4 -0.94 0 0 1
## 5 1.08 0 1 1
## 6 1.60 0 0 1
buy_and_hold_return <- mean(data$Stock.Return)
strategy_a_buy_days <- data[data$Strategy.A == 1, "Stock.Return"]
mean_strategy_a_buy <- mean(strategy_a_buy_days)
cat("Buy-and-Hold Mean Return:", buy_and_hold_return, "\n")
## Buy-and-Hold Mean Return: -0.1585317
cat("Strategy A Conditional Mean Return (Buy days):", mean_strategy_a_buy, "\n")
## Strategy A Conditional Mean Return (Buy days): -0.1308333
if (mean_strategy_a_buy > buy_and_hold_return) {
cat("Strategy A generates higher return on buy days than the buy-and-hold strategy.\n")
} else {
cat("Strategy A does not generate higher return on buy days than the buy-and-hold strategy.\n")
}
## Strategy A generates higher return on buy days than the buy-and-hold strategy.
Therefore,
The Buy-and-Hold Mean Return is approximately -0.1585317
The Strategy A Conditional Mean Return (Buy days) is approximately -0.1308333 Conclusion: Strategy A generates higher return on buy days than the buy-and-hold strategy.
(b).
strategy_b_buy_days <- data[data$Strategy.B == 1, "Stock.Return"]
mean_strategy_b_buy <- mean(strategy_b_buy_days)
cat("Strategy B Conditional Mean Return (Buy days):", mean_strategy_b_buy, "\n")
## Strategy B Conditional Mean Return (Buy days): 0.1352991
if (mean_strategy_b_buy > buy_and_hold_return) {
cat("Strategy B generates higher return on buy days than the buy-and-hold strategy.\n")
} else {
cat("Strategy B does not generate higher return on buy days than the buy-and-hold strategy.\n")
}
## Strategy B generates higher return on buy days than the buy-and-hold strategy.
Therefore,Strategy B Conditional Mean Return (Buy days): 0.1352991
Strategy B generates higher return on buy days than the buy-and-hold strategy.
(c).
strategy_a_returns <- data[data$Strategy.A == 1, "Stock.Return"]
strategy_b_returns <- data[data$Strategy.B == 1, "Stock.Return"]
strategy_c_returns <- data[data$Strategy.C == 1, "Stock.Return"]
anova_result <- aov(cbind(strategy_a_returns, strategy_b_returns, strategy_c_returns) ~ 1)
## Warning in cbind(strategy_a_returns, strategy_b_returns, strategy_c_returns):
## number of rows of result is not a multiple of vector length (arg 1)
summary(anova_result)
## Response strategy_a_returns :
## Df Sum Sq Mean Sq F value Pr(>F)
## Residuals 218 1131.8 5.1916
##
## Response strategy_b_returns :
## Df Sum Sq Mean Sq F value Pr(>F)
## Residuals 218 1169.5 5.3646
##
## Response strategy_c_returns :
## Df Sum Sq Mean Sq F value Pr(>F)
## Residuals 218 1182.4 5.424
Conclusion: The ANOVA test shows that there is no significant difference in the daily returns generated by the three strategies (A, B, and C). All three strategies appear to generate similar returns.