#! /bin/bash
# Last edited on 2015-02-23 21:42:29 by stolfilocal

# Arguments: {NT} {DNAME}

# Reads the trade summary data file "00-DATA/series/fix/{DNAME}.txt", extracts the 
# weighted mean prices, convests to log scale,
# fits a linear model that tries to predict the current value from the previous {NT} values,
# writes the formula to "predict/{DNAME}_n{NT}.frm",
# writes the prediction to "predict/{DNAME}_n{NT}.txt",
# then plots the latter.

nt="$1"; shift
dname="$1"; shift

inDir="00-DATA/series/fix"
tmp="/tmp/$$"

opref="predict/${dname}_n${nt}"

opref="predict/${dname}_var"
cat ${inDir}/${dname}.txt \
  | egrep -v -e '(^[ ]*([#]|$))|[!]' \
  | gawk '// { v = $(16); printf "%5d %+10.7f\n", n, log(v/1000.0)/log(10); n++ }' \
  > ${tmp}-raw.txt
cat ${tmp}-raw.txt \
  | tseries_aggregate -terms ${nt} \
  | egrep -v -i -e '(inf|nan)' \
  > ${tmp}-agg.txt
cat ${tmp}-agg.txt \
  | linear_fit \
    -terms ${nt} \
    -writeFormula ${opref}.frm \
  > ${opref}.txt

./plot_predictions.sh SHOW ${opref}

rm -f ${tmp}{-*,}.txt
