Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revisionLast revisionBoth sides next revision | ||
전산물리학:선형_회귀_분석_linear_regression_analysis [2022/01/16 12:11] – jonghoon | 전산물리학:선형_회귀_분석_linear_regression_analysis [2022/01/17 16:12] – jonghoon | ||
---|---|---|---|
Line 9: | Line 9: | ||
$$ \chi^2 = \sum_{i=1}^{N}\left(\frac{y_i - f(x_i)}{\sigma_i}\right)^2$$ | $$ \chi^2 = \sum_{i=1}^{N}\left(\frac{y_i - f(x_i)}{\sigma_i}\right)^2$$ | ||
- | Python의 경우 scipy의 curve_fitting 함수를 사용할 수 있고, C/C++의 경우 gsl(gnu scientific library)의 gsl_fit_linear 혹은 gsl_fit_wlinear를 사용할 수 있다. 데이터에 표준오차가 포함되어 있는 경우에는 wlinear를 포함되지 않은 경우에는 linear를 사용한다. | + | Python의 경우 scipy의 curve_fitting 함수를 사용할 수 있고, C/C++의 경우 gsl(gnu scientific library)의 gsl_fit_linear 혹은 gsl_fit_wlinear를 사용할 수 있다. 데이터에 표준오차가 포함되어 있는 경우에는 wlinear를, 포함되지 않은 경우에는 linear를 사용한다. |
+ | $$\chi^2 = w(y-f(x))^2; | ||
+ | 으로 계산하기 때문이다. | ||
+ | scipy를 쓸 때는 표준오차가 포함되어 있는 경우에는 absolute_sigma 옵션을 True로 줘야 한다. | ||
+ | ====== 실습 ======= | ||
다음의 데이터를 예로 각 언어로 적합(fitting)해보자. | 다음의 데이터를 예로 각 언어로 적합(fitting)해보자. | ||
Line 35: | Line 39: | ||
| 4.75 | 10.519384785632700 | 0.47616567472190800 | | | 4.75 | 10.519384785632700 | 0.47616567472190800 | | ||
+ | ===== Python ===== | ||
먼저 Python 코드는 아래와 같다. | 먼저 Python 코드는 아래와 같다. | ||
- | <Code:Python> | + | |
- | from scipy.optimize import | + | <code: |
import numpy as np | import numpy as np | ||
+ | from scipy.optimize import curve_fit | ||
func = lambda x, a, b: a*x + b #f(x) = ax + b | func = lambda x, a, b: a*x + b #f(x) = ax + b | ||
- | x = np.linspace(0, | + | x = np.linspace(0, |
y = np.array([1.5891700001638700, | y = np.array([1.5891700001638700, | ||
Line 56: | Line 62: | ||
popt, pcov = curve_fit(func, | popt, pcov = curve_fit(func, | ||
perr = np.sqrt(np.diag(pcov))# | perr = np.sqrt(np.diag(pcov))# | ||
- | chi2 = np.sum((y-func | + | chi2 = np.sum((y - func(x, |
+ | |||
+ | print(" | ||
+ | </ | ||
+ | |||
+ | ===== C++ ===== | ||
+ | 다음은 C++에서 gsl을 이용한 코드이다. | ||
+ | < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | using std:: | ||
+ | using std::pow; | ||
+ | using std:: | ||
+ | |||
+ | // #include < | ||
+ | // #include < | ||
+ | // #include < | ||
+ | // #include < | ||
+ | |||
+ | //using std:: | ||
+ | //using std:: | ||
+ | //using std:: | ||
+ | //using std:: | ||
+ | //using std:: | ||
+ | //using std:: | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | double x[] = {0.0, 0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.25, 3.5, 3.75, 4.0, 4.25, 4.5, 4.75}; | ||
+ | double y[] = {1.5891700001638700, | ||
+ | double sig[] = {0.5320185951347170, | ||
+ | |||
+ | //if saved file is existed | ||
+ | /* | ||
+ | string tmp; | ||
+ | std:: | ||
+ | fin.open(" | ||
+ | |||
+ | vector< | ||
+ | vector< | ||
+ | vector< | ||
+ | vector< | ||
+ | string sbf; | ||
+ | |||
+ | while(!fin.eof()) | ||
+ | { | ||
+ | getline(fin, | ||
+ | if(tmp == "" | ||
+ | istringstream ss(tmp); | ||
+ | while(getline(ss, | ||
+ | { | ||
+ | sv.emplace_bacK(); | ||
+ | } | ||
+ | sv.shrink_to_fit(); | ||
+ | |||
+ | xv.emplace_back(stod(sv[0])); | ||
+ | yv.emplace_back(stod(sv[1])); | ||
+ | sigv.emplace_back(stod(sv[2])); | ||
+ | |||
+ | sv.clear(); | ||
+ | sv.shrink_to_fit(); | ||
+ | } | ||
+ | xv.shrink_to_fit(); | ||
+ | yv.shrink_to_fit(); | ||
+ | sigv.shrink_to_fit(); | ||
+ | |||
+ | double x[xv.capacity()]; | ||
+ | double y[yv.capacity()]; | ||
+ | double sig[sig.capacity()]; | ||
+ | |||
+ | copy(xv.begin(), | ||
+ | copy(yv.begin(), | ||
+ | copy(sigv.begin(), | ||
+ | */ | ||
+ | |||
+ | for(int i = 0; i < sizeof(sig)/ | ||
+ | { | ||
+ | sig[i] = 1/sig[i]; | ||
+ | } | ||
+ | |||
+ | double a, b, cov00, cov01, cov11, chisq; | ||
+ | gsl_fit_wlinear(x, | ||
+ | cout << "y = " << a << "x + "<< | ||
+ | } | ||
+ | </ | ||
- | </ | + | ===== 결과 ===== |
+ | 각각의 결과는 다음과 같다 | ||
+ | $$\text{Python: | ||
+ | $$\text{C++: | ||
+ | 유효숫자는 c++ cout의 기본 자릿수인 6자리에 맞췄다. |