braverock / PerformanceAnalytics

211 stars 105 forks source link

function SemiVariance looks off the target #189

Open kwhkim opened 1 month ago

kwhkim commented 1 month ago

Description

As I look at the source code, https://github.com/braverock/PerformanceAnalytics/blob/master/R/SemiDeviation.R SemiVariance should be return(DownsideDeviation(R, MAR=mean(R), method="subset")^2) not return(DownsideDeviation(R, MAR=mean(R), method="subset"))

According to the definition https://www.investopedia.com/terms/s/semivariance.asp

Minimal, reproducible example

x = c(0.3, 0.2, 0.1, 0, -0.1, -0.2, -0.3, -0.4)
SemiVariance(x) #0.229129

y = x -mean(x)
y2 = ifelse(y>0, 0, y)
y3 = y2[y2<0]

sum(y3^2)/length(y3) # 0.0525
sqrt(sum(y3^2)/length(y3)) # 0.229129
DownsideDeviation(x, MAR=mean(x), method='subset') # 0.229129