public Func <double, double> AreaFromXToUpperLimitFn(SGNFnAParam A) { Func <double, double> dummy = null; if (this.F == null) { dummy = delegate(double x) { return(Tools.NA); }; } else { Func <double, double> area = delegate(double xParm) { return(F(xParm, A)); }; dummy = delegate(double x) { Numerics.Integration.NumericIntegration.Results res; res = Numerics.Integration.NumericIntegration.Integrate(area, x, A.UpperLimit); return(res.Value); }; } return(dummy); }
internal SGNFnAParam Clone() { SGNFnAParam a = (SGNFnAParam)this.MemberwiseClone(); if (this.Range == null) { a.Range = null; } else if (this.Range.Length == 0) { a.Range = new double[0]; } else { a.Range = this.Range.Copy(); } if (this.Truevalues == null) { a.Truevalues = null; } else if (this.Truevalues.Length == 0) { a.Truevalues = new double[0]; } else { a.Truevalues = this.Truevalues.Copy(); } return(a); }
internal override double LogFSecond(double s, SGNFnAParam A) { double z = A.Mu / s; LPhiObject l = WebExpoFunctions3.LPhi(z); return(A.N / Math.Pow(s, 2) - 6.0 * A.B / Math.Pow(s, 4) + A.N * z * ((Math.Pow(z, 2) - 2.0) * l.R[0] + z * l.R2[0]) / Math.Pow(s, 2)); }
internal override double LogFSecond(double sigma, SGNFnAParam A) { double s2 = Math.Pow(sigma, 2); double s4 = Math.Pow(s2, 2); return(A.N / s2 - 6.0 * A.B / s4); }
internal override double LogFPrime(double s, SGNFnAParam A) { double z = A.Mu / s; LPhiObject l = WebExpoFunctions3.LPhi(z); return(-A.N / s + 2.0 * A.B / Math.Pow(s, 3) + A.N * z * l.R[0] / s); }
/* * log.f.inv.remote <- function(target, A) * { * tmp <- 1 + c(-1,1)*sqrt(2*A$cv2*abs(target)) * tmp <- tmp[tmp>0] * roots <- log(tmp/A$y) * * C <- -(A$y-1)^2/2/A$cv2 - A$mu^2/2/A$sigma2 * B <- -1 + (A$y-1)*A$y/A$cv2 + A$mu/A$sigma2 * qA <- -A$y2/2/A$cv2 - 1/2/A$sigma2 * tmp <- quadratic.solution(c(C, B, qA), target=target) * * roots <- c(roots, tmp) * roots * } # end of log.f.inv.remote * } */ internal override List <double> LogFInvRemote(double target, SGNFnAParam A) { List <double> roots = new List <double>(); double z = Math.Sqrt(2.0 * A.CV2 * Math.Abs(target)); double pPetit = 1.0 - z; double pGrand = 1.0 + z; if (pPetit > 0.0) { roots.Add(Math.Log(pPetit / A.Y)); roots.Add(Math.Log(pGrand / A.Y)); } else if (pGrand > 0.0) { roots.Add(Math.Log(pGrand / A.Y)); } double C = -Math.Pow(A.Y - 1.0, 2) / 2.0 / A.CV2 - Math.Pow(A.Mu, 2) / 2.0 / A.Sigma2; double B = -1.0 + (A.Y - 1.0) * A.Y / A.CV2 + A.Mu / A.Sigma2; double qA = -A.Y2 / 2.0 / A.CV2 - 1.0 / 2.0 / A.Sigma2; roots.AddRange(QuadraticEquation.GetRealRoots(Tools.Combine(C, B, qA), target, ROrder: true)); return(roots); }
internal override double LogF(double v, SGNFnAParam A) { return (-A.N * Math.Log(v) - A.B / Math.Pow(v, 2) - A.N * NormalDistribution.PNorm(1.0 / v, log_p: true)); }
} //# end of leftSide.fastScan private L1 DipCharacteristics(Bounds b, ISecuredNR o, SGNFnAParam A, double epsilon = 1e-8) { bool cntn = true; double x = 0, h = 0, hp = 0; while (cntn) { //# intersection between the tangents at both ends of bounds (b) double bDiff = b.Hp.Diff()[0]; if (bDiff == 0) { x = (b.X.Mean()); } else { x = (b.Hp.Multiply(b.X).Diff()[0] - b.H.Diff()[0]) / bDiff; if (x <= b.X[0] | x >= b.X[1]) { x = b.X.Mean(); } } h = o.H(x, A); hp = o.HPrime(x, A); int side = hp < 0 ? 0 : 1; b.X[side] = x; b.H[side] = h; b.Hp[side] = hp; cntn = b.X.Diff()[0] > epsilon; } return(new L1(x, h, hp)); //TODO Voir si l'on peut partager a l'intérieur de SecuredNRSearch, en utilisant des champs, x, h et hp et possiblement d'autres //list(x = x, h = h, hp = hp) } //# end of dip.characteristics
//sqrt.invertedGamma.gen <- function(n, beta, xrange, o=list(), beta.min=1e-8) internal static double SqrtInvertedGammaGen(int n, double beta, double[] xRange, GenObject o, double betaMin = 1E-8) { //# Sample a value from the distrn //# //# f(x) = 1 //# ------- . exp(-beta/x^2) //# x^n double x = Tools.ND; if (n <= 1) { SGNFnAParam A = o.A.Clone(); A.B = beta; Icdf icdf = new Icdf(o, A, xRange); x = icdf.Bidon(start: Math.Sqrt(2.0 * beta), inestLowerLim: xRange[0] == 0); } else if (beta < betaMin) { if (xRange[0] == 0) { xRange[0] = 0.0001; } x = RandomPow(n, xRange); } else { double alpha = (n - 1) / 2.0; double[] invX2Range = xRange.Sqr().Reverse().ToArray().Reciprocal(); double invX2 = RGammaTruncated(alpha, beta, invX2Range); x = 1 / Math.Sqrt(invX2); } return(x); } //# end of sqrt.invertedGamma.gen
internal override double LogF(double s, SGNFnAParam A) { return (-s - Math.Pow(A.Y * Math.Exp(-s) - 1.0, 2) / 2.0 / A.CV2 - Math.Pow(s - A.Mu, 2) / 2.0 / A.Sigma2); }
internal override double LogFSecond(double s, SGNFnAParam A) { return (-2.0 * A.Y2 * Math.Exp(-2 * s) / A.CV2 + A.Y * Math.Exp(-s) / A.CV2 - 1 / A.Sigma2); }
internal override double LogFPrime(double t, SGNFnAParam A) { return (-1.0 / t - (A.Y / Math.Pow(t, 2) - A.Y2 / Math.Pow(t, 3)) / A.CV2 + (A.Mu - t) / A.Sigma2); }
internal override double LogFPrime(double mu, SGNFnAParam A) { double z = mu / A.S; LPhiObject lphio = WebExpoFunctions3.LPhi(z); return(-A.N * (mu - A.MuMean) / A.S2 - A.N * lphio.R[0] / A.S); }
internal override double LogFSecond(double mu, SGNFnAParam A) { double z = mu / A.S; LPhiObject lphio = WebExpoFunctions3.LPhi(z); return(A.N / A.S2 * (z * lphio.R[0] + lphio.R2[0] - 1)); }
internal override double LogF(double s, SGNFnAParam A) { return (-NormalDistribution.PNorm(Math.Exp(s) / A.Ksi, log_p: true) - Math.Pow((A.Y - Math.Exp(s)), 2) / 2.0 / A.Ksi2 - Math.Pow((s - A.Mu), 2) / 2.0 / A.Sigma2); }
internal override double LogF(double t, SGNFnAParam A) { return (-Math.Log(t) - Math.Pow(A.Y - t, 2) / 2.0 / A.CV2 / Math.Pow(t, 2) - Math.Pow(t - A.Mu, 2) / 2.0 / A.Sigma2); }
private double TrueValueGen(GenObject genO, MeasurementError me, double y, double mu, double logY) { SGNFnAParam localA = genO.A.Clone(); localA.Mu = mu; if (genO.LogNormalDistrn) { localA.LogY = logY; } localA.Y = y; localA.Y2 = y * y; if (genO.ThroughSD) { localA.Ksi = me.Parm; localA.Ksi2 = me.Parm * me.Parm; } else { localA.CV2 = me.Parm * me.Parm; } double[] start = genO.Start(localA); //start.Any(zz => !zz.IsFinite()); Icdf icdf = new Icdf(genO, localA, range: genO.Range); double x = icdf.Bidon(start, inestLowerLim: !genO.LogNormalDistrn); if (genO.LogNormalDistrn) { x = Math.Exp(x);// # new_0.11 } return(x); } //# end of truevalue.gen
internal override double LogFSecond(double t, SGNFnAParam A) { return (1.0 / Math.Pow(t, 2) + (2.0 * A.Y / Math.Pow(t, 3) - 3.0 * A.Y2 / Math.Pow(t, 4)) / A.CV2 - 1.0 / A.Sigma2); }
internal override double LogF(double s, SGNFnAParam A) { return (-(A.N + 1) * Math.Log(s) - A.B / Math.Pow(s, 2) - A.N * NormalDistribution.PNorm(A.Mu / s, log_p: true) - (Math.Pow(Math.Log(s) - A.LM, 2)) / (2.0 * A.LS2)); }
}//LogFPrime internal override double LogFSecond(double sigma, SGNFnAParam A) { //(A$N + 1)/ sigma ^ 2 - 6 * A$b / sigma ^ 4 + (log(sigma) - A$lm - 1)/ (A$ls2* sigma^ 2) double s2 = Math.Pow(sigma, 2); double s4 = Math.Pow(s2, 2); double x = (A.N + 1.0) / s2 - (6.0 * A.B / s4) + ((Math.Log(sigma) - A.LM - 1.0) / (A.LS2 * s2)); return(x); } //LogFSecond
internal override double LogFPrime(double s, SGNFnAParam A) { return(-1.0 + ( A.Y2 * Math.Exp(-2 * s) - A.Y * Math.Exp(-s) ) / A.CV2 - (s - A.Mu) / A.Sigma2); }
internal Icdf(GenObject o, SGNFnAParam a, double[] range, double precision = 1E-6, int NRMaxIteration = 200) { this.Ob01 = o; this.A = a == null ? o.A : a; this.Range = (range == null) ? new double[0] : range; this.Start = Tools.Combine(Range.Mean()); // donne NaN si range est de longueur 0. this.Precision = precision; this.NRMaxIter = NRMaxIteration; }
internal override double LogFPrime(double v, SGNFnAParam A) { LPhiObject l = WebExpoFunctions3.LPhi(1 / v); return (-A.N / v + 2.0 * A.B / Math.Pow(v, 3) + A.N * l.R[0] / Math.Pow(v, 2)); }
internal static MEParmGen GetInstance(GenObject o, MeasurementError me, DataSummary data, YGen genY, TrueValuesGen genTV) { MEParmGen instance = new MEParmGen(); double b; double[] tmpY, tmpT; if (me.ThroughCV) { if (o.LogNormalDistrn) { tmpY = Tools.Combine(data.LogY, genY.LogGT, genY.LogLT, genY.LogI); tmpT = Tools.Combine(genTV.LogY, genTV.LogGT, genTV.LogLT, genTV.LogI); b = tmpY.Substract(tmpT).Exp().Substract(1.0).Sqr().Sum(); b /= 2.0; } else { tmpY = Tools.Combine(data.Y, genY.GT, genY.LT, genY.I); tmpT = Tools.Combine(genTV.Y, genTV.GT, genTV.LT, genTV.I); b = tmpY.Divide(tmpT).Substract(1.0).Sqr().Reverse().Sum(); b /= 2.0; } SGNFnAParam localA = o.A.Clone(); localA.B = b; localA.Range = me.GetRange(); double[] range = me.GetRange(); Icdf icdf = new Icdf(o, localA, range); instance.Parm = icdf.Bidon(o.Start(localA), inestLowerLim: range[0] == 0); } else { tmpY = Tools.Combine(data.Y, genY.GT, genY.LT, genY.I); tmpT = Tools.Combine(genTV.Y, genTV.GT, genTV.LT, genTV.I); b = tmpY.Substract(tmpT).Sqr().Sum(); b /= 2.0; if (o.LogNormalDistrn) { SGNFnAParam localA = o.A.Clone(); localA.B = b; localA.Range = me.GetRange(); localA.Truevalues = Tools.Copy(tmpT); //me.parm <- dens.gen.icdf(o, A, range=me$range, inestimable.lower.limit=me$range[1]==0) double[] range = me.GetRange(); Icdf icdf = new Icdf(o, localA, range); instance.Parm = icdf.Bidon(inestLowerLim: range[0] == 0.0); } else { instance.Parm = WebExpoFunctions3.SqrtInvertedGammaGen(data.N, b, me.GetRange(), o); } } return(instance); }
internal override List <double> LogFInvRemote(double target, SGNFnAParam A) { /* # solutions for large s # tmp <- A$y + c(-1,1)*sqrt(2*abs(target)*A$ksi2) # vector of length 2 # tmp <- tmp[tmp>0] # roots <- log(tmp) # */ // solutions for large s List <double> roots = new List <double>(); double z = Math.Sqrt(2 * Math.Abs(target) * A.Ksi2); double pp = A.Y - z; double pg = A.Y + z; if (pp > 0.0) { roots.Add(Math.Log(pp)); roots.Add(Math.Log(pg)); } else if (pg > 0.0) { roots.Add(Math.Log(pg)); } /* * C <- log(2) -A$y2/2/A$ksi2 - A$mu^2/2/A$sigma2 * B <- A$mu/A$sigma2 * qA <- -1/2/A$sigma2 * tmp <- quadratic.solution(c(C, B, qA), target=target) * roots <- c(roots, tmp) * */ //# solutions for small s (large negative values) double C = Math.Log(2) - A.Y2 / 2.0 / A.Ksi2 - Math.Pow(A.Mu, 2) / 2.0 / A.Sigma2; double B = A.Mu / A.Sigma2; double qA = -1.0 / 2.0 / A.Sigma2; roots.AddRange(QuadraticEquation.GetRealRoots(Tools.Combine(C, B, qA), target, ROrder: true)); /* * C <- log(2) -A$y2/2/A$ksi2 - A$mu^2/2/A$sigma2 * B <- A$mu/A$sigma2 * qA <- -1/2/A$sigma2 * tmp <- quadratic.solution(c(C, B, qA), target=target) * roots <- c(roots, tmp) */ //# solutions around 0 C = Math.Log(1.0 / A.Ksi) - Math.Pow(A.Y - 1.0, 2) / 2.0 / A.Ksi2 - Math.Pow(A.Mu, 2) / 2.0 / A.Sigma2; B = A.Mu / A.Sigma2; qA = -1.0 / 2.0 / A.Sigma2; roots.AddRange(QuadraticEquation.GetRealRoots(Tools.Combine(C, B, qA), target, ROrder: true)); return(roots); } // # end of log.f.inv.remote
internal override double[] Start(SGNFnAParam A) { double s0 = Math.Sqrt(2.0 * A.B / A.N); if ((s0 < A.Range[0]) || (s0 > A.Range[1])) { s0 = A.Range.Mean(); } return(Tools.Combine(s0)); }
internal override double LogFSecond(double s, SGNFnAParam A) { // log.f.prime <- function(s, A){z <- exp(s)/A$ksi; l <- lphi(z); -l$r*z + A$y*z/A$ksi - z^2 - (s-A$mu)/A$sigma2} double[] z = new double[] { Math.Exp(s) / A.Ksi }; LPhiObject lphio = WebExpoFunctions3.LPhi(z); double r0 = lphio.R[0]; double z0 = z[0]; return(r0 * (Math.Pow(z0, 3) - z[0]) + Math.Pow(r0 * z0, 2) + (A.Y * Math.Exp(s) - 2.0 * Math.Exp(2 * s)) / A.Ksi2 - 1.0 / A.Sigma2); }
/* * start <- function(A) * { * C <- -1 + A$y2/A$cv2 - A$y/A$cv2 + A$mu/A$sigma2 * B <- -2*A$y2/A$cv2 + A$y/A$cv2 - 1/A$sigma2 * qA <- 2*A$y2/A$cv2 - A$y/2/A$cv2 * tmp <- quadratic.solution(c(C, B, qA)) * * start <- c(tmp, A$mu - A$sigma2) * start * } # end of start */ internal override double[] Start(SGNFnAParam A) { double C = -1.0 + A.Y2 / A.CV2 - A.Y / A.CV2 + A.Mu / A.Sigma2; double B = -2.0 * A.Y2 / A.CV2 + A.Y / A.CV2 - 1.0 / A.Sigma2; double qA = 2.0 * A.Y2 / A.CV2 - A.Y / 2.0 / A.CV2; List <double> roots = QuadraticEquation.GetRealRoots(Tools.Combine(C, B, qA), ROrder: true); roots.Add(A.Mu - A.Sigma2); return(roots.ToArray()); }
internal override List <double> LogFInvRemote(double target, SGNFnAParam A) { List <double> roots = new List <double>(); double D = -A.B; double B = -A.N * (Math.Log(A.Mode) - 1.0) - target; double qA = -A.N / A.Mode; roots.AddRange(CubicEquation.GetRealRoots(Util.Tools.Combine(D, 0, B, qA), l: 0, u: A.Mode, ROrder: true));// # modif_0.11 roots.Add(Math.Exp(Math.Log(2.0) - target / A.N)); return(roots); }
internal override double LogFSecond(double v, SGNFnAParam A) { LPhiObject l = WebExpoFunctions3.LPhi(1 / v); double v2 = Math.Pow(v, 2); double v4 = Math.Pow(v2, 2); return (A.N / v2 - 6.0 * A.B / v4 + A.N / v4 * (l.R[0] * (1 / v - 2.0 * v) + l.R2[0])); }