Jacob Varughese
2015-Jan-03 17:27 UTC
[R] Probability distribution of fitted gaussian distribution.
Hi, I have a discrete set of data on the returns for 3 indices with 206 data points. Since the number of points is less it doesnt exact look like a gaussian distribution. I wanted to fit the data to a gaussian distribution and have used the fitdist function and have gotten the plots and the mean and sd estimates for the gaussian that fits my data. What I then want to do is to get a U=F(x) where U is the uniform variable corresponding to the CDF function applied on the fitted theoritical CDF curve. How can I get that? Equivalent data that I find in matlab. Here the ksdensity gives an array of f and xi values and I could use the f values for my usage. But I am trying to work it out in R. The steps that I am going through in R are below. I have also attached the input sheet that I am using for the indices. Sorry in advance, case its a dumb one. Estimate Density Generate a sample data set from a mixture of two normal distributions. rng default % for reproducibility x = [randn(30,1); 5+randn(30,1)]; Plot the estimated density. [f,xi] = ksdensity(x); figure plot(xi,f); Steps that I am following. # Reading and finding the returns for 3 indices. CDSPrices<-read.csv("CDS.csv") numRows=nrow(CDSPrices) CDSReturnsN225=CDSPrices$N225[2:numRows]/CDSPrices$N225[1:numRows-1]-1 CDSReturnsSPX=CDSPrices$SPX[2:numRows]/CDSPrices$SPX[1:numRows-1]-1 CDSReturnsIBOVESPA=CDSPrices$IBOVESPA[2:numRows]/CDSPrices$IBOVESPA[1:numRows-1]-1 CDS_Returns<-cbind(CDSReturnsN225,CDSReturnsSPX,CDSReturnsIBOVESPA) # Using fitdist to fit a gaussian distribution onto the discrete empirical data I have. library(fitdistrplus) fittedNormal<-fitdist(CDS_Returns[,1],"norm") plot(fittedNormal)> fittedNormal[]$estimate mean sd -0.002035951 0.028654032 $method [1] "mle" $sd mean sd 0.001996421 0.001403953 Reference http://cran.r-project.org/web/packages/fitdistrplus/fitdistrplus.pdf ~ Page 15 -- *Jacob Varughese*
David Winsemius
2015-Jan-03 21:33 UTC
[R] Probability distribution of fitted gaussian distribution.
On Jan 3, 2015, at 9:27 AM, Jacob Varughese wrote:> Hi, > > I have a discrete set of data on the returns for 3 indices with 206 data > points. Since the number of points is less it doesnt exact look like a > gaussian distribution. > > I wanted to fit the data to a gaussian distribution and have used the > fitdist function and have gotten the plots and the mean and sd estimates > for the gaussian that fits my data. > > What I then want to do is to get a U=F(x) where U is the uniform variable > corresponding to the CDF function applied on the fitted theoritical CDF > curve. How can I get that? > > > Equivalent data that I find in matlab. Here the ksdensity gives an array of > f and xi values and I could use the f values for my usage. But I am trying > to work it out in R. The steps that I am going through in R are below. I > have also attached the input sheet that I am using for the indices. Sorry > in advance, case its a dumb one. > > Estimate Density > > Generate a sample data set from a mixture of two normal distributions. > > rng default % for reproducibility > x = [randn(30,1); 5+randn(30,1)]; > > Plot the estimated density. > > [f,xi] = ksdensity(x); > figure > plot(xi,f); > > Steps that I am following. > > # Reading and finding the returns for 3 indices. > CDSPrices<-read.csv("CDS.csv") > numRows=nrow(CDSPrices) > CDSReturnsN225=CDSPrices$N225[2:numRows]/CDSPrices$N225[1:numRows-1]-1 > CDSReturnsSPX=CDSPrices$SPX[2:numRows]/CDSPrices$SPX[1:numRows-1]-1 > CDSReturnsIBOVESPA=CDSPrices$IBOVESPA[2:numRows]/CDSPrices$IBOVESPA[1:numRows-1]-1 > CDS_Returns<-cbind(CDSReturnsN225,CDSReturnsSPX,CDSReturnsIBOVESPA) > > # Using fitdist to fit a gaussian distribution onto the discrete empirical > data I have. > library(fitdistrplus) > fittedNormal<-fitdist(CDS_Returns[,1],"norm") > plot(fittedNormal) > > >> fittedNormal[] > $estimate > mean sd > -0.002035951 0.028654032 > > $method > [1] "mle" > > $sd > mean sd > 0.001996421 0.001403953Wouldn't you get pretty much the same result from: list( mean=mean(CDS_Returns[,1], sd=sd(CDS_Returns[,1]) ) # ? ... since the sample mean is identical to the MLE estimate and the sample SD is at worst only different by a factor of N/(N-1). And for what you are calling " U=F(x) where U is the uniform variable corresponding to the CDF function applied on the fitted theoritical CDF curve", wouldn't that just involve the use of the `qnorm` function?> > Reference > > http://cran.r-project.org/web/packages/fitdistrplus/fitdistrplus.pdf ~ > Page 15 > > > -- > *Jacob Varughese* > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius Alameda, CA, USA