IncrementalMean.md (1485B)
1 # Incremental Mean 2 3 L4 4 5 **Definition:** Incremental mean is a mean calculation where we update the mean according to the next sample without having to calculate the mean by summing all priors. 6 7 This is often used with Monte Carlo Learning where we calculate the empirical mean (perceived mean) not by summing all returns and dividing by iterations, but instead by updating it each time it is visited only based on the change the current finding will make. 8 9 With incremental mean all we need to know is the prior mean, the current sample, and the total number of prior iterations. Obviously, with this information we could multiply the prior mean by the total number of prior iterations and then add the current and divide by total samples, but this is slow. Instead we calculate the incremental mean by adding 1/k * (return - prior mean) to the prior mean. 10 11 Here is a simple python implementation: 12 13 ```python 14 15 import numpy as np 16 arr = np.random.rand(10) 17 18 # compute mean normal way 19 def stdMean(priors): 20 mean = 0 21 for i in priors: 22 mean += i 23 mean = mean/len(priors) 24 return mean 25 26 # compute incremental mean 27 def incMeanCalc(priorMean, k, current): 28 return priorMean + (1/k * (current - priorMean)) 29 30 incMean = 0 31 32 for k in range(0,len(arr)): 33 if -len(arr) + k + 1 == 0: 34 normMean = stdMean(arr) 35 else: 36 normMean = stdMean(arr[:-len(arr) + k + 1]) 37 38 incMean = incMeanCalc(incMean, k + 1, arr[k]) 39 print(incMean) 40 print(normMean) 41 42 ```