#! /bin/bash
# Last edited on 2014-05-10 20:52:08 by stolfilocal

# Plots scatterplots of errors of two price predicitors.
# 
# Arguments: {EX} {EMAX} {PREDX} {PREDY} {IDATE} {FDATE} 
# 
# Where 
#   
#   {EX} is the exchange code ("HUBI", "BSTP", etc.); 
#   {EMAX} max absolute error to plot.
#   {PREDX,PREXY} are the names of the two prediction methods to compare ("Slumber", "Banal", etc.).
#   {IDATE,FDATE} range of dates to consider.
#   
# Reads the file "2014-slumber-predictions-{EX}.txt, expected to contain
# the slumber confidence weights, and actual and predicted prices of
# exchage {EX}, at slumber times. Compares two predictions 
# values for entries with the date inthe range {IDATE..FDATE}.
# Saves a scatterplot of the errors of two selected methods to
# "{IDATE}--{FDATE}-slumber-predictions-{EX}-scatter.png".
#
# Assumes that the prices are integers.

ex="$1"; shift     # Exchange tag.
emax="$1"; shift   # Max absolute error to plot.
predx="$1"; shift  # Name of predictor for horizontal axis.
predy="$1"; shift  # Name of predictor for vertical axis.
idate="$1"; shift  # Initial date to consider.
fdate="$1"; shift  # Final date to consider.

title="Errors for ${ex} from ${idate} to ${fdate}"

show="SHOW"

tmp="/tmp/$$"

# Files
pfile="2014-slumber-predictions-${ex}.txt"      # Input file with prediction data.
eplot="${idate}--${fdate}-slumber-predictions-${ex}-scatter.png"  # Fullsize scatterplot image.
jplot="${idate}--${fdate}-slumber-predictions-${ex}-scatter.jpg"  # Reduced size scatterplot image for posting.
  
# Get the column indices:
function get_pred_index() {
  local tag="$1"; shift;

  # Get the column index (from 1) in {pfile} of the predictor {tag}. 

  if [[ "/${tag}" == "/Slumber" ]]; then
    ix=14;
  elif [[ "/${tag}" == "/Banal" ]]; then
    ix=16;
  else
    echo "** invalid predictor name '${tag}'" 1>&2
  fi
  echo "${ix}"
}

iprx=`get_pred_index "${predx}"` # Column index of X predictions.
ipry=`get_pred_index "${predy}"` # Column index of Y predictions.
iprt=12; # Column index of true price.
iwt=6;   # Column index of weight.

color=( '#0022ff' '#ff0000' '#008800' '#8800dd' '#dd4400' '#0066ff' )

export GDFONTPATH=.:..
echo "plotting... " 1>&2

gnuplot <<EOF
set term png size 1800,1600 font "courbd,24"
set size ratio -1 1,1
set output "${tmp}-full.png"
set title "${title}"

iprx = ${iprx}
ipry = ${ipry}
iprt = ${iprt}
iwt = ${iwt}

emax = ${emax}

# Minimum point weight:
minwt = 0.010     # Min weight.
minps = 0.75       # Min point size.
refps = 3.0       # Max point size.
refwt = 1.0       # Weight for point size = {refps}.
load "plot_slumber_funcs.gnuplot"

nozero(x) = (x == 0 ? 0/0 : x)
price(k) = nozero(column(k)) - column(iprt)
ptsize(k) = wps(column(k))

set grid ytics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'
set grid mytics

set grid xtics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'
set grid mxtics

set key top left
set xlabel "${predx}"
set ylabel "${predy}"

set xrange[(-emax):(emax)]
set yrange[(-emax):(emax)]

set style fill solid 
plot \
  (-x) notitle with lines lt 1 lw 1.0 lc rgb '#0044dd', \
  (x) notitle with lines lt 1 lw 1.0 lc rgb '#0044dd', \
  "${pfile}" using (price(iprx)):(price(ipry)):(ptsize(iwt)) \
    notitle with points pt 7 ps variable lt 1 lw 1.0 lc rgb '#dd4400'
    
quit
EOF

convert ${tmp}-full.png -resize '50%' ${eplot}
convert ${eplot} -resize '600x' ${jplot}

if [[ "/${show}" == "/SHOW" ]]; then
  if [[ -s ${eplot} ]]; then
    display ${eplot}
  fi
fi

rm -fv ${tmp}{-*,}.*
    
        
