WICHTIG: Der Betrieb von goMatlab.de wird privat finanziert fortgesetzt. - Mehr Infos...

Mein MATLAB Forum - goMatlab.de

Mein MATLAB Forum

 
Gast > Registrieren       Autologin?   

Partner:




Forum
      Option
[Erweitert]
  • Diese Seite per Mail weiterempfehlen
     


Gehe zu:  
Neues Thema eröffnen Neue Antwort erstellen

Zufallszahlenüberprüfung

 

grandmasta

Gast


Beiträge: ---
Anmeldedatum: ---
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 15.01.2019, 13:31     Titel: Zufallszahlenüberprüfung
  Antworten mit Zitat      
Habe versucht einen Zufallszahlenüberprüfung mittels run und chi-quadrattest zu programmieren, ich bekomme leider die Fehlercode:
Subscript indices must either be real positive integers or logicals.

Error in BSP25_Geiger_Haun (line 147)
chi_quadrat_table_run = chi(degrees_Of_freedom_run, helper_for_probability_run);

Weiß aber leider nicht was ich ändern muss, dass es funktioniert. Vll kann mir jemand helfen. Danke im voraus.

Code:
n = 100;                                                % creats 100 random numbers
x0 = input('Bitte Startwert eingeben: ');
a = input('Bitte Faktor eingeben: ');              
b = input('Bitte Wert für die Verschiebung eingeben: ');                
m = input('Bitte Modulo eingeben: ');                
runs = 0;
level_of_significance = 0.9500;                                                    % level of significance of 95 percent
probabilities = [0.005,0.01,0.025,0.0500,0.1,0.5,0.9,0.95,0.975,0.99,0.995];      
degrees_Of_freedom_table = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];  
chi = [0.00,0.00,0.00, 0.00, 0.02, 0.45, 2.71, 3.84, 5.02, 6.63, 7.88; ...         % chi-quadrat distribution table
       0.01,0.02,0.05, 0.10, 0.21, 1.39, 4.61, 5.99, 7.38, 9.21,10.60; ...
       0.07,0.11,0.22, 0.35, 0.58, 2.37, 6.25, 7.81, 9.35,11.34,12.84; ...
       0.21,0.30,0.48, 0.71, 1.06, 3.36, 7.78, 9.49,11.14,13.28,14.86; ...
       0.41,0.55,0.83, 1.15, 1.61, 4.35, 9.24,11.07,12.83,15.09,16.75; ...
       0.68,0.87,1.24, 1.64, 2.20, 5.35,10.64,12.59,14.45,16.81,18.55; ...
       0.99,1.24,1.69, 2.17, 2.83, 6.35,12.02,14.07,16.01,18.48,20.28; ...
       1.34,1.65,2.18, 2.73, 3.49, 7.34,13.36,15.51,17.53,20.09,21.95; ...
       1.73,2.09,2.70, 3.33, 4.17, 8.34,14.68,16.92,19.02,21.67,23.59; ...
       2.16,2.56,3.25, 3.94, 4.87, 9.34,15.99,18.31,20.48,23.21,25.19; ...
       2.60,3.05,3.82, 4.57, 5.58,10.34,17.28,19.68,21.92,24.73,26.76; ...
       3.07,3.57,4.40, 5.23, 6.30,11.34,18.55,21.03,23.34,26.22,28.30; ...
       3.57,4.11,5.01, 5.89, 7.04,12.34,19.81,22.36,24.74,27.69,29.82; ...
       4.07,4.66,5.63, 6.57, 7.79,13.34,21.06,23.68,26.12,29.14,31.32; ...
       4.60,5.23,6.26, 7.26, 8.55,14.34,22.31,25.00,27.49,30.58,32.80; ...
       5.14,5.81,6.91, 7.96, 9.31,15.34,23.54,26.30,28.85,32.00,34.27; ...
       5.70,6.41,7.56, 8.67,10.09,16.34,24.77,27.59,30.19,33.41,35.72; ...
       6.26,7.01,8.23, 9.39,10.86,17.34,25.99,28.87,31.53,34.81,37.16; ...
       6.84,7.63,8.91,10.12,11.65,18.34,27.20,30.14,32.85,36.19,38.58; ...
       7.43,8.26,9.59,10.85,12.44,19.34,28.41,31.41,34.17,37.57,40.00];

 for i = 1:n
   calc_rand = a * x0 + b;
   calc_rand = mod(calc_rand, m);
   random_nr(i) = calc_rand/m;                      % to receive real random numbers we devide with m                        
end  

r = sqrt(n);                                        % r = sqrt(n) -> so we get about 10 separate sub quantities

% parameter for chi-quadrat test
npi = n * (1/r);                                
probability_npi = 1/r;

class_limit_low = 0;                                       % lower limit of a class intervall
class_limit_high = probability_npi;                        % upper limit of a class intervall
numbers_in_intervall = zeros(1,r);                         % counts the quantity of random numbers in one class

for i = 1:r
    for j = 1:n
        if (class_limit_low <= random_nr(j)) && (random_nr(j) < class_limit_high)    % which random numbers are in which class
            numbers_in_intervall(i) = numbers_in_intervall(i) + 1;                   % count the quantity of random numbers in one class
        end
    end
    class_limit_low = class_limit_high;                         % for the next intervall set the lower limit of the class equal to the uppper limit                                         % lower limit of the next class is upper limit of the class before
    class_limit_high = class_limit_high+probability_npi;        % the new upper limit is the old upper limit + probability of npi
end

chi_quadrat = 0;
for k = 1:length(numbers_in_intervall)                                       % from 1 to the amount of numbers in class
    chi_quadrat = chi_quadrat + ((numbers_in_intervall(k) - npi)^2)/npi;  % sum of x0 = ((n-np)^2)/np
end

% receive chi quadrat from the table
degrees_Of_freedom_r = r-1;                                          
degrees_Of_freedom = degrees_Of_freedom_table(degrees_Of_freedom_r);      % degrees of freedom in the table

for l = 1:length(probabilities)                                % from 1 to the quantity of probabilities in the table
    if probabilities(l) == level_of_significance               % when the probabiliet is equal to the level of significance
       helper_for_probability = l;                             % in this class is the right probability in the table
    end
end

probability = probabilities(helper_for_probability);                 % probability
chi_quadrat_table = chi(degrees_Of_freedom, helper_for_probability);  % chi quadrat from the table

% compare the calculated chi quadrat and the qui quadrat from the table
fprintf('\n');
fprintf('Ergebnis des Chi-Quadrat-Test:\n');
if chi_quadrat <= chi_quadrat_table
    fprintf('Die angeführten Zahlen werden mit einem Signifikanzniveau von %g % beibeihalten: \n', level_of_significance*100);
    disp(random_nr');
else
    fprintf('Die angeführten Zahlen werden mit einem Signifikanzniveau von %g % verworfen: \n', level_of_significance*100);
    disp(random_nr');
end


% Run-Test

random_nr_run = random_nr;                           % same random numbers for the run test
counter_length = 0;                                  % counts the length
for m = 1:n-1                                        % compare each random number with the following
    if random_nr_run(m) < random_nr_run(m+1)         % if the following random number is bigger
        counter_length = counter_length + 1;         % add one to the counter
    else                                                  
        length_run(m) = counter_length;              
        counter_length = 0;                          % we put the counter to 0
    end
end

max_length_run = max(length_run);                        
for m = 1:max_length_run                                
    counter_runs = 0;                                       % counter for each length is zero at the start
    for n = 1:length(length_run)                     % from 1 to length of length_run
        if length_run(n) == m                        % compare all amounts of runs and counts them togehter
            counter_runs = counter_runs + 1;                   % add one to the counter
            runs(:,m) = counter_runs;              % adds all sumed up amounts of runs to a vector from 1 to max_length
        end
    end
end

n_run = sum(runs);                               % quantity of runs

for o = 1:max_length_run                                % from 1 to maximum length of run
    pi_run(o) = (1/prod(1:(o))) - (1/prod(1:(o+1)));    % formula P(R=l) = sum((1/l!)-(1/(l+1!)))
    npi_run(o) = n_run*pi_run(o);                        
end


chi_quadrat_run = 0;
for p = 1:max_length_run                                % from 1 to the maximum length of the run do
    chi_quadrat_run = chi_quadrat_run + ((runs(p) - npi_run(p))^2)/npi_run(p);  % formula x0 = sum((((n-np)^2)/np)
end

for x = 1:length(probabilities)                       % from 1 to the quantity of probabilities in the table
    if probabilities(x) == level_of_significance          % when is p = the probability in table
       helper_for_probability_run = x;                        % this is the location in the table
    end
end

degrees_Of_freedom_run = (max_length_run - 1);              
probability_run = probabilities(helper_for_probability_run);
chi_quadrat_table_run = chi(degrees_Of_freedom_run, helper_for_probability_run);  


% compare the calculated chi quadrat for the run test and the qui quadrat from the table
fprintf('\n');
fprintf('Run-Test:\n');
if chi_quadrat_run <= chi_quadrat_table_run
    fprintf('Die angeführten Zahlen werden mit einem Signifikanzniveau von %g % beibeihalten: \n', level_of_significance);
    disp(random_nr_run');
else
    fprintf('Die angeführten Zahlen werden mit einem Signifikanzniveau von %g % verworfen: \n', level_of_significance);
    disp(random_nr_run');
end
 


Jan S
Moderator

Moderator


Beiträge: 11.057
Anmeldedatum: 08.07.10
Wohnort: Heidelberg
Version: 2009a, 2016b
     Beitrag Verfasst am: 15.01.2019, 17:45     Titel: Re: Zufallszahlenüberprüfung
  Antworten mit Zitat      
Hallo grandmasta,

Die Fehlermeldung klingt eindeutig: Offenbar ist einer der beiden Inputs kein positiver Integer. Dann ist er nicht als Index benutzbar.
Du kannst einen Breakpoint in die Zeile setzen und so sehr einfach selbst herausfinden, welche Werte die beiden Variablen haben.

Eine Bemerkung: Die Lesbarkeit des Codes wäre höher, wenn die Namen der Variablen etwas kürzer wären. Verständliche Namen sind zwar auf jeden Fall besser als "a,b,c", aber "level_of_significance" sagt auch nicht mehr als "Significance".

Manche Code-Blöcke können vereinfacht werden:
Code:
chi_quadrat = 0;
for k = 1:length(numbers_in_intervall)
    chi_quadrat = chi_quadrat + ((numbers_in_intervall(k) - npi)^2)/npi;  % sum of x0 = ((n-np)^2)/np
end

zu
Code:
chi_quadrat = sum(((numbers_in_intervall - npi) .^ 2) / npi);

Und:
Code:
for l = 1:length(probabilities)                                % from 1 to the quantity of probabilities in the table
    if probabilities(l) == level_of_significance               % when the probabiliet is equal to the level of significance
       helper_for_probability = l;                             % in this class is the right probability in the table
    end
end

zu:
Code:
helper_for_probability = find(probabilities == level_of_significance, 1, 'last');


Gruß, Jan
Private Nachricht senden Benutzer-Profile anzeigen
 
grandmasta

Gast


Beiträge: ---
Anmeldedatum: ---
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 16.01.2019, 15:05     Titel: Zufallszahlenüberprüfung
  Antworten mit Zitat      
Hallo Jan,

vielen Dank für deine Hilfe. Hat super geklappt.

LG
 
Neues Thema eröffnen Neue Antwort erstellen



Einstellungen und Berechtigungen
Beiträge der letzten Zeit anzeigen:

Du kannst Beiträge in dieses Forum schreiben.
Du kannst auf Beiträge in diesem Forum antworten.
Du kannst deine Beiträge in diesem Forum nicht bearbeiten.
Du kannst deine Beiträge in diesem Forum nicht löschen.
Du kannst an Umfragen in diesem Forum nicht mitmachen.
Du kannst Dateien in diesem Forum posten
Du kannst Dateien in diesem Forum herunterladen
.





 Impressum  | Nutzungsbedingungen  | Datenschutz | FAQ | goMatlab RSS Button RSS

Hosted by:


Copyright © 2007 - 2024 goMatlab.de | Dies ist keine offizielle Website der Firma The Mathworks

MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.