Skip to content

Instantly share code, notes, and snippets.

@CaptainZidgel
Created August 23, 2020 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CaptainZidgel/0d648936ee96854bcfcd535ee4de7a31 to your computer and use it in GitHub Desktop.
Save CaptainZidgel/0d648936ee96854bcfcd535ee4de7a31 to your computer and use it in GitHub Desktop.
Kotlin Expanding Mean
//An expanding mean function created as my own implementation of pandas' .expanding(window).mean()
//Only thing missing is the window size option.
fun expandingMean (data: List<Float>): MutableList<Float> {
var result = mutableListOf<Float>()
var i = 0
for (value in data.listIterator()) {
result.add(data.slice(0..i).average().toFloat())
i += 1
}
return result
}
fun main (args: Array<String>) {
val arr = mutableListOf<Float>()
for (i in 1..25) {
arr.add(i.toFloat())
}
val em = expandingMean(arr)
print("$em")
}
//input a range of floats 1-25 (inclusive)
//output is [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0, 10.5, 11.0, 11.5, 12.0, 12.5, 13.0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment