Re: How to call BinomialDistribution from within a package?[2]

*To*: mathgroup at smc.vnet.net*Subject*: [mg32242] Re: How to call BinomialDistribution from within a package?[2]*From*: "Allan Hayes" <hay at haystack.demon.co.uk>*Date*: Wed, 9 Jan 2002 03:17:40 -0500 (EST)*References*: <a1blv0$bd3$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

On re-reading I notice that you need to remove bdist, cdf from Block[{...}] since this is blocking the definitions that you made earlier. Also, I missed a quote out from "Statistics`Common`DistributionsCommon`". I have corrected this at (A) below. But, perhaps of more interest, is that we can avoid having to look inside needed packages to find *their* needed packages (as we have to do with method (2) below) by beginning the package thus: BeginPackage["MedianCI`"]; Needs["Statistics`DiscreteDistributions`"]; Needs["Statistics`DescriptiveStatistics`"]; This is called "hidden import" because it will not make the needed packages "Statistics`DiscreteDistributions`" and "Statistics`DescriptiveStatistics`" usable outside the package "MedianCI`" -- they couold of course be loaded separately. I should be interested to hear a arguments for and against the two techniques. (A) [earlier response - corrected at (**)] I suspect that the problem arises as follows If we lool at the packages "Statistics`DiscreteDistributions`", "Statistics`DescriptiveStatistics`" we find that their working code begins with BeginPackage["Statistics`DiscreteDistributions`", "Statistics`DescriptiveStatistics`","Statistics`Common`DistributionsCommon`" ] BeginPackage["Statistics`DescriptiveStatistics`","Statistics`DataManipulatio n`"] For them to work inside your package you need to include all *their* needed packages as needed packages for your packages , thus: BeginPackage["MedianCI`", "Statistics`DiscreteDistributions`", "Statistics`DescriptiveStatistics`", "Statistics`Common`DistributionsCommon`", (**) "Statistics`DataManipulation`" ] -- Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565 "Merser" <merser at image.dk> wrote in message news:a1blv0$bd3$1 at smc.vnet.net... > I've tried to put the my function: MedianCI into a package. > > As this function works fine running as a plain notebook, it looks like > BinomialDistribution[] isn't working when called from within a package? > > Does anybody has any idear what I'm doing wrong here ? I'm running > Mathematica 4.0 on W2K. > regards > Soren > > > > BeginPackage["MedianCI`", "Statistics`DiscreteDistributions`", > "Statistics`DescriptiveStatistics`"] > Off[General::spell1]; > > MedianCI::usage = "MedianCI[data, conf=.95] Returns median with CI at > confidence level=95%" > > Begin["`Private`"] > > bdist[n_] := BinomialDistribution[n, .5] > cdf[x_, n_] := CDF[bdist[n], x] > > Clear[MedianCI]; > MedianCI[data_, conf_:.95] := > Block[ {d, n, bdist, cdf, alfa, p, plo, phi, lo = 0, hi}, > d = Sort[data]; > n = Length[d]; > > > > alfa = (1 - conf)/2; > > While[(p = cdf[lo, n]) < alfa, > lo++; > plo = p; > ]; > > hi = lo + 1; > While[(phi = cdf[hi, n]) < (1 - alfa), > hi++; > ]; > > m =Median[d]; > {{"Median", "CI low", "CI high", "Confidence level"}, > {m, d[[lo]], d[[hi + 1]], phi - plo}} > ] > End[] > EndPackage[] > > > > > > test: > > Median[Range[11]] // TableForm > > {{"Median", "CI low", "CI high", "Confidence level"}, > > {6, List, 2, -MedianCI`Private`plo + > MedianCI`Private`CDF[BinomialDistribution[11, 0.5`], 1]}} > > > > correct: > > {{"Median", "CI low", "CI high", "Confidence level"}, > > {6, 2, 10, 0.988281}} > > > >