anchor.diff:  	(v-scale (tmp-vec1-of learner) (* alpha training-label) (tmp-vec2-of learner))
   	(v+ (weight-of learner) (tmp-vec2-of learner) (weight-of learner))
   	;; Update bias
  -	(setf (bias-of learner) (+ (bias-of learner) (* alpha (bias-confidence-of learner) training-label)))
  -	;; Update weight-confidence
  +	(setf (bias-of learner) (+ (bias-of learner) (* alpha (sigma0-of learner) training-label)))
  +	;; Update sigma
   	(diagonal-matrix-multiplication (tmp-vec1-of learner) (tmp-vec1-of learner) (tmp-vec1-of learner))
   	(v-scale (tmp-vec1-of learner) beta (tmp-vec1-of learner))
  -	(v- (weight-confidence-of learner) (tmp-vec1-of learner) (weight-confidence-of learner))
  -	;; Update bias-confidence
  -	(setf (bias-confidence-of learner)
  -	      (- (bias-confidence-of learner)
  -		 (* beta (bias-confidence-of learner)
  -		    (bias-confidence-of learner)))))))
  -  learner)
  +	(v- (sigma-of learner) (tmp-vec1-of learner) (sigma-of learner))
  +	;; Update sigma0
  +	(setf (sigma0-of learner)
  +	      (- (sigma0-of learner)
  +		 (* beta (sigma0-of learner)
  +		    (sigma0-of learner)))))))
  +  learner)
  +
  +;;; SCW-I
  +
  +;; Approximation of error function
  +(defun inverse-erf (x)
  +  (let* ((a (/ (* 8d0 (- pi 3d0))
  +	       (* 3d0 pi (- 4d0 pi))))
  +	 (c2/pia (/ 2d0 pi a))
  +	 (ln1-x^2 (log (- 1d0 (* x x))))
  +	 (comp (+ c2/pia (/ ln1-x^2 2d0))))
  +  (* (sign x)
  +     (sqrt (- (sqrt (- (* comp comp) (/ ln1-x^2 a)))
  +	      comp)))))
  +
  +(defun probit (p)
  +  (* (sqrt 2d0)
  +     (inverse-erf (- (* 2d0 p) 1d0))))
  +
  +(defclass$ scw1 (learner)
  +  ;; Meta parameters
  +  eta C
  +  ;; Internal parameters
  +  phi psi zeta sigma sigma0
  +  tmp-vec1 tmp-vec2)
  +
  +(defun make-scw1 (input-dimension eta C)
  +  (check-type input-dimension integer)
  +  (check-type eta double-float)
  +  (check-type C double-float)
  +  (assert (< 0d0 eta 1d0))
  +  (let* ((phi (probit eta))
  +	 (psi (+ 1d0 (/ (* phi phi) 2d0)))
  +	 (zeta (+ 1d0 (* phi phi))))
  +    (make-instance 'scw1
  +       :input-dimension input-dimension
  +       :weight (make-dvec input-dimension 0d0)
  +       :bias 0d0
  +       :eta eta
  +       :C C
  +       :phi phi
  +       :psi psi
  +       :zeta zeta
  +       :sigma (make-dvec input-dimension 1d0)
  +       :sigma0 1d0
  +       :tmp-vec1 (make-dvec input-dimension 0d0)
  +       :tmp-vec2 (make-dvec input-dimension 0d0))))
  +
  +(defmethod update ((learner scw1) input training-label)
  +  (let* ((phi (phi-of learner))
  +	 (m (* training-label (f input (weight-of learner) (bias-of learner))))
  +	 (v (+ (sigma0-of learner)
  +	       (inner-product (diagonal-matrix-multiplication (sigma-of learner) input (tmp-vec1-of learner))
  +			      input)))
  +	 (loss (- (* phi (sqrt v)) m)))
  +    (if (> loss 0d0)
  +      (let* ((psi (psi-of learner))
  +	     (zeta (zeta-of learner))
  +	     (alpha (min (C-of learner)
  +			 (max 0d0
  +			      (- (sqrt (+ (/ (* m m phi phi phi phi) 4d0)
  +					  (* v phi phi zeta)))
  +				 (* m psi)))))
  +	     (u (let ((base (- (sqrt (+ (* alpha alpha v v phi phi) (* 4d0 v))) (* alpha v phi))))
  +		  (/ (* base base) 4d0)))
  +	     (beta (/ (* alpha phi)
  +		      (+ (sqrt u) (* v alpha phi)))))
  +	;; Update weight
  +	(v-scale (tmp-vec1-of learner) (* alpha training-label) (tmp-vec2-of learner))
  +	(v+ (weight-of learner) (tmp-vec2-of learner) (weight-of learner))
  +	;; Update bias
  +	(setf (bias-of learner) (+ (bias-of learner) (* alpha (sigma0-of learner) training-label)))
  +	;; Update sigma
  +	(diagonal-matrix-multiplication (tmp-vec1-of learner) (tmp-vec1-of learner) (tmp-vec1-of learner))
  +	(v-scale (tmp-vec1-of learner) beta (tmp-vec1-of learner))
  +	(v- (sigma-of learner) (tmp-vec1-of learner) (sigma-of learner))
  +	;; Update sigma0
  +	(setf (sigma0-of learner)
  +	      (- (sigma0-of learner)
  +		 (* beta (sigma0-of learner)
  +		    (sigma0-of learner)))))))
  +  learner)
  +
  +;;; SCW-II
  +
  +(defclass$ scw2 (learner)
  +  ;; Meta parameters
  +  eta C
  +  ;; Internal parameters
  +  phi sigma sigma0
  +  tmp-vec1 tmp-vec2)
  +
  +(defun make-scw2 (input-dimension eta C)
  +  (check-type input-dimension integer)
  +  (check-type eta double-float)
  +  (check-type C double-float)
  +  (assert (< 0d0 eta 1d0))
  +  (let ((phi (probit eta)))
  +    (make-instance 'scw2
  +       :input-dimension input-dimension
  +       :weight (make-dvec input-dimension 0d0)
  +       :bias 0d0
  +       :eta eta
  +       :C C
  +       :phi phi
  +       :sigma (make-dvec input-dimension 1d0)
  +       :sigma0 1d0
  +       :tmp-vec1 (make-dvec input-dimension 0d0)
  +       :tmp-vec2 (make-dvec input-dimension 0d0))))
  +
  +(defmethod update ((learner scw2) input training-label)
  +  (let* ((phi (phi-of learner))
  +	 (m (* training-label (f input (weight-of learner) (bias-of learner))))
  +	 (v (+ (sigma0-of learner)
  +	       (inner-product (diagonal-matrix-multiplication (sigma-of learner) input (tmp-vec1-of learner))
  +			      input)))
  +	 (loss (- (* phi (sqrt v)) m)))
  +    (if (> loss 0d0)
  +      (let* ((n (+ v (/ 1d0 (* 2d0 (C-of learner)))))
  +	     (gamma (* phi
  +		       (sqrt (+ (* phi phi m m v v)
  +				(* 4d0 n v (+ n (* v phi phi)))))))
  +	     (alpha (max 0d0
  +			 (/ (- gamma (+ (* 2d0 m n) (* phi phi m v)))
  +			    (* 2d0 (+ (* n n) (* n v phi phi))))))
  +	     (u (let ((base (- (sqrt (+ (* alpha alpha v v phi phi) (* 4d0 v))) (* alpha v phi))))
  +		  (/ (* base base) 4d0)))
  +	     (beta (/ (* alpha phi)
  +		      (+ (sqrt u) (* v alpha phi)))))
  +	;; Update weight
  +	(v-scale (tmp-vec1-of learner) (* alpha training-label) (tmp-vec2-of learner))
  +	(v+ (weight-of learner) (tmp-vec2-of learner) (weight-of learner))
  +	;; Update bias
  +	(setf (bias-of learner) (+ (bias-of learner) (* alpha (sigma0-of learner) training-label)))
  +	;; Update sigma
  +	(diagonal-matrix-multiplication (tmp-vec1-of learner) (tmp-vec1-of learner) (tmp-vec1-of learner))
  +	(v-scale (tmp-vec1-of learner) beta (tmp-vec1-of learner))
  +	(v- (sigma-of learner) (tmp-vec1-of learner) (sigma-of learner))
  +	;; Update sigma0
  +	(setf (sigma0-of learner)
  +	      (- (sigma0-of learner)
  +		 (* beta (sigma0-of learner)
  +		    (sigma0-of learner)))))))
  +  learner)
  
positive.code:   learner)
  
  ;;; Averaged Perceptron
  (defclass averaged-perceptron (perceptron)
    ((data-size :accessor data-size-of :initarg :data-size)
     (averaged-weight :accessor averaged-weight-of :initarg :averaged-weight)
     (averaged-bias :accessor averaged-bias-of :initarg :averaged-bias)
     (counter :accessor counter-of :initarg :counter)
     (tmp-vec :accessor tmp-vec-of :initarg :tmp-vec)))
  
  (defun make-averaged-perceptron (input-dimension data-size)
    (check-type input-dimension integer)
  
negative.code:   (+ (inner-product weight input) bias))
  
  ;;; Definition CLOS object
  (defclass$ learner () input-dimension weight bias)
  
  ;;; Definition generic functions
  (defgeneric predict (learner input))
  (defgeneric test (learner test-data  &key quiet-p))
  (defgeneric update (learner input training-label))
  (defgeneric train (learner training-data))
  (defgeneric train-with-interim-test (learner training-data test-data span))
  
  ;;; Prediction
  (defmethod predict ((learner learner) input)
    (sign (f input (weight-of learner) (bias-of learner))))
  
  ;;; Testing with a test data list (1-pass)
  (defmethod test ((learner learner) test-data &key (quiet-p nil))
    (let* ((len (length test-data))
  	 (n-correct (count-if (lambda (datum)
  				(= (predict learner (cdr datum)) (car datum)))
  			      test-data))
  	 (accuracy (* (/ n-correct len) 100.0)))
      (if (not quiet-p)
        (format t "Accuracy: ~f%, Correct: ~A, Total: ~A~%" accuracy n-correct len))
      (values accuracy n-correct len)))
  
  ;;; Update inner parameters destructively (1step)
  