(ns code.privacy.morph_v3 (:use (code.utils utils preprocess)) (:use (code.pls cliff ncliff)) (:use (incanter core stats))) (def *j*) (def *ans1*) (defn morph3 [n data sav r _ orig-prototype-fn distance-fn] " This version of MORPH seeks to improve the runtime of the algorithm by reducing the number of times the distance function is called. To accomplish this, the original CLIFF algorithm is used to sample the original data. In turn, the mean is found from the remaining instances for a class and its NUN is found. This NUN is used by all instances of the same class for morphing. (NUN here can be looked at as the 'best' NUN for all the instance of a class) distance-fn is numeric since using orig-prototype-fn is ocliff r is range for nudge [0.1501 0.3501]% of difference between points Example: (morph3 10 (pre-data ant13) [10 20] [0.1501 0.3501] 9 ocliff numeric) Output is a morphed data-set " (let [max-vals (apply vector (map #(apply max %) (trans data))) min-vals (apply vector (map #(apply min %) (trans data))) cliff-data (orig-prototype-fn n data 9 9 9) clump-data (group-by last cliff-data) clumps (map matrix (map second clump-data)) klasses (map first clump-data) klass-NUNs (loop [klass klasses clump clumps result []] (if (empty? klass) result (recur (rest klass) (rest clump) (conj result (vector (first klass) (first (get-nearest (centroid (first clump)) (let [mydata (apply bind-rows (map matrix (map second (filter #(not= (first klass) (first %)) clump-data))))] (if (= (nrow mydata) 1) [mydata] mydata)) distance-fn))))))) nuns (fn [klass] (second (first (filter #(= klass (first %)) klass-NUNs)))) nudge-val (fn [orig-val nun-val idx] (if (member? idx sav) orig-val (let [diff (abs (- orig-val nun-val)) nudge (+ (first r) (rand (last r))) max-val (nth max-vals idx) min-val (nth min-vals idx)] (if (< (* nudge diff) min-val) min-val (if (> (* nudge diff) max-val) max-val (* nudge diff)))))) morph-data (loop [d data result []] (if (empty? d) (matrix result) (recur (rest d) (conj result (let [new-x (fn [] (loop [orig-val (first d) nun-val (nuns (last (first d))) i 0 result []] (if (empty? orig-val) result (recur (rest orig-val) (rest nun-val) (inc i) (conj result (nudge-val (first orig-val) (first nun-val) i))))))] (new-x))))))] ; (binding [*j* 0 ; *ans1* (new-x)] ; (while (= *j* 0) ; (if (member? *ans1* data) ; (set! *j* 0) ; (set! *j* 1)) ; (set! *ans1* (new-x))) ; *ans1*))))))] morph-data))