RobTillaart / RunningAverage

Arduino library to calculate the running average by means of a circular buffer.
MIT License
53 stars 10 forks source link

weird output result get when call StandardDeviation() without Average() #25

Closed ghost closed 1 year ago

ghost commented 1 year ago

Note: follow up of - https://github.com/RobTillaart/Statistic/issues/20


Get weird result when calculate standard deviation without average function in sketch.

ESP_LOGD("Stats", "%f %f", stats.getStandardDeviation(), stats.getCount());
[output]
[01:14:25][D][Stats:072]: nan -5486124068793694774077193376186634490938385057925026367753787985335901674201208705258107428552676834622700901594284127560530828493162262565205269470179449410856558896118393471584152670587577693885047222545890015691339878440394965043791461872188297303737712264679017607178892013979610612203518046997839872.00
ESP_LOGD("Stats", "%.2f %.2f %.2f", stats.getStandardDeviation(), stats.getCount(), stats.getAverage());
[output]
[01:20:26][D][Stats:072]: 0.05 0.00 0.70
RobTillaart commented 1 year ago

Think you need %ld instead of %f. (int instead of float) for the getCount() call.

ghost commented 1 year ago

Think you need %ld instead of %f. (int instead of float) for the getCount() call.

No matter what i use float or int, library work without getAverage on some esphome componet and not with others, maybe its esphome issue rather than library. Linking this issue with esphome https://github.com/esphome/issues/issues/4657

ghost commented 1 year ago

Think you need %ld instead of %f. (int instead of float) for the getCount() call.

ESP_LOGD("Stats", "%f %i", stats.getStandardDeviation(), stats.getCount());
[output]
[02:05:47][D][Stats:072]: nan 5
RobTillaart commented 1 year ago

So count is 5, that means the NAN is not caused by divide by zero (_count is used in a division at some point)

What is the output if you swap the two parameters in the print? First count and then stddev?

Hypothesis 1 The order of the calls could initialize an internal variable needed by std dev.

RobTillaart commented 1 year ago

test for hypothesis above.

if you change line 150 in RunningAverage.cpp

  float average = getFastAverage();

into

  float average = getAverage();

does your code print correctly then?

(I try to find out where the NAN is coming from, I cannot replicate so far on a "normal" Arduino).

RobTillaart commented 1 year ago

Linking this issue with esphome https://github.com/esphome/issues/issues/4657

is closed without solution.

ghost commented 1 year ago

Esphome doc's already mentioned that external component can show unknown behaviour, that why they closed that https://github.com/esphome/issues/issues/4657. Maybe it was esphome issue, i have another proof this library also provide std_dev https://github.com/MajenkoLibraries/Average but after last Esphome update it's break and also return nan value. For last 2 Year i was using MajenkoLibraries/Average for std_dev calculation.

[optional]

  1. Now you can only do modify RunningAverage to stricted class based, so they work without any issue with framework like esphome. https://esphome.io/components/sensor/custom.html this can help you for better understanding.
  2. I already request for standard deviation calculation in esphome https://github.com/esphome/feature-requests/issues/2295.
RobTillaart commented 1 year ago

Esphome doc's already mentioned that external component can show unknown behaviour

It means that fixing or adapting code gives no guarantees whatsoever for the future.

https://github.com/MajenkoLibraries/Average uses roughly the same method to calculate the stddev() (of course). So cause is likely to be similar. That is why I asked to change line 150.

Now you can only do modify RunningAverage to stricted class based ...

Feel free to make a PR.

I already request for standard deviation calculation in esphome

Good action.