Search code examples
mathelixiraverageintegral

Calculate average values (temperature) based on values at specific times


Given: A set of temperatures with a timestamp each. The distance between the timestamps varies.

Assumption: I assume that the temperature went linearly from one measurement to the next.

Goal: I want to calculate the average temperature.

example graph showing the problem

What I got so far:

  • I need to use Integral calculation for that. Sadly the information has left my head. It has been a few years since school...
  • I could somehow create mathematical functions for each two timestamp-value pairs and then calculate the average value based on that.

I'm using elixir and postgresql. But any hint on how to approach this is in any language is very welcome. If there is a library which I missed or a general step-by-step approach. Maybe there is even a postgresql function?

I'm putting some example data here as well:

[{~N[2020-03-28 13:08:32], 23.1}, {~N[2020-03-28 13:10:00], 23.3}, {~N[2020-03-28 23:08:32], 6.3}, {~N[2020-03-29 00:00:32], 2.1}, {~N[2020-03-29 04:00:00], 3.8}]

Imagine I'd want to calculate the average temperature between ~N[2020-03-28 13:30:00] and ~N[2020-03-29 03:00:00] based on these values.

EDIT: My understanding of the calculation proposed by High Performance Mark.

triangle calculation

  1. Calculate the pink triangle
  2. Calculate the green square under the triangle
  3. Do that for all points
  4. Sum all of them
  5. Divide it by the time interval (x axis)

How do I get the edge cases?


Solution

  • If the temperature changes in a straight line within an interval, the average temperature in that interval is simply the average of the endpoints of the interval.

    For example, within this interval:

    {~N[2020-03-28 13:08:32], 23.1}, {~N[2020-03-28 13:10:00], 23.3}
    

    The average is (23.1 + 23.3)/2 = 23.2.

    Now, what if you have a period that consists of two or more intervals?

    You have to take a "weighted average": multiply the average of each interval with the length of the interval, add them together, and divide by the length of the entire period.

    Concrete example:

    Suppose you have 3 datapoints:

    • t=0 temperature=4
    • t=2 temperature=2
    • t=5 temperature=8

    This is two intervals. The first has length 2 and average temperature (4+2)/2 = 3. The second has length 3 and average temperature (2+8)/2 = 5.

    The average for the entire period is (2 * 3 + 3 * 5)/(2 + 3) = 4.2.