Cumulative standard deviation: Difference between revisions

Content added Content deleted
m (→‎{{header|R}}: Syntax highlighting.)
m (→‎{{header|R}}: Improved syntax.)
Line 3,605: Line 3,605:
==="Running" SD===
==="Running" SD===
If we desire a solution that gives every "running" standard deviation for each input, rather than only giving one number as our final output, we can do the following. To make this differ from previous solutions, we will not have our code make any mention of missing values, and we will show off R's Reduce and sapply.
If we desire a solution that gives every "running" standard deviation for each input, rather than only giving one number as our final output, we can do the following. To make this differ from previous solutions, we will not have our code make any mention of missing values, and we will show off R's Reduce and sapply.
<lang rsplus>biasedSd<-function(data)#Once again, we have to make a standard deviation function from scratch.
<lang rsplus>biasedSd <- function(data)#Once again, we have to make a standard deviation function from scratch.
{
{
sqrt(mean((data-mean(data))^2))
sqrt(mean((data - mean(data))^2))
}
}


cumSd<-function(data)
cumSd <- function(data)
{
{
sapply(Reduce(c,data,accumulate = T), biasedSd)
sapply(Reduce(c, data, accumulate = TRUE), biasedSd)
}</lang>
}</lang>
{{out}}
{{out}}
Line 3,619: Line 3,619:
===Stateful SD===
===Stateful SD===
If we want a function that remembers and uses the previous inputs, letting us be very strict about the "one at a time" requirement, then we can lift biasedSd from the previous solution and make good use of the distinction between R's <- and <<- methods of assignment.
If we want a function that remembers and uses the previous inputs, letting us be very strict about the "one at a time" requirement, then we can lift biasedSd from the previous solution and make good use of the distinction between R's <- and <<- methods of assignment.
<lang rsplus>cumSDStateful<-function()
<lang rsplus>cumSDStateful <- function()
{
{
data<-numeric(0)
data <- numeric(0)
function(oneNumber)
function(oneNumber)
{
{
data<<-c(data,oneNumber)
data <<- c(data, oneNumber)
biasedSd(data)
biasedSd(data)
}
}
}
}
state<-cumSDStateful()</lang>
state <- cumSDStateful()</lang>
{{out}}
{{out}}
<pre>> state(2);state(4);state(4);state(4);state(5);state(5);state(7);state(9)
<pre>> state(2);state(4);state(4);state(4);state(5);state(5);state(7);state(9)
Line 3,642: Line 3,642:
===Environment solution===
===Environment solution===
R gives us some control over what environment expressions are evaluated in. This lets us shorten the previous solution and get identical output.
R gives us some control over what environment expressions are evaluated in. This lets us shorten the previous solution and get identical output.
<lang rsplus>localCumSD<-local({
<lang rsplus>localCumSD <- local({
data<-numeric(0)
data <- numeric(0)
function(oneNumber)
function(oneNumber)
{
{
data<<-c(data,oneNumber)
data <<- c(data, oneNumber)
biasedSd(data)#Again, lifted from the ""Running" SD" solution.
biasedSd(data)#Again, lifted from the ""Running" SD" solution.
}
}