
define(epanechnikov(Z), 3*(1-(Z**2)/5)/(4*sqrt(5)))$
define(gaussian(Z), exp(-Z^2/2)/sqrt(2*%pi))$

tb: 3$
define(utgaussian(Z), (exp(-Z^2/2) - exp(-tb^2/2))/sqrt(2*%pi) )$

kernels: [epanechnikov, gaussian, utgaussian]$
supports: [sqrt(5), inf, tb]$

/* working on a slightly fancier automatic higher-order kernel generator

computeOKernels(ker, b, order) := block(
  [bml,bmh,bvl,bvh,moms,vars,eqns],
  bml:limit(c+1/2,c,-b),
  bmh:limit(c-1/2,c,b),
  bvl:limit(v-c,c,b),
  bvh:limit(v+c,c,b),
  if (order < 2) then return("error: order should be even and greater than 1"),
  moms : [],
  vars : [A0,A1,A2,A3,A4,A5,A6],
  eqns : [],
  for moment: 0 thru order do (moms : append(moms, [integrate(Z^moment*ker(Z), Z, -b, b)])),
  for j : 1 thru order do (
    for n : 1 thru order do (
      append(eqns, [vars[n]*moms[j] + vars[

  )$

*/

computeKernels(ker, b) := (
bml:limit(c+1/2,c,-b),
bmh:limit(c-1/2,c,b),

bvl:limit(v-c,c,b),
bvh:limit(v+c,c,b),

alpha: integrate(ker(Z), Z, -b, b),
define(kern(Z), ker(Z)/alpha),

ker_k:integrate(kern(Z)^2, Z, -b, b),
ker_k_n:ev(ker_k,numer),
ker_k2:integrate(Z^2*kern(Z), Z, -b, b),
ker_k2_n:ev(ker_k2,numer),
ker_k22:integrate((Z*kern(Z))^2, Z, -b, b),
ker_k22_n:ev(ker_k22,numer),

ker_km:integrate(ker(Z+1/2)*ker(Z-1/2), Z, bml, bmh),
ker_km_n:ev(ker_km,numer),

define(kerder(Z), diff(kern(Z), Z)),
define(kercdf(x), integrate(kern(Z), Z, -b, x)),
define(kerconvol_neg(v), integrate(ker(u)*ker(u-v), u, -b, bvh)),
define(kerconvol_pos(v), integrate(ker(u)*ker(u-v), u, bvl, b)),
  
int_ker:integrate(kern(Z),Z,-b,b), 
int_Z_ker:integrate(Z*kern(Z),Z,-b,b), 
int_Z2_ker:integrate(Z**2*kern(Z),Z,-b,b), 
int_Z3_ker:integrate(Z**3*kern(Z),Z,-b,b), 
int_Z4_ker:integrate(Z**4*kern(Z),Z,-b,b), 

eqn0:A0*int_ker + A1*int_Z_ker + A2*int_Z2_ker=1,
eqn1:A0*int_Z_ker + A1*int_Z2_ker + A2*int_Z3_ker=0,
eqn2:A0*int_Z2_ker + A1*int_Z3_ker + A2*int_Z4_ker=0,

kersolve: solve([eqn0,eqn1,eqn2],[A0,A1,A2]),

define(kerfourth(Z), (rhs(kersolve[1][1])+rhs(kersolve[1][2])*Z+rhs(kersolve[1][3])*Z^2)*kern(Z)),

kerfourth_k:integrate(kerfourth(Z)^2, Z, -b, b),
kerfourth_k_n:ev(kerfourth_k,numer),
kerfourth_k2:integrate(Z^2*kerfourth(Z), Z, -b, b),
kerfourth_k2_n:ev(kerfourth_k2,numer),
kerfourth_k22:integrate((Z*kerfourth(Z))^2, Z, -b, b),
kerfourth_k22_n:ev(kerfourth_k22,numer),

kerfourth_km:integrate(kerfourth(Z+1/2)*kerfourth(Z-1/2), Z, bml, bmh),
kerfourth_km_n:ev(kerfourth_km,numer),

define(kerfourthder(Z), diff(kerfourth(Z), Z)),
define(kerfourthcdf(x), integrate(kerfourth(Z), Z, -b, x)),
define(kerfourthconvol_neg(v), integrate(kerfourth(u)*kerfourth(u-v), u, -b, bvh)),
define(kerfourthconvol_pos(v), integrate(kerfourth(u)*kerfourth(u-v), u, bvl, b)),

int_Z5_ker:integrate(Z**5*kern(Z),Z,-b,b), 
int_Z6_ker:integrate(Z**6*kern(Z),Z,-b,b), 
int_Z7_ker:integrate(Z**7*kern(Z),Z,-b,b), 
int_Z8_ker:integrate(Z**8*kern(Z),Z,-b,b), 
int_Z9_ker:integrate(Z**9*kern(Z),Z,-b,b), 

eqn0:A0*int_ker + A1*int_Z_ker + A2*int_Z2_ker + A3*int_Z3_ker + A4*int_Z4_ker=1,
eqn1:A0*int_Z_ker + A1*int_Z2_ker + A2*int_Z3_ker + A3*int_Z4_ker + A4*int_Z5_ker=0,
eqn2:A0*int_Z2_ker + A1*int_Z3_ker + A2*int_Z4_ker + A3*int_Z5_ker + A4*int_Z6_ker=0,
eqn3:A0*int_Z3_ker + A1*int_Z4_ker + A2*int_Z5_ker + A3*int_Z6_ker + A4*int_Z7_ker=0,
eqn4:A0*int_Z4_ker + A1*int_Z5_ker + A2*int_Z6_ker + A3*int_Z7_ker + A4*int_Z8_ker=0,
eqn5:A0*int_Z5_ker + A1*int_Z6_ker + A2*int_Z7_ker + A3*int_Z8_ker + A4*int_Z9_ker=0,

kersolve: solve([eqn0,eqn1,eqn2,eqn3,eqn4,eqn5],[A0,A1,A2,A3,A4]),

define(kersixth(Z), (rhs(kersolve[1][1])+rhs(kersolve[1][2])*Z+rhs(kersolve[1][3])*Z^2+rhs(kersolve[1][4])*Z^3+rhs(kersolve[1][5])*Z^4)*kern(Z)),

kersixth_k:integrate(kersixth(Z)^2, Z, -b, b),
kersixth_k_n:ev(kersixth_k,numer),
kersixth_k2:integrate(Z^2*kersixth(Z), Z, -b, b),
kersixth_k2_n:ev(kersixth_k2,numer),
kersixth_k22:integrate((Z*kersixth(Z))^2, Z, -b, b),
kersixth_k22_n:ev(kersixth_k22,numer),

kersixth_km:integrate(kersixth(Z+1/2)*kersixth(Z-1/2), Z, bml, bmh),
kersixth_km_n:ev(kersixth_km,numer),

define(kersixthder(Z), diff(kersixth(Z), Z)),
define(kersixthcdf(x), integrate(kersixth(Z), Z, -b, x)),
define(kersixthconvol_neg(v), integrate(kersixth(u)*kersixth(u-v), u, -b, bvh)),
define(kersixthconvol_pos(v), integrate(kersixth(u)*kersixth(u-v), u, bvl, b)),

int_Z10_ker:integrate(Z**10*kern(Z),Z,-b,b), 
int_Z11_ker:integrate(Z**11*kern(Z),Z,-b,b), 
int_Z12_ker:integrate(Z**12*kern(Z),Z,-b,b), 
int_Z13_ker:integrate(Z**13*kern(Z),Z,-b,b), 

eqn0:A0*int_ker + A1*int_Z_ker + A2*int_Z2_ker + A3*int_Z3_ker + A4*int_Z4_ker + A5*int_Z5_ker + A6*int_Z6_ker=1,
eqn1:A0*int_Z_ker + A1*int_Z2_ker + A2*int_Z3_ker + A3*int_Z4_ker + A4*int_Z5_ker + A5*int_Z6_ker + A6*int_Z7_ker=0,
eqn2:A0*int_Z2_ker + A1*int_Z3_ker + A2*int_Z4_ker + A3*int_Z5_ker + A4*int_Z6_ker + A5*int_Z7_ker + A6*int_Z8_ker=0,
eqn3:A0*int_Z3_ker + A1*int_Z4_ker + A2*int_Z5_ker + A3*int_Z6_ker + A4*int_Z7_ker + A5*int_Z8_ker + A6*int_Z9_ker=0,
eqn4:A0*int_Z4_ker + A1*int_Z5_ker + A2*int_Z6_ker + A3*int_Z7_ker + A4*int_Z8_ker + A5*int_Z9_ker + A6*int_Z10_ker=0,
eqn5:A0*int_Z5_ker + A1*int_Z6_ker + A2*int_Z7_ker + A3*int_Z8_ker + A4*int_Z9_ker + A5*int_Z10_ker + A6*int_Z11_ker=0,
eqn6:A0*int_Z6_ker + A1*int_Z7_ker + A2*int_Z8_ker + A3*int_Z9_ker + A4*int_Z10_ker + A5*int_Z11_ker + A6*int_Z12_ker=0,
eqn7:A0*int_Z7_ker + A1*int_Z8_ker + A2*int_Z9_ker + A3*int_Z10_ker + A4*int_Z11_ker + A5*int_Z12_ker + A6*int_Z13_ker=0,

kersolve: solve([eqn0,eqn1,eqn2,eqn3,eqn4,eqn5,eqn6,eqn7],[A0,A1,A2,A3,A4,A5,A6]),

define(kereighth(Z), (rhs(kersolve[1][1])+rhs(kersolve[1][2])*Z+rhs(kersolve[1][3])*Z^2+rhs(kersolve[1][4])*Z^3+rhs(kersolve[1][5])*Z^4+rhs(kersolve[1][6])*Z^5+rhs(kersolve[1][7])*Z^6)*kern(Z)),

kereighth_k:integrate(kereighth(Z)^2, Z, -b, b),
kereighth_k_n:ev(kereighth_k,numer),
kereighth_k2:integrate(Z^2*kereighth(Z), Z, -b, b),
kereighth_k2_n:ev(kereighth_k2,numer),
kereighth_k22:integrate((Z*kereighth(Z))^2, Z, -b, b),
kereighth_k22_n:ev(kereighth_k22,numer),

kereighth_km:integrate(kereighth(Z+1/2)*kereighth(Z-1/2), Z, bml, bmh),
kereighth_km_n:ev(kereighth_km,numer),

define(kereighthder(Z), diff(kereighth(Z), Z)),
define(kereighthcdf(x), integrate(kereighth(Z), Z, -b, x)),
define(kereighthconvol_neg(v), integrate(kereighth(u)*kereighth(u-v), u, -b, bvh)),
define(kereighthconvol_pos(v), integrate(kereighth(u)*kereighth(u-v), u, bvl, b)),

define(pker(Z), fullratsimp(kern(Z))),
define(pkerder(Z), fullratsimp(kerder(Z))),
define(pkercdf(Z), fullratsimp(kercdf(Z))),
define(pkerconvol(Z), expand(kerconvol_pos(abs(Z)))),

define(pkerfourth(Z), fullratsimp(kerfourth(Z))),
define(pkerfourthder(Z), fullratsimp(kerfourthder(Z))),
define(pkerfourthcdf(Z), fullratsimp(kerfourthcdf(Z))),
define(pkerfourthconvol(Z), expand(kerfourthconvol_pos(abs(Z)))),

define(pkersixth(Z), fullratsimp(kersixth(Z))),
define(pkersixthder(Z), fullratsimp(kersixthder(Z))),
define(pkersixthcdf(Z), fullratsimp(kersixthcdf(Z))),
define(pkersixthconvol(Z), expand(kersixthconvol_pos(abs(Z)))),

define(pkereighth(Z), fullratsimp(kereighth(Z))),
define(pkereighthder(Z), fullratsimp(kereighthder(Z))),
define(pkereighthcdf(Z), fullratsimp(kereighthcdf(Z))),
define(pkereighthconvol(Z), expand(kereighthconvol_pos(abs(Z)))),

print("second order ", ker, " kernel:"),

print(ev(pker(Z),numer)),

print("second order ", ker, " derivative kernel:"),
print(ev(pkerder(Z),numer)),

print("second order ", ker, " cdf kernel:"),
print(ev(pkercdf(Z),numer)),

print("second order ", ker, " convolution kernel:"),
print(ev(pkerconvol(Z),numer)),

print("kernel constants:"),
print("k = ",ker_k_n, ", k2 = ", ker_k2_n),
print("k22 = ", ker_k22_n, ", km = ", ker_km_n),


print("fourth order ", ker, " kernel:"),

print(ev(pkerfourth(Z),numer)),

print("fourth order ", ker, " derivative kernel:"),
print(ev(pkerfourthder(Z),numer)),

print("fourth order ", ker, " cdf kernel:"),
print(ev(pkerfourthcdf(Z),numer)),

print("fourth order ", ker, " convolution kernel:"),
print(ev(pkerfourthconvol(Z),numer)),

print("kernel constants:"),
print("k = ",kerfourth_k_n, ", k2 = ", kerfourth_k2_n),
print("k22 = ", kerfourth_k22_n, ", km = ", kerfourth_km_n),


print("sixth order ", ker, " kernel:"),

print(ev(pkersixth(Z),numer)),

print("sixth order ", ker, " derivative kernel:"),
print(ev(pkersixthder(Z),numer)),

print("sixth order ", ker, " cdf kernel:"),
print(ev(pkersixthcdf(Z),numer)),

print("sixth order ", ker, " convolution kernel:"),
print(ev(pkersixthconvol(Z),numer)),

print("kernel constants:"),
print("k = ",kersixth_k_n, ", k2 = ", kersixth_k2_n),
print("k22 = ", kersixth_k22_n, ", km = ", kersixth_km_n),

print("eighth order ", ker, " kernel:"),

print(ev(pkereighth(Z),numer)),

print("eighth order ", ker, " derivative kernel:"),
print(ev(pkereighthder(Z),numer)),

print("eighth order ", ker, " cdf kernel:"),
print(ev(pkereighthcdf(Z),numer)),

print("eighth order ", ker, " convolution kernel:"),
print(ev(pkereighthconvol(Z),numer)),

print("kernel constants:"),
print("k = ",kereighth_k_n, ", k2 = ", kereighth_k2_n),
print("k22 = ", kereighth_k22_n, ", km = ", kereighth_km_n))$

