#! /bin/bash

# Arguments: {ASSET}
#
# where {ASSET} is "bitcoin" or "sucoasma"

asset="$1"; shift

ifile="data-${asset}.txt"

name="deg1-${asset}"
dfile="fit-${name}-data.txt"
cfile="fit-${name}-coeffs.txt"
ffile="fit-${name}-fitted.txt"

cat ${ifile} \
  | gawk \
     ' BEGIN { k = 0; } 
       // {
         dt = $1; pm = $2;
         wt = (pm > 0.0 ? 1.000 : 0.000) 
         t = k/1000;
         printf "%s %+14.6f %6.3f %3.1f %6.3f\n", dt, log(pm + 1.0e-30), wt, 1, t
         k++
       }
     ' \
  > ${dfile}
  
cat ${dfile} \
  | linear_fit \
      -terms 2 \
      -weighted T \
      -writeFormula ${cfile} \
  | gawk \
      ' // {
          dt = $1; lpm = $2; lpf = $3;
          pm = (lpm < -10 ? 0.0 : exp(lpm)); 
          pf = exp(lpf);
          printf "%s %12.5f %12.5f\n", dt, pm, pf;
        }
      ' \
  > ${ffile}
