Advertisement
tomdodd4598

Untitled

Apr 22nd, 2021
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. "SETUP";
  2.  
  3. linear[A_, b_] := A + b*x;
  4. exponential[A_, k_] := A*Exp[k*x];
  5. logistic[A_, k_, s_] := A*k*Exp[k*x]/(A*s + k - A*s*Exp[k*x]);
  6. gaussian[A_, k_, z_] := A*Exp[k*x + z/2*x^2];
  7.  
  8. FunctionFit[d_, f_, p_, x_] :=
  9. NonlinearModelFit[d, f @@ Transpose[p][[1]], p, x];
  10. LinearFit[d_, p_, x_] := FunctionFit[d, linear, p, x];
  11. ExponentialFit[d_, p_, x_] := FunctionFit[d, exponential, p, x];
  12. LogisticFit[d_, p_, x_] := FunctionFit[d, logistic, p, x];
  13. GaussianFit[d_, p_, x_] := FunctionFit[d, gaussian, p, x];
  14.  
  15. Sigma[n_] :=
  16. Probability[-n < x < n, x \[Distributed] NormalDistribution[0, 1]];
  17. Confidence[x_] := Sigma[Sqrt[2] InverseErf[x/100]];
  18. FitStandardErrors[m_] := Diagonal[Sqrt[m["CovarianceMatrix"]]];
  19. FitDegreesOfFreedom[m_] := m["ANOVATableDegreesOfFreedom"][[2]];
  20. FitTFactor[m_, c_] :=
  21. InverseCDF[StudentTDistribution[FitDegreesOfFreedom[m]], (1 + c)/2];
  22. FitConfidenceLimit[m_, c_] := FitStandardErrors[m]*FitTFactor[m, c];
  23. FitCIs[m_, p_, c_] :=
  24. Transpose[{p - FitConfidenceLimit[m, c], p,
  25. p + FitConfidenceLimit[m, c]} /. m["BestFitParameters"]];
  26.  
  27. cross = Graphics[{Line[{{-1, 0}, {1, 0}}], Line[{{0, -1}, {0, 1}}]}];
  28. FitPlots[f_, c_, u_, v_] :=
  29. Plot[{f @@ Transpose[c][[2]], f @@ Transpose[c][[1]],
  30. f @@ Transpose[c][[3]]}, {x, u, v}, PlotRange -> All,
  31. AxesOrigin -> {0, 0}, Filling -> {3 -> {2}},
  32. FillingStyle -> Directive[Opacity[0.5], Gray]];
  33. FitComparePlot[p_, f_, c_, u_, v_] :=
  34. Show[FitPlots[f, c, u, v],
  35. ListPlot[p["Data"], PlotStyle -> Black,
  36. PlotMarkers -> {cross, 1/20}]];
  37.  
  38. "PROFIT BEFORE TAX";
  39.  
  40. profitBeforeTaxData = {{0, 367357}, {1, 908188}, {2, 856965}, {3,
  41. 1120118}, {4, 1458694}};
  42.  
  43. profitBeforeTaxFit["Linear"] =
  44. LinearFit[profitBeforeTaxData, {{A, 500000}, {b, 500000}}, x];
  45. profitBeforeTaxFit["Exponential"] =
  46. ExponentialFit[profitBeforeTaxData, {{A, 500000}, {k, 1/4}}, x];
  47.  
  48. profitBeforeTaxCI["Linear", c_] :=
  49. FitCIs[profitBeforeTaxFit["Linear"], {A, b}, Confidence[c]];
  50. profitBeforeTaxCI["Exponential", c_] :=
  51. FitCIs[profitBeforeTaxFit["Exponential"], {A, k}, Confidence[c]];
  52.  
  53. (*FitComparePlot[profitBeforeTaxFit["Linear"],linear,\
  54. profitBeforeTaxCI["Linear"],0,7];*)
  55. (*FitComparePlot[profitBeforeTaxFit["Exponential"],exponential,\
  56. profitBeforeTaxCI["Exponential"],0,7];*)
  57.  
  58. "TURNOVER";
  59.  
  60. turnoverData = {{0, 4344966}, {1, 5166722}, {2, 5630153}, {3,
  61. 6012395}, {4, 6500607}, {5, 7079930}, {6, 7823504}, {7,
  62. 8672722}, {8, 9923293}, {9, 10572549}, {10, 11121068}};
  63.  
  64. turnoverFit["Linear"] =
  65. LinearFit[turnoverData, {{A, 500000}, {b, 500000}}, x];
  66. turnoverFit["Exponential"] =
  67. ExponentialFit[turnoverData, {{A, 500000}, {k, 1/4}}, x];
  68.  
  69. turnoverCI["Linear", c_] :=
  70. FitCIs[turnoverFit["Linear"], {A, b}, Confidence[c]];
  71. turnoverCI["Exponential", c_] :=
  72. FitCIs[turnoverFit["Exponential"], {A, k}, Confidence[c]];
  73.  
  74. (*FitComparePlot[turnoverFit["Linear"],linear,turnoverCI["Linear"],0,\
  75. 13];*)
  76. (*FitComparePlot[turnoverFit["Exponential"],exponential,turnoverCI[\
  77. "Exponential"],0,13];*)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement