{"id":342,"date":"2022-02-09T17:34:14","date_gmt":"2022-02-09T09:34:14","guid":{"rendered":"https:\/\/www.fanyamin.com\/wordpress\/?p=342"},"modified":"2022-02-11T16:51:34","modified_gmt":"2022-02-11T08:51:34","slug":"%e5%9b%9e%e9%a1%be%e6%9c%80%e5%b0%8f%e4%ba%8c%e4%b9%98%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.fanyamin.com\/wordpress\/?p=342","title":{"rendered":"\u56de\u987e\u6700\u5c0f\u4e8c\u4e58\u6cd5"},"content":{"rendered":"<h1>\u6700\u5c0f\u4e8c\u4e58\u6cd5\u7684\u7528\u9014<\/h1>\n<p>\u6700\u5c0f\u4e8c\u4e58\u6cd5\u6765\u5e38\u5e38\u7528\u6765\u4f30\u8ba1\u7ebf\u6027\u56de\u5f52\u4e2d\u7684\u659c\u7387\uff0c\u4ee5\u4f5c\u7ebf\u6027\u6700\u5c0f\u4e8c\u4e58\u62df\u5408 linear least squares fit<\/p>\n<pre><code>res = ys - (inter + slope * xs)<\/code><\/pre>\n<p>res: \u6b8b\u5dee<br \/>\nys: \u56e0\u53d8\u91cf\u5e8f\u5217<br \/>\nxs: \u81ea\u53d8\u91cf\u5e8f\u5217<br \/>\ninter: \u622a\u8ddd<br \/>\nslope: \u659c\u7387<\/p>\n<p>\u6700\u597d\u662f\u627e\u5230\u5408\u9002\u7684 inter \u548c slope \u4f7f\u6b8b\u5dee\u7684\u7edd\u5bf9\u503c\u6700\u5c0f\uff0c\u5e38\u89c1\u7684\u505a\u6cd5\u662f\u4f7f\u5f97\u6b8b\u5dee\u7684\u5e73\u65b9\u548c\u6700\u5c0f, \u56e0\u4e3a\u5e73\u65b9\u548c\u4e0e\u6b8b\u5dee\u7684\u6b63\u8d1f\u503c\u65e0\u5173\uff0c\u5e76\u4f7f\u8f83\u5927\u7684\u6b8b\u5dee\u5177\u6709\u66f4\u591a\u7684\u6743\u91cd\u3002<\/p>\n<p>\u00b7\u00b7\u00b7<br \/>\nsum(res**2)<br \/>\n\u00b7\u00b7\u00b7<\/p>\n<h1>\u6700\u5c0f\u4e8c\u4e58\u6cd5\u8bf4\u660e<\/h1>\n<p>\u5177\u4f53\u7b97\u6cd5\u53c2\u89c1 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Numerical_methods_for_linear_least_square\">https:\/\/en.wikipedia.org\/wiki\/Numerical_methods_for_linear_least_square<\/a><br \/>\n\u548c <a href=\"https:\/\/zh.wikipedia.org\/wiki\/%E6%9C%80%E5%B0%8F%E4%BA%8C%E4%B9%98%E6%B3%95\">https:\/\/zh.wikipedia.org\/wiki\/%E6%9C%80%E5%B0%8F%E4%BA%8C%E4%B9%98%E6%B3%95<\/a><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.fanyamin.com\/wordpress\/wp-content\/uploads\/2022\/02\/image-1644569479579.png\" alt=\"file\" \/><\/p>\n<h2>\u7ebf\u6027\u51fd\u6570\u6a21\u578b<\/h2>\n<pre><code class=\"language-mathjax\">Q = \\sum_{i=1}^{n} \\ [y_i - f(\\vec{x}_i;\\hat{\\vec{\\beta}})]^2<\/code><\/pre>\n<pre><code class=\"language-mathjax\">y = \\beta_0 + \\beta_1x + \\varepsilon \\, .<\/code><\/pre>\n<pre><code class=\"language-mathjax\">Q = \\sum_{i=1}^{n} \\ [y_i - (\\hat{\\beta}_0 + \\hat{\\beta}_1x_i)]^2 \\, .<\/code><\/pre>\n<p>1) taking partial derivatives of Q with respect to \u03b20^ and \u03b2\u0302 1,<br \/>\n2) setting each partial derivative equal to zero, and<br \/>\n3) solving the resulting system of two equations with two unknowns<\/p>\n<pre><code class=\"language-mathjax\">\\hat{\\beta}_1 = \\frac{\\sum_{i=1}^{n} (x_i-\\bar{x})(y_i-\\bar{y})}{\\sum_{i=1}^{n} (x_i-\\bar{x})^2}<\/code><\/pre>\n<pre><code class=\"language-mathjax\">\\hat{\\beta}_0 = \\bar{y} - \\hat{\\beta}_1\\bar{x}<\/code><\/pre>\n<h1>\u793a\u4f8b\u4ee3\u7801<\/h1>\n<p>Think stats \u8fd9\u672c\u4e66\u4e2d\u7ed9\u4e86 python \u7684\u5b9e\u73b0<\/p>\n<p>\u53c2\u89c1 <a href=\"https:\/\/github.com\/AllenDowney\/ThinkStats2\/blob\/master\/thinkstats2\/thinkstats2.py\">https:\/\/github.com\/AllenDowney\/ThinkStats2\/blob\/master\/thinkstats2\/thinkstats2.py<\/a><\/p>\n<p>\u4ee3\u7801\u7247\u6bb5\u5982\u4e0b<\/p>\n<pre><code>\ndef LeastSquares(xs, ys):\n    &quot;&quot;&quot;Computes a linear least squares fit for ys as a function of xs.\n    Args:\n        xs: sequence of values\n        ys: sequence of values\n    Returns:\n        tuple of (intercept, slope)\n    &quot;&quot;&quot;\n    meanx, varx = MeanVar(xs)\n    meany = Mean(ys)\n\n    slope = Cov(xs, ys, meanx, meany) \/ varx\n    inter = meany - slope * meanx\n\n    return inter, slope\n\ndef MeanVar(xs, ddof=0):\n    &quot;&quot;&quot;Computes mean and variance.\n    Based on http:\/\/stackoverflow.com\/questions\/19391149\/\n    numpy-mean-and-variance-from-single-function\n    xs: sequence of values\n    ddof: delta degrees of freedom\n\n    returns: pair of float, mean and var\n    &quot;&quot;&quot;\n    xs = np.asarray(xs)\n    mean = xs.mean()\n    s2 = Var(xs, mean, ddof)\n    return mean, s2\n\ndef Mean(xs):\n    &quot;&quot;&quot;Computes mean.\n    xs: sequence of values\n    returns: float mean\n    &quot;&quot;&quot;\n    return np.mean(xs)\n\ndef Cov(xs, ys, meanx=None, meany=None):\n    &quot;&quot;&quot;Computes Cov(X, Y).\n    Args:\n        xs: sequence of values\n        ys: sequence of values\n        meanx: optional float mean of xs\n        meany: optional float mean of ys\n    Returns:\n        Cov(X, Y)\n    &quot;&quot;&quot;\n    xs = np.asarray(xs)\n    ys = np.asarray(ys)\n\n    if meanx is None:\n        meanx = np.mean(xs)\n    if meany is None:\n        meany = np.mean(ys)\n\n    cov = np.dot(xs-meanx, ys-meany) \/ len(xs)\n    return cov<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u5c0f\u4e8c\u4e58\u6cd5\u7684\u7528\u9014 \u6700\u5c0f\u4e8c\u4e58\u6cd5\u6765\u5e38\u5e38\u7528\u6765\u4f30\u8ba1\u7ebf\u6027\u56de\u5f52\u4e2d\u7684\u659c\u7387\uff0c\u4ee5\u4f5c\u7ebf\u6027\u6700\u5c0f\u4e8c\u4e58\u62df\u5408 linear least squares fit res = ys &#8211; (inter + slope * xs) res: \u6b8b\u5dee ys: \u56e0\u53d8\u91cf\u5e8f\u5217 xs: \u81ea\u53d8\u91cf\u5e8f\u5217 inter: \u622a\u8ddd slope: \u659c\u7387 \u6700\u597d\u662f\u627e\u5230\u5408\u9002\u7684 inter \u548c slope \u4f7f\u6b8b\u5dee\u7684\u7edd\u5bf9\u503c\u6700\u5c0f\uff0c\u5e38\u89c1\u7684\u505a\u6cd5\u662f\u4f7f\u5f97\u6b8b\u5dee\u7684\u5e73\u65b9\u548c\u6700\u5c0f, \u56e0\u4e3a\u5e73\u65b9\u548c\u4e0e\u6b8b\u5dee\u7684\u6b63\u8d1f\u503c\u65e0\u5173\uff0c\u5e76\u4f7f\u8f83\u5927\u7684\u6b8b\u5dee\u5177\u6709\u66f4\u591a\u7684\u6743\u91cd\u3002 \u00b7\u00b7\u00b7 sum(res**2) \u00b7\u00b7\u00b7 \u6700\u5c0f\u4e8c\u4e58\u6cd5\u8bf4\u660e \u5177\u4f53\u7b97\u6cd5\u53c2\u89c1 https:\/\/en.wikipedia.org\/wiki\/Numerical_methods_for_linear_least_square \u548c https:\/\/zh.wikipedia.org\/wiki\/%E6%9C%80%E5%B0%8F%E4%BA%8C%E4%B9%98%E6%B3%95 \u7ebf\u6027\u51fd\u6570\u6a21\u578b Q = \\sum_{i=1}^{n} \\ [y_i &#8211; f(\\vec{x}_i;\\hat{\\vec{\\beta}})]^2 y = \\beta_0 + \\beta_1x + \\varepsilon \\, [&hellip;] <a class=\"read-more\" href=\"https:\/\/www.fanyamin.com\/wordpress\/?p=342\" title=\"Permanent Link to: \u56de\u987e\u6700\u5c0f\u4e8c\u4e58\u6cd5\">&rarr;Read&nbsp;more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-342","post","type-post","status-publish","format-standard","hentry","category-13"],"_links":{"self":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/342"}],"collection":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=342"}],"version-history":[{"count":7,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/342\/revisions"}],"predecessor-version":[{"id":372,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/342\/revisions\/372"}],"wp:attachment":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}