pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/TheAlgorithms/Python/pull/14895/files

ref="https://github.githubassets.com/assets/github-d59ba7b300e9b8d8.css" /> maths: add autocorrelation function by zain-cs · Pull Request #14895 · TheAlgorithms/Python · GitHub
Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions maths/autocorrelation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
Autocorrelation measures the correlation of a signal with a delayed
copy of itself. It is widely used in time series analysis, signal
processing, and statistics.

Reference: https://en.wikipedia.org/wiki/Autocorrelation
"""


def autocorrelation(data: list[float], lag: int) -> float:
"""
Calculate the autocorrelation of a time series at a given lag.

:param data: A list of numerical values representing the time series.
:param lag: The number of time steps to shift the series.
:return: The autocorrelation coefficient at the given lag.

>>> round(autocorrelation([1, 2, 3, 4, 5], 1), 4)
0.4
>>> round(autocorrelation([1, 2, 3, 4, 5], 0), 4)
1.0
>>> autocorrelation([1, 2, 3], 5)
Traceback (most recent call last):
...
ValueError: Lag must be less than the length of the data.
"""
if lag >= len(data):
raise ValueError("Lag must be less than the length of the data.")

n = len(data)
mean = sum(data) / n
variance = sum((x - mean) ** 2 for x in data) / n

if variance == 0:
raise ValueError("Variance of data is zero, autocorrelation undefined.")

covariance = (
sum((data[i] - mean) * (data[i - lag] - mean) for i in range(lag, n)) / n
)

return covariance / variance


if __name__ == "__main__":
import doctest

doctest.testmod()
data = [1, 2, 3, 4, 5, 4, 3, 2, 1]
for lag in range(5):
print(f"Lag {lag}: {autocorrelation(data, lag):.4f}")
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy