Skip to content

2020.01.13

Standard deviation Source: adopted from here

Introduction

In a standard report on the performance of algo parent orders, the notional weighted average slippage is typically reported. It is fairly simple to calculate weighted average in q using the built-in function wavg. To be consistent, we should also report the notional weighted standard deviation of slippage. Here is the definition of weighted standard deviation.

Question

The function simSlippage simulates the notional and slippage of 10,000 orders.

simSlippage:{
    n:10000;

    system "S -314159";
    slippage:5-0.01*n?1000;

    system "S -314159";
    notional:10000+n?100000;

    ([] notional:notional;slippage:slippage)
  };
perfData:simSlippage[];

Write a function in q to calculate the notional weighted standard deviation of these 10,000 orders using the formula provided above.

Answer

One implementation is suggested as follows. Note the use of inline assignment of variable n and xdm. For curious reader, xdm means "x demeaned".

wsdev:{[w;x]
    $[1>=n:sum w<>0;:0f;sqrt (n%n-1)*w wavg xdm*xdm:x-w wavg x]
  };
exec wsdev[notional;slippage] from perfData