function N = old_generate_data(N0,q,s,M, filename, cohorts, years, surveys, observations,year_offset); % filegenerator(N0,q,s,M, filename, cohorts, years, surveys, observations, year_offset) % % Generates text file for herring problem. Left out parameters are % randomly generated. % % VERSION WITH SIMPLE MODEL - no random effects, and just one M! % % Written by Lennart Frimannslund fprintf(2,'Hey there!\n\nThis is the OLD version. A problem with this version is that\n'); fprintf(2,'it produces survey indices for age 0, which apparently won%st happen in reality \n',char(39)); fprintf(2,'and that the constant cfactor = 1 is too large, which causes the catch to be too\n'); fprintf(2,'large.\n'); if (exist('year_offset','var')==0 | length(year_offset) == 0), year_offset = 1980; end; % Generate filename if not specified if (exist('filename','var') == 0 | length(filename) == 0), c = clock; filename = sprintf('herring-%s-%02d.%02d.%02d',date,c(4),c(5), ... floor(c(6))); clear c; end; % Get number of surveys if (exist('q','var') ~= 0 & length(q) ~= 0), surveys = length(q); end; % s - Generate random number between 0 and 1 if (exist('s','var') == 0 | length(s) == 0), s = rand; end; % M - Generate random number between 0 and 0.2 if (exist('M','var') == 0 | length(M) == 0), M = rand(1,1) * 0.2; end; % If no of cohorts not specified - generate random number between 1 and 6 if (exist('cohorts','var') == 0 | length(cohorts) == 0), cohorts = ceil((rand*5)) + 1; end; % years - if not specified, generate random number between 5 and 25 if (exist('years','var') == 0 | length(years) == 0), years = ceil((rand*20)) + 5; end; % surveys - if not specified, generate random number between 1 and 6 if (exist('surveys','var') == 0 | length(surveys) == 0), surveys = ceil((rand*5)) + 1; end; % if not specified - generate which fraction of maximum possible % observations available if (exist('observations','var') == 0 | length(observations) == 0), observations = ceil(rand * surveys * years * cohorts); end; % Define N0 if not supplied % exp(N0) between 0 and 3 %if (exist('N0','var') == 0 | length(N0) == 0), % N0 = 3 * rand(1,1); %end; % Generate q - between 0.1 and 0.8 if (exist('q','var') == 0 | length(q) == 0), q = 0.7 * rand(1,surveys) + 0.1; end; % Input variables now in place - generate data set itself % % For the most part translated line-by-line from r/s-code. n = years; m = cohorts; %sM = t; logs = log(s); cfactor = 1; alpha = 0; %Z = (rand(1,cohorts)) - 0.5; % TRANSPOSE OF ITS USUAL FORM! N = zeros(n,m); %N(1,:) = N0; % Added "* 100" 01032010 N(1,:) = N0(:)'; % SAME HERE, TRANSPOSE! CC = zeros(size(N)); %u = randn(n+m-1,1) * sM; %for (i=2:n), % % if (i>10), % CC(i-1,:) = rand(1,m) * cfactor/n .* N(i-1,:); % end; % % N(i,:) = (N(i-1,:) - CC(i-1,:)) * exp(-(M+u(i))); % %end; % NEW loop, replaces the one above for (j=1:m), for (i=2:n), if (i>2), CC(i-1,j) = rand(1,1) * cfactor/n * N(i-1,j); end; N(i,j) = (N(i-1,j) - CC(i-1,j)) * exp(-M); end; end; %fprintf(1,'Population trajectory:\n'); %N' II = cell(surveys,1); for (i=1:surveys), II{i} = q(i) * N .* exp(randn(n,m) * exp(logs)); end; surveyraw = []; % now create surveyraw matrix, to be put in file for (l=1:surveys), surveyraw_l = zeros(years*cohorts,4); III = II{l}; for (i = 1:cohorts), for (j=1:years), surveyraw_l((i-1)*years+j,:) = [i j-1 l III(j,i)]; end; end; surveyraw = [surveyraw; surveyraw_l]; end; % Surveyraw matrix now in place, extract #observations number of rows from % it randomly permutation = randperm(years*cohorts*surveys); % Write to file fid = fopen([filename '.dat'],'w'); % disp(fopen('all')); fprintf(fid,'# Randomly generated herring file\n'); fprintf(fid,'# True values:\n'); fprintf(fid,'#\tN0: '); fprintf(fid,'%s ',num2str(N0)); fprintf(fid,'\n'); fprintf(fid,'#\tq: '); fprintf(fid,'%s ',num2str(q)); fprintf(fid,'\n'); fprintf(fid,'#\ts: %8.5f \n#\tM: %8.5f\n#\n',s,M); fprintf(fid,'# True population trajectory, transpose(N):\n#\n'); for (i=1:size(N,1)), fprintf(fid,'# '); for (j=1:size(N,2)), fprintf(fid,'%s ',num2str(N(i,j))); end; fprintf(fid,'\n'); end; fprintf(fid,'#\n'); fprintf(fid,'\n# m (Antall kohorter)\n %3d \n\n',cohorts); % Endret 01032010 -- gammel kode hadde et sluttår for mye fprintf(fid,'# Startaar for kohorter\n'); fprintf(fid,'%i ',[0:cohorts-1]+year_offset); fprintf(fid,'\n# Sluttaar for kohorter\n'); fprintf(fid,'%i ',[years-1:years-1+cohorts-1]+year_offset); fprintf(fid,'\n# Catches (one cohort per line)\n'); for (j=1:cohorts), fprintf(fid,'%5.3f ',CC(:,j)'); fprintf(fid,'\n'); end; fprintf(fid,'\n#Number of surveys\n %3d \n',surveys); fprintf(fid,'#Number of survey indices\n %4d \n',observations); fprintf(fid,'# Survey table\n'); for (i=1:observations), fprintf(fid,'%3d %3d %3d %6.3f\n',surveyraw(permutation(i),:)); end; % %for (i=1:n), % for (j=1:m), % fprintf(fid,'%3d %3d %3d %6.3f\n',j,i,1,II(i,j)); % % end; %end; fclose(fid); % % Now generate start values for optimisation, namely % true values as above % ADMB format fid = fopen([filename '.pin'],'w'); %Changed 01032010 %fprintf(fid,'%8.3f',(N0 / 100)); fprintf(fid,'# N0\n %s \n# q',num2str(N0)); fprintf(fid,'\n'); fprintf(fid,'%8.5f',q); fprintf(fid,'\n#logs\n'); fprintf(fid,'%8.5f',log(s)); %fprintf(fid,'\n#log(tau)\n'); %fprintf(fid,'%8.5f',log(t)); fprintf(fid,'\n#M\n'); fprintf(fid,'%8.5f\n',M); %fprintf(fid,'\n#Z\n'); %fprintf(fid,'%d ',zeros(1,cohorts)); %fprintf(fid,'\n#epsilon\n'); %fprintf(fid,'%d ',zeros(1,years+cohorts-1)); fclose(fid); % Added 25032010 -- input file with N0, not Z, as the variable %fid = fopen([filename '-noZ.pin'],'w'); % %fprintf(fid,'# N0\n'); %fprintf(fid,' %8.3f ',N(1,:)/100); % %fprintf(fid,'\n#q\n'); %fprintf(fid,'%8.5f',q); %fprintf(fid,'\n#logs\n'); %fprintf(fid,'%8.5f',log(s)); %fprintf(fid,'\n#log(tau)\n'); %fprintf(fid,'%8.5f',log(t)); %fprintf(fid,'\n#M\n'); %fprintf(fid,'%8.5f',M); %fprintf(fid,'\n#Z\n'); %fprintf(fid,'%d ',zeros(1,cohorts)); %fprintf(fid,'\n#epsilon\n'); %fprintf(fid,'%d ',zeros(1,years+cohorts-1)); %fclose(fid); return;