Same code but different results on Mac and PC

31 次查看(过去 30 天)
I was trying to prove the methods of moments in statistics using Matlab, where the first central moment should be equal to the mean and the second central moment should be equal to the variance.
Here I assume the mean of the distribution to be 105 and the variance being 10.
The following is my code:
clearvars
mu=105;
sigma2=10;
sigma = sqrt(sigma2);
xmin=mu-10*sigma2;
xmax=mu+10*sigma2;
n_grids = 100000;
x=linspace(xmin,xmax,n_grids);
fx = pdf('norm',x,mu,sigma);
fx = fx./sum(fx);
mu_verify = sum(x.*fx);
sigma2_verify = sum(((x-mu).^2).*fx);
sigma_2 = sum((x.^2).*fx) - mu_verify^2;
sigma2_verify and sigma_2 both should be 10, which is what I got from my PC, using Matlab R2022b 64 bit pc version.
However, when I switch to my Macbook Air (M2), using Matlab R2022b 64 bit mac version,
sigma2_verify and sigma_2 both became 3.1623, which is the same as sigma.
What can I do fix my Matlab on Macbook?

回答(2 个)

Shaik
Shaik 2023-5-16
Hi Scott
It seems like you are experiencing a difference in the results of your code when running it on different platforms (PC and Mac). The issue might be related to the precision of floating-point calculations, which can sometimes lead to slight variations in the computed values.
To address this, you can try increasing the precision of your calculations by using the vpa function, which performs variable-precision arithmetic.

Steven Lord
Steven Lord 2023-5-16
When you step through your code in both the Windows and Mac versions of MATLAB using the debugging tools in MATLAB, on which line of the code do you first see a major difference in the results?
Can you confirm that you're using the same function on both versions? You can check which function is being used by calling the which function. For pdf and sum:
which -all pdf
/MATLAB/toolbox/stats/stats/pdf.m /MATLAB/toolbox/stats/stats/@gmdistribution/pdf.m % gmdistribution method /MATLAB/toolbox/stats/stats/@piecewisedistribution/pdf.m % piecewisedistribution method
which -all sum
built-in (/MATLAB/toolbox/matlab/datafun/sum) built-in (/MATLAB/toolbox/matlab/datafun/@double/sum) % double method built-in (/MATLAB/toolbox/matlab/datafun/@int16/sum) % int16 method built-in (/MATLAB/toolbox/matlab/datafun/@int32/sum) % int32 method built-in (/MATLAB/toolbox/matlab/datafun/@int64/sum) % int64 method built-in (/MATLAB/toolbox/matlab/datafun/@int8/sum) % int8 method built-in (/MATLAB/toolbox/matlab/datafun/@logical/sum) % logical method built-in (/MATLAB/toolbox/matlab/datafun/@single/sum) % single method built-in (/MATLAB/toolbox/matlab/datafun/@uint16/sum) % uint16 method built-in (/MATLAB/toolbox/matlab/datafun/@uint32/sum) % uint32 method built-in (/MATLAB/toolbox/matlab/datafun/@uint64/sum) % uint64 method built-in (/MATLAB/toolbox/matlab/datafun/@uint8/sum) % uint8 method /MATLAB/toolbox/matlab/datatypes/tabular/@tabular/sum.m % tabular method /MATLAB/toolbox/matlab/datatypes/duration/@duration/sum.m % duration method /MATLAB/toolbox/matlab/timeseries/@timeseries/sum.m % timeseries method sum is a built-in method % connector.internal.LoggerLevel method sum is a built-in method % matlab.internal.lang.capability.Capability method sum is a built-in method % matlab.lang.OnOffSwitchState method sum is a built-in method % matlab.internal.reference.property.RefEntityType method sum is a built-in method % matlab.internal.reference.api.EntityPrecision method sum is a built-in method % matlab.internal.reference.property.DeprecationStatus method sum is a built-in method % matlab.internal.reference.property.FunctionType method sum is a built-in method % matlab.internal.reference.property.SyntaxType method sum is a built-in method % matlab.internal.reference.api.EntityCaseSensitivity method sum is a built-in method % simulink.FindSystemTask.Status method sum is a built-in method % mf.zero.meta.Language method sum is a built-in method % dig.config.CommandType method sum is a built-in method % dig.config.HorizontalAlignment method sum is a built-in method % dig.model.DisplayState method sum is a built-in method % dig.model.EventDataType method sum is a built-in method % dig.model.FunctionType method sum is a built-in method % dig.model.ValidInBdType method sum is a built-in method % dig.model.ViewMode method sum is a built-in method % dastudio_util.cooperative.AsyncFunctionRepeaterTask.Status method sum is a built-in method % coderdictionary.data.AccessFunctionSignaturesEnum method sum is a built-in method % coderdictionary.data.AccessInterfaceEnum method sum is a built-in method % coderdictionary.data.AccessModeEnum method sum is a built-in method % coderdictionary.data.AllowedAccessEnum method sum is a built-in method % coderdictionary.data.DataAccessEnum method sum is a built-in method % coderdictionary.data.DataAccessTypeEnum method sum is a built-in method % coderdictionary.data.DataInitEnum method sum is a built-in method % coderdictionary.data.DataInstantiationEnum method sum is a built-in method % coderdictionary.data.DataTypeScopeEnum method sum is a built-in method % coderdictionary.data.LatchingModeEnum method sum is a built-in method % coderdictionary.data.PlacementEnum method sum is a built-in method % coderdictionary.data.ScopeEnum method sum is a built-in method % coderdictionary.data.StatementsSurroundEnum method sum is a built-in method % coderdictionary.data.StatusEnum method sum is a built-in method % matlab.unittest.internal.fixtures.FolderScope method sum is a built-in method % matlab.automation.Verbosity method sum is a Java method % java.lang.Long method sum is a Java method % java.lang.Double method sum is a built-in method % matlab.internal.timer.CallBackTypeEnum method sum is a built-in method % matlab.internal.timer.BusyModeEnum method sum is a built-in method % matlab.internal.timer.ExecutionModeEnum method /MATLAB/toolbox/matlab/bigdata/@tall/sum.m % tall method /MATLAB/toolbox/coder/half/@half/sum.p % half method /MATLAB/toolbox/nnet/deep/@dlarray/sum.m % dlarray method /MATLAB/toolbox/parallel/gpu/@gpuArray/sum.m % gpuArray method /MATLAB/toolbox/parallel/parallel/@codistributed/sum.m % codistributed method /MATLAB/toolbox/slcoverage/@cvdata/sum.p % cvdata method /MATLAB/toolbox/symbolic/symbolic/@sym/sum.m % sym method
I want to check that you haven't accidentally created your own pdf.m or sum.m functions that are being called instead of the functions in Statistics and Machine Learning Toolbox or MATLAB respectively.
  1 个评论
Scott Shyh-Chang Kao
编辑:Scott Shyh-Chang Kao 2023-5-16
The bottom two lines (sigma2_verify & sigma_2) are the only ones that went wrong.
The rest are working properly.
I checked the function on both computers, and they seem to using the same function on both versions.
I just added the symbolic toolbox to test if vpa can solve the problem, so that's why there is an additional file on my Mac.
I also checked other functions such as linspace & sqrt, but there are the same on both computers.
On my Mac:
which -all pdf
/Applications/MATLAB_R2022b.app/toolbox/stats/stats/pdf.m
/Applications/MATLAB_R2022b.app/toolbox/stats/stats/@piecewisedistribution/pdf.m % piecewisedistribution method
/Applications/MATLAB_R2022b.app/toolbox/stats/stats/@gmdistribution/pdf.m % gmdistribution method
which -all sum
built-in (/Applications/MATLAB_R2022b.app/toolbox/matlab/datafun/sum)
built-in (/Applications/MATLAB_R2022b.app/toolbox/matlab/datafun/@double/sum) % double method
built-in (/Applications/MATLAB_R2022b.app/toolbox/matlab/datafun/@int16/sum) % int16 method
built-in (/Applications/MATLAB_R2022b.app/toolbox/matlab/datafun/@int32/sum) % int32 method
built-in (/Applications/MATLAB_R2022b.app/toolbox/matlab/datafun/@int64/sum) % int64 method
built-in (/Applications/MATLAB_R2022b.app/toolbox/matlab/datafun/@int8/sum) % int8 method
built-in (/Applications/MATLAB_R2022b.app/toolbox/matlab/datafun/@logical/sum) % logical method
built-in (/Applications/MATLAB_R2022b.app/toolbox/matlab/datafun/@single/sum) % single method
built-in (/Applications/MATLAB_R2022b.app/toolbox/matlab/datafun/@uint16/sum) % uint16 method
built-in (/Applications/MATLAB_R2022b.app/toolbox/matlab/datafun/@uint32/sum) % uint32 method
built-in (/Applications/MATLAB_R2022b.app/toolbox/matlab/datafun/@uint64/sum) % uint64 method
built-in (/Applications/MATLAB_R2022b.app/toolbox/matlab/datafun/@uint8/sum) % uint8 method
sum is a built-in method % matlab.lang.OnOffSwitchState method
sum is a built-in method % matlab.internal.reference.property.RefEntityType method
sum is a built-in method % matlab.internal.reference.api.EntityPrecision method
sum is a built-in method % matlab.internal.reference.property.DeprecationStatus method
sum is a built-in method % matlab.internal.reference.property.FunctionType method
sum is a built-in method % matlab.internal.reference.api.EntityCaseSensitivity method
/Applications/MATLAB_R2022b.app/toolbox/matlab/bigdata/@tall/sum.m % tall method
/Applications/MATLAB_R2022b.app/toolbox/matlab/datatypes/duration/@duration/sum.m % duration method
/Applications/MATLAB_R2022b.app/toolbox/matlab/timeseries/@timeseries/sum.m % timeseries method
/Applications/MATLAB_R2022b.app/toolbox/symbolic/symbolic/@sym/sum.m % sym method
On the PC:
which -all pdf
C:\Program Files\MATLAB\R2022b\toolbox\stats\stats\pdf.m
C:\Program Files\MATLAB\R2022b\toolbox\stats\stats\@gmdistribution\pdf.m % gmdistribution method
C:\Program Files\MATLAB\R2022b\toolbox\stats\stats\@piecewisedistribution\pdf.m % piecewisedistribution method
which -all sum
built-in (C:\Program Files\MATLAB\R2022b\toolbox\matlab\datafun\sum)
built-in (C:\Program Files\MATLAB\R2022b\toolbox\matlab\datafun\@double\sum) % double method
built-in (C:\Program Files\MATLAB\R2022b\toolbox\matlab\datafun\@int16\sum) % int16 method
built-in (C:\Program Files\MATLAB\R2022b\toolbox\matlab\datafun\@int32\sum) % int32 method
built-in (C:\Program Files\MATLAB\R2022b\toolbox\matlab\datafun\@int64\sum) % int64 method
built-in (C:\Program Files\MATLAB\R2022b\toolbox\matlab\datafun\@int8\sum) % int8 method
built-in (C:\Program Files\MATLAB\R2022b\toolbox\matlab\datafun\@logical\sum) % logical method
built-in (C:\Program Files\MATLAB\R2022b\toolbox\matlab\datafun\@single\sum) % single method
built-in (C:\Program Files\MATLAB\R2022b\toolbox\matlab\datafun\@uint16\sum) % uint16 method
built-in (C:\Program Files\MATLAB\R2022b\toolbox\matlab\datafun\@uint32\sum) % uint32 method
built-in (C:\Program Files\MATLAB\R2022b\toolbox\matlab\datafun\@uint64\sum) % uint64 method
built-in (C:\Program Files\MATLAB\R2022b\toolbox\matlab\datafun\@uint8\sum) % uint8 method
sum is a built-in method % connector.internal.LoggerLevel method
C:\Program Files\MATLAB\R2022b\toolbox\matlab\bigdata\@tall\sum.m % tall method
C:\Program Files\MATLAB\R2022b\toolbox\matlab\datatypes\duration\@duration\sum.m % duration method
C:\Program Files\MATLAB\R2022b\toolbox\matlab\timeseries\@timeseries\sum.m % timeseries method

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by