目次 ⇒ Javaアルゴリズムライブラリ-Artery-サンプル
Q01_01.java
package jp.avaj.lib.algo;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import jp.avaj.lib.test.L;
/**
* List<売上>から移動平均、初期からの変動率・変動幅、N個前からの変動率・変動幅、パーセントを求めて、List<Double>を作成する
*
*/
public class Q01_01 {
public static void main(String[] args) throws Exception {
// 売上データを取得する
List<Sales> list = createSalesData();
// ArCreatorの定義 ⇒ Salesクラスの売上数(count)を抽出する
ArCreator<Sales,Integer> creator = new ArCreator<Sales,Integer>() {
@Override
public Integer convert(Sales obj) throws Exception {
return obj.getCount();
}
};
List<? extends Number> result;
L.p("四か月移動平均");
result = ArList.movingAvg(list, creator,4);
L.p(ArObj.toString(result));
L.p("初期からの変動率");
result = ArList.fluctuationRatio(list,creator);
L.p(ArObj.toString(result));
L.p("初期からの変動幅");
result = ArList.fluctuationWidth(list,creator);
L.p(ArObj.toString(result));
L.p("4個前からの変動率");
result = ArList.fluctuationRatio(list,creator,4);
L.p(ArObj.toString(result));
L.p("4個前からの変動幅");
result = ArList.fluctuationWidth(list,creator,4);
L.p(ArObj.toString(result));
L.p("パーセント");
result = ArList.percent(list,creator);
L.p(ArObj.toString(result));
}
/** 売上データを作成する */
private static List<Sales> createSalesData() {
// 商品名
final String NAME = "item";
Random ran = new Random();
List<Sales> list = new ArrayList<Sales>();
// 二年分のデータを作成する
for (int i=0; i<24; i++) {
Sales sales = new Sales(NAME,1000+i*10+ran.nextInt(200));
list.add(sales);
}
L.p("元データ");
L.p(ArObj.toString(list));
return list;
}
/** ある商品のある月の売上データ */
static class Sales {
/** コンストラクタ */
public Sales(String name,int count) {
this.name = name;
this.count = count;
}
/** 商品名 */
private String name;
/** 売上数 */
private int count;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public String toString() {
return count+"";
}
}
}
結果は次の通り
result.txt
元データ
[1189, 1191, 1177, 1205, 1216, 1129, 1253, 1108, 1218, 1164, 1253, 1307, 1288, 1267, 1178, 1153, 1200, 1349, 1266, 1378, 1381, 1230, 1402, 1273]
四か月移動平均
[1189, 1190, 1185.6666666666667, 1190.5, 1197.25, 1181.75, 1200.75, 1176.5, 1177, 1185.75, 1185.75, 1235.5, 1253, 1278.75, 1260, 1221.5, 1199.5, 1220, 1242, 1298.25, 1343.5, 1313.75, 1347.75, 1321.5]
初期からの変動率
[1, 1.001682085786375, 0.9899074852817493, 1.0134566862910008, 1.022708158116064, 0.9495374264087468, 1.0538267451640033, 0.9318755256518082, 1.024390243902439, 0.9789739276703112, 1.0538267451640033, 1.0992430613961313, 1.0832632464255676, 1.065601345668629, 0.990748528174937, 0.9697224558452481, 1.0092514718250631, 1.1345668629100083, 1.0647603027754415, 1.1589571068124473, 1.1614802354920102, 1.0344827586206897, 1.1791421362489487, 1.0706476030277545]
初期からの変動幅
[0, 2, -12, 16, 27, -60, 64, -81, 29, -25, 64, 118, 99, 78, -11, -36, 11, 160, 77, 189, 192, 41, 213, 84]
4個前からの変動率
[1, 1.001682085786375, 0.9899074852817493, 1.0134566862910008, 1.022708158116064, 0.9479429051217464, 1.064570943075616, 0.9195020746887966, 1.0016447368421053, 1.0310008857395925, 1, 1.1796028880866427, 1.0574712643678161, 1.088487972508591, 0.9401436552274541, 0.8821729150726856, 0.9316770186335404, 1.0647198105761642, 1.0747028862478778, 1.1951431049436254, 1.1508333333333334, 0.9117865085248332, 1.1074249605055293, 0.9238026124818578]
4個前からの変動幅
[0, 2, -12, 16, 27, -62, 76, -97, 2, 35, 0, 199, 70, 103, -75, -154, -88, 82, 88, 225, 181, -119, 136, -105]
パーセント
[3.99328295549958, 4, 3.9529806884970613, 4.047019311502939, 4.083963056255247, 3.7917716204869856, 4.208228379513014, 3.7212426532325775, 4.090680100755668, 3.9093198992443323, 4.208228379513014, 4.389588581024349, 4.325776658270361, 4.255247691015953, 3.956339210747271, 3.8723761544920237, 4.030226700251889, 4.530646515533165, 4.251889168765743, 4.6280436607892526, 4.638119227539883, 4.130982367758186, 4.70864819479429, 4.275398824517213]