2013-03-06 5 views
3

나는 python에 익숙하지 않고 lmfit 패키지를 사용하여 자신의 계산을 확인하려고 노력하지만 다음 테스트 (및 2)에 대해 데이터 오류 (sig)를 포함하는 방법에 대해서는 확실하지 않습니다. 내가 conf_interval2d 아래 그림 참조)으로 얻을 오류 :lmfit 최소 제곱 최소화에서 데이터에 오류를 포함하려면 어떻게해야합니까? lmfit의 conf_interval2d 함수에서이 오류는 무엇입니까?

import numpy as np 
    from lmfit import Parameters, Minimizer, conf_interval, conf_interval2d, minimize, printfuncs 


    x=np.array([ 0.18, 0.26, 1.14, 0.63, 0.3 , 0.22, 1.16, 0.62, 0.84,0.44, 1.24, 0.89, 1.2 , 0.62, 0.86, 0.45, 1.17, 0.59, 0.85, 0.44]) 
    data=np.array([ 68.59, 71.83, 22.52,44.587,67.474 , 55.765, 20.9,41.33783784,45.79 , 47.88, 6.935, 34.15957447,44.175, 45.89230769, 57.29230769, 60.8,24.24335594, 34.09121287, 42.21504003, 26.61161674]) 
    sig=np.array([ 11.70309409, 11.70309409, 11.70309409, 11.70309409,11.70309409, 11.70309409, 11.70309409, 11.70309409,11.70309409, 11.70309409, 11.70309409, 11.70309409,11.70309409, 11.70309409, 11.70309409, 11.70309409,11.70309409, 11.70309409, 11.70309409, 11.70309409]) 

    def residual(pars, x, data=None): 
     a=pars['a'].value 
     b=pars['b'].value 
     model = a + (b*x) 
     if data is None: 
      return model 
     return model-data 

    params=Parameters() 
    params.add('a', value=70.0) 
    params.add('b', value=40.0) 

    mi=minimize(residual, params, args=(x, data)) 
    #mi=minimize(residual, params, args=(x,), kws={'data': data})#is this more correct? 
    ci, trace = conf_interval(mi, trace=True) 

내가 계산 할 수 있도록)이 지금까지 잘 작동하지만 위의 질문으로, 어떻게 데이터 (sig_chla에 대한 오류를 포함 않는 가중치 카이 제곱 감소 ?

제 2 부 : 또한, 나는 신뢰 구간, XS, YS, 그리드 = conf_interval2d (MI, 'A', 'B', 20, 20)

을 그릴 수 있도록 다음을 사용하려고 할 때

*에 ValueError :

나는 다음과 같은 오류가 의도 (| 숨기기 캐시) | 작성하는 데 실패 옵션 array--는 차원을 정의해야하지만 (0)

또는

있어요 평가 일상 DGESV에 ameter 4 DGESV에서 잘못된 맥 OS BLAS 매개 변수 오류, 매개 변수 # 0 (사용할 수 없음)이었다 당신은 residual() 기능에서 오류가 직접 데이터를 무겁게한다 0

답변

4

입니다. (하지만 아주 쉽게 찾을 수 없습니다,)를 lmfit 문서에서

는 :

Note that the calculation of chi-square and reduced chi-square assume that the returned residual function is scaled properly to the uncertainties in the data. For these statistics to be meaningful, the person writing the function to be minimized must scale them properly.

그래도, 어떻게 그렇게 어렵지 않다.

If, however, the measurements are uncorrelated but have different uncertainties, a modified approach might be adopted. Aitken showed that when a weighted sum of squared residuals is minimized, is BLUE if each weight is equal to the reciprocal of the variance of the measurement.

그러나, lmfit는, 잔류, 제곱 잔류하지 취하는 그래서 대신에 당신이로 가져 scipy으로 (이 같은 것을해야

# This is what you do with no errorbars -> equal weights. 
    resids = model - data 
    return resids 

가는 : 위키 백과 weighted least-square fitting에 대한 항목에서 sp) :

# Do this to include errors as weights. 
    resids = model - data 
    weighted = sp.sqrt(resids ** 2/sig ** 2) 
    return weighted 

이렇게하면 제대로 가중 착용감을 제공해야합니다.

+0

여기서 오류의 의미는 무엇입니까? 그것은 변화입니까? 표준 편차 ?? – Mehdi

+0

죄송합니다.이 경우 "오류"는 표준 편차를 의미합니다. – Thriveth

+0

문서가 명시 적으로 잔류 함수가 잔차 만 반환해야한다고 명시 했으므로 나는 이것이 옳다고 생각하지 않습니다. – rhody