Common-size Balance Sheet
///////////////////////////////

infile<-"http://datayyy.com/data_R/ibmBS.RData"
a<-load(url(infile))
print(a)
head(.x)
item<-"total assets"
TA<-.x[toupper(rownames(.x))==toupper(item),]
TA2<-as.numeric(TA)
result <- sweep(.x, 2,TA2, FUN = "/")
commonBS<- as.data.frame(result)

/////////////////////////////////

SEC filings
///////////////////////////////
SEC EGDAR   
    https://www.sec.gov/edgar/searchedgar/companysearch.html

SEC index   
    https://www.sec.gov/Archives/edgar/full-index/
/////////////////////////////////

Sharpe ratio
/////////////////////////////////////


                   R_mean - Rf
 Sharpe ratio =   ---------------
                       sigma 

 Sigma is the standard deviation of returns. 
   This is used to measure the total risk. 

/////////////////////////////////////

LPSD
///////////////////////////////////////////
 LPSD stands for Lower Partial Standard Deviation. 
     Assume that we have n returns, R1, R2, ..., Rn
     For a given benchmark return R_benchmark, we drop all
        the returns that are higher than this benchmark return. 


           (R1-R_mean)^2 + .... + (Rn-R_mean)^2
   var2 =  -----------------------------------   if(Ri<R_benchmark)   (3)  
                       n-1

   LPSD= sqrt(var2)                                                   (4)

/////////////////////////////////////

VBA 4 LPSD
/////////////////////////////////////

Function LPSD(returns As Range, MAR As Variant) As Variant
   Dim n As Integer
   Dim i As Integer
   Dim avgReturn As Double
   Dim mm2d As Double
   Dim downDev As Double
 
   n = returns.Rows.Count
   avgReturn = WorksheetFunction.Average(returns)
   moment2d = 0
   For i = 1 To n
      If returns(i) - MAR < 0 Then
         mm2d = mm2d + ((returns(i) - MAR) ^ 2)
    End If
  Next
  downDev = Sqr(mm2d / (n - 1))
  If downDev > 0 Then
     LPSD = downDev
  Else
     LPSD = "undefined"
  End If
End Function

Function LPSDhelp() As String
     LPSDhelp= "usage:  =LPSD(returnMatrix, benchmarkRate)"
End Function

/////////////////////////////////////