% This file is licensed under the Allen Institute Terms of Use, which can be found at:
%http://www.alleninstitute.org/Media/policies/terms_of_use_content.html

%------------------------------------------------------------------------------------

%Script to generate the best fit exponentials and the sum of squared
%residuals for the first few moments and tail weight numbers 
%corresponding to the first 1222 randomly generated synaptic weight
%distributions
%
%NOTE: This script uses the MATLAB function nlinfit, which requires the
%Statistics Toolbox

%NOTE: The .mat file FigS10_randdist.mat consisting of the original
%distributions used for FigS10 has also been made available and can be 
%used to reproduce the figure exactly. 

clear variables;

load FigS10_randdist.mat;
load FigS10_randdist_DiPDEoutput.mat;
load FigS10_transient_times.mat;
load FigS10_randdist_Poisson_conv.mat;
load FigS10_norm_moms1to10.mat

norms = norms(1:1222,:);      %first 1222 normalized moments 

%Fitting for tail weight numbers
m=10;
beta=zeros(m,3,2);
n=length(outs(:,4));
res=zeros(m,3,n);
for i=1:m
    for j=1:8
        [beta(i,j,:),res(i,j,:)]=nlinfit(r_dist(:,i+1),outs(:,j+1),@expfunfit,[0.01,-1]);
    end
end
save('beta.mat','beta');
save('res.mat','res');

%Fitting for moments
m=10;
betam=zeros(m,3,2);
n=length(outs(:,4));
resm=zeros(m,3,n);
for i=1:m
    for j=1:8
        [betam(i,j,:),resm(i,j,:)]=nlinfit(norms(:,i),outs(:,j+1),@expfunfit,[0.01,-1]);
    end
end
save('betam.mat','betam');
save('resm.mat','resm');
