Skip to content

Instantly share code, notes, and snippets.

@fatihgune
Created December 6, 2020 15:58
Show Gist options
  • Save fatihgune/6c83b58ec99f79214d214f051a66a705 to your computer and use it in GitHub Desktop.
Save fatihgune/6c83b58ec99f79214d214f051a66a705 to your computer and use it in GitHub Desktop.
Calculate the ROC Curve values for the companies of the country (PHP)
<?
public function calcCurve(): void
{
// might take a while, preferred way is to add this to ini
ini_set('max_execution_time', 600);
$years = [
'2006',
'2007',
'2008',
'2009',
'2010',
'2011',
'2012',
'2013',
'2014',
'2015',
'2016',
'2017',
'2018',
'2019',
];
// get the X equation results for the country
$bankruptedCompaniesCount = Xequation::join('companies AS c', 'company_id', '=', 'c.id')
->where('c.country', '=', 'COUNTRY_NAME_HERE')
->where('c.type', '=', 'listed')
->where('c.bankrupted', '=', 'yes')
->count();
$result = [];
$plus = 0.00;
for ($i = 1; $i <= 100; $i++) {
$count = 0;
$plus += 0.01;
foreach ($years as $year) {
// get the X equation results for the country
$normDist = Xequation::join('companies AS c', 'company_id', '=', 'c.id')
->where('c.country', '=', 'COUNTRY_NAME_HERE')
->where('c.type', '=', 'listed')
->where('year', '=', $year)->get()
->sortBy('z', SORT_NUMERIC);
$companyIds = $normDist->pluck('company_id')->unique()->count();
$percentage = round($companyIds * $plus, 0, PHP_ROUND_HALF_DOWN);
$takenValues = $normDist->take($percentage);
$numberOfBankruptedCompanies = $takenValues->where('bankrupted', 'yes')->count();
$count += $numberOfBankruptedCompanies;
}
$result[$i] = ($count / $bankruptedCompaniesCount) .';';
}
print_r(['COUNTRY_NAME_HERE' => $result]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment