Skip to content

Instantly share code, notes, and snippets.

@fatihgune
Created December 6, 2020 16:12
Show Gist options
  • Save fatihgune/be0b2d90addd52271373822ca14cf2d0 to your computer and use it in GitHub Desktop.
Save fatihgune/be0b2d90addd52271373822ca14cf2d0 to your computer and use it in GitHub Desktop.
Calculate Volatility of Asset (PHP - Laravel)
<?
$years = config('years');
$companies = Company::where('bankrupted', 'yes')->where('country', 'COUNTRY_NAME_HERE')->where('type', 'unlisted')->get();
foreach ($years as $year) {
foreach ($companies as $company) {
if (AssetMatch::where('unlisted_company_name', $company->name)
->where('year', $year)->exists()) {
$marketCapitalization = AssetMatch::where('unlisted_company_name', $company->name)
->where('year', $year)
->first()
->listed_company_market_cap;
$totalLiability = $company->totalLiabilities->where('year', $year);
$marketValueOfAsset = $company->marketValueOfAssets->where('year', $year);
$volatilityOfLiability = $company->volatilityOfLiabilities->where('year', $year);
$standardDeviation = AssetMatch::where('unlisted_company_name', $company->name)
->where('year', $year)
->first()
->listed_company_standard_dev;
if (! is_null($totalLiability) && $totalLiability->count() != 0) {
$totalLiability = $totalLiability->first();
if (! is_null($totalLiability) && $totalLiability->count() != 0) {
$totalLiability = $totalLiability->toArray();
$totalLiabilityVal = (float)$totalLiability['value'];
}
if (! is_null($marketValueOfAsset) && $marketValueOfAsset->count() != 0) {
$marketValueOfAsset = $marketValueOfAsset->first();
if (! is_null($marketValueOfAsset) && $marketValueOfAsset->count() != 0) {
$marketValueOfAsset = $marketValueOfAsset->toArray();
$marketValueOfAssetVal = (float)$marketValueOfAsset['value'];
}
if (! is_null($volatilityOfLiability) && $volatilityOfLiability->count() != 0) {
$volatilityOfLiability = $volatilityOfLiability->first();
if (! is_null($volatilityOfLiability) && $volatilityOfLiability->count() != 0) {
$volatilityOfLiability = $volatilityOfLiability->toArray();
$volatilityOfLiabilityVal = (float)$volatilityOfLiability['value'];
$result = (float)(($marketCapitalization / $marketValueOfAssetVal) * $standardDeviation) + (($totalLiabilityVal / $marketValueOfAssetVal) * $volatilityOfLiabilityVal);
DB::transaction(function () use ($company, $result, $year) {
VolatilityOfAsset::create([
'company_id' => $company->id,
'year' => $year,
'value' => $result,
]);
});
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment