算法从数据库中计算最稳定的,连续的值(Algorithm for calculating most

2019-10-21 17:23发布

我有一些问题,我需要你输入的。

说我有充满2000-3000行的数据库表,每一行都有一个值和一些标识符。 我需要最稳定的值(最低传播)退出连续〜100行的。 没事的几个跳线值,如果你能排除它们。 你将如何做到这一点,你会用什么算法?

我目前使用SAS Enterprise Guide中对我的数据库运行在甲骨文。 我真的不知道多少的通用语言SAS那,但我不知道我会用什么样的语言呢? 一些脚本语言? 我有限的编程知识,但这个任务似乎很容易,对不对?

我一直在想的是算法:

  1. 选择100个连续行和计算标准偏差。 加1 SELECT语句,并重新计算标准偏差。 循环通过整个表。 用最低的标准偏差导出行

  2. 同1,但计算方差,而不是标准偏差(基本上是相同的东西)。 当整个表已循环,再做一次,但不包括1排它具有从平均的最高值。 直到5个跳线重复过程已被排除,并比较结果。 利弊相比方法1?

问题:

  • 最佳和最简单的方法是什么?
  • 喜欢的语言? 可能在SAS?
  • 你有你会建议任何其他方法?

提前致谢

/尼克拉斯

Answer 1:

下面的代码会做你的要求。 它只是使用一些样本数据,并且仅Calcs(计算)它为10个观察值(而不是100)。 我要把它留给你的要求相适应。

创建一些示例数据。 适用于所有的SAS设备:

data xx;
  set sashelp.stocks;
  where stock = 'IBM';
  obs = _n_;
run;

创建行号和排序,上去下来。 使得它更容易calc下STDDEV:

proc sort data=xx;
  by descending obs;
run;

使用数组来保持随后的10个OBS的每一行。 计算使用数组(除最后10行每行STDDEV。记住,我们通过数据向后工作。

data calcs;
  set xx;

  array a[10] arr1-arr10;

  retain arr1-arr10 .;

  do tmp=10 to 2 by -1;
    a[tmp] = a[tmp-1];
  end;
  a[1] = close;

  if _n_ ge 10 then do;
    std = std(of arr1-arr10);
  end;

run;

找哪家OBS(即行)的最低STDDEV计算。 将它保存到一个宏变种。

proc sql noprint;
  select obs into :start_row
  from calcs
  having std = min(std)
  ;
quit;

选择从参与calcing最低STDDEV该样本数据的10个观测。

proc sql noprint;
  create table final as
  select *
  from xx
  where obs between &start_row and %eval(&start_row+10)
  order by obs
  ;
quit;


Answer 2:

一个除了罗伯特的解决方案,但与第2部分包括在内,从而形成第二阵列,然后通过循环和去除顶部5的值。 您仍罗伯茨解决方案的最后部分与最低标准偏差,那么相应的附行提取一行。 你没有指定你想怎么处理有最大去除,使他们留在数据集中的差异。

data want;
*set arrays for looping;
/*used to calculate the std*/
array p{0:9} _temporary_;
/*used to copy the array over to reduce variables*/
array ps(1:10) _temporary_; 
/*used to store the var with 5 max values removed*/
array s{1:5} var1-var5;

set sample; 


p{mod(_n_,10)} = open;


if _n_ ge 10 then std = std(of p{*});

*remove max values to calculate variance;
if _n_ ge 10 then do;
*copy array over to remove values;
    do i=1 to 10;
        ps(i)=p(i-1);
    end;

    do i=1 to 5;
        index=whichn(max(of ps(*)), of ps(*));
        ps(index)=.;
        s(i)=var(of ps(*));
    end;
end;
run;


文章来源: Algorithm for calculating most stable, consecutive values from a database