{"id":2153,"date":"2016-02-22T06:00:57","date_gmt":"2016-02-22T04:00:57","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=2153"},"modified":"2022-03-26T11:33:37","modified_gmt":"2022-03-26T09:33:37","slug":"regresion-lineal-1","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/regresion-lineal-1\/","title":{"rendered":"Regresi\u00f3n lineal"},"content":{"rendered":"<p>Dadas dos listas de valores<\/p>\n<pre lang=\"text\">\n   xs = [x(1), x(2), ..., x(n)]\n   ys = [y(1), y(2), ..., y(n)]\n<\/pre>\n<p>la ecuaci\u00f3n de la recta de regresi\u00f3n de ys sobre xs es y = a+bx, donde<\/p>\n<pre lang=\"text\">\n   b = (n\u03a3x(i)y(i) - \u03a3x(i)\u03a3y(i)) \/ (n\u03a3x(i)\u00b2 - (\u03a3x(i))\u00b2)\n   a = (\u03a3y(i) - b\u03a3x(i)) \/ n\n<\/pre>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   regresionLineal :: [Double] -> [Double] -> (Double,Double)\n<\/pre>\n<p>tal que (regresionLineal xs ys) es el par (a,b) de los coeficientes de la recta de regresi\u00f3n de ys sobre xs. Por ejemplo, para los valores<\/p>\n<pre lang=\"text\">\n   ejX, ejY :: [Double]\n   ejX = [5,  7, 10, 12, 16, 20, 23, 27, 19, 14]\n   ejY = [9, 11, 15, 16, 20, 24, 27, 29, 22, 20]\n<\/pre>\n<p>se tiene<\/p>\n<pre lang=\"text\">\n   \u03bb> regresionLineal ejX ejY\n   (5.195045748716805,0.9218924347243919)\n<\/pre>\n<p>Definir el procedimiento<\/p>\n<pre lang=\"text\">\n   grafica :: [Double] -> [Double] -> IO ()\n<\/pre>\n<p>tal que (grafica xs ys) pinte los puntos correspondientes a las listas de valores xs e ys y su recta de regresi\u00f3n. Por ejemplo, con (grafica ejX ejY) se obtiene el siguiente dibujo<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2016\/02\/Regresion_lineal.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2016\/02\/Regresion_lineal.png?resize=625%2C374\" alt=\"Regresion_lineal\" width=\"625\" height=\"374\" class=\"aligncenter size-full wp-image-2154\" srcset=\"https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2016\/02\/Regresion_lineal.png?w=625&amp;ssl=1 625w, https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2016\/02\/Regresion_lineal.png?resize=300%2C179&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2016\/02\/Regresion_lineal.png?resize=100%2C59&amp;ssl=1 100w, https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2016\/02\/Regresion_lineal.png?resize=150%2C89&amp;ssl=1 150w\" sizes=\"(max-width: 625px) 100vw, 625px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Graphics.Gnuplot.Simple\n\nejX, ejY :: [Double]\nejX = [5,  7, 10, 12, 16, 20, 23, 27, 19, 14]\nejY = [9, 11, 15, 16, 20, 24, 27, 29, 22, 20]\n\nregresionLineal :: [Double] -> [Double] -> (Double,Double)\nregresionLineal xs ys = (a,b)\n    where n     = fromIntegral (length xs)\n          sumX  = sum xs\n          sumY  = sum ys\n          sumX2 = sum (zipWith (*) xs xs)\n          sumY2 = sum (zipWith (*) ys ys)\n          sumXY = sum (zipWith (*) xs ys)\n          b     = (n*sumXY - sumX*sumY) \/ (n*sumX2 - sumX^2)\n          a     = (sumY - b*sumX) \/ n\n\ngrafica :: [Double] -> [Double] -> IO ()\ngrafica xs ys = \n    plotPathsStyle \n      [YRange (0,10+mY)] \n      [(defaultStyle {plotType = Points,\n                      lineSpec = CustomStyle [LineTitle \"Datos\",\n                                              PointType 2,\n                                              PointSize 2.5]},\n                     zip xs ys),\n       (defaultStyle {plotType = Lines,\n                      lineSpec = CustomStyle [LineTitle \"Ajuste\",\n                                              LineWidth 2]},\n                     [(x,a+b*x) | x <- [0..mX]])]\n    where (a,b) = regresionLineal xs ys                      \n          mX    = maximum xs \n          mY    = maximum ys\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Dadas dos listas de valores xs = [x(1), x(2), &#8230;, x(n)] ys = [y(1), y(2), &#8230;, y(n)] la ecuaci\u00f3n de la recta de regresi\u00f3n de ys sobre xs es y = a+bx, donde b = (n\u03a3x(i)y(i) &#8211; \u03a3x(i)\u03a3y(i)) \/ (n\u03a3x(i)\u00b2 &#8211; (\u03a3x(i))\u00b2) a = (\u03a3y(i) &#8211; b\u03a3x(i)) \/ n Definir la funci\u00f3n regresionLineal :: [Double]&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"footnotes":"","_jetpack_memberships_contains_paid_content":false},"categories":[4],"tags":[258,376,15,329,40,76],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2153"}],"collection":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/comments?post=2153"}],"version-history":[{"count":5,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2153\/revisions"}],"predecessor-version":[{"id":6353,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2153\/revisions\/6353"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=2153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=2153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=2153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}