Get the optimal order AR spectrum by minimizing the BIC.

def optimal_AR_spectrum(dx, Y, ndegrees=None,return_min=True):
    '''
    Get the optimal order AR spectrum by minimizing the BIC.
    '''
    
    if ndegrees is None : ndegrees=len(Y)-ndegrees
    
    aicc=np.arange(ndegrees)
    aic=aicc.copy()
    bic=aicc.copy()
    tmpStr=[]
    for i in np.arange(1, ndegrees):
        dum=yule_walker_regression(dx,Y, i)
        tmpStr.append(dum)
        aicc[i-1]=(tmpStr[i-1])['esd'].model['aicc']
        aic[i-1]=(tmpStr[i-1])['esd'].model['aic']
        bic[i-1]=(tmpStr[i-1])['esd'].model['bic']
    
    if return_min : return np.argmin(bic)+1
    else : return {'aicc':aicc,'aic':aic,'bic':bic}