~ubuntu-branches/ubuntu/maverick/openturns/maverick

« back to all changes in this revision

Viewing changes to debian/rotRPackage/R/estimateTruncatedNormalParameters.R

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Prud'homme
  • Date: 2008-10-03 10:55:20 UTC
  • Revision ID: james.westby@ubuntu.com-20081003105520-i4sqk110f19vp7hd
Tags: 0.12.1-6
* debian/rules: Fix FTBS on hppa, sparc, arm, and armel because of
  __sync_fetch_and_add_4 not being available, the remedy is to use
  -DBOOST_SP_USE_PTHREADS
* debian/rules: add --disable-debug option to configure and set the
  compiler flags to -g -O2 (no -Wall)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"estimateTruncatedNormalParameters" <-
 
2
function(numericalSample, testLevel = 0.975){
 
3
  
 
4
  # Checking for NAs in sample
 
5
  numericalSample <- numericalSample[ ! is.na(numericalSample)]
 
6
 
 
7
  a <- min(numericalSample)
 
8
  b <- max(numericalSample)
 
9
  # First guess for the optimization step. Should find a better solution.
 
10
  mean <- mean(numericalSample)
 
11
  var <- var(numericalSample)
 
12
  mean0 <- mean
 
13
  var0 <- var
 
14
  for (i in 1:100) {
 
15
    sd <- sqrt(var)
 
16
    phiA <- dnorm(a, mean, sd)
 
17
    phiB <- dnorm(b, mean, sd)
 
18
    PhiA <- pnorm(a, mean, sd)
 
19
    PhiB <- pnorm(b, mean, sd)
 
20
    denom <- (PhiB - PhiA - (mean0 - a) * phiA - (b - mean0) * phiB)
 
21
    var <- var0 * (PhiB - PhiA) / denom
 
22
    mean <- mean0 + var * (phiB - phiA) / (PhiB - PhiA)
 
23
  }
 
24
  
 
25
  # For reasons of data types and calls, we need to use an auxiliary function. However, 
 
26
  # this is just an intermediary step, we still use fitdistr on the Beta Pdf.
 
27
  AuxiliaryTruncatedNormal <- function(numericalSample,...){
 
28
    # Call to fitdistr (MASS package) .
 
29
    estimation <- fitdistr(numericalSample, computeTruncatedNormalPdf, 
 
30
                           start = list(mu = mean, sigma = sqrt(var)), ...)
 
31
  }
 
32
 
 
33
  estimation <- NA
 
34
  try(estimation <- AuxiliaryTruncatedNormal(numericalSample, a = (a - .Machine$double.eps), b = (b + .Machine$double.eps)))
 
35
  if (is.na(estimation))
 
36
  {
 
37
    # If the first method of estimation fails, we try the L-BFGS-B method.
 
38
    estimation <- AuxiliaryTruncatedNormal(numericalSample, a = (a - .Machine$double.eps), b = (b + .Machine$double.eps), method = "L-BFGS-B", lower = c(-Inf,0))
 
39
  }
 
40
 
 
41
  # Checking whether loglikelihood is supported in the current R version. 
 
42
  if (length(estimation$loglik) == 0) {
 
43
    warning("Warning: the current R version does not support the loglikelihood computation")
 
44
    estimation$loglik = "Warning: the current R version does not support the loglikelihood computation"
 
45
  }
 
46
 
 
47
  # Computation of the t-value used in the CIs
 
48
 
 
49
  tValue <- qt(testLevel, length(numericalSample)-1)
 
50
 
 
51
  confidenceIntervalmu <- c(as.numeric(estimation$estimate[1]) - 
 
52
                              tValue * as.numeric(estimation$sd[1]),
 
53
                            as.numeric(estimation$estimate[1]) + 
 
54
                              tValue * as.numeric(estimation$sd[1]))
 
55
 
 
56
  confidenceIntervalsigma <- c(as.numeric(estimation$estimate[2]) - 
 
57
                                 tValue * as.numeric(estimation$sd[2]),
 
58
                               as.numeric(estimation$estimate[2]) + 
 
59
                                 tValue * as.numeric(estimation$sd[2]))
 
60
  
 
61
  estimatedTruncatedNormal <- list(distribution = "TruncatedNormal",
 
62
                        mu = as.numeric(estimation$estimate[1]),
 
63
                        sigma = as.numeric(estimation$estimate[2]),
 
64
                        a = a,
 
65
                        b = b,
 
66
                        confidenceIntervalmu = confidenceIntervalmu,
 
67
                        confidenceIntervalsigma = confidenceIntervalsigma,
 
68
                        logLikelihood = estimation$loglik)
 
69
}