{"id":13413740,"url":"https://github.com/VividCortex/ewma","last_synced_at":"2025-03-14T19:33:13.829Z","repository":{"id":9356264,"uuid":"11208890","full_name":"VividCortex/ewma","owner":"VividCortex","description":"Exponentially Weighted Moving Average algorithms for Go.","archived":false,"fork":false,"pushed_at":"2023-12-14T10:51:41.000Z","size":32,"stargazers_count":430,"open_issues_count":5,"forks_count":36,"subscribers_count":26,"default_branch":"master","last_synced_at":"2024-04-22T13:32:11.879Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/VividCortex.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2013-07-05T21:33:25.000Z","updated_at":"2024-04-15T17:02:45.000Z","dependencies_parsed_at":"2024-01-27T08:38:40.012Z","dependency_job_id":"195f5c40-f09d-4f07-9e58-53de4c4c64ef","html_url":"https://github.com/VividCortex/ewma","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VividCortex%2Fewma","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VividCortex%2Fewma/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VividCortex%2Fewma/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VividCortex%2Fewma/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/VividCortex","download_url":"https://codeload.github.com/VividCortex/ewma/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221498774,"owners_count":16833061,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T20:01:47.860Z","updated_at":"2024-10-26T05:31:19.840Z","avatar_url":"https://github.com/VividCortex.png","language":"Go","readme":"# EWMA\n\n[![GoDoc](https://godoc.org/github.com/VividCortex/ewma?status.svg)](https://godoc.org/github.com/VividCortex/ewma)\n![build](https://github.com/VividCortex/ewma/workflows/build/badge.svg)\n[![codecov](https://codecov.io/gh/VividCortex/ewma/branch/master/graph/badge.svg)](https://codecov.io/gh/VividCortex/ewma)\n\nThis repo provides Exponentially Weighted Moving Average algorithms, or EWMAs for short, [based on our\nQuantifying Abnormal Behavior talk](https://vividcortex.com/blog/2013/07/23/a-fast-go-library-for-exponential-moving-averages/).\n\n### Exponentially Weighted Moving Average\n\nAn exponentially weighted moving average is a way to continuously compute a type of\naverage for a series of numbers, as the numbers arrive. After a value in the series is\nadded to the average, its weight in the average decreases exponentially over time. This\nbiases the average towards more recent data. EWMAs are useful for several reasons, chiefly\ntheir inexpensive computational and memory cost, as well as the fact that they represent\nthe recent central tendency of the series of values.\n\nThe EWMA algorithm requires a decay factor, alpha. The larger the alpha, the more the average\nis biased towards recent history. The alpha must be between 0 and 1, and is typically\na fairly small number, such as 0.04. We will discuss the choice of alpha later.\n\nThe algorithm works thus, in pseudocode:\n\n1. Multiply the next number in the series by alpha.\n2. Multiply the current value of the average by 1 minus alpha.\n3. Add the result of steps 1 and 2, and store it as the new current value of the average.\n4. Repeat for each number in the series.\n\nThere are special-case behaviors for how to initialize the current value, and these vary\nbetween implementations. One approach is to start with the first value in the series;\nanother is to average the first 10 or so values in the series using an arithmetic average,\nand then begin the incremental updating of the average. Each method has pros and cons.\n\nIt may help to look at it pictorially. Suppose the series has five numbers, and we choose\nalpha to be 0.50 for simplicity. Here's the series, with numbers in the neighborhood of 300.\n\n![Data Series](https://user-images.githubusercontent.com/279875/28242350-463289a2-6977-11e7-88ca-fd778ccef1f0.png)\n\nNow let's take the moving average of those numbers. First we set the average to the value\nof the first number.\n\n![EWMA Step 1](https://user-images.githubusercontent.com/279875/28242353-464c96bc-6977-11e7-9981-dc4e0789c7ba.png)\n\nNext we multiply the next number by alpha, multiply the current value by 1-alpha, and add\nthem to generate a new value.\n\n![EWMA Step 2](https://user-images.githubusercontent.com/279875/28242351-464abefa-6977-11e7-95d0-43900f29bef2.png)\n\nThis continues until we are done.\n\n![EWMA Step N](https://user-images.githubusercontent.com/279875/28242352-464c58f0-6977-11e7-8cd0-e01e4efaac7f.png)\n\nNotice how each of the values in the series decays by half each time a new value\nis added, and the top of the bars in the lower portion of the image represents the\nsize of the moving average. It is a smoothed, or low-pass, average of the original\nseries.\n\nFor further reading, see [Exponentially weighted moving average](http://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average) on wikipedia.\n\n### Choosing Alpha\n\nConsider a fixed-size sliding-window moving average (not an exponentially weighted moving average)\nthat averages over the previous N samples. What is the average age of each sample? It is N/2.\n\nNow suppose that you wish to construct a EWMA whose samples have the same average age. The formula\nto compute the alpha required for this is: alpha = 2/(N+1). Proof is in the book\n\"Production and Operations Analysis\" by Steven Nahmias.\n\nSo, for example, if you have a time-series with samples once per second, and you want to get the\nmoving average over the previous minute, you should use an alpha of .032786885. This, by the way,\nis the constant alpha used for this repository's SimpleEWMA.\n\n### Implementations\n\nThis repository contains two implementations of the EWMA algorithm, with different properties.\n\nThe implementations all conform to the MovingAverage interface, and the constructor returns\nthat type.\n\nCurrent implementations assume an implicit time interval of 1.0 between every sample added.\nThat is, the passage of time is treated as though it's the same as the arrival of samples.\nIf you need time-based decay when samples are not arriving precisely at set intervals, then\nthis package will not support your needs at present.\n\n#### SimpleEWMA\n\nA SimpleEWMA is designed for low CPU and memory consumption. It **will** have different behavior than the VariableEWMA\nfor multiple reasons. It has no warm-up period and it uses a constant\ndecay.  These properties let it use less memory.  It will also behave\ndifferently when it's equal to zero, which is assumed to mean\nuninitialized, so if a value is likely to actually become zero over time,\nthen any non-zero value will cause a sharp jump instead of a small change.\n\n#### VariableEWMA\n\nUnlike SimpleEWMA, this supports a custom age which must be stored, and thus uses more memory.\nIt also has a \"warmup\" time when you start adding values to it. It will report a value of 0.0\nuntil you have added the required number of samples to it. It uses some memory to store the\nnumber of samples added to it. As a result it uses a little over twice the memory of SimpleEWMA.\n\n## Usage\n\n### API Documentation\n\nView the GoDoc generated documentation [here](http://godoc.org/github.com/VividCortex/ewma).\n\n```go\npackage main\n\nimport \"github.com/VividCortex/ewma\"\n\nfunc main() {\n\tsamples := [100]float64{\n\t\t4599, 5711, 4746, 4621, 5037, 4218, 4925, 4281, 5207, 5203, 5594, 5149,\n\t}\n\n\te := ewma.NewMovingAverage()  //=\u003e Returns a SimpleEWMA if called without params\n\ta := ewma.NewMovingAverage(5) //=\u003e returns a VariableEWMA with a decay of 2 / (5 + 1)\n\n\tfor _, f := range samples {\n\t\te.Add(f)\n\t\ta.Add(f)\n\t}\n\n\te.Value() //=\u003e 13.577404704631077\n\ta.Value() //=\u003e 1.5806140565521463e-12\n}\n```\n\n## Contributing\n\nWe only accept pull requests for minor fixes or improvements. This includes:\n\n* Small bug fixes\n* Typos\n* Documentation or comments\n\nPlease open issues to discuss new features. Pull requests for new features will be rejected,\nso we recommend forking the repository and making changes in your fork for your use case.\n\n## License\n\nThis repository is Copyright (c) 2013 VividCortex, Inc. All rights reserved.\nIt is licensed under the MIT license. Please see the LICENSE file for applicable license terms.\n","funding_links":[],"categories":["Science and Data Analysis","\u003cspan id=\"科学和数据分析-science-and-data-analysis\"\u003e科学和数据分析 Science and Data Analysis\u003c/span\u003e","科学与数据分析","科学和数据分析","科學及數據分析","数据分析与数据科学","Relational Databases","科学及数据分析"],"sub_categories":["Advanced Console UIs","HTTP Clients","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","HTTP客户端","高級控制台界面","查询语","交流","高级控制台界面"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FVividCortex%2Fewma","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FVividCortex%2Fewma","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FVividCortex%2Fewma/lists"}