A Free Sample of Autocorrelation Consider a 1-dimensional, real-valued vector x that represents observations of some process measured at equally spaced intervals over time. We call x a time series. 

Let n denote the length of x and x̄ denote the arithmetic mean of x. The sample autocovariance function is defined as



for all -n &lt; h &lt; n. This measures the linear dependence between two points on the same series observed at different times.

The sample autocorrelation function, or ACF, is defined as



This measures the linear predictability of the series x at time t, which we denote xt, using only the value xt+h.

Note that these sample estimates do not match the naïve calculations based on the theoretical properties. That is, the sample autocorrelation function is not equal to the Pearson correlation coefficient of x with the h-step lag of x.

Task

Given an array x and a nonnegative integer h, print or return the first h+1 lag autocorrelations of x, beginning with lag 0. The lag autocorrelations are those corresponding to negative inputs in the above formulas.

You can assume that 0 &lt; h &lt; n, where n is the length of x, and that 2 &lt; n &lt; 256.

Output should be correct to within 1E-4. There are no restrictions on the use of built-in functions or running time.

Examples

h, x -&gt; output
--------------
5, [2.4, 2.4, 2.4, 2.2, 2.1, 1.5, 2.3, 2.3, 2.5, 2] -&gt; [1.00000000,  0.07659298, -0.06007802, -0.51144343, -0.02912874, -0.10468140]
1, [2134, 1863, 1877, 1877, 1492, 1249] -&gt; [1.0000000, 0.3343041]
2, [13067.3, 13130.5, 13198.4] -&gt; [1.0000000000, -0.0002854906, -0.4997145094]
R, 3 31 25 bytes

# changes the builtin to only return the acf
body(acf)=body(acf)[1:18]


Usage (returns an array with the autocorrelations)

(acf(c(2.4, 2.4, 2.4, 2.2, 2.1, 1.5, 2.3, 2.3, 2.5, 2),5))
# , , 1
#
#             [,1]
# [1,]  1.00000000
# [2,]  0.07659298
# [3,] -0.06007802
# [4,] -0.51144343
# [5,] -0.02912874
# [6,] -0.10468140


Background:

The 31 byte solution based on the original acf built in

function(n,h)c(acf(n,h,,F)$acf)


Note that the 3 byte option  acf is the originalbuilt in that will plot (and print to 3 digits) while returning the required autocorrelation as an element in a list.

usage

 acf(c(2.4, 2.4, 2.4, 2.2, 2.1, 1.5, 2.3, 2.3, 2.5, 2),5)


output:

#    Autocorrelations of series ‘c(2.4, 2.4, 2.4, 2.2, 2.1, 1.5, 2.3, 2.3, 2.5, 2)’, by lag
#
#     0      1      2      3      4      5 
# 1.000  0.077 -0.060 -0.511 -0.029 -0.105 


If we want the correlations to display to more than 3 decimal places then 28 bytes will do it (or 31, if we want to suppress the plot)

# will still plot (28 bytes)
function(n,h)c(acf(n,h)$acf)
# won't plot (31 bytes)
function(n,h)c(acf(n,h,,F)$acf)