{"id":1104,"date":"2023-07-27T15:57:43","date_gmt":"2023-07-27T15:57:43","guid":{"rendered":"https:\/\/statorials.org\/it\/pitone-di-prova-della-scatola-di-ljung\/"},"modified":"2023-07-27T15:57:43","modified_gmt":"2023-07-27T15:57:43","slug":"pitone-di-prova-della-scatola-di-ljung","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/pitone-di-prova-della-scatola-di-ljung\/","title":{"rendered":"Come eseguire un test ljung-box in python"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Il <strong>test Ljung-Box<\/strong> \u00e8 un test statistico che verifica se esiste autocorrelazione in una serie temporale.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Utilizza le seguenti ipotesi:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>H <sub>0<\/sub> :<\/strong> I residui sono distribuiti in modo indipendente.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>H <sub>A<\/sub> :<\/strong> I residui non sono distribuiti in modo indipendente; mostrano una correlazione seriale.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Idealmente, vorremmo non rifiutare l\u2019ipotesi nulla. Vorremmo cio\u00e8 che il valore p del test fosse maggiore di 0,05, perch\u00e9 ci\u00f2 significa che i residui del nostro modello di serie temporali sono indipendenti, che \u00e8 spesso un&#8217;ipotesi che facciamo quando creiamo un modello.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Questo tutorial spiega come eseguire un test Ljung-Box in Python.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Esempio: test Ljung-Box in Python<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Per eseguire il test Ljung-Box su una serie di dati in Python, puoi utilizzare la funzione <a href=\"https:\/\/www.statsmodels.org\/stable\/generated\/statsmodels.stats.diagnostic.acorr_ljungbox.html\" target=\"_blank\" rel=\"noopener noreferrer\">acorr_ljungbox()<\/a> dalla libreria <strong>statsmodels<\/strong> che utilizza la seguente sintassi:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>acorr_ljungbox(x, offset=Nessuno)<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">Oro:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>x:<\/strong> la serie di dati<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>ritardi:<\/strong> numero di ritardi da testare<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Questa funzione restituisce una statistica di test e un valore p corrispondente. Se il valore p \u00e8 inferiore ad una certa soglia (ad esempio \u03b1 = 0,05), \u00e8 possibile rifiutare l&#8217;ipotesi nulla e concludere che i residui non sono distribuiti in modo indipendente.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come utilizzare questa funzione per eseguire il test Ljung-Box sul set di dati statsmodels integrato chiamato &#8220;SUNACTIVITY&#8221;:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #107d3f;\">import<\/span> statsmodels.api <span style=\"color: #107d3f;\">as<\/span> sm\n\n<span style=\"color: #008080;\">#load data series<\/span>\ndata = sm.datasets.sunspots.load_pandas().data\n\n<span style=\"color: #008080;\">#view first ten rows of data series<\/span> \ndata[:5]\n\nYEAR SUNACTIVITY\n0 1700.0 5.0\n1 1701.0 11.0\n2 1702.0 16.0\n3 1703.0 23.0\n4 1704.0 36.0\n\n<span style=\"color: #008080;\">#fit ARMA model to dataset\n<\/span>res = sm. <span style=\"color: #3366ff;\">tsa<\/span> . <span style=\"color: #3366ff;\">ARMA<\/span> (data[\" <span style=\"color: #993300;\">SUNACTIVITY<\/span> \"],(1,1)). <span style=\"color: #3366ff;\">fit<\/span> (disp=-1)\n\n<span style=\"color: #008080;\">#perform Ljung-Box test on residuals with lag=5<\/span>\nsm. <span style=\"color: #3366ff;\">stats<\/span> . <span style=\"color: #3366ff;\">acorr_ljungbox<\/span> (res. <span style=\"color: #3366ff;\">resid<\/span> , lags=[5], return_df= <span style=\"color: #008000;\">True<\/span> )\n\n          lb_stat lb_pvalue\n5 107.86488 1.157710e-21<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">La statistica del test \u00e8 <strong>107,86488<\/strong> e il valore p del test \u00e8 <strong>1,157710e-21<\/strong> , che \u00e8 molto inferiore a 0,05. Pertanto, rifiutiamo l\u2019ipotesi nulla del test e concludiamo che i residui non sono indipendenti.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Tieni presente che in questo esempio scegliamo di utilizzare un valore di offset pari a 5, ma puoi scegliere qualsiasi valore desideri utilizzare per l&#8217;offset. Ad esempio, potremmo invece utilizzare un valore pari a 20:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#perform Ljung-Box test on residuals with lag=20<\/span>\nsm. <span style=\"color: #3366ff;\">stats<\/span> . <span style=\"color: #3366ff;\">acorr_ljungbox<\/span> (res. <span style=\"color: #3366ff;\">resid<\/span> , lags=[20], return_df= <span style=\"color: #008000;\">True<\/span> )\n\n           lb_stat lb_pvalue\n20 343.634016 9.117477e-61<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">La statistica del test \u00e8 <strong>343.634016<\/strong> e il valore p del test \u00e8 <strong>9.117477e-61<\/strong> , che \u00e8 molto inferiore a 0,05. Pertanto, rifiutiamo ancora una volta l\u2019ipotesi nulla del test e concludiamo che i residui non sono indipendenti.<\/span><\/p>\n<p> <span style=\"color: #000000;\">A seconda della situazione particolare, \u00e8 possibile scegliere un valore inferiore o superiore da utilizzare per l&#8217;offset.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Il test Ljung-Box \u00e8 un test statistico che verifica se esiste autocorrelazione in una serie temporale. Utilizza le seguenti ipotesi: H 0 : I residui sono distribuiti in modo indipendente. H A : I residui non sono distribuiti in modo indipendente; mostrano una correlazione seriale. Idealmente, vorremmo non rifiutare l\u2019ipotesi nulla. Vorremmo cio\u00e8 che il [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Come eseguire un test Ljung-Box in Python - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come eseguire un test Ljung-Box in Python, con un esempio.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/statorials.org\/it\/pitone-di-prova-della-scatola-di-ljung\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come eseguire un test Ljung-Box in Python - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come eseguire un test Ljung-Box in Python, con un esempio.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/pitone-di-prova-della-scatola-di-ljung\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-27T15:57:43+00:00\" \/>\n<meta name=\"author\" content=\"Benjamin anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/pitone-di-prova-della-scatola-di-ljung\/\",\"url\":\"https:\/\/statorials.org\/it\/pitone-di-prova-della-scatola-di-ljung\/\",\"name\":\"Come eseguire un test Ljung-Box in Python - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-27T15:57:43+00:00\",\"dateModified\":\"2023-07-27T15:57:43+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come eseguire un test Ljung-Box in Python, con un esempio.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/pitone-di-prova-della-scatola-di-ljung\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/pitone-di-prova-della-scatola-di-ljung\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/pitone-di-prova-della-scatola-di-ljung\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come eseguire un test ljung-box in python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/it\/#website\",\"url\":\"https:\/\/statorials.org\/it\/\",\"name\":\"Statorials\",\"description\":\"La tua guida all&#039;alfabetizzazione statistica!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/it\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"it-IT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\",\"name\":\"Benjamin anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"it-IT\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Benjamin anderson\"},\"description\":\"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9\",\"sameAs\":[\"https:\/\/statorials.org\/it\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Come eseguire un test Ljung-Box in Python - Statorials","description":"Questo tutorial spiega come eseguire un test Ljung-Box in Python, con un esempio.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/statorials.org\/it\/pitone-di-prova-della-scatola-di-ljung\/","og_locale":"it_IT","og_type":"article","og_title":"Come eseguire un test Ljung-Box in Python - Statorials","og_description":"Questo tutorial spiega come eseguire un test Ljung-Box in Python, con un esempio.","og_url":"https:\/\/statorials.org\/it\/pitone-di-prova-della-scatola-di-ljung\/","og_site_name":"Statorials","article_published_time":"2023-07-27T15:57:43+00:00","author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"2 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/pitone-di-prova-della-scatola-di-ljung\/","url":"https:\/\/statorials.org\/it\/pitone-di-prova-della-scatola-di-ljung\/","name":"Come eseguire un test Ljung-Box in Python - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-27T15:57:43+00:00","dateModified":"2023-07-27T15:57:43+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come eseguire un test Ljung-Box in Python, con un esempio.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/pitone-di-prova-della-scatola-di-ljung\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/pitone-di-prova-della-scatola-di-ljung\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/pitone-di-prova-della-scatola-di-ljung\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come eseguire un test ljung-box in python"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/it\/#website","url":"https:\/\/statorials.org\/it\/","name":"Statorials","description":"La tua guida all&#039;alfabetizzazione statistica!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/it\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"it-IT"},{"@type":"Person","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae","name":"Benjamin anderson","image":{"@type":"ImageObject","inLanguage":"it-IT","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Benjamin anderson"},"description":"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9","sameAs":["https:\/\/statorials.org\/it"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/1104"}],"collection":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/comments?post=1104"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/1104\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=1104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=1104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=1104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}