Incorrect Values for Standard Deviation on Market Page



  • THe values shown on the market page when you hover over a comodity is the average price and standard deviaton over the last 14 days . But It seems like the standard deviation calculation is absolutely wrong. The standard deviation is just a weighted average over the 14 days which is the wrong way to calculate standard deviaton over a series.

    Here is the proof

    0_1633932204191_f14bb65c-b38c-4bd0-9d39-5c8e86c46336-image.png

    Here is the function whose output is shown above

    const misc = (resourceType?: ResourceConstant) => {
        const data = Game.market.getHistory().filter(o => o.resourceType === resourceType);
        const totalVolume = data.reduce((sum, d) => sum + d.volume, 0);
        const avg = data.map(d => d.avgPrice * d.volume).reduce((sum, x) => sum + x, 0) / totalVolume;
        const wrongStd = data.map(d => d.stddevPrice * d.volume).reduce((sum, x) => sum + x, 0) / totalVolume;
        // This is the correct way to calculate standard deviation : https://stats.stackexchange.com/a/442050/143357
        const correctStd = Math.sqrt(data.map(d => (d.stddevPrice ** 2) * (d.volume - 1) + d.volume * ((avg - d.avgPrice) ** 2)).reduce((sum, x) => sum + x, 0) / (totalVolume - 1));
        console.log(`${avg.toFixed(3)} ± ${correctStd.toFixed(3)} | Wrong STD = ${wrongStd.toFixed(3)}`);
    };
    

    I believe this is a bug. You can take a look at this stackexchange question on the correct formulas to calculate standard deviation : https://stats.stackexchange.com/a/442050/143357