Skip to content
📦 An R package for numerical differentiation of noisy, nonsmooth data
Branch: master
Clone or download
Nathaniel Price
Nathaniel Price Fix notation
Latest commit 01dfbf0 May 16, 2019

README.md

tvdiff

lifecycle Travis build status

The tvdiff package is an R translation of the Matlab implementation of the Total Variation Regularized Numerical Differentiation algorithm by Rick Chartrand. The package implements the methods found in Rick Chartrand,“Numerical differentiation of noisy, nonsmooth data,” ISRN Applied Mathematics, Vol. 2011, Article ID 164564, 2011.

Installation

The tvdiff package is currently only available from Github.

devtools::install_github("natbprice/tvdiff")

Example

A simple example based on the function f(x) = \mid x - 0.5 \mid with Gaussian noise of standard deviation 0.05. The derivative is estimated from the noisy observations using Total Variation Regularized Differentiation. A prediction of the original function is obtained from the estimated derivative through numerical integration.

Load demo data

data("smalldemodata")
str(smalldemodata)
#> Classes 'tbl_df', 'tbl' and 'data.frame':    100 obs. of  3 variables:
#>  $ x   : num  0 0.0101 0.0202 0.0303 0.0404 ...
#>  $ true: num  0.49 0.48 0.47 0.46 0.45 0.44 0.43 0.42 0.41 0.4 ...
#>  $ obs : num  0.495 0.459 0.496 0.427 0.444 ...

Use TVRegDiffR to perform numerical integration on the demo data

# Build dataframe
smallEx <-
  smalldemodata %>%
  # Estimate derivative
  mutate(dydx =
           TVRegDiffR(
             data = obs,
             iter = 1e3,
             alph = 0.2,
             scale = "small",
             ep = 1e-6,
             dx = 0.01
           )[-1]) %>% 
  # Simple numerical integration
  mutate(dx = lead(x) - x,
         pred = obs[1] + cumsum(dydx*dx)) %>% 
  # Collect in long form
  gather(key, value, -x, -dx)

# Plot observed vs predicted
ggplot(smallEx %>% 
         filter(key != "dydx"), 
       aes(x = x, y = value, color = key, linetype = key)) +
  geom_line() +
  theme_minimal() +
  theme(legend.title = element_blank(),
        legend.position = "bottom") +
  labs(x = "x",
       y = "y")

# Plot derivative
ggplot(smallEx %>% 
         filter(key == "dydx"), 
       aes(x = x, y = value)) +
  geom_point() +
  theme_minimal() +
  labs(x = "x",
       y = "dydx")

References

Rick Chartrand, “Numerical differentiation of noisy, nonsmooth data,” ISRN Applied Mathematics, Vol. 2011, Article ID 164564, 2011.

https://sites.google.com/site/dnartrahckcir/home/tvdiff-code

Python translation by Simone Sturniolo: https://github.com/stur86/tvregdiff

You can’t perform that action at this time.