Clear[GetBeta, Cost];
GetBeta[{cc_,epsilon_,k1_,k2_}]:=Module[{beta,a,b,c,d},
d=cc;
c=k2^2+epsilon^2;
b=k1*k2;
a=k1^2/3;

Return[{d,c,b,a}]
];

Cost[pars_,lam2_,x_,y_]:=Module[{beta,X,nllf,s,i},
beta = GetBeta[pars];

s=2*y-1;
X=Transpose[Table[x^i,{i,0,3}]];
nllf = Total[Log[1+Exp[-s*(X.beta)]]];

Return[nllf + lam2 Total[pars^2]]
]

(*TRIM, log*)
x={0.7444588568509963,0.4294306635860587,0.2424791244329582,0.1722166015253219,0.5155957608380367,-0.0044000248522457,0.2619898405306257,0.3707014579741952,0.3458779082346233,-0.0230997734892581,1.4235506263993014,0.1974151389300383,0.3479428293749495,0.038497594143739,-0.0494408800705622,0.1562561841152657,0.2440810426279506,0.738605475021583,0.3388466870335315,0.2730058878681247,0.2349466740981531,0.0152345391191637,-0.1306983645886834,0.3863126943346241,0.32696785342031,0.3960496407131636,0.116543550839155,0.3745678322136136,0.3902449371047537,-0.0224728544974076,0.3120049574170508,0.140909818171296,0.4065884615650837,0.1347990203891564,0.1747544412028135,0.8025435019583259,0.3424702625117166,-0.2576749555237702,0.2098741187455145,0.1827383448113199,0.4344451063845957,0.3628263770555691,0.3418422883292066,0.2690071065545839,0.3785734614926337,0.2619248908950089,0.232085349459071,0.1599028123673964,0.5800908847567773,0.208084169807583,-0.0239525163747711,0.2010508033108026,0.4280507666730073,0.4184257480672208,0.444582693795233,0.2107665896841349,0.3642384221371574}
y={1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
p={-1.1319747348643707 10^1,-6.5316161792581431* 10^-5,1.6944026199144295,3.9357124963564112}
lam2=10^-3

Cost[p,lam2,x,y]

Needs["NumericalCalculus`"]

p

(*gradient*)
DCost[i_,p_]:=Module[{u,ii},ND[Cost[Table[If[i==ii,u,p[[ii]]],{ii,Length[p]}],lam2,x,y],u,p[[i]],Terms->10]];
Table[DCost[i,p],{i,Length[p]}]

(* Hessian*)
HCost[i_,j_,p]:=Module[{u,v,k,F,G},
F[v_]:=ND[Cost[Table[If[i==k,u,If[j==k,v,p[[k]]]],{k,Length[p]}],lam2,x,y],u,p[[i]],Terms->10];
G:=ND[Cost[Table[If[i==k,u,p[[k]]],{k,Length[p]}],lam2,x,y],{u,2},p[[i]],Terms->10];

If[i!=j,
ND[F[v],v,p[[j]]],
G]
];

H0=Table[HCost[i,j,p],{i,Length[p]},{j,Length[p]}]

MatrixForm[H0]

MatrixForm[Inverse[H0]]