i have a function that calculates portfolio variance but i am getting a bit of trouble here it bugs with (List.sum(eachlist)) / (decimal(List.length(eachlist))
)
for clarification, I am picking each list from the lists of lists and getting its mean/average and passing
Error FS0001 This expression was expected to have type
'List<decimal> -> 'a'
let assetreturns = [[1M;2M;3M];[4M;5M;6M];[7M;8M;9M];[10M;11M;12M]]
let weights = [1M;2M;3M;4M]
let var = assetreturns |> List.map(fun eachlist -> eachlist
|> (List.sum (eachlist)) / (decimal(List.length(eachlist)))
|> List.averageBy (fun x -> sqr (eachlist - x)))
let weightssquared = weights |> List.map(fun x -> x * x)
let porvarleft = List.zip weightssquared var |> List.sumBy(fun (a,b) -> a * b)
let porvarright = List.zip var weights |> List.sumBy(fun (a,b) -> a * b)
let portfoliovarright = porvarright * 2M
let portfoliovariance =(porvarleft + portfoliovarright)
portfoliovariance```
The problem is that (List.sum eachlist) / (decimal(List.length(eachlist))) returns a decimal and you are piping it into a function that expects a list (List.averageBy f). Try this instead:
let var =
assetreturns
|> List.map (fun eachlist ->
let avg = List.average eachlist
eachlist |> List.averageBy (fun x -> pown (x - avg) 2))