Use Gaussian distribution to solve the exception detection problem (2)

Source: Internet
Author: User

(For Original Articles, please indicate the source for reprinting !)

This article uses Gaussian distribution to solve the problem of exception detection (1) describes how to use Gaussian distribution to solve the problem of exception detection, this article uses R Programming to Implement the two models described in the first article: Multiple one-dimensional Gaussian distribution models and a multivariate Gaussian distribution model.

I. Multiple Gaussian distribution models

 1 ## parameters: 2 ##     x  -  a vector, which is the data of new samples. 3 ##     X  -  a matrix, which stores samples‘ data. 4 ##     parameterFile - path of paramter file,  5 ##                     the paramter file stores the paramters of the MultiUnivariate Norm model. 6 ##     isTraining - flag, TRUE will trigger the training,  7 ##                        FALSE will skip the training. 8 funMultiUnivariateNorm <- function(x, X = NULL, parameterFile = ".MultiUnivariateNorm", isTraining = FALSE)  9 {10     if (isTraining == TRUE) {11         if (is.null(X) == TRUE) {12             cat("X is NULL, MultiUnivariateNorm model Can‘t be trained\n")13             return14         } 15         numOfSamples <- dim(X)[1]16         numOfFeatures  <- dim(X)[2]17         18         vectrMean <- colMeans(X)19         vectrSD <- numeric(0)20         for (i in 1:numOfFeatures) {21             vectrSD[i] <- sd(X[,i])22         }23         24         ## write the parameters to the file25         ##   1st line is means divided by one blank 26         ##   2nd line is SDs divided by one blank27         matrixMeanSD <- matrix(c(vectrMean, vectrSD), ncol=numOfFeatures, byrow=TRUE)28         # checking of parameterFile leaves to write.table29         write.table(x=matrixMeanSD, file=parameterFile, row.names=FALSE, col.names=FALSE, sep=" ")30     } else {31         matrixMeanSD <- read.table(file=parameterFile)32         matrixMeanSD <- as.matrix(matrixMeanSD)33         vectrMean <- matrixMeanSD[1,]34         vectrSD <- matrixMeanSD[2,]        35     }36     37     vectrProbabilityNewSample <- dnorm(x, mean = vectrMean, sd = vectrSD, log = FALSE)38     prod(vectrProbabilityNewSample)  # probability of the new sample39 }

 

2. A Multivariate Gaussian distribution model

 1 ## Before using this function the package mvtnorm need to be installed. 2 ## To install package mvtnorm, issuing command install.packages("mvtnorm") 3 ## and using command library(mvtnorm) to load the package to R workspace. 4 ##  5 ## parameters: 6 ##     x  -  a vector, the data of one samples that need to be calculate the output by the MultiUnivariate Norm model. 7 ##           a matrix, each line is one sample that need to be calculate the output by the MultiUnivariate Norm model. 8 ##     X  -  a matrix, which stores samples‘ data. 9 ##     parameterFile - path of paramter file, 10 ##                     the paramter file stores the paramters of the MultiUnivariate Norm model.11 ##     isTraining - flag, TRUE will trigger the training, 12 ##                        FALSE will skip the training.13 funMultivariateNorm <- function(x, X = NULL, parameterFile = ".MultivariateNorm", isTraining = FALSE) 14 {15     if (isTraining == TRUE) {16         if (is.null(X) == TRUE) {17             cat("X is NULL, MultivariateNorm model Can‘t be trained\n")18             return19         } 20         21         vectrMean <- colMeans(X)22         matrixSigma <- cov(X)23         ## write the parameters to the file24         ##   1st line is means divided by one blank 25         ##   from the 2nd line to the last line are variances divided by one blank26         matrixMeanCov <- rbind(vectrMean, matrixSigma)27         # checking of parameterFile leaves to write.table28         write.table(x=matrixMeanCov, file=parameterFile, row.names=FALSE, col.names=FALSE, sep=" ")29     } else {30         matrixMeanCov <- read.table(file=parameterFile)31         matrixMeanCov <- as.matrix(matrixMeanCov)32         vectrMean <- matrixMeanCov[1,]33         matrixSigma <- matrixMeanCov[c(2:dim(matrixMeanCov)[1]),] 34     }35     36     dmvnorm(x, mean = vectrMean, sigma = matrixSigma, log = FALSE) # probability of the new samples
37 }

Use Gaussian distribution to solve the exception detection problem (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.