From 51c24b0439bcd8ad71518a9440c20d3c27f68266 Mon Sep 17 00:00:00 2001 From: PESchoenberg Date: Tue, 7 Apr 2020 11:46:48 -0300 Subject: [PATCH] New functions. --- grsp2.scm | 16 +++++++++-- grsp5.scm | 80 ++++++++++++++++++++++++++++++++----------------------- 2 files changed, 60 insertions(+), 36 deletions(-) diff --git a/grsp2.scm b/grsp2.scm index aa0ade2..0d3fe7b 100644 --- a/grsp2.scm +++ b/grsp2.scm @@ -57,7 +57,8 @@ grsp-method-euler grsp-lerp grsp-givens-rotation - grsp-fitin)) + grsp-fitin + grsp-fitin-0-1)) ; grsp-gtels - Finds if p_n1 is greater, equal or smaller than p_n2. @@ -780,5 +781,16 @@ ((< p_n1 p_nmin) (set! res1 p_nmin))) - res1)) + res1)) + + +; grsp-fitin-0-1 - applies grsp-fitin to p_n1 within the interval [0.0,1.0] +; +; Arguments: +; p_n1: real. +; +(define (grsp-fitin-0-1 p_n1) + + (grsp-fitin p_n1 0.0 1.0)) + diff --git a/grsp5.scm b/grsp5.scm index 272bc33..17f49f3 100644 --- a/grsp5.scm +++ b/grsp5.scm @@ -23,7 +23,9 @@ ; ; ============================================================================== + ; https://en.wikipedia.org/wiki/Probability +; https://en.wikipedia.org/wiki/Bayes%27_theorem (define-module (grsp grsp5) #:use-module (grsp grsp0) @@ -42,20 +44,29 @@ grsp-pcond)) -; grsp-feature-scaling - scales (normalizes) p_n between [0,1]. +; grsp-feature-scaling - scales p_n to the interval [p_nmin, p_nmax]. ; ; Arguments: -; - p_n: scalar. +; - p_n1: scalar, real. ; - p_nmin: min value for p_n. ; - p_max: max value for p_x. ; ; Sources: ; - https://www.statisticshowto.datasciencecentral.com/normalized/ ; -(define (grsp-feature-scaling p_n p_nmin p_nmax) - (let ((res1 0)) +; Notes: +; - If p_n1 lies outside the interval [p_nmin, p_nmax] the function will trunctate +; p_n1 to fit it within the interval. +; +(define (grsp-feature-scaling p_n1 p_nmin p_nmax) + (let ((res1 0.0)) - (set! res1 (* 1.0 (/ (- p_n p_nmin) (- p_nmax p_nmin)))) + (cond ((> p_n1 p_nmax) + (set! p_nmax p_n1)) + ((< p_n1 p_nmin) + (set! p_nmin p_n1))) + (set! res1 (* 1.0 (/ (- p_n1 p_nmin) (- p_nmax p_nmin)))) + res1)) @@ -70,9 +81,10 @@ ; - https://www.statisticshowto.datasciencecentral.com/normalized/ ; (define (grsp-z-score p_n1 p_m1 p_s1) - (let ((res1 0)) + (let ((res1 0.0)) (set! res1 (* 1.0 (/ (- p_n1 p_m1) p_s1))) + res1)) @@ -90,7 +102,7 @@ ; - p_n3: real. ; (define (grsp-binop p_s1 p_n1 p_n2 p_n3) - (let ((res1 0)) + (let ((res1 0.0)) (cond ((equal? p_s1 "#+") (set! res1 (expt (+ p_n1 p_n2) p_n3))) @@ -110,9 +122,9 @@ ;- p_n1: real representing a probability in [0,1] ; (define (grsp-pnot p_n1) - (let ((res1 1)) + (let ((res1 1.0)) - (set! res1 (- res1 (grsp-fitin p_n1 0.0 1.0))) + (set! res1 (- res1 (grsp-fitin-0-1 p_n1))) res1)) @@ -125,28 +137,28 @@ ;- p_n2: real repesenting a probability in [0,1] ; (define (grsp-pand p_n1 p_n2) - (let ((res1 1)) + (let ((res1 1.0)) - (set! res1 (* (grsp-fitin p_n1 0.0 1.0) (grsp-fitin p_n2 0.0 1.0))) + (set! res1 (* (grsp-fitin-0-1 p_n1) (grsp-fitin-0-1 p_n2))) res1)) ; grsp-pnand - calculates the probability of p_n1 and p_n2 happening, -; being not independent. +; being those not independent. ; ; Arguments: ;- p_n1: real repesenting a probability in [0,1] ;- p_n2: real repesenting a probability in [0,1] ; (define (grsp-pnand p_n1 p_n2) - (let ((res1 1) - (n1 0) - (n2 0)) + (let ((res1 1.0) + (n1 0.0) + (n2 0.0)) - (set! n1 (grsp-fitin p_n1 0.0 1.0)) - (set! n2 (grsp-fitin p_n2 0.0 1.0)) - (set! res1 (* (grsp-pcond n1 n2) n2)) + (set! n1 (grsp-fitin-0-1 p_n1)) + (set! n2 (grsp-fitin-0-1 p_n2)) + (set! res1 (* (grsp-pand n1 n2) n2)) res1)) @@ -158,12 +170,12 @@ ;- p_n2: real repesenting a probability in [0,1] ; (define (grsp-por p_n1 p_n2) - (let ((res1 1) - (n1 0) - (n2 0)) + (let ((res1 1.0) + (n1 0.0) + (n2 0.0)) - (set! n1 (grsp-fitin p_n1 0.0 1.0)) - (set! n2 (grsp-fitin p_n2 0.0 1.0)) + (set! n1 (grsp-fitin-0-1 p_n1)) + (set! n2 (grsp-fitin-0-1 p_n2)) (set! res1 (- (+ n1 n2) (grsp-pand n1 n2))) res1)) @@ -177,12 +189,12 @@ ;- p_n2: real repesenting a probability in [0,1] ; (define (grsp-pxor p_n1 p_n2) - (let ((res1 0) - (n1 0) - (n2 0)) + (let ((res1 0.0) + (n1 0.0) + (n2 0.0)) - (set! n1 (grsp-fitin p_n1 0.0 1.0)) - (set! n2 (grsp-fitin p_n2 0.0 1.0)) + (set! n1 (grsp-fitin-0-1 p_n1)) + (set! n2 (grsp-fitin-0-1 p_n2)) (cond ((<= (+ n1 n2) 1) (set! res1 (+ n1 n2)))) @@ -196,13 +208,13 @@ ;- p_n2: real repesenting a probability in [0,1] ; (define (grsp-pcond p_n1 p_n2) - (let ((res1 0) - (n1 0) - (n2 0)) + (let ((res1 0.0) + (n1 p_n1) + (n2 p_n2)) - (set! n1 (grsp-fitin p_n1 0.0 1.0)) - (set! n2 (grsp-fitin p_n2 0.0 1.0)) - (cond ((> n2 0) + (set! n1 (grsp-fitin-0-1 p_n1)) + (set! n2 (grsp-fitin-0-1 p_n2)) + (cond ((> n2 0.0) (set! res1 (/ (grsp-pand n1 n2) n2)))) res1))