variogram calculates the isotropic and anisotropic experimental variogram in various dimensions.
d = variogram(x,y)
d = variogram(x,y,'pn','pv',...)
The function uses parseargs (objectId=10670) by Malcolm wood as subfunction.
Currently, the function calculates all variogram values at one step. While this is fast for small data sets (n<2000), it may fail on large data sets owing to memory constraints. For large datasets you may thus want to use the 'subsample' option.
Hi Wolfgang, what reference did you follow for this script?
@claudiaE -- distances are arbitrary in this case. Just set the distance between neighboring pixels as "1" (or whatever) and calculate your variogram. Ultimately if you're doing a GP or whatever, you'll fit a model to the variogram for the distance you chose.
Thanks for sharing the file.
I was wondering if you had any suggestion on how to perform spatial continuity and the variogram on 2D images.
Thank you for your file! One problem: I found parseargs package by Malcolm Wood is no longer available -- it can be downloaded, but not unzipped. Instead, I use FMINSEARCHBND by John D'Errico ( https://www.mathworks.com/matlabcentral/fileexchange/8277-fminsearchbnd--fminsearchcon). It works! How do you think about it?
Thank you again for sharing your file!
I tested your suggestion for 3d variogram (variogram([x(:) y(:) z(:)], values, ...)), but, it do not work very well.
Could you please any another suggestion for 3d variogram?
Thanks for this awesome submission. It was very helpful. I however have a questions..
On line 165 you perform the following operation
edges(end) = inf;
Can you explain the logic behind it?
The reason that I ask it that when the data is uniformly distributed in space the number of data pairs separated by a lag (h) decreases as h increases.
However, as written line 165 then makes later lines (l78 and 179)
S.val = accumarray(ixedge,...
S.num = accumarray(ixedge,ones(size(lam))...
Calculate the gamma value and the number of pairs using the the rest of the data set. You then do a final modification of the arrays but that does eliminate the extra data poins used int he Variogram calculation..
Let me give you an example.
My edges vector before line 165
250 500 750 1000 1250 1500 1750 2000 2250 2500 2750 3000
My edges vector after line 165
250 500 750 1000 1250 1500 1750 2000 2250 2500 2750 Inf
Then on lines
178,179 and 186, 187
you produce the S.distance,S.num and S.val struct arrays
This is what the S.distance says:
Then this is what the S.num says:
You see That 59 should not be there there are no 59 number of data pairs with an average lag distance of 2875. In fact there are only 30.
If I comment line 165 then I get the expected result of monotonically decreasing number of pairs as lag distance increases.
Let me know what you think...
Fantastic code - just what I needed. However, I am trying to modify it a bit for cross-variogram. Can I do it this way: save the
lam = (y(iid(:,1))-y(iid(:,2)));
for two separate variable (say A and B) Combine=bsxfun(@times, lamA,lamB);
Combine_cross = accumarray([ixedge ixtheta],Combine,...
Is this approach correct?
Thank you for this, it is a very good one!
Could you please explain me, where is your algorithm calculate the separation (or lag) vector(h). Theoretically, if I am calculating a variogram, I use this formula:
Did you used the same? If yes, how do you calculated h? (in which row?)
@JG: Yes, [x y] can also be a nx1 vector with n number of points in time. Use datenum to calculate a numeric vector from your dates. Variogram doesn't do the conversion for you.
Thanks Wolfgang. Also, can this be used for temporal separation too? i.e., can [x y] be time?
@JG: This syntax is correct. Take care, however, if latitude and longitude refer to geographic coordinates... The function doesn't calculate distances on a sphere.
Thanks for this!
I just want to make sure I'm using it correctly;
If I have latitude and longitude coordinates ([x y]), and my data (z),
I use: variogram([x y],z)
Hi Edgar, yes, this seems to be an error in the function. Thanks for reporting it! I hardly needed those plot options so I probably forgot to care for them when adding anisotropy. Regarding your earlier question, I suppose that this should be ok.
I'll see if I can make a update in the next couple of days. Unfortunately, my time to support these functions at the moment is quite scarce...
When types "cloud1" and "cloud2" are used in combination with anisotropy, the function does not return an array (a column for each theta tested). I'm guessing this is an error in the function?
I'm working with meteorological data, I can see a mountain range drives the spatial variation of the meteorological fields. The mountain range has an orientation (larger axis) at 110° (calculated clockwise from top, i.e. positive latitud axis). The corresponding semi-variogram from your script would be the one at 20°? Thanks for your help. Really nice algorithm, congrats!
Yes, that's a decision that concerns me these days, because either I use one variogram per time sample or as i have a lot of timesamples, I can do a training phase for kriging and I get a variogram characteristic from the system, which I will use in the test phase. I am currently working on my own 3d kriging and i wanted to compare results.
Thank you very much!
Hi Ricardo, I am afraid my kriging won't work in three dimensions. However, it shouldn't be a very big deal do modify the function to do so. However, I am skeptical how you want to handle time and space dimensions in calculating one variogram.
I just realize the NaN make sense in my data.
But still i have some questions.
Once I already know that variogram can work with 3d input, I tryed the kriging script but it seems like the input is 2dimensional, is there a way to use 3D data on the kriging script?
I am working with temperature samples along a period of time, and i wonder if i can in the variogram function give as values the entire time samples matrix, instead only one time sample only.
I hope i am being clear enough.
Thank you so much
Thank you so much Wolfgang!
I tried and at the beginning i had an error but i changed what @Shen said
d = sqrt(sum((X(i,:)-X(j,:)).^2,2));
and it worked, but the d.val has a lot of NaN, is that OK? because I have very bad fits on the variogramfit script.
@Riccardo: variogram supports higher dimensional data. In your case, just call the function like
variogram([x(:) y(:) z(:)], values, ...)
I am trying to apply Kriging to a 3d mesh but first of all i don't know if i can use your variogram from a 3d data, it seems that only accepts X ans Y, not Z coordinates. Is there any way to use it with 3d input?
Thank you very much
you are right! Thanks for pointing out this bug. Seems I have not thoroughly checked the multidimensional support since I've never needed it. I'll correct for it as soon as possible.
I am not sure if the following had been spotted:
In Line 253, when calculating the distances for more than two dimensions, the code was
d = sqrt(sum((X(i,:)-X(j,:)).^2));
I think it should be
d = sqrt(sum((X(i,:)-X(j,:)).^2,2));
as we need the sums of the rows but not the columns. After this correction, everything works perfectly in calculating 3-dimensional variograms.
Many thanks for your function. It is absolutely fabulous!
@vipul utsav: the nugget variance is the variogram value at zero lag distance. You can estimate it visually from the experimental variogram or (better) use variogramfit (http://www.mathworks.de/matlabcentral/fileexchange/25948) to estimate it from the data.
nugget variance from variogram ,how it is possible?
@dd: Right now, this is not possible, but with a minor modification of the function, it is not a problem. Just enter following line in the section where the output for the type 'cloud1' is calculated (Line 202)
S.ij = iid(:,[1 2]);
This gives you the indices for each point pair. You can now take these to calculate e.g. the correlation coefficient for a binned lag distance.
x = rand(1000,1)*4-2;
y = rand(1000,1)*4-2;
z = 3*sin(x*15)+ randn(size(x));
d = variogram([x y],z,'type','cloud1');
% should give you this
distance: [382476x1 double]
val: [382476x1 double]
ij: [382476x2 double]
distbins = unique(d.distance);
% calculate the correlation coefficient
% for the second distance bin
I = d.distance == distbins(2);
Hope it works out for you.
I would need it mostly for didactic purpose: how gamma, corr. and covar. are evolving along with lag distance.
Another question: how can I produce a crossplot of a variable between two lag distances?
@dd: the covariance (C(h)) as a function of lag distance h is directly related to the semi-variogram (gamma(h)) and can be calculated by
gamma(h) = C(0)-C(h)
You can take the sill variance as C(0) but this only works for bounded variograms.
Why do you need the correlation or covariance?
I was wondering if there would be a simple way to include correlation and covariance in the analysis?
Great function...helped me do a quick analysis of spatial correlation. One enhancement would be to return a handle to the plotted data, so users could change the color, symbols, etc.
I keep on getting these error messages when running this code. There's however nothing special to my data:
'Error using set
Bad property value found.
Object Name : axes
Property Name : 'XLim'
Values must be increasing and non-NaN.
Error in axis>LocSetLimits (line 208)
Error in axis (line 94)
Error in variogram (line 217)
axis([0 params.maxdist 0 max(S.val)*1.1]);'
are those data gridded? If yes, you should call the function like this:
d = variogram([x(:) y(:)], impedance(:), 'nrbins',50,'anisotropy',true,'thetastep',30);
Thank you Wolfgang ,
I got both plots OK(for theta = 0 and theta = 90 degrees). Now I am trying to apply your program to a real data set but I am having some problems. The data set consist of 200 x 50 points (impedances) contained in a 2D rectangular cross section. The coordinates x and y and the impedance are arranged in three vectors, each of them of dimension 200x 50 = 10000. The function in the script file is given by:
d = variogram([x y], impedance, 'nrbins',50,'anisotropy',true, 'thetastep',30);
The error message: “ Anisotropy is only supported for 2D",
What am I doing wrong?
Hi Jorge. I have just uploaded a new version with a bug removed. Please take this one as soon as it is available (probably by 25 Nov 2011). Then, in order to obtain variograms for different directions, just calculate the anisotropic variogram. E.g. for obtaining variograms in 0 and 90° direction, you can do following:
d = variogram([x y],z,...
d.val(:,find(d.theta==0 | d.theta==pi/2)));
You may choose tighter angular bins. In this case it will be 0+-15 and 90+-15.
Very good function; is possible calculate variograms at different angles, for example vertical and horizontal variograms? If you can do this how do you specify the angles and how do you plot them,
@ Marian and others
I am sorry that IPDM is currently not available on the FEX. You can send me a mail via the contact formular with your email and I'll send you the function then. I'll be working on a workaround next week.
Thanks for your interest,
Is there anything else we can use instead of IPDM since there is no such function on Matlab FEX
Nugget effect and sill are required when you fit a variogram model to the experimental variogram and, hence, they are not accepted as parameters here. However, you may want to try variogramfit, which enables to set initial values for a nugget model and sill.
Very good. Does this code accept the other parameters such as nugget effect and sill?
Like Variogramfit, it is for me an invaluable contribution. Very nice code, very nice comments, well I've learned a lot with these two pieces of code, Thanks a lot !
Thanks a lot for this function, it works very well (though as you mentioned it is a bit of a memory hog, I had to subsample my data but the variogram was robust).
sorry, I found out that these values are saved already in variable 'd' as a structure. thanks though
that was what I meant, thank you :)
one more question, how to pass the data that has been plot? i mean here:
I need to do some curve fitting on those values...
the distance measure used here is the euclidean distance. I guess that is what you mean by direct distance, do you?
very nice program indeed. I had a question by the way. how the distance is calculated? direct distance or along X and Y streets?
Bug removed that occurred when calculating the distances for more than two dimensions.
removed a bug in calculating the number of angular bins for anisotropic variograms.
Removed dependency from IPDM. Fortunately, it is even faster now on my machine. The relatively large memory overhead is still unresolved. Added subsampling option.
dependence on consolidator was removed,
removed an error in the calculation of the number of bins