# $\ell_0$ Trend Filtering

This file folder contains all the R scripts that are used to replicate the results in our paper. Before running these scripts, you need to install [R](https://cran.rstudio.com/) and [RStudio](https://posit.co/downloads/). 


* [AMIAS](AMIAS) folder contain the source code for implementing the AMIAS algorithm proposed in our paper. This folder is used to develop the AMIAS R package, which should be installed before running any R scripts or Rmarkdown files in this folder.

* SimuL0TF.Rmd: generate Figures 2-7 and include some more illustrative simulated examples. The output HTML file should look like [SimuL0TF](../results/SimuL0TF.pdf).
* AlgoAnalysis.Rmd: replicate the reuslts and generate Figures 8-11 in Section 4.1. The output HTML file should look like [AlgoAnalysis](../results/AlgoAnalysis.pdf).
* utils.R and amiasutils.R: source codes used in AlgoAnalysis.Rmd. To gurantee the success execution of AlgoAnalysis.Rmd, these two files should be under the same path with the file AlgoAnalysis.Rmd.
* RealData.R: genrate Figure 1, replicate the results and generate all the graphs in Section 4.3. It outputs four png files: [HKAQ.png](../results/HKAQ.png), [HKAQ_Path.png](../results/HKAQ_Path.png), [HKAQ_Fits.png](../results/HKAQ_Fits.png) and [HKAQ_Zoom.png](../results/HKAQ_Zoom.png).
* AlgoAnalysis_APP.Rmd: replicate the reuslts and generate Figures B.1-B.7 in Appendix B.1. The output HTML file should look like [AlgoAnalysis_APP](../results/AlgoAnalysis_APP.pdf).

* [simu](simu) folder contains R scripts used in Appendix B.2. These scripts should be executed before runing the scripts in the [simu_plots](simu_plots) folder. By running these scripts, six RData file should be outputed. These RData files store the simulation results for the three examples of varying sample sizes and different noise distributions, by using our method and other competing methods. 
    * nsimu.R and tsimu.R: replicate the results for all methods except for the l0-MIP with large sample size.
    * nsimul0tfc.R and tsimul0tfc.R: replicate the results of the l0-MIP method when sample size is large.
    
* [simu_plots](simu_plots) folder contains the R Scripts used to generate Figures B.9-B.20 in Appendix B.2. It loads the RData files generated by the scripts in the [simu](simu) folder, and then output png files with prefix **pre_** or **aft_** (See [results](../results/) for the detailed output png files).
    * combine_RData.R: combine the RData and needed to be run before generating all the figures.
    * post_plot.R: generate Figures B.9, B.11 and B.13. 
    * pre_plot.R: generate Figures B.10, B.12 and B.14.
    * post_tplot.R: generate Figures B.15, B.17 and B.19. 
    * pre_tplot.R: generate Figures B.16, B.18 and B.20. 

