{"id":2664,"date":"2023-07-21T06:56:19","date_gmt":"2023-07-21T06:56:19","guid":{"rendered":"https:\/\/statorials.org\/it\/test-kpss-in-python\/"},"modified":"2023-07-21T06:56:19","modified_gmt":"2023-07-21T06:56:19","slug":"test-kpss-in-python","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/test-kpss-in-python\/","title":{"rendered":"Come eseguire un test kpss in python"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Un <strong>test KPSS<\/strong> pu\u00f2 essere utilizzato per determinare se una serie temporale ha un trend stazionario.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Questo test utilizza la seguente ipotesi nulla e alternativa:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>H <sub>0<\/sub><\/strong> : La serie storica ha un andamento stazionario.<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>H <sub>A<\/sub><\/strong> : La serie storica <em>non ha<\/em> un andamento stazionario.<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Se il <a href=\"https:\/\/statorials.org\/it\/valori-p-significativita-statistica\/\" target=\"_blank\" rel=\"noopener\">p-value<\/a> del test \u00e8 inferiore ad un certo livello di significativit\u00e0 (es. \u03b1 = 0,05), allora rifiutiamo l&#8217;ipotesi nulla e concludiamo che la serie storica non ha un trend stazionario.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Altrimenti non riusciremo a rifiutare l\u2019ipotesi nulla.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Gli esempi seguenti mostrano come eseguire un test KPSS in Python.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 1: test KPSS in Python (con dati stazionari)<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Innanzitutto, creiamo alcuni dati falsi in Python con cui lavorare:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008000;\">import<\/span> numpy <span style=\"color: #008000;\">as<\/span> np<\/span>\n<span style=\"color: #000000;\"><span style=\"color: #008000;\">import<\/span> matplotlib. <span style=\"color: #3366ff;\">pyplot<\/span> <span style=\"color: #008000;\">as<\/span> plt\n\n<span style=\"color: #008080;\">#make this example reproducible\n<\/span>n.p. <span style=\"color: #3366ff;\">random<\/span> . <span style=\"color: #3366ff;\">seed<\/span> ( <span style=\"color: #008000;\">1<\/span> )\n\n<span style=\"color: #008080;\">#create time series data\n<\/span>data = np. <span style=\"color: #3366ff;\">random<\/span> . <span style=\"color: #3366ff;\">normal<\/span> (size= <span style=\"color: #008000;\">100<\/span> )\n\n<span style=\"color: #008080;\">#create line plot of time series data\n<\/span>plt. <span style=\"color: #3366ff;\">plot<\/span> (data)<\/span><\/span><\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-23462 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/kpss3.jpg\" alt=\"\" width=\"524\" height=\"351\" srcset=\"\" sizes=\"\"><\/p>\n<p> <span style=\"color: #000000;\">Possiamo utilizzare la funzione <strong>kpss()<\/strong> dal pacchetto <b>statsmodels<\/b> per eseguire un test KPSS su questi dati di serie temporali:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008000;\">import<\/span> statsmodels. <span style=\"color: #3366ff;\">api<\/span> <span style=\"color: #008000;\">as<\/span> sm\n\n<span style=\"color: #008080;\">#perform KPSS test\n<\/span>sm. <span style=\"color: #3366ff;\">tsa<\/span> . <span style=\"color: #3366ff;\">stattools<\/span> . <span style=\"color: #3366ff;\">kpss<\/span> (data, regression=' <span style=\"color: #ff0000;\">ct<\/span> ')\n\n(0.0477617848370993,\n 0.1,\n 1,\n {'10%': 0.119, '5%': 0.146, '2.5%': 0.176, '1%': 0.216})\n\nInterpolationWarning: The test statistic is outside of the range of p-values available\nin the look-up table. The actual p-value is greater than the p-value returned.\n<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Ecco come interpretare il risultato:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">La statistica del test KPSS: <strong>0,04776<\/strong><\/span><\/li>\n<li> <span style=\"color: #000000;\">Il valore p: <strong>0,1<\/strong><\/span><\/li>\n<li> <span style=\"color: #000000;\">Il parametro di offset del troncamento: <strong>1<\/strong><\/span><\/li>\n<li> <span style=\"color: #000000;\">Valori critici al <strong>10%<\/strong> , <strong>5%<\/strong> , <strong>2,5%<\/strong> e <strong>1%<\/strong><\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Il valore p \u00e8 <strong>0,1<\/strong> . Poich\u00e9 questo valore non \u00e8 inferiore a 0,05, non riusciamo a rifiutare l&#8217;ipotesi nulla del test KPSS.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Ci\u00f2 significa che possiamo assumere che la serie storica abbia un andamento stazionario.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Nota 1<\/strong> : il valore p in realt\u00e0 \u00e8 ancora maggiore di 0,1, ma il valore pi\u00f9 basso prodotto dalla funzione kpss() \u00e8 0,1.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Nota 2<\/strong> : \u00e8 necessario utilizzare l&#8217;argomento <strong>regression=&#8217;ct&#8217;<\/strong> per specificare che l&#8217;ipotesi nulla del test \u00e8 che i dati abbiano un trend stazionario.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 2: test KPSS in Python (con dati non stazionari)<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Innanzitutto, creiamo alcuni dati falsi in Python con cui lavorare:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008000;\">import<\/span> numpy <span style=\"color: #008000;\">as<\/span> np<\/span>\n<span style=\"color: #000000;\"><span style=\"color: #008000;\">import<\/span> matplotlib. <span style=\"color: #3366ff;\">pyplot<\/span> <span style=\"color: #008000;\">as<\/span> plt\n\n<span style=\"color: #008080;\">#make this example reproducible\n<\/span>n.p. <span style=\"color: #3366ff;\">random<\/span> . <span style=\"color: #3366ff;\">seed<\/span> ( <span style=\"color: #008000;\">1<\/span> )\n\n<span style=\"color: #008080;\">#create time series data\n<\/span>data =np. <span style=\"color: #3366ff;\">array<\/span> ([0, 3, 4, 3, 6, 7, 5, 8, 15, 13, 19, 12, 29, 15, 45, 23, 67, 45])\n\n<span style=\"color: #008080;\">#create line plot of time series data\n<\/span>plt. <span style=\"color: #3366ff;\">plot<\/span> (data)<\/span><\/span><\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-23463 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/kpss4.jpg\" alt=\"\" width=\"525\" height=\"350\" srcset=\"\" sizes=\"\"><\/p>\n<p> <span style=\"color: #000000;\">Ancora una volta, possiamo utilizzare la funzione <strong>kpss()<\/strong> del pacchetto <b>statsmodels<\/b> per eseguire un test KPSS su questi dati di serie temporali:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008000;\">import<\/span> statsmodels. <span style=\"color: #3366ff;\">api<\/span> <span style=\"color: #008000;\">as<\/span> sm\n\n<span style=\"color: #008080;\">#perform KPSS test\n<\/span>sm. <span style=\"color: #3366ff;\">tsa<\/span> . <span style=\"color: #3366ff;\">stattools<\/span> . <span style=\"color: #3366ff;\">kpss<\/span> (data, regression=' <span style=\"color: #ff0000;\">ct<\/span> ')\n\n(0.15096358910843685,\n 0.04586367574296928,\n 3,\n {'10%': 0.119, '5%': 0.146, '2.5%': 0.176, '1%': 0.216})\n<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Ecco come interpretare il risultato:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">La statistica del test KPSS: <strong>0,1509<\/strong><\/span><\/li>\n<li> <span style=\"color: #000000;\">Il valore p: <strong>0,0458<\/strong><\/span><\/li>\n<li> <span style=\"color: #000000;\">Il parametro di offset del troncamento: <strong>3<\/strong><\/span><\/li>\n<li> <span style=\"color: #000000;\">Valori critici al <strong>10%<\/strong> , <strong>5%<\/strong> , <strong>2,5%<\/strong> e <strong>1%<\/strong><\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Il valore p \u00e8 <strong>0,0458<\/strong> . Essendo questo valore inferiore a 0,05, rifiutiamo l&#8217;ipotesi nulla del test KPSS.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Ci\u00f2 significa che la serie storica <em>non \u00e8<\/em> stazionaria.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Nota<\/strong> : puoi trovare la documentazione completa per la funzione kpss() nel pacchetto statsmodels <a href=\"https:\/\/www.statsmodels.org\/stable\/generated\/statsmodels.tsa.stattools.kpss.html\" target=\"_blank\" rel=\"noopener\">qui<\/a> .<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><span style=\"color: #000000;\"><br \/><\/span><\/h3>\n<p> <span style=\"color: #000000;\">I seguenti tutorial forniscono informazioni aggiuntive su come lavorare con i dati delle serie temporali in Python:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/pitone-di-prova-fuller-dickey\/\" target=\"_blank\" rel=\"noopener\">Come eseguire un test Dickey-Fuller aumentato in Python<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/mann-kendall-testa-python\/\" target=\"_blank\" rel=\"noopener\">Come eseguire un test di tendenza di Mann-Kendall in Python<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/serie-temporali-matplotlib\/\" target=\"_blank\" rel=\"noopener\">Come tracciare una serie temporale in Matplotlib<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Un test KPSS pu\u00f2 essere utilizzato per determinare se una serie temporale ha un trend stazionario. Questo test utilizza la seguente ipotesi nulla e alternativa: H 0 : La serie storica ha un andamento stazionario. H A : La serie storica non ha un andamento stazionario. Se il p-value del test \u00e8 inferiore ad un [&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 KPSS in Python - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come eseguire un test KPSS in Python, con diversi esempi.\" \/>\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\/test-kpss-in-python\/\" \/>\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 KPSS in Python - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come eseguire un test KPSS in Python, con diversi esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/test-kpss-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-21T06:56:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/kpss3.jpg\" \/>\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=\"3 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/test-kpss-in-python\/\",\"url\":\"https:\/\/statorials.org\/it\/test-kpss-in-python\/\",\"name\":\"Come eseguire un test KPSS in Python - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-21T06:56:19+00:00\",\"dateModified\":\"2023-07-21T06:56:19+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come eseguire un test KPSS in Python, con diversi esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/test-kpss-in-python\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/test-kpss-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/test-kpss-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come eseguire un test kpss 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 KPSS in Python - Statorials","description":"Questo tutorial spiega come eseguire un test KPSS in Python, con diversi esempi.","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\/test-kpss-in-python\/","og_locale":"it_IT","og_type":"article","og_title":"Come eseguire un test KPSS in Python - Statorials","og_description":"Questo tutorial spiega come eseguire un test KPSS in Python, con diversi esempi.","og_url":"https:\/\/statorials.org\/it\/test-kpss-in-python\/","og_site_name":"Statorials","article_published_time":"2023-07-21T06:56:19+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/kpss3.jpg"}],"author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"3 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/test-kpss-in-python\/","url":"https:\/\/statorials.org\/it\/test-kpss-in-python\/","name":"Come eseguire un test KPSS in Python - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-21T06:56:19+00:00","dateModified":"2023-07-21T06:56:19+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come eseguire un test KPSS in Python, con diversi esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/test-kpss-in-python\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/test-kpss-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/test-kpss-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come eseguire un test kpss 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\/2664"}],"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=2664"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/2664\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=2664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=2664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=2664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}