derivative of a composite function - composite

In some notes in my book they are deriving a composite function but I'm having trouble replicating it.
In particular, I am trying
g(y) = f(x^y) = f(h(y)) = f'(h(y))h'(y) = f'(x^y)(x_1-x_0)
but the f would be with respect to h not x so I'm not sure what I'm not seeing.

It looks like you are looking for: d/dy f(x^y).
Let's see how to evaluate that:
d/dy f(x^y)= f'(x^y) d/dy(x^y) = f'(x^y) x^y ln(x)
How to calculate d/dy(x^y)?
Notice that this is the elementary derivative:
'(a^x), which is a^x ln(a).

Related

How to load Seurat Object into WGCNA Tutorial Format

As far as I can find, there is only one tutorial about loading Seurat objects into WGCNA (https://ucdavis-bioinformatics-training.github.io/2019-single-cell-RNA-sequencing-Workshop-UCD_UCSF/scrnaseq_analysis/scRNA_Workshop-PART6.html). I am really new to programming so it's probably just my inexperience, but I am not sure how to load my Seurat object into a format that works with WGCNA's tutorials (https://horvath.genetics.ucla.edu/html/CoexpressionNetwork/Rpackages/WGCNA/Tutorials/).
Here is what I have tried thus far:
This tries to replicate datExpr and datTraits from part I.1:
library(WGCNA)
library(Seurat)
#example Seurat object -----------------------------------------------
ERlist <- list(c("CPB1", "RP11-53O19.1", "TFF1", "MB", "ANKRD30B",
"LINC00173", "DSCAM-AS1", "IGHG1", "SERPINA5", "ESR1",
"ILRP2", "IGLC3", "CA12", "RP11-64B16.2", "SLC7A2",
"AFF3", "IGFBP4", "GSTM3", "ANKRD30A", "GSTT1", "GSTM1",
"AC026806.2", "C19ORF33", "STC2", "HSPB8", "RPL29P11",
"FBP1", "AGR3", "TCEAL1", "CYP4B1", "SYT1", "COX6C",
"MT1E", "SYTL2", "THSD4", "IFI6", "K1AA1467", "SLC39A6",
"ABCD3", "SERPINA3", "DEGS2", "ERLIN2", "HEBP1", "BCL2",
"TCEAL3", "PPT1", "SLC7A8", "RP11-96D1.10", "H4C8",
"PI15", "PLPP5", "PLAAT4", "GALNT6", "IL6ST", "MYC",
"BST2", "RP11-658F2.8", "MRPS30", "MAPT", "AMFR", "TCEAL4",
"MED13L", "ISG15", "NDUFC2", "TIMP3", "RP13-39P12.3", "PARD68"))
tnbclist <- list(c("FABP7", "TSPAN8", "CYP4Z1", "HOXA10", "CLDN1",
"TMSB15A", "C10ORF10", "TRPV6", "HOXA9", "ATP13A4",
"GLYATL2", "RP11-48O20.4", "DYRK3", "MUCL1", "ID4", "FGFR2",
"SHOX2", "Z83851.1", "CD82", "COL6A1", "KRT23", "GCHFR",
"PRICKLE1", "GCNT2", "KHDRBS3", "SIPA1L2", "LMO4", "TFAP2B",
"SLC43A3", "FURIN", "ELF5", "C1ORF116", "ADD3", "EFNA3",
"EFCAB4A", "LTF", "LRRC31", "ARL4C", "GPNMB", "VIM",
"SDR16C5", "RHOV", "PXDC1", "MALL", "YAP1", "A2ML1",
"RP1-257A7.5", "RP11-353N4.6", "ZBTB18", "CTD-2314B22.3", "GALNT3",
"BCL11A", "CXADR", "SSFA2", "ADM", "GUCY1A3", "GSTP1",
"ADCK3", "SLC25A37", "SFRP1", "PRNP", "DEGS1", "RP11-110G21.2",
"AL589743.1", "ATF3", "SIVA1", "TACSTD2", "HEBP2"))
genes = c(unlist(c(ERlist,tnbclist)))
mat = matrix(rnbinom(500*length(genes),mu=500,size=1),ncol=500)
rownames(mat) = genes
colnames(mat) = paste0("cell",1:500)
sobj = CreateSeuratObject(mat)
sobj = NormalizeData(sobj)
sobj$ClusterName = factor(sample(0:1,ncol(sobj),replace=TRUE))
sobj$Patient = paste0("Patient", 1:500)
sobj = AddModuleScore(object = sobj, features = tnbclist,
name = "TNBC_List",ctrl=5)
sobj = AddModuleScore(object = sobj, features = ERlist,
name = "ER_List",ctrl=5)
#WGCNA -----------------------------------------------------------------
sobjwgcna <- sobj
sobjwgcna <- FindVariableFeatures(sobjwgcna, selection.method = "vst", nfeatures = 2000,
verbose = FALSE, assay = "RNA")
options(stringsAsFactors = F)
sobjwgcnamat <- GetAssayData(sobjwgcna)
datExpr <- t(sobjwgcnamat)[,VariableFeatures(sobjwgcna)]
datTraits <- sobjwgcna#meta.data
datTraits = subset(datTraits, select = -c(nCount_RNA, nFeature_RNA))
I then copy-paste the code as written in the WGCNA I.2a tutorial (https://horvath.genetics.ucla.edu/html/CoexpressionNetwork/Rpackages/WGCNA/Tutorials/FemaleLiver-02-networkConstr-auto.pdf), and that all works until I get to this line in the I.3 tutorial (https://horvath.genetics.ucla.edu/html/CoexpressionNetwork/Rpackages/WGCNA/Tutorials/FemaleLiver-03-relateModsToExt.pdf):
MEList = moduleEigengenes(datExpr, colors = moduleColors)
Error in t.default(expr[, restrict1]) : argument is not a matrix
I tried converting both moduleColors and datExpr into a matrix with as.matrix(), but the error still persists.
Hopefully this makes sense, and thanks for reading!
So doing as.matrix(datExpr) right after datExpr <- t(sobjwgcnamat)[,VariableFeatures(sobjwgcna)] worked. I had been trying it right before MEList = moduleEigengenes(datExpr, colors = moduleColors)
and that didn't work. Seems simple but order matters I guess.

Incomplete structured construct

i am new with f# , will be great if some 1 can help , nearly half a day gone solving this problem Thank you
module Certificate =
type T = {
Id: int
IsECert: bool
IsPrintCert: bool
CertifiedBy: string
Categories: Category.T list
}
let createPending now toZonedDateTime toBeCertifiedByName (job: Models.Job.T) (certificateType: Models.CertificateType.T) (pendingCertificate: Models.PendingCertificate.T) visualization (categories: Category.T list) =
let forCompletion = Models.PendingCertificate.getCertificateForCompletion pendingCertificate
{
Id = forCompletion.Id |> CertificateId.toInt
IsECert = Models.PendingCertificate.isECertificate pendingCertificate
IsPrintCert = Models.PendingCertificate.isPrintCertificate pendingCertificate
CertifiedBy = toBeCertifiedByName
Categories = categories}
i am getting an error in "Incomplete structured construct at or before this point"
Your formatting is all off. I will assume here that this is just a result of posting to StackOverflow, and your actual code is well indented.
The error comes from the definition of createPending: this function does not have a result. All its body consists of defining a forCompletion value, but there is nothing after it. Here's a simpler example that has the same problem:
let f x =
let y = 5
This function will produce the same error, because it also doesn't have a result. In F#, every function has to return something. The body cannot contain only definitions of helper functions or values. For example, I could fix my broken function above like this:
let f x =
let y = 5
x + y
This function first defines a helper value y, then adds it to its argument x, and returns the result.
> f 2
> 7
>
> f 0
> 5
How exactly you need to fix your function depends on what exactly you want it to mean. I can't help you here, because you haven't provided that information.

Solving simulataneous equation using maxima?

Hi I want to solve three equation, please find the details in picture. I want to find the value of alpha, h and Q in terms of alpha and h how do i do this using Maxima?
[B1=\frac{{{h}^{2}}}{\mathit{Qs}}]
[B2={{h}^{2}}+\alpha+1]
[B3=\frac{1}{\mathit{Qs}}]
[B0={{h}^{2}}]
[C2=\frac{{{h}^{4}}}{{{\mathit{Qs}}^{2}}}-2{{h}^{2}}\,\left( {{h}^{2}}+\alpha+1\right) =0]
[C4={{\left( {{h}^{2}}+\alpha+1\right) }^{2}}-\frac{2{{h}^{2}}}{{{\mathit{Qs}}^{2}}}-2{{h}^{2}}=0]
[C6=\frac{1}{{{\mathit{Qs}}^{2}}}-2\left( {{h}^{2}}+\alpha+1\right) =0]
algsys([C2,C4,C6],[h,alpha,Qs]);?????
I see that by just adding QS to the list of variables to solve for, I get some solutions. I didn't check them. Some of them may be redundant, I didn't check that either.
(%i20) algsys ([C2, C4, C6], [h, alpha, QS]);
(%o20) [[h = -1,alpha = -(2^(3/2)-2)/(sqrt(2)-2),QS = sqrt(2-sqrt(2))/2],
[h = 1,alpha = -(2^(3/2)-2)/(sqrt(2)-2),QS = sqrt(2-sqrt(2))/2],
[h = -1,alpha = -(2^(3/2)-2)/(sqrt(2)-2),QS = -sqrt(2-sqrt(2))/2],
[h = 1,alpha = -(2^(3/2)-2)/(sqrt(2)-2),QS = -sqrt(2-sqrt(2))/2],
[h = -1,alpha = -(2^(3/2)+2)/(sqrt(2)+2),QS = sqrt(sqrt(2)+2)/2],
[h = 1,alpha = -(2^(3/2)+2)/(sqrt(2)+2),QS = sqrt(sqrt(2)+2)/2],
[h = -1,alpha = -(2^(3/2)+2)/(sqrt(2)+2),QS = -sqrt(sqrt(2)+2)/2],
[h = 1,alpha = -(2^(3/2)+2)/(sqrt(2)+2),QS = -sqrt(sqrt(2)+2)/2]]
Try replacing h^2 with a single variable, say h2 (and replace h^4 with h2^2).
Try simplifying the equations with expand and/or ratsimp.
In solve(eqns, vars), the variables vars are the ones you are trying to solve for. So I think you want alpha, h, Q (well, actually alpha, h2, Q if you follow my previous advice).
Finally, PLEASE paste your input and output into the question instead of linking to an image, which makes it much more difficult for someone else to try working with your equations.

Functional Programming Exercise

As a functional programming exercise, I thought I'd write a little program to rank crafting recipes in an mmo by profitability.
In an OO language, I'd make strategy objects for each recipe, with Cost(), ExpectedRevenue(), and Volume() as members. I'd then put all the objects in a list and sort them by a profitability/time function.
Trying to accomplish the same result in F#, but I'm not certain how to go about it. I have some disjointed cost functions, for example:
let cPM (ss,marble) = (15.0 * ss + 10.0 * marble + 0.031) / 5.0
let cTRef (tear,clay) = (tear + 10.0 * clay + 0.031) / 5.0
and then revenue and volume definitions like:
let rPM = 1.05
let vPM = 50
but I'm not sure what to do now. Make a list of tuples that look like
(name: string, cost:double, revenue:double, volume:int)
and then sort the list? It feels like I'm missing something- still thinking in OO, not to mention adding new recipes in this fashion will be rather awkward.
Has anyone any tips to use the functional concepts in a better way? It seemed like this type of calculation problem would be a good fit for the functional style.
Much appreciated.
This is a fairly complex question with multiple possible answers. Also, it is quite hard to guess anything about your domain (I don't know what game you're playing :-)), so I'll try to make something up, based on the example.
The basic functional approach would be to model the different recipes using a discriminated union.
type Recipe =
| FancySword of gold:float * steel:float // Sword can be created from gold & steel
| MagicalStone of frogLegs:float // Magical stone requires some number of frog legs
Also, we need to know the prices of things in the game:
type Prices = { Gold : float; Steel : float; FrogLegs : float }
Now you can write functions to calculate the cost and expected revenue of the recipes:
let cost prices recipe =
match recipe with
| FancySword(g, s) ->
// To create a sword, we need 2 pieces of gold and 15 pieces of steel
2.0 * g * prices.Gold + s * 15.0 * prices.Steel
| MagicalStone(l) -> l * prices.FrogLeg
This takes the record with all the prices and it takes a recipe that you want to evaluate.
The example should give you some idea - starting with a discriminated union to model the problem domain (different recipes) and then writing a function with pattern matching in it is usually a good way to get started - but it's hard to say more with the limited information in your question.
In functional languages you can do anything only with functions. Here you can define common profitability function and sort your recipes with it and List.sortBy:
// recipe type with constants for Revenue, Volume and (ss,marble)
type recipe = {r: float; v: float; smth: float * float}
// list of recipes
let recipes = [
{r = 1.08; v = 47.0; smth = (28.0, 97.0)};
{r = 1.05; v = 50.0; smth = (34.0, 56.0)} ]
// cost function
let cPM (ss,marble) = (15.0 * ss + 10.0 * marble + 0.031) / 5.0
// profitability function with custom coefficients
let profitability recipe = recipe.r * 2.0 + recipe.v * 3.0 + cPM recipe.smth
// sort recipes by profitability
let sortedRecipes =
List.sortBy profitability recipes
// note: it's reordered now
printfn "%A" sortedRecipes
The accepted answer is a little lacking in type safety, I think - you already stated that a FancySword is made of gold and steel, so you shouldn't have to remember to correctly pair the gold quantity with the gold price! The type system ought to check that for you, and prevent an accidental g * prices.Steel mistake.
If the set of possible resource types is fixed, then this is a nice use-case for Units of Measure.
[<Measure>] type Gold
[<Measure>] type Steel
[<Measure>] type FrogLegs
[<Measure>] type GameMoney
type Recipe = {
goldQty : float<Gold>
steelQty : float<Steel>
frogLegsQty : int<FrogLegs>
}
type Prices = {
goldPrice : float<GameMoney/Gold>
steelPrice : float<GameMoney/Steel>
frogLegsPrice : float<GameMoney/FrogLegs>
}
let recipeCost prices recipe =
prices.goldPrice * recipe.goldQty +
prices.steelPrice * recipe.steelQty +
// frog legs must be converted to float while preserving UoM
prices.frogLegsPrice * (recipe.frogLegsQty |> float |> LanguagePrimitives.FloatWithMeasure)
let currentPrices = {goldPrice = 100.0<GameMoney/Gold>; steelPrice = 50.0<GameMoney/Steel>; frogLegsPrice = 2.5<GameMoney/FrogLegs> }
let currentCost = recipeCost currentPrices
let fancySwordRecipe = {goldQty = 25.4<Gold>; steelQty = 76.4<Steel>; frogLegsQty = 0<FrogLegs>}
let fancySwordCost = currentCost fancySwordRecipe
The compiler will now ensure that all calculations check out. In the recipeCost function, for example, it ensures that the total is a float<GameMoney>.
Since you mentioned volume, I think you can see how you can replicate the same pattern to write type-safe functions that will calculate total recipe volumes as a value of type int<InventoryVolume>.

How to create { x = y | y = z | z = 0 } block dynamically from hash?

Although the context is likely irrelevant, I'm using the Chewy gem to filter Elasticsearch results using this code:
scope = scope.filter {
(send('facet_properties').send(property_ids[0], :or) == val.map(&:to_i)) |
(send('facet_properties').send(property_ids[1], :or) == val.map(&:to_i))
}
I'm looking for a way to loop through each element in property_ids rather than calling property_ids[0], property_ids[1], etc., separated by an or individually. In actual use property_ids will not be a fixed length.
Not sure exactly what your structure looks like or exactly what you are trying to achieve but have you tried something like this?
vals = val.map(&:to_i)
prop_hash = Hash[property_ids.map{|prop| [prop,vals]}]
# alternately prop_hash = property_ids.each_with_object({}){|prop,obj| obj[prop] = vals}
scope.filter(facet_properties: {prop_hash}).filter_mode(:or)
Since chewy has a #filter_mode method to set the join type for where conditions it seems like this should work for you.

Resources