March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -0,0 +1,21 @@
SMA = object.new
SMA.init = { period |
my.period = period
my.list = []
my.average = 0
}
SMA.prototype.add = { num |
true? my.list.length >= my.period
{ my.list.deq }
my.list << num
my.average = my.list.reduce(:+) / my.list.length
}
sma3 = SMA.new 3
sma5 = SMA.new 5
[1, 2, 3, 4, 5, 5, 4, 3, 2, 1].each { n |
p n, " - SMA3: ", sma3.add(n), " SMA5: ", sma5.add(n)
}

View file

@ -0,0 +1,17 @@
sma = { period |
list = []
{ num |
true? list.length >= period
{ list.deq }
list << num
list.reduce(:+) / list.length
}
}
sma3 = sma 3
sma5 = sma 5
[1, 2, 3, 4, 5, 5, 4, 3, 2, 1].each { n |
p n, " - SMA3: ", sma3(n), " SMA5: ", sma5(n)
}

View file

@ -1,25 +1,24 @@
import std.stdio, std.traits, std.algorithm;
auto sma(T, int period)() {
T[period] data = 0; // D FP are default-initialized to NaN
T[period] data = 0;
T sum = 0;
int index, nfilled;
int index, nFilled;
// return (in T v) nothrow {
return delegate (in T v) nothrow {
return (in T v) nothrow {
sum += -data[index] + v;
data[index] = v;
index = (index + 1) % period;
nfilled = min(period, nfilled + 1);
return cast(CommonType!(T, float))sum / nfilled;
nFilled = min(period, nFilled + 1);
return CommonType!(T, float)(sum) / nFilled;
};
}
void main() {
auto s3 = sma!(int, 3)();
auto s5 = sma!(double, 5)();
immutable s3 = sma!(int, 3);
immutable s5 = sma!(double, 5);
foreach (e; [1, 2, 3, 4, 5, 5, 4, 3, 2, 1])
foreach (immutable e; [1, 2, 3, 4, 5, 5, 4, 3, 2, 1])
writefln("Added %d, sma(3) = %f, sma(5) = %f",
e, s3(e), s5(e));
}

View file

@ -3,14 +3,14 @@ import std.stdio, std.traits, std.algorithm;
struct SMA(T, int period) {
T[period] data = 0;
T sum = 0;
int index, nfilled;
int index, nFilled;
auto opCall(in T v) pure nothrow {
sum += -data[index] + v;
data[index] = v;
index = (index + 1) % period;
nfilled = min(period, nfilled + 1);
return cast(CommonType!(T, float))sum / nfilled;
nFilled = min(period, nFilled + 1);
return CommonType!(T, float)(sum) / nFilled;
}
}
@ -18,7 +18,7 @@ void main() {
SMA!(int, 3) s3;
SMA!(double, 5) s5;
foreach (e; [1, 2, 3, 4, 5, 5, 4, 3, 2, 1])
foreach (immutable e; [1, 2, 3, 4, 5, 5, 4, 3, 2, 1])
writefln("Added %d, sma(3) = %f, sma(5) = %f",
e, s3(e), s5(e));
}

View file

@ -0,0 +1,94 @@
#import <Foundation/Foundation.h>
@interface MovingAverage : NSObject {
unsigned int period;
NSMutableArray *window;
double sum;
}
- (instancetype)initWithPeriod:(unsigned int)thePeriod;
@end
@implementation MovingAverage
// init with default period
- (instancetype)init {
self = [super init];
if(self) {
period = 10;
window = [[NSMutableArray alloc] init];
sum = 0.0;
}
return self;
}
// init with specified period
- (instancetype)initWithPeriod:(unsigned int)thePeriod {
self = [super init];
if(self) {
period = thePeriod;
window = [[NSMutableArray alloc] init];
sum = 0.0;
}
return self;
}
// add a new number to the window
- (void)add:(double)val {
sum += val;
[window addObject:@(val)];
if([window count] > period) {
NSNumber *n = window[0];
sum -= [n doubleValue];
[window removeObjectAtIndex:0];
}
}
// get the average value
- (double)avg {
if([window count] == 0) {
return 0; // technically the average is undefined
}
return sum / [window count];
}
// set the period, resizes current window
- (void)setPeriod:(unsigned int)thePeriod {
// make smaller?
if(thePeriod < [window count]) {
for(int i = 0; i < thePeriod; ++i) {
NSNumber *n = window[0];
sum -= [n doubleValue];
[window removeObjectAtIndex:0];
}
}
period = thePeriod;
}
// get the period (window size)
- (unsigned int)period {
return period;
}
// clear the window and current sum
- (void)clear {
[window removeAllObjects];
sum = 0;
}
@end
int main (int argc, const char * argv[]) {
@autoreleasepool {
double testData[10] = {1,2,3,4,5,5,4,3,2,1};
int periods[2] = {3,5};
for(int i = 0; i < 2; ++i) {
MovingAverage *ma = [[MovingAverage alloc] initWithPeriod:periods[i]];
for(int j = 0; j < 10; ++j) {
[ma add:testData[j]];
NSLog(@"Next number = %f, SMA = %f", testData[j], [ma avg]);
}
NSLog(@"\n");
}
}
return 0;
}

View file

@ -1,8 +1,7 @@
sub sma(Int $period where * > 0) returns Sub {
sub sma(Int \P where * > 0) returns Sub {
sub ($x) {
state @a = 0 xx $period;
@a.push($x);
@a.shift;
$period R/ [+] @a;
state @a = 0 xx P;
@a.push($x).shift;
P R/ [+] @a;
}
}

View file

@ -1,7 +1,18 @@
sub sma ($)
{my ($period, $sum, @a) = shift, 0;
return sub
{unshift @a, shift;
$sum += $a[0];
@a > $period and $sum -= pop @a;
return $sum / @a;}}
sub sma_generator {
my $period = shift;
my (@list, $sum);
return sub {
my $number = shift;
push @list, $number;
$sum += $number;
$sum -= shift @list if @list > $period;
return $sum / @list;
}
}
# Usage:
my $sma = sma_generator(3);
for (1, 2, 3, 2, 7) {
printf "append $_ --> sma = %.2f (with period 3)\n", $sma->($_);
}