function res=gm(r,sigma) 

% given x = [r ,sigma] see how good these parameters are to predict
% options prices.  
% res = sum over all options of ( market price - BS price(r,sigma) )^2  

% GM Friday Oct 16 close 33.15  (next ex div in December) 

% nov 20 calls and puts - I removed some with low open interest 
% data from yahoo finance 

%   strike  last bid  ask    vol openint  impvol 
C= [28.00	5.56 4.95 5.75   2	 101      62.60
    29.00	4.35 4.00 4.55   8	 1033     46.19
    30.00	3.60 3.15 3.70   36	 4409     43.60
    31.00	2.64 2.30 2.61   43	 13511    32.08
    32.00	1.71 1.73 1.83   239 3050     29.40
    33.00	1.11 1.10 1.16   634 2978     26.91
    34.00	0.61 0.64 0.67   456 15470    25.44
    35.00	0.34 0.32 0.34   160 4413     24.22
    36.00	0.19 0.14 0.16   16	 577      23.78]; 

%    strike last  bid  ask    vol   openint impvol 
P = [23.00	0.05  0.01 0.07   10	1403    59.38
     24.00	0.08  0.02 0.07   3     521     54.30
     25.00	0.15  0.03 0.09   10	776     50.78
     26.00	0.10  0.04 0.10   3     5414    49.41
     27.00	0.08  0.06 0.09   384	132     42.19
     28.00	0.14  0.06 0.14   1     1327    39.84
     29.00	0.16  0.14 0.17   38	1759    35.16
     30.00	0.27  0.22 0.24   3     6739    31.74
     31.00	0.38  0.36 0.39   104	11486   29.79
     32.00	0.69  0.59 0.62   997	831     27.78
     33.00	1.00  0.95 0.99   415	1443    26.42
     34.00  1.55  1.44 1.70   4     5103    30.08
     35.00	2.25  2.09 2.43   118	147     31.40]; 

T=25;       % expiry is Friday Nov 20, 25 days away 
S=33.15;
res=0;
for i=1:size(C,1)
    K  =  C(i,1) ;
    d1 =  (log(S/K) + (r + sigma^2/2) *T)/ sigma / sqrt(T); 
    d2 =  (log(S/K) + (r - sigma^2/2) *T)/ sigma / sqrt(T); 
    c   = normcdf(d1) * S - normcdf(d2) * K * exp(-r*T);
    res = res + (c - (C(i,3)+C(i,4))/2 )^2;
end 
for i=1:size(P,1)
    K =   P(i,1) ;
    d1 =  (log(S/K) + (r + sigma^2/2) *T)/ sigma / sqrt(T); 
    d2 =  (log(S/K) + (r - sigma^2/2) *T)/ sigma / sqrt(T); 
    p  = - normcdf(-d1) * S + normcdf(-d2) * K * exp(-r*T);
    res = res + (p - (P(i,3)+P(i,4))/2 )^2;
end 